-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwiftSetup.cmake
More file actions
94 lines (81 loc) · 3.41 KB
/
Copy pathSwiftSetup.cmake
File metadata and controls
94 lines (81 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#
# SwiftSetup.cmake
# SwiftZephyr
# -----
# Copyright (c) 2025 - 2026 Hunter Baker hunter@literallyanything.net
# Licensed under the MIT License
#
# This is a helper script to setup Swift settings for embedded Swift in Zephyr
# Set the LLVM target triple
# - Note: This list is completely incomplete, so if you need something that is not listed yer, open a PR and add it.
if(NOT DEFINED CMAKE_Swift_COMPILER_TARGET)
if(DEFINED CONFIG_ARCH AND "${CONFIG_ARCH}" STREQUAL "arm")
if(DEFINED CONFIG_CPU_CORTEX_M7)
set(LLVM_TARGET_ARCH armv6m)
endif()
endif()
# Ensure that we found the arch
if(NOT DEFINED LLVM_TARGET_ARCH)
message(FATAL_ERROR "Swift target triple is not set, and can't be autodetected. CMAKE_Swift_COMPILER_TARGET")
endif()
set(CMAKE_Swift_COMPILER_TARGET "${LLVM_TARGET_ARCH}-none-none-eabi")
message(STATUS "Set Swift target triple to '${CMAKE_Swift_COMPILER_TARGET}'")
endif()
# Set some general settings for Swift
set(CMAKE_Swift_COMPILATION_MODE wholemodule)
add_compile_options(
# Enable Embedded Swift
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-enable-experimental-feature Embedded>"
# Enable function sections to enable dead code stripping on elf
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -function-sections>"
# Use compacted C enums matching GCC
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -fshort-enums>"
# Disable PIC
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -fno-pic>"
# Disable PIE
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -fno-pie>"
# Add Libc include paths
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -I -Xcc ${ZEPHYR_SDK_INSTALL_DIR}/arm-zephyr-eabi/picolibc/include>"
# Disable the stack protector
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -disable-stack-protector>"
# Save the Swift index for LSP
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-index-store-path ${CMAKE_BINARY_DIR}/swift-index>"
)
# By default, enable the strict memory safety mode in Swift
if(NOT DEFINED DISABLE_STRICT_MEMORY_SAFETY)
add_compile_options(
"$<$<COMPILE_LANGUAGE:Swift>:-strict-memory-safety>"
)
endif()
# Normally, EmbeddedExistentials are enabled. This will disable them.
if(DEFINED DISABLE_EMBEDDED_EXISTENTIALS)
add_compile_options(
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-disable-experimental-feature EmbeddedExistentials>"
)
endif()
# Import TOOLCHAIN_C_FLAGS from Zephyr as -Xcc flags
foreach(flag ${TOOLCHAIN_C_FLAGS})
# Skip flags that are not known to swiftc
string(FIND "${flag}" "-imacro" is_imacro)
string(FIND "${flag}" "-mfp16-format" is_mfp16)
if(NOT is_imacro EQUAL -1 OR NOT is_mfp16 EQUAL -1)
continue()
endif()
add_compile_options("$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc ${flag}>")
endforeach()
# Add definitions from Zephyr to -Xcc flags
get_target_property(ZEPHYR_DEFINES zephyr_interface INTERFACE_COMPILE_DEFINITIONS)
if(ZEPHYR_DEFINES)
foreach(flag ${ZEPHYR_DEFINES})
# Ignore expressions like "$<SOMETHING>"
string(FIND "${flag}" "$<" start_of_expression)
if(NOT start_of_expression EQUAL -1)
continue()
endif()
add_compile_options("$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -D${flag}>")
endforeach()
endif()
# Add stubs file to the main app target
target_sources(app PRIVATE "${CMAKE_CURRENT_LIST_DIR}/Stubs.c")
# Remove unused sections
zephyr_linker_sources(SECTIONS "${CMAKE_CURRENT_LIST_DIR}/sections.ld")