Swift 3.0.2 For Raspberry Pi Zero/1/2/3
Posted on December 30, 2016
Update May 2017: Swift 3.1.1 is now available here.
I’ve finally got around to building the latest Swift release (3.0.2, distributed with XCode8.2.1) for all the Raspberry Pi boards.
The ARMv6 version that runs on the original RaspberryPi (A,B,A+,B+) and the Zero, requires the latest Raspbian released on November 2016. This version does not have a working Swift Package Manager at the moment, so for now you’ll have to do without it. Get it here.
The ARMv7 version for RaspberryPi 2 and 3 built on the same Raspbian is available here and a build that runs on Ubuntu 16.04 LTS can be downloaded from here.
On Debian you’ll need these dependencies: libedit2, libpython2.7, curl, libxml2.
As usual, all versions have been tested with SwiftyGPIO and everything appears to work.
A Few Notes On Compiling For The Original Raspberry (ARMv6)
Currently Swift requires a few patches to compile on ARM boards that are collected and kept up to date by their author, William Dillon, in the repositories of swift-arm.
Building for the original Raspberry Pi and the Zero requires an additional small patch and a few manual steps.
I’ve created a repository that contains the scripts I’ve used to clone, patch and build Swift (derived from package-swift) for those who want to replicate what I did.
As usual, you’ll need at least 8Gb of storage space and 2 Gb of swap (configured with dphy-swapfile), I recommend using a single 16Gb or more class 10+ SD-card for both.
Building on the original Raspberry using the release of Raspbian from November will require a slightly different set of packages:
sudo apt-get install git cmake ninja-build clang-3.7 python uuid-dev libicu-dev icu-devtools libbsd-dev libedit-dev libxml2-dev libsqlite3-dev swig libpython-dev libncurses5-dev pkg-config libblocksruntime-dev libcurl4-openssl-dev autoconf libtool systemtap-sdt-dev libkqueue-dev
Luckily Clang 3.7 is now available from the official Raspbian repository and the only additional package we need is libkqueue-dev
.
Since SPM does not work at the moment, we don’t need to build that project, so its build option can be omitted from the build command:
./swift/utils/build-script --build-subdir mybuild_linux -R --lldb --llbuild --xctest --foundation --libdispatch -- --install-libdispatch --install-foundation --install-swift --install-lldb --install-llbuild --install-xctest --install-prefix=/usr '--swift-install-components=autolink-driver;compiler;clang-builtin-headers;stdlib;swift-remote-mirror;sdk-overlay;dev' --build-swift-static-stdlib --build-swift-static-sdk-overlay --install-destdir=${INSTALL_DIR} --installable-package=${PACKAGE} --verbose-build
Once we have cloned the main repository and all the repositories for the subprojects, that command will start the build.
The build will fail near the end of the compilation of the swift-linux-armv6
directory with this error:
FAILED: cd /home/pi/buildswift/build/mybuild_linux/swift-linux-armv6/stdlib/public/Platform && /usr/lo
cal/bin/cmake -E make_directory /home/pi/buildswift/build/mybuild_linux/swift-linux-armv6/stdlib/public/Pl
atform/linux/armv6 && /usr/local/bin/cmake -E make_directory /home/pi/buildswift/build/mybuild_linux/swift
-linux-armv6/./lib/swift/linux/armv6 && /home/pi/buildswift/swift/utils/line-directive /home/pi/buildswift/
swift/stdlib/public/Platform/Glibc.swift /home/pi/buildswift/swift/stdlib/public/Platform/Platform.swift /h
ome/pi/buildswift/swift/stdlib/public/Platform/TiocConstants.swift /home/pi/buildswift/build/mybuild_linux
/swift-linux-armv6/stdlib/public/Platform/4/tgmath.swift -- /home/pi/buildswift/build/mybuild_linux/swift-
linux-armv6/./bin/swiftc -c -sdk / -target armv6-unknown-linux-gnueabihf -resource-dir /home/pi/buildswift/
build/mybuild_linux/swift-linux-armv6/./lib/swift -O -D INTERNAL_CHECKS_ENABLED -I /home/pi/buildswift/bui
ld/mybuild_linux/swift-linux-armv6/./lib/swift/linux/armv6 -module-cache-path /home/pi/buildswift/build/bu
ildbot_linux/swift-linux-armv6/./module-cache -no-link-objc-runtime -autolink-force-load -module-link-name
swiftGlibc -force-single-frontend-invocation -parse-as-library -emit-module -emit-module-path /home/pi/buil
dswift/build/mybuild_linux/swift-linux-armv6/./lib/swift/linux/armv6/Glibc.swiftmodule -o /home/pi/buildsw
ift/build/mybuild_linux/swift-linux-armv6/stdlib/public/Platform/linux/armv6/Glibc.o /home/pi/buildswift/s
wift/stdlib/public/Platform/Glibc.swift /home/pi/buildswift/swift/stdlib/public/Platform/Platform.swift /ho
me/pi/buildswift/swift/stdlib/public/Platform/TiocConstants.swift /home/pi/buildswift/build/mybuild_linux/
swift-linux-armv6/stdlib/public/Platform/4/tgmath.swift
/home/pi/buildswift/swift/stdlib/public/Platform/Glibc.swift:13:19: error: no such module 'SwiftGlibc'
@_exported import SwiftGlibc // Clang module
^
ninja: build stopped: subcommand failed.
To get past this point, go to build/mybuild_linux/swift-linux-armv6
and complete manually the creation of the SwiftGlibc
module using:
ninja swiftGlibc-linux-armv6-static
Once this is done, you can go back to the root and launch again the build command. Nothing else was needed for 3.0.2, future releases will likely need further adjustments.
Did you like this article? Let me know on Twitter!