Skip to content

Commit 0f6ef9b

Browse files
author
Siva Chandra Reddy
committed
[libc][NFC] Put definitions of stdout and friends into their own object files.
This is done so that tests which only require platform file but not the platform streams can be run as unit tests. The tests which use platform streams can only be hermetic tests to avoid conflicts with the system-libc streams. Fixes: llvm#64663 llvm#63558 Reviewed By: jhuber6 Differential Revision: https://reviews.llvm.org/D158193
1 parent e2d06ff commit 0f6ef9b

18 files changed

+262
-114
lines changed

libc/src/__support/File/CMakeLists.txt

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,32 @@ if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
3535
endif()
3636

3737
add_subdirectory(${LIBC_TARGET_OS})
38-
set(target_file libc.src.__support.File.${LIBC_TARGET_OS}.${LIBC_TARGET_OS}_file)
38+
39+
set(target_file libc.src.__support.File.${LIBC_TARGET_OS}.file)
40+
set(target_stdout libc.src.__support.File.${LIBC_TARGET_OS}.stdout)
41+
set(target_stderr libc.src.__support.File.${LIBC_TARGET_OS}.stderr)
42+
set(target_stdin libc.src.__support.File.${LIBC_TARGET_OS}.stdin)
43+
44+
set(file_targets "${target_file};${target_stdout};${target_stdin};${target_stderr}")
45+
set(file_aliases "platform_file;platform_stdout;platform_stdin;platform_stderr")
46+
47+
foreach(alias target IN ZIP_LISTS file_aliases file_targets)
48+
if(TARGET ${target})
49+
add_object_library(
50+
${alias}
51+
ALIAS
52+
${target}
53+
DEPENDS
54+
${target}
55+
)
56+
endif()
57+
endforeach()
58+
3959
set(target_dir libc.src.__support.File.${LIBC_TARGET_OS}.${LIBC_TARGET_OS}_dir)
40-
if((NOT TARGET ${target_file}) OR (NOT TARGET ${target_dir}))
60+
if(NOT TARGET ${target_dir})
4161
return()
4262
endif()
4363

