Skip to content

Commit 27879f9

Browse files
authored
Pico SDK - Fix Compiler Definitions & RISC-V RP2350 support (#63)
* Delete pico-blink-sdk/hacky_cmake_helper.cmake * Update CMakeLists.txt * Update README.md * Refactor CLANG_ARCH_ABI_FLAGS Refactored the CLANG_ARCH_ABI_FLAGS back into each if/else block. * Remove unnecessary output to file of compiler flags (1/2) * Remove unnecessary output to file of compiler flags (2/2) * Remove rendundant function * Added RISC-V info Fixes: #59
1 parent c617e5b commit 27879f9

File tree

3 files changed

+117
-9
lines changed

3 files changed

+117
-9
lines changed

pico-blink-sdk/CMakeLists.txt

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,81 @@ else()
1010
execute_process(COMMAND which swiftc OUTPUT_VARIABLE SWIFTC OUTPUT_STRIP_TRAILING_WHITESPACE)
1111
endif()
1212

13-
1413
set(SWIFT_TARGET "armv6m-none-none-eabi") # default for rp2040
1514

1615
if(PICO_PLATFORM STREQUAL "rp2350-arm-s")
1716
message(STATUS "PICO_PLATFORM is set to rp2350-arm-s, using armv7em")
1817
set(SWIFT_TARGET "armv7em-none-none-eabi")
18+
list(APPEND CLANG_ARCH_ABI_FLAGS "-Xcc" "-mfloat-abi=soft")
19+
elseif(PICO_PLATFORM STREQUAL "rp2040")
20+
message(STATUS "PICO_PLATFORM is set to RP2040, using armv6m")
21+
list(APPEND CLANG_ARCH_ABI_FLAGS "-Xcc" "-mfloat-abi=soft")
1922
elseif(PICO_PLATFORM STREQUAL "rp2350-riscv")
20-
# Untested, gives PICO-SDK errors when building
21-
message(WARNING "PICO_PLATFORM is set to rp2350-riscv, using riscv32 (untested). It is recommended to use rp2350-arm-s.")
23+
message(STATUS "PICO_PLATFORM is set to rp2350-riscv, using riscv32.")
2224
set(SWIFT_TARGET "riscv32-none-none-eabi")
25+
list(APPEND CLANG_ARCH_ABI_FLAGS "-Xcc" "-march=rv32imac_zicsr_zifencei_zba_zbb_zbs_zbkb" "-Xcc" "-mabi=ilp32")
2326
endif()
2427

2528
add_executable(swift-blinky)
29+
30+
target_link_libraries(swift-blinky
31+
pico_stdlib hardware_uart hardware_gpio
32+
)
33+
34+
# Gather compile definitions from all dependencies
35+
36+
set_property(GLOBAL PROPERTY visited_targets "")
37+
set_property(GLOBAL PROPERTY compilerdefs_list "")
38+
39+
function(gather_compile_definitions_recursive target)
40+
# Get the current value of visited_targets
41+
get_property(visited_targets GLOBAL PROPERTY visited_targets)
42+
43+
# make sure we don't visit the same target twice
44+
# and that we don't visit the special generator expressions
45+
if (${target} MATCHES "\\$<" OR ${target} MATCHES "::@" OR ${target} IN_LIST visited_targets)
46+
return()
47+
endif()
48+
49+
# Append the target to visited_targets
50+
list(APPEND visited_targets ${target})
51+
set_property(GLOBAL PROPERTY visited_targets "${visited_targets}")
52+
53+
# Get the current value of compilerdefs_list
54+
get_property(compilerdefs_list GLOBAL PROPERTY compilerdefs_list)
55+
56+
get_target_property(target_definitions ${target} INTERFACE_COMPILE_DEFINITIONS)
57+
if (target_definitions)
58+
# Append the target definitions to compilerdefs_list
59+
list(APPEND compilerdefs_list ${target_definitions})
60+
set_property(GLOBAL PROPERTY compilerdefs_list "${compilerdefs_list}")
61+
endif()
62+
63+
get_target_property(target_linked_libs ${target} INTERFACE_LINK_LIBRARIES)
64+
if (target_linked_libs)
65+
foreach(linked_target ${target_linked_libs})
66+
# Recursively gather compile definitions from dependencies
67+
gather_compile_definitions_recursive(${linked_target})
68+
endforeach()
69+
endif()
70+
endfunction()
71+
72+
gather_compile_definitions_recursive(swift-blinky)
73+
get_property(COMPILE_DEFINITIONS GLOBAL PROPERTY compilerdefs_list)
74+
75+
# Parse compiler definitions into a format that swiftc can understand
76+
list(REMOVE_DUPLICATES COMPILE_DEFINITIONS)
77+
list(PREPEND COMPILE_DEFINITIONS "")
78+
string(REPLACE "$<TARGET_PROPERTY:PICO_TARGET_BINARY_TYPE>" "$<TARGET_PROPERTY:swift-blinky,PICO_TARGET_BINARY_TYPE>" COMPILE_DEFINITIONS "${COMPILE_DEFINITIONS}")
79+
string(REPLACE ";" ";-Xcc;-D" COMPILE_DEFINITIONS "${COMPILE_DEFINITIONS}")
80+
2681
add_custom_command(
2782
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
2883
COMMAND
2984
${SWIFTC}
30-
-target ${SWIFT_TARGET} -Xcc -mfloat-abi=soft -Xcc -fshort-enums
85+
-target ${SWIFT_TARGET} -Xcc -fshort-enums
86+
${COMPILE_DEFINITIONS}
87+
${CLANG_ARCH_ABI_FLAGS}
3188
-Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library
3289
$$\( echo '$<TARGET_PROPERTY:swift-blinky,INCLUDE_DIRECTORIES>' | tr '\;' '\\n' | sed -e 's/\\\(.*\\\)/-Xcc -I\\1/g' \)
3390
$$\( echo '${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}' | tr ' ' '\\n' | sed -e 's/\\\(.*\\\)/-Xcc -I\\1/g' \)
@@ -40,8 +97,8 @@ add_custom_command(
4097
)
4198
add_custom_target(swift-blinky-swiftcode DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o)
4299

100+
43101
target_link_libraries(swift-blinky
44-
pico_stdlib hardware_uart hardware_gpio
45102
${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
46103
)
47104
add_dependencies(swift-blinky swift-blinky-swiftcode)

pico-blink-sdk/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ This example demonstrates how to integrate with the Pico SDK which is using CMak
88

99
- A Raspberry Pi Pico (non-W) board. If you have a Pico W instead, refer to the [pico-w-blink-sdk](../pico-w-blink-sdk) sample instead.
1010
- Follow the setup steps at https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf, in particular you'll need:
11-
- A checkout of the [pico-sdk](https://github.com/raspberrypi/pico-sdk.git), with git submodules checked out.
11+
- A checkout of the [pico-sdk](https://github.com/raspberrypi/pico-sdk.git), with git submodules checked out. If you'd like to try RISC-V support for RP2350, you'll need Pico SDK 2.0.1 or later (currently in the *development branch only*).
1212
- A checkout of the [pico-examples](https://github.com/raspberrypi/pico-examples.git).
1313
- CMake.
14-
- The [Arm Embedded Toolchain](https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads).
14+
- The [Arm Embedded Toolchain](https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads), or the RISC-V tollchain if you want to build for the RISC-V cores on the RP2350.
1515
- Before trying to use Swift with the Pico SDK, make sure your environment works and can build the provided C/C++ sample projects, in particular:
1616
- Try building and running the "blink" example from pico-examples written in C.
1717

@@ -25,8 +25,9 @@ This example demonstrates how to integrate with the Pico SDK which is using CMak
2525
$ cd pico-blink-sdk
2626
$ export TOOLCHAINS='<toolchain-identifier>'
2727
$ export PICO_BOARD='<board-name>' # Examples: pico, pico2
28+
$ export PICO_PLATFORM='<optional-platform-name>' # Optional; useful if you'd like to compile for RISC-V. Examples: rp2040, rp2350-arm-s, rp2350-riscv
2829
$ export PICO_SDK_PATH='<path-to-your-pico-sdk>'
29-
$ export PICO_TOOLCHAIN_PATH='<path-to-the-arm-toolchain>' # A default `brew` install will be located at `/Applications/ArmGNUToolchain/[version]/arm-none-eabi`
30+
$ export PICO_TOOLCHAIN_PATH='<path-to-the-compiler-toolchain>' # A default `brew` install will be located at `/Applications/ArmGNUToolchain/[version]/arm-none-eabi`. This can also be a RISC-V toolchain for the RP2350.
3031
$ cmake -B build -G Ninja .
3132
$ cmake --build build
3233
```

pico-w-blink-sdk/CMakeLists.txt

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,62 @@ execute_process(COMMAND which swiftc OUTPUT_VARIABLE SWIFTC OUTPUT_STRIP_TRAILIN
1111
endif()
1212

1313
add_executable(swift-blinky)
14+
target_link_libraries(swift-blinky
15+
pico_stdlib hardware_uart hardware_gpio pico_lwip_arch pico_cyw43_arch_none
16+
)
17+
18+
# Gather compile definitions from all dependencies
19+
set_property(GLOBAL PROPERTY visited_targets "")
20+
set_property(GLOBAL PROPERTY compilerdefs_list "")
21+
22+
function(gather_compile_definitions_recursive target)
23+
# Get the current value of visited_targets
24+
get_property(visited_targets GLOBAL PROPERTY visited_targets)
25+
26+
# make sure we don't visit the same target twice
27+
# and that we don't visit the special generator expressions
28+
if (${target} MATCHES "\\$<" OR ${target} MATCHES "::@" OR ${target} IN_LIST visited_targets)
29+
return()
30+
endif()
31+
32+
# Append the target to visited_targets
33+
list(APPEND visited_targets ${target})
34+
set_property(GLOBAL PROPERTY visited_targets "${visited_targets}")
35+
36+
# Get the current value of compilerdefs_list
37+
get_property(compilerdefs_list GLOBAL PROPERTY compilerdefs_list)
38+
39+
get_target_property(target_definitions ${target} INTERFACE_COMPILE_DEFINITIONS)
40+
if (target_definitions)
41+
# Append the target definitions to compilerdefs_list
42+
list(APPEND compilerdefs_list ${target_definitions})
43+
set_property(GLOBAL PROPERTY compilerdefs_list "${compilerdefs_list}")
44+
endif()
45+
46+
get_target_property(target_linked_libs ${target} INTERFACE_LINK_LIBRARIES)
47+
if (target_linked_libs)
48+
foreach(linked_target ${target_linked_libs})
49+
# Recursively gather compile definitions from dependencies
50+
gather_compile_definitions_recursive(${linked_target})
51+
endforeach()
52+
endif()
53+
endfunction()
54+
55+
gather_compile_definitions_recursive(swift-blinky)
56+
get_property(COMPILE_DEFINITIONS GLOBAL PROPERTY compilerdefs_list)
57+
58+
# Parse compiler definitions into a format that swiftc can understand
59+
list(REMOVE_DUPLICATES COMPILE_DEFINITIONS)
60+
list(PREPEND COMPILE_DEFINITIONS "") # adds a semicolon at the beginning
61+
string(REPLACE "$<TARGET_PROPERTY:PICO_TARGET_BINARY_TYPE>" "$<TARGET_PROPERTY:swift-blinky,PICO_TARGET_BINARY_TYPE>" COMPILE_DEFINITIONS "${COMPILE_DEFINITIONS}")
62+
string(REPLACE ";" ";-Xcc;-D" COMPILE_DEFINITIONS "${COMPILE_DEFINITIONS}")
63+
1464
add_custom_command(
1565
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
1666
COMMAND
1767
${SWIFTC}
1868
-target armv6m-none-none-eabi -Xcc -mfloat-abi=soft -Xcc -fshort-enums
69+
${COMPILE_DEFINITIONS}
1970
-Xcc -DCYW43_LWIP
2071
-Xcc -DPICO_CYW43_ARCH_THREADSAFE_BACKGROUND
2172
-Xcc -I$ENV{PICO_SDK_PATH}/lib/lwip/src/include
@@ -33,7 +84,6 @@ add_custom_command(
3384
add_custom_target(swift-blinky-swiftcode DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o)
3485

3586
target_link_libraries(swift-blinky
36-
pico_stdlib hardware_uart hardware_gpio pico_lwip_arch pico_cyw43_arch_none
3787
${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
3888
)
3989
add_dependencies(swift-blinky swift-blinky-swiftcode)

0 commit comments

Comments
 (0)