Simplest way to develop on macOS and build on Linux

It should be possible to cross-compile from macOS to linux, just as others have from macOS to Android. You will need a C cross-compilation toolchain for linux and the Swift stdlib and core libraries for linux, which you can just get from the official Swift toolchain for linux.

Once you have those, you simply pass the right flags with those cross-compilation paths and some flags to the Swift compiler, as every Swift compiler is a cross-compiler just like clang, and you should be able to build for linux on macOS.

Take a look at the flags used to cross-compile for Android:

  1. -tools-directory should point to the cross-compilation toolchain, ie the C compiler and linker.
  2. -sdk to the C headers and libraries for linux, ie the directory containing /usr/include and /usr/lib/ with those files.
  3. You can specify the path to the Swift stdlib and core libraries separately with the -resource-dir flag not used in that documentation, ie /path/to/linux-swiftc/usr/lib/swift.

It may take some time to get this all setup, but you should have the quick iteration you're looking for after that.

2 Likes