Skip to content

Instantly share code, notes, and snippets.

@uraimo
Last active March 9, 2018 03:13
Show Gist options
  • Save uraimo/b74d2e8dbe7ab1a6831f4cc6eba8928c to your computer and use it in GitHub Desktop.
Save uraimo/b74d2e8dbe7ab1a6831f4cc6eba8928c to your computer and use it in GitHub Desktop.
Building a cross-compiling toolchain for Swift 3.0.2 on Raspbian with working SPM

To build a Raspbian toolchain for Swift 3.0.2 a few things need to be changed in the original guide.

Download the Swift 3.0.2 packages instead of the 3.1.1 ones:

curl -o /tmp/swift-3.0.2-RPi1Zero-RaspbianNov16.tgz  https://www.dropbox.com/s/dkrkt9ht1c3hog2/swift-3.0.2-RPi1Zero-RaspbianNov16.tgz
curl -o /tmp/swift-3.0.2-RELEASE-osx.pkg https://swift.org/builds/swift-3.0.2-release/xcode/swift-3.0.2-RELEASE/swift-3.0.2-RELEASE-osx.pkg

Launch the script with these parameters:

./build_rpi_ubuntu_cross_compilation_toolchain \
/tmp/ \
/tmp/swift-3.0.2-RELEASE-osx.pkg \
/tmp/swift-3.0.2-RPi23-RaspbianNov16.tgz

The toolchain will build correctly but SPM will not work because Swift 3.0.2 on host does not support the new -swift-version parameter.

Let's fix it.

Clone SPM:

  git clone https://github.com/apple/swift-package-manager.git

Apply this patch (patch -p1 < file.diff):

diff --git a/Sources/Build/BuildPlan.swift b/Sources/Build/BuildPlan.swift
index 5c48fb57..af50f539 100644
--- a/Sources/Build/BuildPlan.swift
+++ b/Sources/Build/BuildPlan.swift
@@ -260,7 +260,7 @@ public final class SwiftTargetDescription {
     /// The arguments needed to compile this target.
     public func compileArguments() -> [String] {
         var args = [String]()
-        args += ["-swift-version", String(swiftVersion)]
+        //args += ["-swift-version", String(swiftVersion)]
         args += buildParameters.toolchain.extraSwiftCFlags
         args += buildParameters.swiftCompilerFlags
         args += optimizationArguments
diff --git a/Sources/PackageLoading/ManifestLoader.swift b/Sources/PackageLoading/ManifestLoader.swift
index 9f8f3cbd..02cde4be 100644
--- a/Sources/PackageLoading/ManifestLoader.swift
+++ b/Sources/PackageLoading/ManifestLoader.swift
@@ -306,7 +306,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
     ) -> [String] {
         var cmd = [String]()
         let runtimePath = self.runtimePath(for: manifestVersion)
-        cmd += ["-swift-version", String(manifestVersion.rawValue)]
+        //cmd += ["-swift-version", String(manifestVersion.rawValue)]
         cmd += ["-I", runtimePath.asString]
       #if os(macOS)
         cmd += ["-target", "x86_64-apple-macosx10.10"]

Compile SPM

  swift build

Move the SPM you are using right now and copy this one:

  cd /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2017-05-10-a.xctoolchain/usr/bin/
  sudo mv swift-build swift-build.old
  sudo cp /tmp/swift-package-manager/.build/debug/swift-build /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2017-05-10-a.xctoolchain/usr/bin/

You can use the toolchain now:

  swift build --destination /tmp/cross-toolchain/rpi-ubuntu-xenial-destination.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment