How to add libswiftUnicodeDataTables.a to a esp-idf project

Hi, I'm trying to program an ESP32 in Swift and I would like to use Strings. I read the user manual here swift/docs/EmbeddedSwift/UserManual.md at main · swiftlang/swift · GitHub, but, unfortunately, I could not find any examples of how to use ld with the CMake. Trying to learn CMake now, but it looks like a quite big task. Maybe you have an example? I'm basing my code on esp-idf examples (and CMake configuration) from here: swift-embedded-examples/esp32-led-blink-sdk at main · apple/swift-embedded-examples · GitHub

Thanks!

2 Likes

This boils down to implementing the linking steps from the manual:

... link in the libswiftUnicodeDataTables.a that's in Swift toolchain's resource directory (lib/swift/) under the target triple that you're using:

$ swiftc <inputs> -target armv6m-none-none-eabi -enable-experimental-feature Embedded -wmo -c -o output.o
$ ld ... -o binary output.o $(dirname `which swiftc`)/../lib/swift/embedded/armv6m-none-none-eabi/libswiftUnicodeDataTables.a

in your build system. In this case it's CMake, so adding the following lines to your CMakeLists.txt should do (and in ESP IDF, ${COMPONENT_LIB} is the CMake target with the Swift source code):

get_filename_component(compiler_bin_dir ${CMAKE_Swift_COMPILER} DIRECTORY)
target_link_directories(${COMPONENT_LIB} PUBLIC "${compiler_bin_dir}/../lib/swift/embedded/riscv32-none-none-eabi")
target_link_libraries(${COMPONENT_LIB} PUBLIC
    -Wl,--whole-archive
    swiftUnicodeDataTables
    -Wl,--no-whole-archive
    )

(For some reason that I don't fully understand, I had to use the --whole-archive/--no-whole-archive flags to get this to link. If someone has better insights into the semantics of ELF linking, I'd love to learn why :slight_smile:)

2 Likes

Got it, thanks a lot!