1
- cmake_minimum_required (VERSION 3.13 )
1
+ cmake_minimum_required (VERSION 3.29 )
2
2
include ($ENV{PICO_SDK_PATH} /external/pico_sdk_import.cmake)
3
3
4
+ set (CMAKE_Swift_COMPILATION_MODE wholemodule)
5
+ set (CMAKE_Swift_COMPILER_WORKS YES )
6
+
4
7
project (swift-blinky)
5
8
pico_sdk_init()
6
-
7
- if (APPLE )
8
- execute_process (COMMAND xcrun -f swiftc OUTPUT_VARIABLE SWIFTC OUTPUT_STRIP_TRAILING_WHITESPACE)
9
- else ()
10
- execute_process (COMMAND which swiftc OUTPUT_VARIABLE SWIFTC OUTPUT_STRIP_TRAILING_WHITESPACE)
11
- endif ()
9
+ enable_language (Swift)
12
10
13
11
set (SWIFT_TARGET "armv6m-none-none-eabi" ) # default for rp2040
14
12
15
13
if (PICO_PLATFORM STREQUAL "rp2350-arm-s" )
16
14
message (STATUS "PICO_PLATFORM is set to rp2350-arm-s, using armv7em" )
17
15
set (SWIFT_TARGET "armv7em-none-none-eabi" )
18
- list (APPEND CLANG_ARCH_ABI_FLAGS "-Xcc" " -mfloat-abi=soft" )
16
+ list (APPEND CLANG_ARCH_ABI_FLAGS "-Xcc -mfloat-abi=soft" )
19
17
elseif (PICO_PLATFORM STREQUAL "rp2040" )
20
18
message (STATUS "PICO_PLATFORM is set to RP2040, using armv6m" )
21
- list (APPEND CLANG_ARCH_ABI_FLAGS "-Xcc" " -mfloat-abi=soft" )
19
+ list (APPEND CLANG_ARCH_ABI_FLAGS "-Xcc -mfloat-abi=soft" )
22
20
elseif (PICO_PLATFORM STREQUAL "rp2350-riscv" )
23
21
message (STATUS "PICO_PLATFORM is set to rp2350-riscv, using riscv32." )
24
22
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" )
23
+ list (APPEND CLANG_ARCH_ABI_FLAGS "-Xcc -march=rv32imac_zicsr_zifencei_zba_zbb_zbs_zbkb -Xcc -mabi=ilp32" )
26
24
endif ()
27
25
28
- add_executable (swift-blinky)
26
+ add_executable (swift-blinky
27
+ # Source files:
28
+ Main.swift
29
+ )
30
+
31
+ set_target_properties (swift-blinky PROPERTIES LINKER_LANGUAGE CXX)
29
32
30
33
target_link_libraries (swift-blinky
31
34
pico_stdlib hardware_uart hardware_gpio
32
35
)
33
36
34
- # Gather compile definitions from all dependencies
37
+ # Clear the default COMPILE_OPTIONS which include C specific compiler flags that the Swift compiler will not accept
38
+ # Instead, set those options to only apply when compiling C code.
39
+ set_target_properties (pico_standard_link PROPERTIES INTERFACE_COMPILE_OPTIONS "" )
40
+ target_compile_options (pico_standard_link INTERFACE "$<$<COMPILE_LANGUAGE:C>:SHELL: -ffunction-sections -fdata-sections>" )
35
41
42
+ # Gather C compile definitions from all dependencies
36
43
set_property (GLOBAL PROPERTY visited_targets "" )
37
44
set_property (GLOBAL PROPERTY compilerdefs_list "" )
38
45
@@ -42,20 +49,18 @@ function(gather_compile_definitions_recursive target)
42
49
43
50
# make sure we don't visit the same target twice
44
51
# and that we don't visit the special generator expressions
45
- if (${target} MATCHES "\\ $<" OR ${target} MATCHES "::@" OR ${target} IN_LIST visited_targets)
52
+ if (${target} MATCHES "\\\ $ <" OR ${target} MATCHES "::@" OR ${target} IN_LIST visited_targets)
46
53
return ()
47
54
endif ()
48
55
49
56
# Append the target to visited_targets
50
57
list (APPEND visited_targets ${target} )
51
58
set_property (GLOBAL PROPERTY visited_targets "${visited_targets} " )
52
59
53
- # Get the current value of compilerdefs_list
54
- get_property (compilerdefs_list GLOBAL PROPERTY compilerdefs_list)
55
-
56
60
get_target_property (target_definitions ${target} INTERFACE_COMPILE_DEFINITIONS )
57
61
if (target_definitions)
58
62
# Append the target definitions to compilerdefs_list
63
+ get_property (compilerdefs_list GLOBAL PROPERTY compilerdefs_list)
59
64
list (APPEND compilerdefs_list ${target_definitions} )
60
65
set_property (GLOBAL PROPERTY compilerdefs_list "${compilerdefs_list} " )
61
66
endif ()
@@ -71,35 +76,38 @@ endfunction()
71
76
72
77
gather_compile_definitions_recursive(swift-blinky)
73
78
get_property (COMPILE_DEFINITIONS GLOBAL PROPERTY compilerdefs_list)
79
+ get_property (INCLUDES GLOBAL PROPERTY includes_list)
74
80
75
- # Parse compiler definitions into a format that swiftc can understand
81
+ # Convert compiler definitions into a format that swiftc can understand
76
82
list (REMOVE_DUPLICATES COMPILE_DEFINITIONS )
77
- list (PREPEND COMPILE_DEFINITIONS "" )
83
+ list (PREPEND COMPILE_DEFINITIONS "" ) # adds a semicolon at the beginning
78
84
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
-
81
- add_custom_command (
82
- OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /_swiftcode.o
83
- COMMAND
84
- ${SWIFTC}
85
- -target ${SWIFT_TARGET} -Xcc -fshort-enums
86
- ${COMPILE_DEFINITIONS}
85
+ string (REPLACE ";" " -Xcc -D" COMPILE_DEFINITIONS "${COMPILE_DEFINITIONS} " )
86
+
87
+ # Compute -Xcc flags to set up the C and C++ header search paths for Swift (for bridging header).
88
+ set (IMPLICIT_INCLUDES)
89
+ foreach (dir ${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES} )
90
+ string (CONCAT IMPLICIT_INCLUDES ${IMPLICIT_INCLUDES} "-Xcc " )
91
+ string (CONCAT IMPLICIT_INCLUDES ${IMPLICIT_INCLUDES} "-I${dir} " )
92
+ endforeach ()
93
+ foreach (dir ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES} )
94
+ string (CONCAT IMPLICIT_INCLUDES ${IMPLICIT_INCLUDES} "-Xcc " )
95
+ string (CONCAT IMPLICIT_INCLUDES ${IMPLICIT_INCLUDES} "-I${dir} " )
96
+ endforeach ()
97
+
98
+ target_compile_options (swift-blinky PUBLIC
99
+ "$<$<COMPILE_LANGUAGE:Swift>:SHELL:
100
+ -target ${SWIFT_TARGET}
101
+ -enable-experimental-feature Embedded
102
+ -parse-as-library
103
+ -module-name swift_blinky
104
+
87
105
${CLANG_ARCH_ABI_FLAGS}
88
- -Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library
89
- $$\( echo '$<TARGET_PROPERTY:swift-blinky,INCLUDE_DIRECTORIES >' | tr '\;' '\\n' | sed -e 's/\\\(.*\\\)/-Xcc -I\\1/g' \)
90
- $$\( echo '${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES} ' | tr ' ' '\\n' | sed -e 's/\\\(.*\\\)/-Xcc -I\\1/g' \)
106
+ -Xcc -fshort-enums
107
+ -Xfrontend -function-sections
91
108
-import-bridging-header ${CMAKE_CURRENT_LIST_DIR} /BridgingHeader.h
92
- ${CMAKE_CURRENT_LIST_DIR} /Main.swift
93
- -c -o ${CMAKE_CURRENT_BINARY_DIR} /_swiftcode.o
94
- DEPENDS
95
- ${CMAKE_CURRENT_LIST_DIR} /BridgingHeader.h
96
- ${CMAKE_CURRENT_LIST_DIR} /Main.swift
97
- )
98
- add_custom_target (swift-blinky-swiftcode DEPENDS ${CMAKE_CURRENT_BINARY_DIR} /_swiftcode.o)
99
-
109
+ ${COMPILE_DEFINITIONS}
110
+ ${IMPLICIT_INCLUDES}
111
+ >" )
100
112
101
- target_link_libraries (swift-blinky
102
- ${CMAKE_CURRENT_BINARY_DIR} /_swiftcode.o
103
- )
104
- add_dependencies (swift-blinky swift-blinky-swiftcode)
105
113
pico_add_extra_outputs(swift-blinky)
0 commit comments