Skip to content

Commit 443cf3e

Browse files
authored
Add Pico-SDK support for compiler definitions
Similar to swiftlang/swift-embedded-examples#63, this addresses issues regarding compiler definitions when using the Pico SDK with swiftc.
1 parent 4f53931 commit 443cf3e

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

docs/EmbeddedSwift/IntegratingWithSDKs.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Finally, we need to define the application's build rules in CMake that will be u
6060

6161
We'll also make sure to dynamically set the Swift compiler's target architecture based on the pico board used. This is to support both RP2040 and RP2350 in ARM mode, and RP2350's RISC-V mode.
6262

63+
Finally, we'll make sure to recursively gather all Pico SDK-related compiler definitions in order to append them to our `swiftc` command.
6364

6465
```cmake
6566
cmake_minimum_required(VERSION 3.13)
@@ -86,11 +87,56 @@ elseif(PICO_PLATFORM STREQUAL "rp2350-riscv")
8687
endif()
8788
8889
add_executable(swift-blinky)
90+
91+
target_link_libraries(swift-blinky
92+
pico_stdlib hardware_uart hardware_gpio
93+
)
94+
95+
set_property(GLOBAL PROPERTY visited_targets "")
96+
set_property(GLOBAL PROPERTY compilerdefs_list "")
97+
98+
function(gather_compile_definitions_recursive target)
99+
get_property(visited_targets GLOBAL PROPERTY visited_targets)
100+
101+
if (${target} MATCHES "\\$<" OR ${target} MATCHES "::@" OR ${target} IN_LIST visited_targets)
102+
return()
103+
endif()
104+
105+
list(APPEND visited_targets ${target})
106+
set_property(GLOBAL PROPERTY visited_targets "${visited_targets}")
107+
108+
get_property(compilerdefs_list GLOBAL PROPERTY compilerdefs_list)
109+
110+
get_target_property(target_definitions ${target} INTERFACE_COMPILE_DEFINITIONS)
111+
if (target_definitions)
112+
list(APPEND compilerdefs_list ${target_definitions})
113+
set_property(GLOBAL PROPERTY compilerdefs_list "${compilerdefs_list}")
114+
endif()
115+
116+
get_target_property(target_linked_libs ${target} INTERFACE_LINK_LIBRARIES)
117+
if (target_linked_libs)
118+
foreach(linked_target ${target_linked_libs})
119+
gather_compile_definitions_recursive(${linked_target})
120+
endforeach()
121+
endif()
122+
endfunction()
123+
124+
gather_compile_definitions_recursive(swift-blinky)
125+
get_property(COMPILE_DEFINITIONS GLOBAL PROPERTY compilerdefs_list)
126+
127+
list(REMOVE_DUPLICATES COMPILE_DEFINITIONS)
128+
list(PREPEND COMPILE_DEFINITIONS "") # -Xcc -D
129+
string(REPLACE "$<TARGET_PROPERTY:PICO_TARGET_BINARY_TYPE>" "$<TARGET_PROPERTY:swift-blinky,PICO_TARGET_BINARY_TYPE>" COMPILE_DEFINITIONS "${COMPILE_DEFINITIONS}")
130+
string(REPLACE ";" " -Xcc -D" COMPILE_DEFINITIONS "${COMPILE_DEFINITIONS}")
131+
132+
file(GENERATE OUTPUT ${CMAKE_BINARY_DIR}/swiftc_flags.txt CONTENT "${COMPILE_DEFINITIONS}")
133+
89134
add_custom_command(
90135
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
91136
COMMAND
92137
${SWIFTC}
93138
${SWIFT_TARGET} ${CLANG_ARCH_ABI_FLAGS} -Xcc -fshort-enums
139+
@${CMAKE_BINARY_DIR}/swiftc_flags.txt
94140
-Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library
95141
$$\( echo '$<TARGET_PROPERTY:swift-blinky,INCLUDE_DIRECTORIES>' | tr '\;' '\\n' | sed -e 's/\\\(.*\\\)/-Xcc -I\\1/g' \)
96142
$$\( echo '${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}' | tr ' ' '\\n' | sed -e 's/\\\(.*\\\)/-Xcc -I\\1/g' \)
@@ -104,7 +150,6 @@ add_custom_command(
104150
add_custom_target(swift-blinky-swiftcode DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o)
105151
106152
target_link_libraries(swift-blinky
107-
pico_stdlib hardware_uart hardware_gpio
108153
${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
109154
)
110155
add_dependencies(swift-blinky swift-blinky-swiftcode)

0 commit comments

Comments
 (0)