Embedded Swift support for ESP32-S3, ESP32-S2 and original ESP32 (Xtensa arch)

There have been several community requests to add support for older Espressif MCUs based on the Xtensa architecture—specifically, the ESP32-S3, ESP32-S2, and the original ESP32. While newer MCUs are based on RISC-V and are supported by the upstream compiler, those using Xtensa require a custom compiler build.

I’ve prepared a repository containing a build script that should generate a compatible version of the compiler: GitHub - georgik/swift-xtensa: ESP32 - Xtensa support build of Swift

The build process requires two additional forked repositories (the script downloads them automatically):

This work is derived from efforts made for the Rust compiler. LLVM patches from Espressif’s LLVM version have been applied to the Swift fork of LLVM. Additionally, Xtensa-related targets have been registered in the Swift fork. In theory, this approach should work.

However, the test build is currently failing with the following error:

Error details:
<unknown>:0: error: embedded swift cannot be enabled in a compiler built without Swift sources

Installed Swift libraries:
swift-xtensa/install/lib/swift/shims/EmbeddedShims.h
swift-xtensa/install/lib/swift/macosx/libswiftCore.dylib
swift-xtensa/install/lib/swift/macosx/libswiftCompatibilitySpan.dylib
swift-xtensa/install/lib/swift/macosx/libswiftSwiftOnoneSupport.dylib

It seems that stdlib is not properly built.
I would appreciate any insights into what might be missing.

3 Likes

What's the build-script invocation that you're using to build this?

Part specific to custom build of Swift is here: swift-xtensa/swift-xtensa-build.sh at main · georgik/swift-xtensa · GitHub

To be able to build this more easily and upstream these changes if that's possible at all, I'd suggest migrating to build-script instead of invoking cmake directly on the top-level swift directory. There are a lot of assumptions in CMake files that expect options passed in certain combinations from build-script and corresponding presets, especially on macOS.

1 Like

Thank you for the suggestion, I've added new branch feature/xtensa-2025-08, changing the build method to build-script.
The script is here: swift-xtensa/swift-xtensa-build-script.sh at feature/build-script · georgik/swift-xtensa · GitHub
The build seems to be failing on several places. It will require further investigation.

1 Like

I'd surmise you have to clean up --llvm-cmake-options and --swift-cmake-options, as most of these are inferred by build-script and some things overridden manually could conflict with inferred options.

Wondering is there any progress on this?

Hi. Unfortunatelly there is no progress on Xtensa part. There are many issues. I would recommend using RISC-V MCUs from Espressif portfolio which works with Swift without problem.

Thanks for the answer!