Skip to content

Commit b9bc89a

Browse files
authored
Adopt CMake 3.29's native Swift support in ESP32 examples (#41)
1 parent 767e9ac commit b9bc89a

File tree

5 files changed

+77
-75
lines changed

5 files changed

+77
-75
lines changed

esp32-led-blink-sdk/main/CMakeLists.txt

+38-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
# Register the app as an IDF component
12
idf_component_register(
2-
SRCS "Dummy.c"
3-
INCLUDE_DIRS "."
3+
SRCS /dev/null # We don't have any C++ sources
4+
PRIV_INCLUDE_DIRS "."
45
LDFRAGMENTS "linker.lf"
56
)
67

@@ -22,33 +23,45 @@ else()
2223
set(mabi_flag "ilp32")
2324
endif()
2425

25-
if(APPLE)
26-
execute_process(COMMAND xcrun -f swiftc OUTPUT_VARIABLE SWIFTC OUTPUT_STRIP_TRAILING_WHITESPACE)
27-
else()
28-
execute_process(COMMAND which swiftc OUTPUT_VARIABLE SWIFTC OUTPUT_STRIP_TRAILING_WHITESPACE)
29-
endif()
26+
# Clear the default COMPILE_OPTIONS which include a lot of C/C++ specific compiler flags that the Swift compiler will not accept
27+
get_target_property(var ${COMPONENT_LIB} COMPILE_OPTIONS)
28+
set_target_properties(${COMPONENT_LIB} PROPERTIES COMPILE_OPTIONS "")
29+
30+
# Compute -Xcc flags to set up the C and C++ header search paths for Swift (for bridging header).
31+
set(SWIFT_INCLUDES)
32+
foreach(dir ${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES})
33+
string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-Xcc ")
34+
string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-I${dir} ")
35+
endforeach()
36+
foreach(dir ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
37+
string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-Xcc ")
38+
string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-I${dir} ")
39+
endforeach()
3040

31-
add_custom_command(
32-
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
33-
COMMAND
34-
${SWIFTC}
41+
# Swift compiler flags to build in Embedded Swift mode, optimize for size, choose the right ISA, ABI, etc.
42+
target_compile_options(${COMPONENT_LIB} PUBLIC "$<$<COMPILE_LANGUAGE:Swift>:SHELL:
3543
-target riscv32-none-none-eabi
3644
-Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library -Osize
3745
-Xcc -march=${march_flag} -Xcc -mabi=${mabi_flag}
38-
$$\( echo '$<TARGET_PROPERTY:__idf_main,INCLUDE_DIRECTORIES>' | tr '\;' '\\n' | sed -e 's/\\\(.*\\\)/-Xcc -I\\1/g' \)
39-
$$\( echo '${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}' | tr ' ' '\\n' | sed -e 's/\\\(.*\\\)/-Xcc -I\\1/g' \)
46+
47+
-pch-output-dir /tmp
48+
-Xfrontend -enable-single-module-llvm-emission
49+
50+
${SWIFT_INCLUDES}
51+
4052
-import-bridging-header ${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h
41-
${CMAKE_CURRENT_LIST_DIR}/Main.swift
42-
${CMAKE_CURRENT_LIST_DIR}/Led.swift
43-
-c -o ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
44-
DEPENDS
45-
${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h
46-
${CMAKE_CURRENT_LIST_DIR}/Main.swift
47-
${CMAKE_CURRENT_LIST_DIR}/Led.swift
48-
)
49-
add_custom_target(main-swiftcode DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o)
53+
>")
54+
55+
# Enable Swift support in CMake, force Whole Module builds (required by Embedded Swift), and use "CMAKE_Swift_COMPILER_WORKS" to
56+
# skip the trial compilations which don't (yet) correctly work when cross-compiling.
57+
set(CMAKE_Swift_COMPILER_WORKS YES)
58+
set(CMAKE_Swift_COMPILATION_MODE_DEFAULT wholemodule)
59+
set(CMAKE_Swift_COMPILATION_MODE wholemodule)
60+
enable_language(Swift)
5061

51-
target_link_libraries(__idf_main
52-
${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
62+
# List of Swift source files to build.
63+
target_sources(${COMPONENT_LIB}
64+
PRIVATE
65+
Main.swift
66+
LED.swift
5367
)
54-
add_dependencies(__idf_main main-swiftcode)

esp32-led-blink-sdk/main/Dummy.c

-12
This file was deleted.

esp32-led-strip-sdk/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
cmake_minimum_required(VERSION 3.16)
1+
cmake_minimum_required(VERSION 3.29)
22
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
33
project(main)

esp32-led-strip-sdk/main/CMakeLists.txt

+38-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
# Register the app as an IDF component
12
idf_component_register(
2-
SRCS "Dummy.c"
3-
INCLUDE_DIRS "."
3+
SRCS /dev/null # We don't have any C++ sources
4+
PRIV_INCLUDE_DIRS "."
45
LDFRAGMENTS "linker.lf"
56
)
67

@@ -22,33 +23,45 @@ else()
2223
set(mabi_flag "ilp32")
2324
endif()
2425

25-
if(APPLE)
26-
execute_process(COMMAND xcrun -f swiftc OUTPUT_VARIABLE SWIFTC OUTPUT_STRIP_TRAILING_WHITESPACE)
27-
else()
28-
execute_process(COMMAND which swiftc OUTPUT_VARIABLE SWIFTC OUTPUT_STRIP_TRAILING_WHITESPACE)
29-
endif()
26+
# Clear the default COMPILE_OPTIONS which include a lot of C/C++ specific compiler flags that the Swift compiler will not accept
27+
get_target_property(var ${COMPONENT_LIB} COMPILE_OPTIONS)
28+
set_target_properties(${COMPONENT_LIB} PROPERTIES COMPILE_OPTIONS "")
29+
30+
# Compute -Xcc flags to set up the C and C++ header search paths for Swift (for bridging header).
31+
set(SWIFT_INCLUDES)
32+
foreach(dir ${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES})
33+
string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-Xcc ")
34+
string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-I${dir} ")
35+
endforeach()
36+
foreach(dir ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
37+
string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-Xcc ")
38+
string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-I${dir} ")
39+
endforeach()
3040

31-
add_custom_command(
32-
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
33-
COMMAND
34-
${SWIFTC}
41+
# Swift compiler flags to build in Embedded Swift mode, optimize for size, choose the right ISA, ABI, etc.
42+
target_compile_options(${COMPONENT_LIB} PUBLIC "$<$<COMPILE_LANGUAGE:Swift>:SHELL:
3543
-target riscv32-none-none-eabi
3644
-Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library -Osize
3745
-Xcc -march=${march_flag} -Xcc -mabi=${mabi_flag}
38-
$$\( echo '$<TARGET_PROPERTY:__idf_main,INCLUDE_DIRECTORIES>' | tr '\;' '\\n' | sed -e 's/\\\(.*\\\)/-Xcc -I\\1/g' \)
39-
$$\( echo '${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}' | tr ' ' '\\n' | sed -e 's/\\\(.*\\\)/-Xcc -I\\1/g' \)
46+
47+
-pch-output-dir /tmp
48+
-Xfrontend -enable-single-module-llvm-emission
49+
50+
${SWIFT_INCLUDES}
51+
4052
-import-bridging-header ${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h
41-
${CMAKE_CURRENT_LIST_DIR}/Main.swift
42-
${CMAKE_CURRENT_LIST_DIR}/LedStrip.swift
43-
-c -o ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
44-
DEPENDS
45-
${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h
46-
${CMAKE_CURRENT_LIST_DIR}/Main.swift
47-
${CMAKE_CURRENT_LIST_DIR}/LedStrip.swift
48-
)
49-
add_custom_target(main-swiftcode DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o)
53+
>")
54+
55+
# Enable Swift support in CMake, force Whole Module builds (required by Embedded Swift), and use "CMAKE_Swift_COMPILER_WORKS" to
56+
# skip the trial compilations which don't (yet) correctly work when cross-compiling.
57+
set(CMAKE_Swift_COMPILER_WORKS YES)
58+
set(CMAKE_Swift_COMPILATION_MODE_DEFAULT wholemodule)
59+
set(CMAKE_Swift_COMPILATION_MODE wholemodule)
60+
enable_language(Swift)
5061

51-
target_link_libraries(__idf_main
52-
${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
62+
# List of Swift source files to build.
63+
target_sources(${COMPONENT_LIB}
64+
PRIVATE
65+
Main.swift
66+
LedStrip.swift
5367
)
54-
add_dependencies(__idf_main main-swiftcode)

esp32-led-strip-sdk/main/Dummy.c

-12
This file was deleted.

0 commit comments

Comments
 (0)