|
| 1 | +# Register the app as an IDF component |
| 2 | +idf_component_register( |
| 3 | + SRCS /dev/null # We don't have any C++ sources |
| 4 | + PRIV_INCLUDE_DIRS "." |
| 5 | + REQUIRES arduino |
| 6 | +) |
| 7 | + |
| 8 | +idf_build_get_property(target IDF_TARGET) |
| 9 | +idf_build_get_property(arch IDF_TARGET_ARCH) |
| 10 | + |
| 11 | +if("${arch}" STREQUAL "xtensa") |
| 12 | + message(FATAL_ERROR "Not supported target: ${target}") |
| 13 | +endif() |
| 14 | + |
| 15 | +# Extract the -march flag and remove any vendor-specific extensions (_x*) |
| 16 | +string(REGEX MATCH "-march=[^ ]+" march_flag "${CMAKE_C_FLAGS}") |
| 17 | +string(REGEX REPLACE "_x[^ ]*" "" march_flag "${march_flag}") |
| 18 | + |
| 19 | +# Extract the -mabi flag or set a default value if not present |
| 20 | +string(REGEX MATCH "-mabi=[^ ]+" mabi_flag "${CMAKE_C_FLAGS}") |
| 21 | +if("${mabi_flag}" STREQUAL "") |
| 22 | + set(mabi_flag "-mabi=ilp32") |
| 23 | +endif() |
| 24 | + |
| 25 | +# Clear the default COMPILE_OPTIONS which include a lot of C/C++ specific compiler flags that the Swift compiler will not accept |
| 26 | +get_target_property(var ${COMPONENT_LIB} COMPILE_OPTIONS) |
| 27 | +set_target_properties(${COMPONENT_LIB} PROPERTIES COMPILE_OPTIONS "") |
| 28 | + |
| 29 | +# Compute -Xcc flags to set up the C and C++ header search paths for Swift (for bridging header). |
| 30 | +set(SWIFT_INCLUDES) |
| 31 | +foreach(dir ${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}) |
| 32 | + string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-Xcc ") |
| 33 | + string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-I${dir} ") |
| 34 | +endforeach() |
| 35 | +foreach(dir ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}) |
| 36 | + string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-Xcc ") |
| 37 | + string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-I${dir} ") |
| 38 | +endforeach() |
| 39 | + |
| 40 | +# Swift compiler flags to build in Embedded Swift mode, optimize for size, choose the right ISA, ABI, etc. |
| 41 | +target_compile_options(${COMPONENT_LIB} PUBLIC "$<$<COMPILE_LANGUAGE:Swift>:SHELL: |
| 42 | + -target riscv32-none-none-eabi |
| 43 | + -Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library -Osize -cxx-interoperability-mode=default |
| 44 | + -Xcc ${march_flag} -Xcc ${mabi_flag} -Xcc -fno-pic -Xcc -fno-pie -Xcc -fno-exceptions |
| 45 | +
|
| 46 | + -pch-output-dir /tmp |
| 47 | + -Xfrontend -enable-single-module-llvm-emission |
| 48 | +
|
| 49 | + ${SWIFT_INCLUDES} |
| 50 | +
|
| 51 | + -import-bridging-header ${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h |
| 52 | + >") |
| 53 | + |
| 54 | +# Enable Swift support in CMake, force Whole Module builds (required by Embedded Swift), and use "CMAKE_Swift_COMPILER_WORKS" to |
| 55 | +# skip the trial compilations which don't (yet) correctly work when cross-compiling. |
| 56 | +set(CMAKE_Swift_COMPILER_WORKS YES) |
| 57 | +set(CMAKE_Swift_COMPILATION_MODE_DEFAULT wholemodule) |
| 58 | +set(CMAKE_Swift_COMPILATION_MODE wholemodule) |
| 59 | +enable_language(Swift) |
| 60 | + |
| 61 | +# List of Swift source files to build. |
| 62 | +target_sources(${COMPONENT_LIB} |
| 63 | + PRIVATE |
| 64 | + Main.swift |
| 65 | + Led.swift |
| 66 | +) |
| 67 | + |
| 68 | +add_custom_command( |
| 69 | + TARGET ${COMPONENT_LIB} |
| 70 | + POST_BUILD |
| 71 | + COMMAND ${CMAKE_OBJCOPY} --remove-section .swift_modhash |
| 72 | + $<TARGET_FILE:${COMPONENT_LIB}> $<TARGET_FILE:${COMPONENT_LIB}> |
| 73 | +) |
0 commit comments