1if (NOT TARGET pico_printf)
2    # library to be depended on - we make this depend on particular implementations using per target generator expressions
3    pico_add_library(pico_printf)
4
5    # no custom implementation; falls thru to compiler
6    pico_add_library(pico_printf_compiler)
7
8    target_include_directories(pico_printf_headers SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)
9
10    # add alias "default" which is just pico.
11    add_library(pico_printf_default INTERFACE)
12    target_link_libraries(pico_printf_default INTERFACE pico_printf_pico)
13
14    set(PICO_DEFAULT_PRINTF_IMPL pico_printf_default)
15
16    target_link_libraries(pico_printf INTERFACE
17            $<IF:$<BOOL:$<TARGET_PROPERTY:PICO_TARGET_PRINTF_IMPL>>,$<TARGET_PROPERTY:PICO_TARGET_PRINTF_IMPL>,${PICO_DEFAULT_PRINTF_IMPL}>)
18
19    pico_add_library(pico_printf_pico)
20    target_sources(pico_printf_pico INTERFACE
21            ${CMAKE_CURRENT_LIST_DIR}/printf.c
22    )
23    target_link_libraries(pico_printf_pico INTERFACE pico_printf_headers)
24
25    pico_add_library(pico_printf_none)
26    target_sources(pico_printf_none INTERFACE
27            ${CMAKE_CURRENT_LIST_DIR}/printf_none.S
28    )
29    target_link_libraries(pico_printf_none INTERFACE pico_printf_headers)
30
31    function(wrap_printf_functions TARGET)
32        # note that printf and vprintf are in pico_stdio so we can provide thread safety
33        pico_wrap_function(${TARGET} sprintf)
34        pico_wrap_function(${TARGET} snprintf)
35        pico_wrap_function(${TARGET} vsnprintf)
36    endfunction()
37
38    wrap_printf_functions(pico_printf_pico)
39    wrap_printf_functions(pico_printf_none)
40
41    # always hook printf for printf_none with out weak impl, as it is handled by pico_stdio
42    # but that library may not be included
43    pico_wrap_function(pico_printf_none printf)
44
45    macro(pico_set_printf_implementation TARGET IMPL)
46        get_target_property(target_type ${TARGET} TYPE)
47        if ("EXECUTABLE" STREQUAL "${target_type}")
48            set_target_properties(${TARGET} PROPERTIES PICO_TARGET_PRINTF_IMPL "pico_printf_${IMPL}")
49        else()
50            message(FATAL_ERROR "printf implementation must be set on executable not library")
51        endif()
52    endmacro()
53endif()
54