Are macros supported on Android?

I built the simple stringify macro using android toolchain but failed, there is a waring:

compiler plugin not loaded: /data/Downloads/swift-macro-examples/.build/aarch64-unknown-linux-android24/debug/MacroExamplesPlugin; loader error: Exec format error

The host compiler I'm using is ubuntu swift-5.9-DEVELOPMENT-SNAPSHOT-2023-09-05-a. And the swift stdlib and foundation are built referencing the GitHub - finagolfin/swift-android-sdk: Android SDKs for Swift provided by @Finagolfin

detail error:

user@user13-ubuntu:/data/Downloads/swift-macro-examples$ swift build --destination swiftpm-aarch64.json
Fetching GitHub - apple/swift-syntax: A set of Swift libraries for parsing, inspecting, generating, and transforming Swift source code. from cache
Fetched GitHub - apple/swift-syntax: A set of Swift libraries for parsing, inspecting, generating, and transforming Swift source code. (7.85s)
Creating working copy for GitHub - apple/swift-syntax: A set of Swift libraries for parsing, inspecting, generating, and transforming Swift source code.
Working copy of GitHub - apple/swift-syntax: A set of Swift libraries for parsing, inspecting, generating, and transforming Swift source code. resolved at main
Building for debugging...
/data/Downloads/swift-macro-examples/MacroExamplesPlugin/StringifyMacro.swift:19:31: warning: 'argumentList' is deprecated: renamed to 'arguments'
guard let argument = node.argumentList.first?.expression else {
^
/data/Downloads/swift-macro-examples/MacroExamplesPlugin/StringifyMacro.swift:19:31: note: use 'arguments' instead
guard let argument = node.argumentList.first?.expression else {
^~~~~~~~~~~~
arguments
:0: warning: compiler plugin not loaded: /data/Downloads/swift-macro-examples/.build/aarch64-unknown-linux-android24/debug/MacroExamplesPlugin; loader error: Exec format error
/data/Downloads/swift-macro-examples/MacroExamplesLib/Macros.swift:5:40: warning: external macro implementation type 'MacroExamplesPlugin.StringifyMacro' could not be found for macro 'stringify'
@freestanding(expression) public macro stringify(_ value: T) -> (T, String) = #externalMacro(module: "MacroExamplesPlugin", type: "StringifyMacro")
^
:0: warning: compiler plugin not loaded: /data/Downloads/swift-macro-examples/.build/aarch64-unknown-linux-android24/debug/MacroExamplesPlugin; loader error: Exec format error
/data/Downloads/swift-macro-examples/MacroExamplesLib/Macros.swift:5:40: warning: external macro implementation type 'MacroExamplesPlugin.StringifyMacro' could not be found for macro 'stringify'
@freestanding(expression) public macro stringify(_ value: T) -> (T, String) = #externalMacro(module: "MacroExamplesPlugin", type: "StringifyMacro")
^
warning: Could not read SDKSettings.json for SDK at: /home/user/Android/Sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/linux-x86_64/sysroot
error: emit-module command failed with exit code 1 (use -v to see invocation)
:0: warning: compiler plugin not loaded: /data/Downloads/swift-macro-examples/.build/aarch64-unknown-linux-android24/debug/MacroExamplesPlugin; loader error: Exec format error
/data/Downloads/swift-macro-examples/MacroExamples/main.swift:10:7: error: external macro implementation type 'MacroExamplesPlugin.StringifyMacro' could not be found for macro 'stringify'
print(#stringify(x + y))
^
/data/Downloads/swift-macro-examples/MacroExamplesLib/Macros.swift:5:40: note: 'stringify' declared here
@freestanding(expression) public macro stringify(_ value: T) -> (T, String) = #externalMacro(module: "MacroExamplesPlugin", type: "StringifyMacro")
^
:0: warning: compiler plugin not loaded: /data/Downloads/swift-macro-examples/.build/aarch64-unknown-linux-android24/debug/MacroExamplesPlugin; loader error: Exec format error
/data/Downloads/swift-macro-examples/MacroExamples/main.swift:10:7: error: external macro implementation type 'MacroExamplesPlugin.StringifyMacro' could not be found for macro 'stringify'
print(#stringify(x + y))
^
/data/Downloads/swift-macro-examples/MacroExamplesLib/Macros.swift:5:40: note: 'stringify' declared here
@freestanding(expression) public macro stringify(_ value: T) -> (T, String) = #externalMacro(module: "MacroExamplesPlugin", type: "StringifyMacro")
^
error: fatalError

user@user13-ubuntu:/data/Downloads/swift-macro-examples$ swift --version
Swift version 5.9-dev (LLVM 299c632af12fe28, Swift f42cd3b53880c62)
Target: x86_64-unknown-linux-gnu
user@user13-ubuntu:/data/Downloads/swift-macro-examples$ cat swiftpm-aarch64.json
{
"version": 1,
"target": "aarch64-unknown-linux-android24",
"toolchain-bin-dir": "/data/.compiler-swift/swift-5.9-DEVELOPMENT-SNAPSHOT-2023-09-05-a-ubuntu20.04/usr/bin/",
"sdk": "/home/user/Android/Sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/linux-x86_64/sysroot",
"extra-cc-flags": [
"-fPIC"
],
"extra-swiftc-flags": [
"-resource-dir", "/home/user/swift/toolchain/5.9-0905/swift-android-sdk/swift-devel-android-aarch64-2023-09-05-24-sdk/usr/lib/swift",
"-tools-directory", "/home/user/Android/Sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/linux-x86_64/bin",
"-L", "/home/user/swift/toolchain/5.9-0905/swift-android-sdk/swift-devel-android-aarch64-2023-09-05-24-sdk/usr/lib/swift/android/aarch64",
],
"extra-cpp-flags": [

]

}

Macros are a compile-time feature that only run on the host platform, and since you are cross-compiling from a linux host for an Android target, whether they run on Android doesn't matter. I have not tried Macros much yet, as they were just added to the linux compiler a week ago, but it looks like this might be a bug where target flags are being passed to the host macro.

@NeoNacho, I see you added some Macro support to SwiftPM, any idea what's going on here?

@woxiang04, my daily Android CI uploads the latest Android SDKs built from the development snapshots, so you can download the sdk-tests-devel-<arch> Android SDK for Swift 5.9 corresponding to that tag and use that instead of the older 5.8 SDK.

Edit: never mind, I see you are using that CI-built SDK in your destination config.

Macros aren't currently supported when cross-compiling since they end up building for the target, instead of the host.

Any plans to support macros when cross-compiling?

We'll have to support them eventually, but there are no plans to address this in the very immediate future.