diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ad4190..c75081c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,9 +48,18 @@ ) endif() +# Validate selected printf library +set(MBED_PRINTF_LIB_TYPES std minimal-printf) +if(NOT ${MBED_PRINTF_LIB} IN_LIST MBED_PRINTF_LIB_TYPES) + message(FATAL_ERROR + "Invalid printf library type '${MBED_PRINTF_LIB}'. Possible values:\n ${MBED_PRINTF_LIB_TYPES}" + ) +endif() + mbed_set_cpu_core_options(mbed-os ${MBED_TOOLCHAIN}) mbed_set_toolchain_options(mbed-os) mbed_set_c_lib(mbed-os ${MBED_C_LIB}) +mbed_set_printf_lib(mbed-os ${MBED_PRINTF_LIB}) mbed_set_language_standard(mbed-os) mbed_set_profile_options(mbed-os ${MBED_TOOLCHAIN}) diff --git a/docs/design-documents/tools/cmake.md b/docs/design-documents/tools/cmake.md index 96ee296..3237197 100644 --- a/docs/design-documents/tools/cmake.md +++ b/docs/design-documents/tools/cmake.md @@ -98,6 +98,7 @@ * `MBED_CPU_CORE` * `MBED_C_LIB` * `MBED_TARGET_SUPPORTED_C_LIBS` +* `MBED_PRINTF_LIB` The tools also generate an `MBED_TARGET_LABELS` variable, containing the labels, components and feature definitions from `targets.json`, used to select the required Mbed OS components to be built. diff --git a/tools/cmake/toolchains/ARM.cmake b/tools/cmake/toolchains/ARM.cmake index 98fd4e1..548fa47 100644 --- a/tools/cmake/toolchains/ARM.cmake +++ b/tools/cmake/toolchains/ARM.cmake @@ -79,3 +79,13 @@ ) endif() endfunction() + +# Configure the toolchain to select the selected printf library +function(mbed_set_printf_lib target lib_type) + if (${lib_type} STREQUAL "minimal-printf") + target_compile_definitions(${target} + PUBLIC + MBED_MINIMAL_PRINTF + ) + endif() +endfunction() diff --git a/tools/cmake/toolchains/GCC_ARM.cmake b/tools/cmake/toolchains/GCC_ARM.cmake index 1fe4a19..e4b26b4 100644 --- a/tools/cmake/toolchains/GCC_ARM.cmake +++ b/tools/cmake/toolchains/GCC_ARM.cmake @@ -91,3 +91,28 @@ ) endif() endfunction() + +# Configure the toolchain to select the selected printf library +function(mbed_set_printf_lib target lib_type) + if (${lib_type} STREQUAL "minimal-printf") + target_compile_definitions(${target} + PUBLIC + MBED_MINIMAL_PRINTF + ) + + list(APPEND link_options + "-Wl,--wrap,printf" + "-Wl,--wrap,sprintf" + "-Wl,--wrap,snprintf" + "-Wl,--wrap,vprintf" + "-Wl,--wrap,vsprintf" + "-Wl,--wrap,vsnprintf" + "-Wl,--wrap,fprintf" + "-Wl,--wrap,vfprintf" + ) + target_link_options(${target} + PUBLIC + ${link_options} + ) + endif() +endfunction()