Skip to content

Commit d4b7834

Browse files
committed
Make clib output backend selectable
Add compile-time selection for C library output backends in printf.cpp, putchar.cpp and puts.cpp. When CONFIG_CLIB_USE_UART0 or CONFIG_CLIB_USE_NULL is defined the code aliases uart0::PutChar/Puts or a no-op implementation, otherwise it falls back to console::PutChar/Puts. Call sites were updated to use the generic PutChar/Puts identifiers. Also collapse the flag enum into a single line and tidy some qualification usage. This enables building the C library with UART0 or a null/no-op output backend.
1 parent 9847fb8 commit d4b7834

3 files changed

Lines changed: 46 additions & 15 deletions

File tree

‎lib-clib/src/printf.cpp‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,22 @@
3030
#include <cstring>
3131
#include <climits>
3232

33+
#if defined(CONFIG_CLIB_USE_UART0)
34+
namespace uart0 {
35+
void PutChar(int);
36+
} // namespace uart0
37+
using uart0::PutChar;
38+
#elif defined(CONFIG_CLIB_USE_NULL)
39+
namespace {
40+
void PutChar([[maybe_unused]] int) {}
41+
} // namespace
42+
using ::PutChar;
43+
#else
3344
namespace console {
3445
void PutChar(int);
35-
}
46+
} // namespace console
47+
using console::PutChar;
48+
#endif
3649

3750
struct Context {
3851
int flag;
@@ -42,15 +55,7 @@ struct Context {
4255
int capacity;
4356
};
4457

45-
enum {
46-
kFlagPrecision = (1U << 0),
47-
kFlagUppercase = (1U << 1),
48-
kFlagLong = (1U << 2),
49-
kFlagNegative = (1U << 3),
50-
kFlagMinWidth = (1U << 4),
51-
kFlagZeroPadded = (1U << 5),
52-
kFlagLeftJustified = (1U << 6)
53-
};
58+
enum { kFlagPrecision = (1U << 0), kFlagUppercase = (1U << 1), kFlagLong = (1U << 2), kFlagNegative = (1U << 3), kFlagMinWidth = (1U << 4), kFlagZeroPadded = (1U << 5), kFlagLeftJustified = (1U << 6) };
5459

5560
static char* outptr = nullptr;
5661

@@ -64,7 +69,7 @@ inline static void PutChar(struct Context* ctx, int c) {
6469
return;
6570
}
6671

67-
console::PutChar(c);
72+
PutChar(c);
6873
}
6974

7075
static void FormatHex(struct Context* ctx, unsigned int arg) {

‎lib-clib/src/putchar.cpp‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,24 @@
2323
* THE SOFTWARE.
2424
*/
2525

26+
#if defined(CONFIG_CLIB_USE_UART0)
27+
namespace uart0 {
28+
void PutChar(int);
29+
} // namespace uart0
30+
using uart0::PutChar;
31+
#elif defined(CONFIG_CLIB_USE_NULL)
32+
namespace {
33+
void PutChar([[maybe_unused]] int) {}
34+
} // namespace
35+
using ::PutChar;
36+
#else
2637
namespace console {
2738
void PutChar(int);
28-
}
39+
} // namespace console
40+
using console::PutChar;
41+
#endif
2942

3043
extern "C" int putchar(int c) { // NOLINT
31-
console::PutChar(c);
44+
PutChar(c);
3245
return 1;
3346
}

‎lib-clib/src/puts.cpp‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,24 @@
2323
* THE SOFTWARE.
2424
*/
2525

26+
#if defined(CONFIG_CLIB_USE_UART0)
27+
namespace uart0 {
28+
void Puts(const char* s);
29+
} // namespace uart0
30+
using uart0::Puts;
31+
#elif defined(CONFIG_CLIB_USE_NULL)
32+
namespace {
33+
void Puts([maybe_unused]] const char* s) {}
34+
} // namespace
35+
using ::Puts;
36+
#else
2637
namespace console {
27-
void Puts(const char*);
38+
void Puts(const char* s);
2839
} // namespace console
40+
using console::Puts;
41+
#endif
2942

3043
extern "C" int puts(const char* s) { // NOLINT
31-
console::Puts(s);
44+
Puts(s);
3245
return 1;
3346
}

0 commit comments

Comments
 (0)