Skip to content

Commit

Permalink
Allow disabling pico_stdio function wrapping
Browse files Browse the repository at this point in the history
 - Copy the approach used by pico_printf and apply it to pico_stdio to
   allow a developer to opt out of stdio printf/putchar/etc. wrappers.
 - Add some #if/#endif around some functions that are not used if
   PICO_PRINTF_PICO isn't 1 to stop compiler warnings about unused
   functions.
  • Loading branch information
gemarcano committed Jul 16, 2024
1 parent c93c3f4 commit 310337e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
45 changes: 38 additions & 7 deletions src/rp2_common/pico_stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
if (NOT TARGET pico_stdio)
# library to be depended on - we make this depend on particular implementations using per target generator expressions
pico_add_library(pico_stdio)

# no custom implementation; falls thru to compiler
pico_add_library(pico_stdio_compiler)
target_sources(pico_stdio_compiler INTERFACE
${CMAKE_CURRENT_LIST_DIR}/stdio.c
)

target_include_directories(pico_stdio_headers INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)

target_sources(pico_stdio INTERFACE
# add alias "default" which is just pico
add_library(pico_stdio_default INTERFACE)
target_link_libraries(pico_stdio_default INTERFACE pico_stdio_pico)

set(PICO_DEFAULT_STDIO_IMPL pico_stdio_default)

target_link_libraries(pico_stdio INTERFACE
$<IF:$<BOOL:$<TARGET_PROPERTY:PICO_TARGET_STDIO_IMPL>>,$<TARGET_PROPERTY:PICO_TARGET_STDIO_IMPL>,${PICO_DEFAULT_STDIO_IMPL}>)

pico_add_library(pico_stdio_pico)
target_sources(pico_stdio_pico INTERFACE
${CMAKE_CURRENT_LIST_DIR}/stdio.c
)
target_link_libraries(pico_stdio_pico INTERFACE pico_stdio_headers)

function(wrap_stdio_functions TARGET)
pico_wrap_function(${TARGET} printf)
pico_wrap_function(${TARGET} vprintf)
pico_wrap_function(${TARGET} puts)
pico_wrap_function(${TARGET} putchar)
pico_wrap_function(${TARGET} getchar)
endfunction()

wrap_stdio_functions(pico_stdio_pico)

pico_wrap_function(pico_stdio printf)
pico_wrap_function(pico_stdio vprintf)
pico_wrap_function(pico_stdio puts)
pico_wrap_function(pico_stdio putchar)
pico_wrap_function(pico_stdio getchar)
macro(pico_set_stdio_implementation TARGET IMPL)
get_target_property(target_type ${TARGET} TYPE)
if ("EXECUTABLE" STREQUAL "${target_type}")
set_target_properties(${TARGET} PROPERTIES PICO_TARGET_STDIO_IMPL "pico_stdio_${IMPL}")
else()
message(FATAL_ERROR "stdio implementation must be set on executable not library")
endif()
endmacro()

if (TARGET pico_printf)
pico_mirrored_target_link_libraries(pico_stdio INTERFACE pico_printf)
endif()
endif()
endif()
2 changes: 2 additions & 0 deletions src/rp2_common/pico_stdio/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ typedef struct stdio_stack_buffer {
char buf[PICO_STDIO_STACK_BUFFER_SIZE];
} stdio_stack_buffer_t;

#if LIB_PICO_PRINTF_PICO
static void stdio_stack_buffer_flush(stdio_stack_buffer_t *buffer) {
if (buffer->used) {
for (stdio_driver_t *d = drivers; d; d = d->next) {
Expand All @@ -243,6 +244,7 @@ static void stdio_buffered_printer(char c, void *arg) {
}
buffer->buf[buffer->used++] = c;
}
#endif

int WRAPPER_FUNC(vprintf)(const char *format, va_list va) {
bool serialzed = stdout_serialize_begin();
Expand Down

0 comments on commit 310337e

Please sign in to comment.