Cross-Compiling macOS to Linux

Okay, so the first thing that occurs to me is that the script aims to build a "Xenial" aka 16.04 toolchain but you're trying to build a 18.04 one. Probably, that's not the only issue however.

To build all of Vapor, you'll need to add at least openssl (and its development libraries) to the toolchain builder script. Unfortunately, the toolchain builder script you're using doesn't resolve the dependencies automatically, so you'll have to add them all manually :frowning:. I do apologise, the script is really only a demonstration and should be much better but at the time where I wrote it, it did the job for me. These days I think the folks from the SwiftCrossCompilers project know best how to use and drive the Swift cross compilation support.

Does a simple project work for you? So if you tried

cd /tmp && mkdir supersimple && swift package init --type=executable
swift build --destination /tmp/...destination.json

Does that work? Because if not, please report back and we can start filing bugs :).

Yes, the executable Swift package does work. Thanks, this is more of a curiosity than anything, so it’s interesting to see what needs to be built up.

1 Like

My current error message is similar to this Swift bug which was fixed.

1 Like

Here is how we are building for Android without using --destination flag (armv7 example):

  1. Create a shell script which contains needed flags for Android armv7 target:

    #!/bin/bash
    
    # File: swiftc-arm-linux-androideabi
    
    /PathToSwitCompilerBuiltForAndroid/swift-android-toolchain/usr/bin/swiftc \
    -v -Xcc -v \
    -swift-version 5 \
    -target armv7-none-linux-androideabi \
    -tools-directory /usr/local/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64/bin \
    -Xclang-linker --sysroot=/usr/local/ndk/20.1.5948944/platforms/android-24/arch-arm \
    -Xclang-linker --gcc-toolchain=/usr/local/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64 \
    -Xcc -I/usr/local/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include \
    -Xcc -I/usr/local/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/arm-linux-androideabi \
    -L /usr/local/ndk/20.1.5948944/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a \
    -L /usr/local/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x \
    -L /usr/local/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/arm-linux-androideabi/24 \
    -L /PathToLibrariesBuiltForAndroid/swift-android-toolchain/usr/lib/swift/android/armv7 \
    $@
    
  2. Pass this shell script as an argument for swift build to build a package.

    SWIFT_EXEC=/PathToThisFileShownAbove/bin/swiftc-arm-linux-androideabi swift build \
    -c release \
    -Xswiftc -sdk -Xswiftc /PathToToolchainWhichBuiltForAndroid/swift-android-toolchain
    

    As result, swift package manager will produce binaries for armv7 target. Dynamic libraries will have a so extension.

1 Like

Um, Is this still so bad in 2022?

Related thread: Cross compiling from macOS to Linux