Building Swift 3.0 on an ARMv7 System

swift-og

Swift 3.0 Swift 3.0 Swift 3.0

Editor’s Note: This post supercedes our other posts on building Swift 3.0 for the BeagleBone Black or Raspberry Pi 3 as it utilizes the repository’s hosted by the Swift ARM organization.

To get started with building Swift 3.0 natively on an ARMv7 you’re going to need:

  • a suitably powerful ARMv7 system; we have used the BeagleBoard X15 with build times of about 4 hours and Raspberry Pi 3 for initial build times of 6 hours
  • high-speed IO on either a USB3 flash drive or a UHS-I/class 10 microSD card with a capacity of 16GB or greater (I like the Patriot EP Series for the price combined with relatively high write speeds)
  • one of the supported Ubuntu releases such as Ubuntu Xenial 16.04
  • Patience

Install Build Prerequisites

Compiling Swift requires a number of prerequisites. Get them in one fell swoop with:

# sudo apt-get install -y git cmake ninja-build clang uuid-dev libicu-dev icu-devtools libbsd-dev libedit-dev libxml2-dev libsqlite3-dev swig libpython-dev libncurses5-dev pkg-config autoconf libtool systemtap-sdt-dev libcurl4-openssl-dev

Add Some Swap

You will need at a minimum 2G of RAM on an ARMv7 system to build Swift. If you are working with the Pi3 you will want to add some swap to double up from the base 1G.

# cd /var/cache
# sudo mkdir swap
# cd swap
# sudo fallocate -l 1G 1G.swap
# sudo mkswap 1G.swap
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=184d002a-2f15-4b23-8360-8e792badc6a2
# sudo chmod 600 1G.swap
# sudo swapon 1G.swap

Using build-swift

build-swift is our Github repository that has several helper scripts to make life a little easier building Swift on ARM.

# git clone https://github.com/swift-arm/build-swift
# cd build-swift

By default build-swift will check out to the swift-3.0 branch.

The contained scripts (located in the scripts directory) are pretty simple:

  • get.sh – Downloads all of the required repositories for building Swift; these repositories are from the Swift ARM organization and have been patched specifically for building ARMv7
  • update.sh – Updates all of your local repositories with the latest on Github
  • package.sh – Runs the Swift build-script
  • clean.sh – Deletes the build artifacts
  • distclean.sh – Deletes both the build artifacts and the deletes the repositories

I suggest you look at each shell script and walk through the logic before executing. They are straightforward but it’s worth the time understanding what they are doing.

Let’s Do This

Remember to make sure you have:

  • installed all of the prerequisite packages
  • have plenty of RAM available on your system (2G minimum)
  • cloned build-swift from Github

Now, clone all of the required repositories:

# cd build-swift
# ./scripts/get.sh

On a high-speed SD card this step took about 15 minutes to download and write everything.

Now, compile. The Swift on ARM team uses Jenkins to run this in a headless build job, but if you use nohup to ensure your compile continues even if the terminal detaches you should be fine:

# nohup ./scripts/package.sh > swiftbuild.log&

To watch the output of the build use tail -F swiftbuild.log.

Now, be prepared to wait! A clean build of Swift on a Raspberry Pi 3 can take upwards of 6 hours. The reward is well worth it, however, and that is a swift.tar.gz bundle that can be installed on either a Raspberry Pi 2 or Pi 3 running Ubuntu Xenial (do not try to install this on a Raspbian machine, you will be sorry). I like to install Swift in /opt/swift/ like so:

# cd /opt
# mkdir -p swift/swift-3.0
# cd swift/swift-3.0
# tar -xzvf /path/to/swift.tar.gz

You can then set your PATH with export PATH=/opt/swift/swift-3.0/usr/bin:$PATH. To properly use swiftc and swift build you will also need to run the following commands:

sudo apt-get install -y libicu-dev
sudo apt-get install -y clang-3.6
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.6 100
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-3.6 100

Getting Help

If you are having trouble compiling feel free to join our swift-arm Slack.

4 comments

  1. SerhiiReply

    Hey!
    I’ve successfully built Swift-3.0 on Raspberry Pi B+ (armv6) and Raspberry Pi 3 (armv7) running raspbian.
    But there’s always some “but”. Swift Package Manager can no be built because swift-build-tool throws an error: “malloc(): memory corruption: 0x76cc157e”. Also on armv6 i had to modify all Makefiles in libdispatch generated build folder to make “SWIFTC” have value of “/build-swift/build/buildbot_linux/swift-linux-armv6/lib/swift/linux/armv6”, not “/build-swift/build/buildbot_linux/swift-linux-armv6/lib/swift/linux/armv6l”

  2. SerhiiReply

    Hey!
    It’s me again. I’ve updated my raspbian to testing version and successfully built full toolchain including swift package manager. I believe that memory corruption was caused by old version of libglib.
    Few more things: I had to fix swift/utils/build-script-impl script – on line 818 theres one extra bracket (not present in apple version of source). And this bug https://github.com/apple/swift/pull/5296 is present in ARM version of sources.

  3. ArieDReply

    I have only partially successfully build Swift 3-0 according to these instructions on a RPi 3B with Ubuntu MATE 16.04.1 LTS distribution. Swift and swiftc are working but “swift build” is not. There is one error in the swift build.log that may be the cause of this:

    CMake Error at scripts/cmake_install.cmake:36 (file):
    file INSTALL cannot find
    “/home/pi/Swift30/build-swift/build/buildbot_linux/lldb-linux-armv7/lib/python2.7″.
    Call Stack (most recent call first):
    cmake_install.cmake:42 (include)

    The …/python2.7 is indeed missing, but instead a …/python3.5 is present. Both 3.5 and 2.7 python versions are present in the ubuntu mate distribution, 2.7.12 is the shown when you run ‘python -V’.

    I looked at many cmake files, but both 3.5 and 2.7 are used, so I was unable to find any root cause. Would be nice if someone could solve this.

  4. KrystianReply

    Hi I tried to compile Swift on OrangePi Lite with Armbian (Ubuntu Xenial) but I have this error while linking:
    Full log here: https://gist.github.com/krystiansliwa/ecd7964e3a35021d95f622a05c7d5905

    clang: error: unable to execute command: Killed
    clang: error: linker command failed due to signal (use -v to see invocation)
    [456/607] Linking CXX executable bin/swift-remoteast-test
    ninja: build stopped: subcommand failed.
    ./swift/utils/build-script: fatal error: command terminated with a non-zero exit status 1, aborting
    + Fixing up the install package for ARM
    cp: cannot create directory ‘/home/krystian/swift/build-swift/scripts/../install/usr/lib/swift’: No such file or directory
    cp: cannot stat ‘./build/buildbot_linux/libdispatch-linux-armv7/src/swift/Dispatch.swiftdoc’: No such file or directory
    cp: cannot stat ‘./build/buildbot_linux/libdispatch-linux-armv7/src/swift/Dispatch.swiftmodule’: No such file or directory
    cp: cannot stat ‘./build/buildbot_linux/libdispatch-linux-armv7/src/libdispatch.la’: No such file or directory
    cp: cannot stat ‘./build/buildbot_linux/libdispatch-linux-armv7/src/.libs/libdispatch.so’: No such file or directory
    + Retar installation package

Leave a Reply

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