|
| 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 | + LDFRAGMENTS "linker.lf" |
| 6 | + PRIV_REQUIRES esp_driver_uart vfs |
| 7 | +) |
| 8 | + |
| 9 | +idf_build_get_property(target IDF_TARGET) |
| 10 | +idf_build_get_property(arch IDF_TARGET_ARCH) |
| 11 | + |
| 12 | +if("${arch}" STREQUAL "xtensa") |
| 13 | + message(FATAL_ERROR "Not supported target: ${target}") |
| 14 | +endif() |
| 15 | + |
| 16 | +# Extract -march and -mabi flags |
| 17 | +set(march_flag "") |
| 18 | +set(mabi_flag "") |
| 19 | + |
| 20 | +# Method 1: Read from IDF's cflags response file (IDF 6.0+) |
| 21 | +if(DEFINED IDF_TOOLCHAIN_BUILD_DIR AND EXISTS "${IDF_TOOLCHAIN_BUILD_DIR}/cflags") |
| 22 | + file(STRINGS "${IDF_TOOLCHAIN_BUILD_DIR}/cflags" cflags_lines) |
| 23 | + foreach(line IN LISTS cflags_lines) |
| 24 | + if(line MATCHES "^-march=") |
| 25 | + set(march_flag "${line}") |
| 26 | + elseif(line MATCHES "^-mabi=") |
| 27 | + set(mabi_flag "${line}") |
| 28 | + endif() |
| 29 | + endforeach() |
| 30 | +endif() |
| 31 | + |
| 32 | +# Method 2: Fallback to parsing CMAKE_C_FLAGS directly (older IDF versions) |
| 33 | +if(NOT march_flag OR NOT mabi_flag) |
| 34 | + string(REGEX MATCH "-march=[^ ]+" march_flag_fallback "${CMAKE_C_FLAGS}") |
| 35 | + string(REGEX MATCH "-mabi=[^ ]+" mabi_flag_fallback "${CMAKE_C_FLAGS}") |
| 36 | + if(NOT march_flag AND march_flag_fallback) |
| 37 | + set(march_flag "${march_flag_fallback}") |
| 38 | + endif() |
| 39 | + if(NOT mabi_flag AND mabi_flag_fallback) |
| 40 | + set(mabi_flag "${mabi_flag_fallback}") |
| 41 | + endif() |
| 42 | +endif() |
| 43 | + |
| 44 | +# Default mabi if not found |
| 45 | +if(NOT mabi_flag) |
| 46 | + set(mabi_flag "-mabi=ilp32") |
| 47 | +endif() |
| 48 | + |
| 49 | +# Strip Espressif custom extensions not supported by Swift |
| 50 | +if(march_flag) |
| 51 | + string(REGEX REPLACE "_x[^ ]*" "" march_flag "${march_flag}") |
| 52 | +endif() |
| 53 | + |
| 54 | +# Clear the default COMPILE_OPTIONS which include a lot of C/C++ specific compiler flags that the Swift compiler will not accept |
| 55 | +get_target_property(var ${COMPONENT_LIB} COMPILE_OPTIONS) |
| 56 | +set_target_properties(${COMPONENT_LIB} PROPERTIES COMPILE_OPTIONS "") |
| 57 | + |
| 58 | +# Compute -Xcc flags to set up the C and C++ header search paths for Swift (for bridging header). |
| 59 | +set(SWIFT_INCLUDES) |
| 60 | +foreach(dir ${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}) |
| 61 | + string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-Xcc ") |
| 62 | + string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-I${dir} ") |
| 63 | +endforeach() |
| 64 | +foreach(dir ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}) |
| 65 | + string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-Xcc ") |
| 66 | + string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-I${dir} ") |
| 67 | +endforeach() |
| 68 | + |
| 69 | +# Swift compiler flags to build in Embedded Swift mode, optimize for size, choose the right ISA, ABI, etc. |
| 70 | +target_compile_options(${COMPONENT_LIB} PUBLIC "$<$<COMPILE_LANGUAGE:Swift>:SHELL: |
| 71 | + -target riscv32-none-none-eabi |
| 72 | + -Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library -Osize |
| 73 | + -Xcc ${march_flag} -Xcc ${mabi_flag} -Xcc -fno-pic -Xcc -fno-pie |
| 74 | + |
| 75 | + -pch-output-dir /tmp |
| 76 | + -Xfrontend -enable-single-module-llvm-emission |
| 77 | + |
| 78 | + ${SWIFT_INCLUDES} |
| 79 | +
|
| 80 | + -import-bridging-header ${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h |
| 81 | + >") |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +# Enable Swift support in CMake, force Whole Module builds (required by Embedded Swift), and use "CMAKE_Swift_COMPILER_WORKS" to |
| 86 | +# skip the trial compilations which don't (yet) correctly work when cross-compiling. |
| 87 | +set(CMAKE_Swift_COMPILER_WORKS YES) |
| 88 | +set(CMAKE_Swift_COMPILATION_MODE_DEFAULT wholemodule) |
| 89 | +set(CMAKE_Swift_COMPILATION_MODE wholemodule) |
| 90 | +enable_language(Swift) |
| 91 | + |
| 92 | +# List of Swift source files to build. |
| 93 | +target_sources(${COMPONENT_LIB} |
| 94 | + PRIVATE |
| 95 | + Main.swift |
| 96 | + Uart.swift |
| 97 | + SwiftStubs.c |
| 98 | +) |
0 commit comments