Skip to content

Commit 581740f

Browse files
committed
fixup! fixup! fixup! [libc] Add putc, fputc, and fprintf to stdio/baremetal
1 parent 20b1e65 commit 581740f

File tree

5 files changed

+60
-23
lines changed

5 files changed

+60
-23
lines changed

libc/src/stdio/baremetal/CMakeLists.txt

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ add_entrypoint_object(
44
fprintf.cpp
55
HDRS
66
../fprintf.h
7-
write_utils.h
8-
DEPENDS
7+
DEPENDS
98
libc.src.__support.CPP.string_view
109
libc.src.stdio.printf_core.printf_main
10+
.baremetal_write_utils
1111
)
1212

1313
add_entrypoint_object(
@@ -16,9 +16,9 @@ add_entrypoint_object(
1616
fputc.cpp
1717
HDRS
1818
../fputc.h
19-
write_utils.h
2019
DEPENDS
2120
libc.src.__support.CPP.string_view
21+
.baremetal_write_utils
2222
)
2323

2424
add_entrypoint_object(
@@ -49,9 +49,9 @@ add_entrypoint_object(
4949
putc.cpp
5050
HDRS
5151
../putc.h
52-
write_utils.h
53-
DEPENDS
52+
DEPENDS
5453
libc.src.__support.CPP.string_view
54+
.baremetal_write_utils
5555
)
5656

5757
add_entrypoint_object(
@@ -60,10 +60,10 @@ add_entrypoint_object(
6060
printf.cpp
6161
HDRS
6262
../printf.h
63-
write_utils.h
64-
DEPENDS
63+
DEPENDS
6564
libc.src.stdio.printf_core.printf_main
6665
libc.src.__support.arg_list
66+
.baremetal_write_hooks
6767
)
6868

6969
add_entrypoint_object(
@@ -119,6 +119,7 @@ add_entrypoint_object(
119119
DEPENDS
120120
libc.src.stdio.printf_core.printf_main
121121
libc.src.__support.arg_list
122+
.baremetal_write_hooks
122123
)
123124

124125
add_entrypoint_object(
@@ -134,6 +135,16 @@ add_entrypoint_object(
134135
libc.src.__support.OSUtil.osutil
135136
)
136137

138+
add_header_library(
139+
baremetal_write_hooks
140+
HDRS
141+
write_hooks.h
142+
DEPENDS
143+
libc.src.__support.OSUtil.osutil
144+
libc.src.__support.CPP.string_view
145+
libc.src.stdio.printf_core.core_structs
146+
)
147+
137148
add_header_library(
138149
baremetal_write_utils
139150
HDRS
@@ -143,6 +154,7 @@ add_header_library(
143154
libc.hdr.stdio_macros
144155
libc.src.__support.OSUtil.osutil
145156
libc.src.__support.CPP.string_view
157+
.baremetal_write_hooks
146158
.stdout
147159
.stderr
148160
)

libc/src/stdio/baremetal/printf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include "src/__support/arg_list.h"
1212
#include "src/__support/macros/config.h"
13-
#include "src/stdio/baremetal/write_utils.h"
13+
#include "src/stdio/baremetal/write_hooks.h"
1414
#include "src/stdio/printf_core/printf_main.h"
1515

1616
#include <stdarg.h>

libc/src/stdio/baremetal/vprintf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include "src/__support/arg_list.h"
1212
#include "src/__support/macros/config.h"
13-
#include "src/stdio/baremetal/write_utils.h"
13+
#include "src/stdio/baremetal/write_hooks.h"
1414
#include "src/stdio/printf_core/printf_main.h"
1515

1616
#include <stdarg.h>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- Baremetal helper functions for writing to stdout/stderr -*- C++ -*-===//
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/CPP/string_view.h"
10+
#include "src/__support/OSUtil/io.h"
11+
#include "src/__support/macros/config.h"
12+
#include "src/stdio/printf_core/core_structs.h" // For printf_core::WRITE_OK
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
namespace write_utils {
16+
17+
LIBC_INLINE int stdout_write_hook(cpp::string_view new_str, void *) {
18+
write_to_stdout(new_str);
19+
return printf_core::WRITE_OK;
20+
}
21+
22+
LIBC_INLINE int stderr_write_hook(cpp::string_view new_str, void *) {
23+
write_to_stderr(new_str);
24+
return printf_core::WRITE_OK;
25+
}
26+
27+
} // namespace write_utils
28+
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdio/baremetal/write_utils.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,33 @@
1010
#include "hdr/types/FILE.h"
1111
#include "src/__support/CPP/string_view.h"
1212
#include "src/__support/OSUtil/io.h"
13+
#include "src/__support/libc_errno.h"
1314
#include "src/__support/macros/config.h"
14-
#include "src/stdio/printf_core/core_structs.h" // For printf_core::WRITE_OK
15+
#include "src/stdio/baremetal/write_hooks.h"
1516

1617
namespace LIBC_NAMESPACE_DECL {
1718
namespace write_utils {
1819

19-
LIBC_INLINE int stdout_write_hook(cpp::string_view new_str, void *) {
20-
write_to_stdout(new_str);
21-
return printf_core::WRITE_OK;
22-
}
23-
24-
LIBC_INLINE int stderr_write_hook(cpp::string_view new_str, void *) {
25-
write_to_stderr(new_str);
26-
return printf_core::WRITE_OK;
27-
}
28-
2920
LIBC_INLINE void write(::FILE *f, cpp::string_view new_str) {
3021
if (f == stdout) {
3122
write_to_stdout(new_str);
32-
} else {
23+
} else if (f == stderr) {
3324
write_to_stderr(new_str);
25+
} else {
26+
libc_errno = 1;
3427
}
3528
}
3629

3730
using StreamWriter = int (*)(cpp::string_view, void *);
3831
LIBC_INLINE StreamWriter get_write_hook(::FILE *f) {
39-
if (f == stdout)
32+
if (f == stdout) {
4033
return &stdout_write_hook;
34+
} else if (f == stderr) {
35+
return &stderr_write_hook;
36+
}
4137

42-
return &stderr_write_hook;
38+
libc_errno = 1;
39+
return NULL;
4340
}
4441

4542
} // namespace write_utils

0 commit comments

Comments
 (0)