An Example of Scripting with Swift on Linux

swift-og

Swift 3.0 Linux

If you follow us on Twitter (@iachievedit) you know that we build open source Swift a lot, and for a number of different OSes and architectures. There’s no rhyme or reason as to when we decide to start building, but it’s nice to keep track of the various git revisions of the repositories that went into a given artifact.

Since we’re excited about Swift eventually displacing other scripting languages on Linux (their names rhyme with Bython and Buby), it felt like a good time to write up a script (in Swift) that did this for us.

Some notes here:

  • we’re using a subscript extension of String that will likely never be a part of the core language for reasons.
  • exec is our version of backticks (output = `ls`) in other scripting languages. We want to execute a command and read its output.
  • there’s certainly more optimization and tightening that can be done.

With that out of the way, here is swiftrevs.swift:

We use this as follows:

# swift swiftrevs.swift
swift,https://github.com/apple/swift.git,master,cf3fdff04
llvm,https://github.com/apple/swift-llvm.git,stable,8d0086ac3
clang,https://github.com/apple/swift-clang.git,stable,460d629e8
lldb,https://github.com/apple/swift-lldb.git,master,da120cf91
compiler-rt,https://github.com/apple/swift-compiler-rt.git,stable,9daa1b32c
cmark,https://github.com/apple/swift-cmark.git,master,5af77f3c1
llbuild,https://github.com/apple/swift-llbuild.git,master,7aadcf936
swiftpm,https://github.com/apple/swift-package-manager.git,master,423c2a1c8
swift-corelibs-xctest,https://github.com/apple/swift-corelibs-xctest.git,master,03905f564
swift-corelibs-foundation,https://github.com/apple/swift-corelibs-foundation.git,master,4c15543f8
swift-integration-tests,https://github.com/apple/swift-integration-tests.git,master,4eda8055a
swift-corelibs-libdispatch,https://github.com/apple/swift-corelibs-libdispatch.git,master,e922531c2

This, of course, is run in a directory that has all of the various repositories required to build Swift cloned. If you’re interested in learning how to build Swift for X86 or certain ARM devices, check out our package-swift repository. It not only contains swiftrevs.swift but also a handful of scripts aimed at making building Swift (along with the REPL, Foundation, and Swift Package Manager) nearly painless.

Leave a Reply

Your email address will not be published. Required fields are marked *