1pico_add_library(pico_runtime)
2
3target_sources(pico_runtime INTERFACE
4        ${CMAKE_CURRENT_LIST_DIR}/runtime.c
5)
6
7target_include_directories(pico_runtime_headers SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)
8
9pico_mirrored_target_link_libraries(pico_runtime INTERFACE
10        pico_base
11        pico_runtime_init
12)
13
14if (TARGET hardware_gpio_headers)
15    target_link_libraries(pico_runtime INTERFACE hardware_gpio_headers) # to determine if we should init GPIO copro
16endif()
17if (TARGET hardware_riscv_headers)
18    target_link_libraries(pico_runtime INTERFACE hardware_riscv_headers)
19endif()
20
21set(PICO_RUNTIME_LIBRARIES
22        hardware_uart
23        pico_bit_ops
24        pico_divider
25        pico_double
26        pico_int64_ops
27        pico_float
28        pico_malloc
29        pico_mem_ops
30        pico_atomic
31        pico_cxx_options
32        pico_standard_binary_info
33        pico_standard_link
34        pico_sync
35        pico_printf
36        pico_crt0
37        pico_clib_interface
38        pico_stdio
39)
40
41foreach(LIB IN LISTS PICO_RUNTIME_LIBRARIES)
42    if (TARGET ${LIB})
43        pico_mirrored_target_link_libraries(pico_runtime INTERFACE ${LIB})
44    endif()
45endforeach()
46
47# todo is this correct/needed?
48if (PICO_C_COMPILER_IS_GNU)
49    target_link_options(pico_runtime INTERFACE "--specs=nosys.specs")
50elseif (PICO_C_COMPILER_IS_CLANG)
51   # target_link_options(pico_runtime INTERFACE "-nostdlib")
52endif()
53
54# pico_minimize_runtime((INCLUDE ...) (EXCLUDE ...))
55#
56# INCLUDE/EXCLUDE can contain any of the following (all defaulting to not included)
57#
58# DEFAULT_ALARM_POOL    - default alarm pool setup
59# PRINTF                - full printf support
60# PRINTF_MINIMAL        - printf support without the following
61# PRINTF_FLOAT          - to control float support if printf is enabled
62# PRINTF_EXPONENTIAL
63# PRINTF_LONG_LONG
64# PRINTF_PTRDIFF_T
65# FLOAT                 - support for single-precision floating point
66# DOUBLE                - support for double-precision floating point
67# FPGA_CHECK            - checks for FPGA which allows Raspberry Pi to run your binary on FPGA
68# PANIC                 - default panic impl which brings in stdio
69# AUTO_INIT_MUTEX       - auto init mutexes; without this you get no printf mutex either               -
70function(pico_minimize_runtime TARGET)
71    set(ALL_ITEMS
72            DEFAULT_ALARM_POOL
73            PRINTF
74            FLOAT
75            DOUBLE
76            FPGA_CHECK
77            PANIC
78            AUTO_INIT_MUTEX)
79    cmake_parse_arguments(RUNTIME "" ""
80            "INCLUDE;EXCLUDE" ${ARGN} )
81    foreach (INCL_EXCL IN ITEMS INCLUDE EXCLUDE)
82        if (INCL_EXCL STREQUAL "INCLUDE")
83            set(VAL 1)
84        else()
85            set(VAL 0)
86        endif()
87        foreach(VAR IN LISTS RUNTIME_${INCL_EXCL})
88            if (VAR STREQUAL "ALL")
89                foreach(ITEM IN LISTS ALL_ITEMS)
90                    set(RUNTIME_INCLUDE_${ITEM} ${VAL})
91                endforeach ()
92            else()
93                set(RUNTIME_INCLUDE_${VAR} ${VAL})
94            endif()
95        endforeach ()
96    endforeach ()
97    if (NOT RUNTIME_INCLUDE_DEFAULT_ALARM_POOL)
98        target_compile_definitions(${TARGET} PRIVATE PICO_TIME_DEFAULT_ALARM_POOL_DISABLED=1)
99    endif()
100    if (NOT RUNTIME_INCLUDE_FLOAT)
101        pico_set_float_implementation(${TARGET} none)
102    endif()
103    if (NOT RUNTIME_INCLUDE_DOUBLE)
104        pico_set_double_implementation(${TARGET} none)
105    endif()
106    if (NOT RUNTIME_INCLUDE_AUTO_INIT_MUTEX)
107        target_compile_definitions(${TARGET} PRIVATE PICO_RUNTIME_SKIP_INIT_MUTEX=1)
108    endif()
109
110    if (RUNTIME_INCLUDE_PRINTF)
111        if (NOT RUNTIME_INCLUDE_PRINTF_MINIMAL)
112            set( RUNTIME_INCLUDE_PRINTF_FLOAT 1)
113            set( RUNTIME_INCLUDE_PRINTF_EXPONENTIAL 1)
114            set( RUNTIME_INCLUDE_PRINTF_LONG_LONG 1)
115            set( RUNTIME_INCLUDE_PRINTF_PTRDIFF_T 1)
116        endif()
117        if (NOT RUNTIME_INCLUDE_PRINTF_FLOAT)
118            target_compile_definitions(${TARGET} PRIVATE PICO_PRINTF_SUPPORT_FLOAT=0)
119        endif()
120        if (NOT RUNTIME_INCLUDE_PRINTF_EXPONENTIAL)
121            target_compile_definitions(${TARGET} PRIVATE PICO_PRINTF_SUPPORT_EXPONENTIAL=0)
122        endif()
123        if (NOT RUNTIME_INCLUDE_PRINTF_LONG_LONG)
124            target_compile_definitions(${TARGET} PRIVATE PICO_PRINTF_SUPPORT_LONG_LONG=0)
125        endif()
126        if (NOT RUNTIME_INCLUDE_PRINTF_PTRDIFF_T)
127            target_compile_definitions(${TARGET} PRIVATE PICO_PRINTF_SUPPORT_PTRDIFF_T=0)
128        endif()
129    else()
130        pico_set_printf_implementation(${TARGET} none)
131    endif()
132
133    if (NOT RUNTIME_INCLUDE_PANIC)
134        target_compile_definitions(${TARGET} PRIVATE
135                PICO_PANIC_FUNCTION= #the default uses puts
136        )
137    endif()
138    if (NOT RUNTIME_INCLUDE_AUTO_INIT_MUTEX)
139        target_compile_definitions(${TARGET} PRIVATE
140                PICO_RUNTIME_NO_INIT_MUTEX=1
141                PICO_STDOUT_MUTEX=0 # currently pulls in mutex code otherwise
142        )
143    endif()
144    if (NOT RUNTIME_INCLUDE_FPGA_CHECK)
145        target_compile_definitions(${TARGET} PRIVATE PICO_NO_FPGA_CHECK=1)
146    endif()
147endfunction()