Skip to content

Commit

Permalink
Make printf weak and add libprintf_flt.a
Browse files Browse the repository at this point in the history
This allows substituting a fully-featured printf that supports floating
point via -lprintf_flt, without blowing up the binaries for most
applications. This is the approach that AVR uses.
  • Loading branch information
mysterymath committed Nov 30, 2023
1 parent 6fa609b commit 5597699
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 5 additions & 1 deletion mos-platform/common/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ add_platform_library(common-c
)
# Prevent the implementation of libcalls from being reduced to a call of those libcalls.
set_property(SOURCE mem.c PROPERTY COMPILE_OPTIONS -fno-builtin-memset)
set_property(SOURCE printf.c PROPERTY COMPILE_DEFINITIONS
set_property(TARGET common-c PROPERTY COMPILE_DEFINITIONS
PRINTF_DISABLE_SUPPORT_FLOAT
PRINTF_DISABLE_SUPPORT_EXPONENTIAL
PRINTF_WEAK
)
target_include_directories(common-c SYSTEM BEFORE PUBLIC ${INCLUDE_DIR})
target_link_libraries(common-c PRIVATE common-asminc)

add_platform_library(common-printf_flt printf.c)
target_include_directories(common-printf_flt SYSTEM BEFORE PUBLIC ${INCLUDE_DIR})
16 changes: 11 additions & 5 deletions mos-platform/common/c/printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@
#define PRINTF_SUPPORT_PTRDIFF_T
#endif

#if PRINTF_WEAK
#define PRINTF_ATTR __attribute__((weak))
#else
#define PRINTF_ATTR
#endif

///////////////////////////////////////////////////////////////////////////////

// internal flag definitions
Expand Down Expand Up @@ -938,35 +944,35 @@ static int _vsnprintf(out_fct_type out, char *buffer, const size_t maxlen,

///////////////////////////////////////////////////////////////////////////////

int printf(const char *format, ...) {
PRINTF_ATTR int printf(const char *format, ...) {
va_list va;
va_start(va, format);
const int ret = vprintf(format, va);
va_end(va);
return ret;
}

int sprintf(char *buffer, const char *format, ...) {
PRINTF_ATTR int sprintf(char *buffer, const char *format, ...) {
va_list va;
va_start(va, format);
const int ret = vsnprintf(buffer, (size_t)-1, format, va);
va_end(va);
return ret;
}

int snprintf(char *buffer, size_t count, const char *format, ...) {
PRINTF_ATTR int snprintf(char *buffer, size_t count, const char *format, ...) {
va_list va;
va_start(va, format);
const int ret = vsnprintf(buffer, count, format, va);
va_end(va);
return ret;
}

int vprintf(const char *format, va_list va) {
PRINTF_ATTR int vprintf(const char *format, va_list va) {
char buffer[1];
return _vsnprintf(_out_char, buffer, (size_t)-1, format, va);
}

int vsnprintf(char *buffer, size_t count, const char *format, va_list va) {
PRINTF_ATTR int vsnprintf(char *buffer, size_t count, const char *format, va_list va) {
return _vsnprintf(_out_buffer, buffer, count, format, va);
}

0 comments on commit 5597699

Please sign in to comment.