Skip to content

Commit 7cd5c2b

Browse files
More revisions to Zephyr doc
1 parent 8be26f8 commit 7cd5c2b

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

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

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,27 @@ Refer to the [Zephyr Releases](https://docs.zephyrproject.org/latest/releases/in
3131

3232
## Project Setup
3333

34-
Once Zephyr is setup, the next step is to setup a project with a bridging header, `CMakeLists.txt`, `Main.swift`, and `prj.conf`:
34+
Once Zephyr is setup, the next step is to setup a project with the following files included:
3535

3636
```plain
37-
SwiftZephyrProject/BridgingHeader.h
37+
SwiftZephyrProject/src/BridgingHeader.h
38+
SwiftZephyrProject/src/Main.swift
39+
SwiftZephyrProject/src/Stubs.c
3840
SwiftZephyrProject/CMakeLists.txt
39-
SwiftZephyrProject/Main.swift
4041
SwiftZephyrProject/prj.conf
4142
```
4243

43-
These are the minimum required files in order to build a Zephyr project. For example, `prj.conf` is required even if it is empty, or the project will not build.
44+
These are the minimum required files in order to build a Zephyr project. By convention, source files should be placed in the src/ subdirectory, but this is not a hard requirement. Also, `prj.conf` is required even if it is empty, or the project will not build.
4445

45-
Inside of `BridgingHeader.h`, add the following content as a minimum:
46+
Inside of `src/BridgingHeader.h`, add the following content as a minimum:
4647

4748
```c
4849
#pragma once
4950

5051
#include <autoconf.h>
5152
```
5253

53-
The `Main.swift` file must contain a `static func main()` as follows:
54+
The `src/Main.swift` file must contain a `static func main()` as follows:
5455

5556
```swift
5657
@main
@@ -61,6 +62,30 @@ struct Main {
6162
}
6263
```
6364

65+
Since Embedded Swift requires posix_memalign to be defined, add the following to `src/Stubs.c` to define it:
66+
67+
```c
68+
#include <stdlib.h>
69+
#include <errno.h>
70+
71+
void *aligned_alloc(size_t alignment, size_t size);
72+
73+
// Embedded Swift currently requires posix_memalign, but the C libraries in the
74+
// Zephyr SDK do not provide it. Let's implement it and forward the calls to
75+
// aligned_alloc(3).
76+
int
77+
posix_memalign(void **memptr, size_t alignment, size_t size)
78+
{
79+
void *p = aligned_alloc(alignment, size);
80+
if (p) {
81+
*memptr = p;
82+
return 0;
83+
}
84+
85+
return errno;
86+
}
87+
```
88+
6489
### CMakeLists.txt Setup
6590
6691
The `CMakeLists.txt` setup is more involved and complex since target, compilation flags, and library linking must be specified for Swift. First, some initial setup and flags:
@@ -69,20 +94,19 @@ The `CMakeLists.txt` setup is more involved and complex since target, compilatio
6994
cmake_minimum_required(VERSION 3.29)
7095
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7196
72-
# Use the armv7em-none-none-eabi target triple for Swift
73-
set(CMAKE_Swift_COMPILER_TARGET armv7em-none-none-eabi)
7497
# Enable "wmo" as needed by Embedded Swift
7598
set(CMAKE_Swift_COMPILATION_MODE wholemodule)
76-
# FIXME: Skip checking if the compiler works
77-
set(CMAKE_Swift_COMPILER_WORKS true)
7899
79100
# Create a new project called "SwiftZephyrProject" and enable "Swift" as a supported language
80101
project(SwiftZephyrProject Swift)
81102
```
82103

83-
The following flags have been curated to work well with `armv7em-none-none-eabi` target with `swiftc`. Some flags may need to change depending on the chosen target:
104+
The following flags have been curated to work well with the `armv7em-none-none-eabi` target with `swiftc`. Some flags may need to change depending on the chosen target:
84105

85106
```cmake
107+
# Use the armv7em-none-none-eabi target triple for Swift
108+
set(CMAKE_Swift_COMPILER_TARGET armv7em-none-none-eabi)
109+
86110
# Set global Swift compiler flags
87111
add_compile_options(
88112
# Enable Embedded Swift
@@ -126,9 +150,11 @@ endif()
126150
Finally, setup targets, libraries, and additional compile options:
127151

128152
```cmake
153+
target_sources(app PRIVATE src/Stubs.c)
154+
129155
# The Swift code providing "main" needs to be in an OBJECT library (instead of STATIC library) to make sure it actually gets linker.
130156
# A STATIC library would get dropped from linking because Zephyr provides a default weak empty main definition.
131-
add_library(app_swift OBJECT Main.swift)
157+
add_library(app_swift OBJECT src/Main.swift)
132158
133159
add_dependencies(app_swift syscall_list_h_target)
134160
target_compile_options(app_swift PRIVATE
@@ -139,7 +165,7 @@ target_compile_options(app_swift PRIVATE
139165
-Xfrontend -disable-stack-protector
140166
141167
# FIXME: add dependency on BridgingHeader.h
142-
-import-bridging-header ${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h
168+
-import-bridging-header ${CMAKE_CURRENT_LIST_DIR}/src/BridgingHeader.h
143169
)
144170
145171
# Copy include paths from C target to Swift target

0 commit comments

Comments
 (0)