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:
-
-tools-directoryshould point to the cross-compilation toolchain, ie the C compiler and linker. -
-sdkto the C headers and libraries for linux, ie the directory containing/usr/includeand/usr/lib/with those files. - You can specify the path to the Swift stdlib and core libraries separately with the
-resource-dirflag 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.