Skip to content

Commit 3b5af6e

Browse files
Add Zephyr target compatibility section, improve CMakeLists.txt setup
1 parent d635ca1 commit 3b5af6e

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

Sources/EmbeddedSwift/Documentation.docc/SDKSupport/IntegrateWithZephyr.md

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ For an introduction and motivation into Embedded Swift, please see "[A Vision fo
66

77
The following document outlines how to setup a Swift to Zephyr project for an emulated ARM Cortex M0, explaining a few key concepts along the way. For a complete working example on real hardware, however, refer to the [nrfx-blink-sdk](../../../../nrfx-blink-sdk/) project that is compatible with nRF or other boards.
88

9+
## Zephyr Target Architecture Compatibility
10+
11+
Zephyr [supports quite a few target architectures](https://docs.zephyrproject.org/latest/introduction/index.html), but not all are supported by Embedded Swift. Please refer to the following table for an overview of Zephyr-supported architectures that are supported by Swift, along with the correct target triple to use:
12+
13+
| Architecture | Details | Swift Triple |
14+
|--------------|---------------------|-------------------------|
15+
| ARMv6-M | Cortex M0, M1, M3 | armv6m-none-none-eabi |
16+
| ARMv7-M | Cortex M4, M7 | armv7em-none-none-eabi |
17+
| ARMv8-M | Cortex M23-85 | aarch64-none-none-elf |
18+
| Intel | 32-bit (i686) | i686-unknown-none-elf |
19+
| Intel | 64-bit (x86_64) | x86_64-unknown-none-elf |
20+
| RISC-V | 32-bit | riscv32-none-none-eabi |
21+
| RISC-V | 64-bit | riscv64-none-none-eabi |
22+
923
## Zephyr Setup
1024

1125
Before setting up a Swift project that works with Zephyr, you need to setup dependencies and a Zephyr workspace as per the [Getting Started Guide](https://docs.zephyrproject.org/latest/develop/getting_started/index.html). Regardless of your platform (macOS or Linux), ensure that you can build the blinky example without errors before starting with Swift integration:
@@ -113,12 +127,16 @@ set(CMAKE_Swift_COMPILATION_MODE wholemodule)
113127
project(SwiftZephyrProject Swift)
114128
```
115129

116-
Next, set the compiler target to the arch you are building for. For this example we use `armv6m-none-none-eabi` which is compatible with the [Cortex-M0](https://docs.zephyrproject.org/latest/boards/qemu/cortex_m0/doc/index.html) which we will compile for in a later step. The `mfloat-abi=soft`, `-fshort-enums`, and `-fno-pic` flags are specifically for 32-bit arm architectures, so for other architectures they can be removed. However, the other flags and required for building Swift for Embedded and against Zephyr:
130+
Next, set the compiler target to the arch you are building for. For this example we use `armv6m-none-none-eabi` which is compatible with the [Cortex-M0](https://docs.zephyrproject.org/latest/boards/qemu/cortex_m0/doc/index.html) which we will compile for in a later step. If you are targeting a different architecture, see the [Zephyr Target Architecture Compatibility](#zephyr-target-architecture-compatibility) to see which target triple to use.
117131

118132
```cmake
119133
# Use the armv6m-none-none-eabi target triple for Swift
120134
set(CMAKE_Swift_COMPILER_TARGET armv6m-none-none-eabi)
135+
```
136+
137+
After setting the target triple, some additional additional Swift compiler flags need to be defined:
121138

139+
```cmake
122140
# Set global Swift compiler flags
123141
add_compile_options(
124142
# Enable Embedded Swift
@@ -127,21 +145,31 @@ add_compile_options(
127145
# Enable function sections to enable dead code stripping on elf
128146
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -function-sections>"
129147
130-
# Use software floating point operations matching GCC
131-
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -mfloat-abi=soft>"
132-
133-
# Use compacted C enums matching GCC
134-
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -fshort-enums>"
135-
136148
# Disable PIC
137149
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -fno-pic>"
138150
139-
# Add Libc include paths
140-
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -I -Xcc ${ZEPHYR_SDK_INSTALL_DIR}/arm-zephyr-eabi/picolibc/include>"
151+
# Disable PIE
152+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -fno-pie>"
141153
)
142154
```
143155

144-
The following block will automatically grab Zephyr compilation flags (such as `-D__ZEPHYR__=1` and `-DKERNEL`) and set them as Swift compiler definitions. This is required to successfully build Swift code that works with Zephyr:
156+
There are quite a few other Zephyr flags that must also be imported in order to get Zephyr include paths and flags such `-mcpu`, `-mfloat-abi`, and so on:
157+
158+
```cmake
159+
# Import TOOLCHAIN_C_FLAGS from Zephyr as -Xcc flags
160+
foreach(flag ${TOOLCHAIN_C_FLAGS})
161+
# Skip flags that are not known to swiftc
162+
string(FIND "${flag}" "-imacro" is_imacro)
163+
string(FIND "${flag}" "-mfp16-format" is_mfp16)
164+
if(NOT is_imacro EQUAL -1 OR NOT is_mfp16 EQUAL -1)
165+
continue()
166+
endif()
167+
168+
add_compile_options("$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc ${flag}>")
169+
endforeach()
170+
```
171+
172+
Next, add the following block to automatically grab Zephyr compilation flags (such as `-D__ZEPHYR__=1` and `-DKERNEL`) and set them as Swift compiler definitions. This is required to successfully build Swift code that works with Zephyr:
145173

146174
```cmake
147175
# Add definitions from Zephyr to -Xcc flags
@@ -287,7 +315,7 @@ Loop
287315
Loop
288316
```
289317

290-
This setup may also desirable since `west flash` is also available and can be used instead of invoking the flashing tools manually.
318+
This setup may also be desirable since `west flash` is also available and can be used instead of invoking the flashing tools manually.
291319

292320
If compiling a firmware for a real/physical board such as the `nrf52840dk/nrf52840`, `west flash` will work if the SEGGER J-Link host tools are installed. As an example, with the [nrfx-blink-sdk](../../../../nrfx-blink-sdk/) project:
293321

0 commit comments

Comments
 (0)