Compiling Swift on Linux

DZone 's Guide to

Compiling Swift on Linux

Step-by-step instructions for compiling Swift on Linux—including, among other steps, some significant waiting.

· Performance Zone ·
Free Resource

tl;dr  ZONE: ??Performance or web maybe?

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, so give your VM plenty of CPUs! My Macbook Pro has a quad-core processor, so I tell Virtualbox that it can use all four using this configuration in my Vagrantfile:

  config.vm.provider "virtualbox" do |vb|
    vb.memory = "8192"
    vb.cpus = 4

You then need all the dependencies inside the VM:

$ sudo apt-get install git cmake ninja-build clang python uuid-dev libicu-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 inbuild/Ninja-ReleaseAssert/swift-linux-x86_64/bin.

Image title

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
ios ,linux ,swift


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}