Compiling Swift on Linux
Swift is open source, which means that we can build it ourselves. This isn’t too hard to do, but takes some time.
Set up the dependencies and grab the code
Firstly, you need a lot of memory and as it takes ages, give your VM plenty of CPUs! My Macbook Pro has a quad core process, so I tell Virtualbox that it can use all 4 using this configuration in my Vagrantfile:
config.vm.provider "virtualbox" do |vb| vb.memory = "8192" vb.cpus = 4 end
You then need all the dependencies inside the VM:
$ sudo apt-get install git cmake ninja-build clang python uuid-dev libicu-dev \ icu-devtools libbsd-dev libedit-dev libxml2-dev libsqlite3-dev swig libpython-dev \ libncurses5-dev pkg-config
If you want to build the docs, then you also need Sphinx:
$ sudo easy_install -U Sphinx==1.3.4
Now grab the code from GitHub:
$ mkdir swift-dev && cd swift-dev $ git clone git@github.com:apple/swift.git
In addition to the core swift repository, you also need a number of other repositories and fortunately, there’s a script to do this for us:
$ swift/utils/update-checkout --clone-with-ssh
Alternatively, if you don’t want to do all this manually, you can just grab this Vagrantfile from IBM.
At a later date, if you want to collect the latest changes to the source files just run update-checkout with no arguments:
swift/utils/update-checkout --all
Introducing build_script
To compile Swift we use a script to do it all for us. This one is called build-script which takes a number of different parameters. Use -h to see what it provides.
In order to successfully build in 8GB of RAM, we need to build without the debug symbols, so we use the -R switch for that. To also run the tests, we need -t and if you want to build Foundation and XCTest, you need to add the switches --xctest and --foundation.
The simplest build command, however, is:
$ swift/utils/build-script -R
This is a good time to get a cup of tea. On my computer, it takes 55 minutes to build… Once built, the binaries are in build/Ninja-ReleaseAssert/swift-linux-x86_64/bin.
Compiling for usage
When you build with -R, you’re building for contributing to the compiler and associated libraries. If you want to build so that you can then use the tool chain for writing Swift applications, then the easiest way is to use preset which creates an installable package:
$ swift/utils/build-script --preset=buildbot_linux_1510 installable_package=~/swift.tar.gz install_destdir=~/swift-install
Again, this takes a while to do!!
Once done, you have a fully working swift package at ~/swift-install.
Test using:
$ swift-install/usr/bin/swift -v
Thank you for this. Please also note that rsync is required as well for compiling for usage (used by swiftpm/Utilities/bootstrap).