1
+ # Register the app as an IDF component
1
2
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 "."
4
5
LDFRAGMENTS "linker.lf"
5
6
)
6
7
@@ -22,33 +23,45 @@ else()
22
23
set (mabi_flag "ilp32" )
23
24
endif ()
24
25
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 ()
30
40
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:
35
43
-target riscv32-none-none-eabi
36
44
-Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library -Osize
37
45
-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
+
40
52
-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)
50
61
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
53
67
)
54
- add_dependencies (__idf_main main-swiftcode)
0 commit comments