44-
add_object_library(
45-
platform_file
46-
ALIAS
47-
${target_file}
48-
DEPENDS
49-
${target_file}
50-
)
51-
5264
add_object_library(
5365
platform_dir
5466
ALIAS

libc/src/__support/File/linux/CMakeLists.txt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
add_object_library(
2-
linux_file
2+
file
33
SRCS
44
file.cpp
5+
HDRS
6+
file.h
57
DEPENDS
68
libc.include.fcntl
79
libc.include.stdio
@@ -13,6 +15,30 @@ add_object_library(
1315
libc.src.__support.File.file
1416
)
1517

18+
add_object_library(
19+
stdout
20+
SRCS
21+
stdout.cpp
22+
DEPENDS
23+
.file
24+
)
25+
26+
add_object_library(
27+
stdin
28+
SRCS
29+
stdin.cpp
30+
DEPENDS
31+
.file
32+
)
33+
34+
add_object_library(
35+
stderr
36+
SRCS
37+
stderr.cpp
38+
DEPENDS
39+
.file
40+
)
41+
1642
add_object_library(
1743
linux_dir
1844
SRCS

libc/src/__support/File/linux/file.cpp

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
//===--- Linux specialization of the File data structure ------------------===//
1+
//===--- Implementation of the Linux specialization of File ---------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "file.h"
10+
911
#include "src/__support/File/file.h"
1012

1113
#include "src/__support/CPP/new.h"
@@ -18,31 +20,7 @@
1820

1921
namespace __llvm_libc {
2022

21-
namespace {
22-
23-
FileIOResult write_func(File *, const void *, size_t);
24-
FileIOResult read_func(File *, void *, size_t);
25-
ErrorOr<long> seek_func(File *, long, int);
26-
int close_func(File *);
27-
28-
} // anonymous namespace
29-
30-
class LinuxFile : public File {
31-
int fd;
32-
33-
public:
34-
constexpr LinuxFile(int file_descriptor, uint8_t *buffer, size_t buffer_size,
35-
int buffer_mode, bool owned, File::ModeFlags modeflags)
36-
: File(&write_func, &read_func, &seek_func, &close_func, buffer,
37-
buffer_size, buffer_mode, owned, modeflags),
38-
fd(file_descriptor) {}
39-
40-
int get_fd() const { return fd; }
41-
};
42-
43-
namespace {
44-
45-
FileIOResult write_func(File *f, const void *data, size_t size) {
23+
FileIOResult linux_file_write(File *f, const void *data, size_t size) {
4624
auto *lf = reinterpret_cast<LinuxFile *>(f);
4725
int ret = __llvm_libc::syscall_impl<int>(SYS_write, lf->get_fd(), data, size);
4826
if (ret < 0) {
@@ -51,7 +29,7 @@ FileIOResult write_func(File *f, const void *data, size_t size) {
5129
return ret;
5230
}
5331

54-
FileIOResult read_func(File *f, void *buf, size_t size) {
32+
FileIOResult linux_file_read(File *f, void *buf, size_t size) {
5533
auto *lf = reinterpret_cast<LinuxFile *>(f);
5634
int ret = __llvm_libc::syscall_impl<int>(SYS_read, lf->get_fd(), buf, size);
5735
if (ret < 0) {
@@ -60,7 +38,7 @@ FileIOResult read_func(File *f, void *buf, size_t size) {
6038
return ret;
6139
}
6240

63-
ErrorOr<long> seek_func(File *f, long offset, int whence) {
41+
ErrorOr<long> linux_file_seek(File *f, long offset, int whence) {
6442
auto *lf = reinterpret_cast<LinuxFile *>(f);
6543
long result;
6644
#ifdef SYS_lseek
@@ -90,7 +68,7 @@ ErrorOr<long> seek_func(File *f, long offset, int whence) {
9068
return result;
9169
}
9270

93-
int close_func(File *f) {
71+
int linux_file_close(File *f) {
9472
auto *lf = reinterpret_cast<LinuxFile *>(f);
9573
int ret = __llvm_libc::syscall_impl<int>(SYS_close, lf->get_fd());
9674
if (ret < 0) {
@@ -100,8 +78,6 @@ int close_func(File *f) {
10078
return 0;
10179
}
10280

103-
} // anonymous namespace
104-
10581
ErrorOr<File *> openfile(const char *path, const char *mode) {
10682
using ModeFlags = File::ModeFlags;
10783
auto modeflags = File::mode_flags(mode);
@@ -166,28 +142,4 @@ int get_fileno(File *f) {
166142
return lf->get_fd();
167143
}
168144

169-
constexpr size_t STDIN_BUFFER_SIZE = 512;
170-
uint8_t stdin_buffer[STDIN_BUFFER_SIZE];
171-
static LinuxFile StdIn(0, stdin_buffer, STDIN_BUFFER_SIZE, _IOFBF, false,
172-
File::ModeFlags(File::OpenMode::READ));
173-
File *stdin = &StdIn;
174-
175-
constexpr size_t STDOUT_BUFFER_SIZE = 1024;
176-
uint8_t stdout_buffer[STDOUT_BUFFER_SIZE];
177-
static LinuxFile StdOut(1, stdout_buffer, STDOUT_BUFFER_SIZE, _IOLBF, false,
178-
File::ModeFlags(File::OpenMode::APPEND));
179-
File *stdout = &StdOut;
180-
181-
constexpr size_t STDERR_BUFFER_SIZE = 0;
182-
static LinuxFile StdErr(2, nullptr, STDERR_BUFFER_SIZE, _IONBF, false,
183-
File::ModeFlags(File::OpenMode::APPEND));
184-
File *stderr = &StdErr;
185-
186145
} // namespace __llvm_libc
187-
188-
// Provide the external defintitions of the standard IO streams.
189-
extern "C" {
190-
FILE *stdin = reinterpret_cast<FILE *>(&__llvm_libc::StdIn);
191-
FILE *stderr = reinterpret_cast<FILE *>(&__llvm_libc::StdErr);
192-
FILE *stdout = reinterpret_cast<FILE *>(&__llvm_libc::StdOut);
193-
}

libc/src/__support/File/linux/file.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===--- Linux specialization of the File data structure ------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/File/file.h"
10+
11+
namespace __llvm_libc {
12+
13+
FileIOResult linux_file_write(File *, const void *, size_t);
14+
FileIOResult linux_file_read(File *, void *, size_t);
15+
ErrorOr<long> linux_file_seek(File *, long, int);
16+
int linux_file_close(File *);
17+
18+
class LinuxFile : public File {
19+
int fd;
20+
21+
public:
22+
constexpr LinuxFile(int file_descriptor, uint8_t *buffer, size_t buffer_size,
23+
int buffer_mode, bool owned, File::ModeFlags modeflags)
24+
: File(&linux_file_write, &linux_file_read, &linux_file_seek,
25+
&linux_file_close, buffer, buffer_size, buffer_mode, owned,
26+
modeflags),
27+
fd(file_descriptor) {}
28+
29+
int get_fd() const { return fd; }
30+
};
31+
32+
} // namespace __llvm_libc
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===--- Definition of Linux stderr ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "file.h"
10+
#include <stdio.h>
11+
12+
namespace __llvm_libc {
13+
14+
constexpr size_t STDERR_BUFFER_SIZE = 0;
15+
static LinuxFile StdErr(2, nullptr, STDERR_BUFFER_SIZE, _IONBF, false,
16+
File::ModeFlags(File::OpenMode::APPEND));
17+
File *stderr = &StdErr;
18+
19+
} // namespace __llvm_libc
20+
21+
extern "C" {
22+
FILE *stderr = reinterpret_cast<FILE *>(&__llvm_libc::StdErr);
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===--- Definition of Linux stdin ----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "file.h"
10+
#include <stdio.h>
11+
12+
namespace __llvm_libc {
13+
14+
constexpr size_t STDIN_BUFFER_SIZE = 512;
15+
uint8_t stdin_buffer[STDIN_BUFFER_SIZE];
16+
static LinuxFile StdIn(0, stdin_buffer, STDIN_BUFFER_SIZE, _IOFBF, false,
17+
File::ModeFlags(File::OpenMode::READ));
18+
File *stdin = &StdIn;
19+
20+
} // namespace __llvm_libc
21+
22+
extern "C" {
23+
FILE *stdin = reinterpret_cast<FILE *>(&__llvm_libc::StdIn);
24+
} // extern "C"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===--- Definition of Linux stdout ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "file.h"
10+
#include <stdio.h>
11+
12+
namespace __llvm_libc {
13+
14+
constexpr size_t STDOUT_BUFFER_SIZE = 1024;
15+
uint8_t stdout_buffer[STDOUT_BUFFER_SIZE];
16+
static LinuxFile StdOut(1, stdout_buffer, STDOUT_BUFFER_SIZE, _IOLBF, false,
17+
File::ModeFlags(File::OpenMode::APPEND));
18+
File *stdout = &StdOut;
19+
20+
} // namespace __llvm_libc
21+
22+
extern "C" {
23+
FILE *stdout = reinterpret_cast<FILE *>(&__llvm_libc::StdOut);
24+
} // extern "C"

libc/src/stdio/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ if(LLVM_LIBC_FULL_BUILD)
422422
list(APPEND printf_deps
423423
libc.src.__support.File.file
424424
libc.src.__support.File.platform_file
425+
libc.src.__support.File.platform_stdout
425426
)
426427
else()
427428
set(printf_copts "-DLIBC_COPT_PRINTF_USE_SYSTEM_FILE")

libc/src/stdio/generic/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ add_entrypoint_object(
7272
libc.src.errno.errno
7373
libc.include.stdio
7474
libc.src.__support.File.file
75-
libc.src.__support.File.platform_file
75+
libc.src.__support.File.platform_stdout
7676
)
7777

7878
add_entrypoint_object(
@@ -84,7 +84,7 @@ add_entrypoint_object(
8484
DEPENDS
8585
libc.include.stdio
8686
libc.src.__support.File.file
87-
libc.src.__support.File.platform_file
87+
libc.src.__support.File.platform_stdin
8888
)
8989

9090
add_entrypoint_object(
@@ -96,7 +96,7 @@ add_entrypoint_object(
9696
DEPENDS
9797
libc.include.stdio
9898
libc.src.__support.File.file
99-
libc.src.__support.File.platform_file
99+
libc.src.__support.File.platform_stdout
100100
)
101101

102102
add_entrypoint_object(
@@ -108,5 +108,5 @@ add_entrypoint_object(
108108
DEPENDS
109109
libc.include.stdio
110110
libc.src.__support.File.file
111-
libc.src.__support.File.platform_file
111+
libc.src.__support.File.platform_stderr
112112
)

libc/src/unistd/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ add_entrypoint_object(
266266
libc.src.__support.CPP.optional
267267
libc.src.__support.CPP.string_view
268268
libc.src.__support.File.file
269+
libc.src.__support.File.platform_stderr
269270
libc.src.stdio.fprintf
270271
)
271272

0 commit comments

Comments
 (0)