State of cross-compilation support (eg: from mac OS to android)

What is the state of cross-compiling swift code from different OS (and in particular, from mac os to android) ?

I've seen @Finagolfin work on cross-compiling toolchain (which i'm still not sure what kind of cross-compilation combination the toolchain support), however the official "compile to Android" documentation here https://github.com/apple/swift/blob/main/docs/Android.md only mentions compiling from a linux installation.

I've been looking at resources everywhere, but i ended up quite confused about the official state on this topic.

1 Like

What is the state of cross-compiling swift code from different OS (and in particular, from mac os to android) ?

It should be pretty good, considering most Swift code is likely cross-compiled from macOS to iOS. As for Android, it can be done with my SDK, though you're probably better off using the pure OSS toolchain from Homebrew with it. You may also find useful this recent work by @KittyMac.

Hey, thanks for answering.
Not sure who to ask, but It would be nice to update the Android.md on swift documentation to reflect the fact that building from linux isn't compulsory and provide steps on how to build for android from a mac.

The very first times working with cross-compilation can be quite daunting for newcomers, so reducing the numbers of steps would likely be welcome..

2 Likes

@Finagolfin i'm sorry if i sound obtuse, but the work you've linked from @KittyMac seems to be about using a docker container based on an ubuntu distribution to actually build swift code targetting android. Does this mean one can't compile swift code to target android directly from a macos, without any kind of linux installation ?

To better understand what i mean : in golang, one only needs to set a couple of environment variables (cpu architecture, operating system) before calling "go build" (on any platform), and the resulting library / executable will be directly working on the target system. Without having to install any kind of docker container, or VM or anything (i assume the build system downloads whatever headers are required to properly link the code to system libraries in the background). Eg : How to cross-compile Go programs for Windows, macOS, and Linux

Are there any equivalent in swift for targeting the android platform ? (having to download the android NDK would of course be fine).

It would be nice to update the Android.md on swift documentation to reflect the fact that building from linux isn't compulsory and provide steps on how to build for android from a mac.

I don't use macOS, so can't be me (I may be the only person using Swift who's never used it on an Apple platform, as I own no Apple hardware, not even an iPhone).

the work you've linked from @KittyMac seems to be about using a docker container based on an ubuntu distribution to actually build swift code targetting android.

Yes, many people find using such prebuilt Docker containers the easiest way to use a new toolchain, and of course Docker works fine on macOS, though he reports his work doesn't run on Apple Silicon, so you need macOS x86_64.

Does this mean one can't compile swift code to target android directly from a macos, without any kind of linux installation ?

Of course not, @v.gorlov maintained a mac->droid toolchain for several years, before recently discontinuing it.

in golang, one only needs to set a couple of environment variables (cpu architecture, operating system) before calling "go build" (on any platform), and the resulting library / executable will be directly working on the target system. Without having to install any kind of docker container, or VM or anything

golang's corporate sponsor, google, is highly multi-platform and the language is more aimed at such cross-platform use, unlike Swift's corporate sponsor. That said, what you want is available in these community-provided projects, though it requires more configuration.

Are there any equivalent in swift for targeting the android platform ?

I recently saw this company that is working on something like that, another one posted in the forum before. As my first Docker link shows, bridging Swift and Kotlin/JVM requires some work, so only these companies go to that extent: no OSS project does all that for free.

Ok, thanks a lot, everything is clearer now..