(Cross-)Compiling Swift for Raspberry PI
Asked Answered
B

2

21

Swift is now Open Source. Did anyone tried compiling Swift for a Raspberry PI? I started to do, but my 8 GB SD card seems to be too small for it ;) Is it possible to cross compile it from Ubuntu?

Beckerman answered 5/12, 2015 at 16:31 Comment(8)
8gb too small? can you elaborate on that? Even iOS doesn't take up 8gb.Soulier
For compiling... There was an error "fatal error: error in backend: IO failure on output stream."Beckerman
Yup that seems to be an out-of-disk-space error. Some people on the interwebs had some succes with a reboot. Can you compile to a different location and check the resulting size?Soulier
I now compiled with Xubuntu... the complete folder had 6.9 GB. I retry it with a Rasbian lite, but I think it will not work... swift.org says: "Currently x86_64 is the only supported architecture on Linux."Beckerman
Did you say you got it to compile? I have been trying to get it to compile (64G card) but haven't been able to get it to work (compiles for 24h then errors). How are you doing it?Grindery
No, I just could compile it with Xubuntu. With Raspberry I also get errors (firstly because of lacking space)Beckerman
Andrew Madsen has compiled it. Here is the link to his blog: blog.andrewmadsen.com/post/136137396480/swift-on-raspberry-pi Have not tried it myself due to lack of a free SD Card, but it does look promisingStringendo
@Lupurus: If the problem is really lack os disk space, you may attach an external HD (USB) or map a network disk and use PREFIX to compile and store the compiled objects on the new disk.Triangular
S
7

A 8GB SD Card works ok, but you'll need to extend the root volume. I have it working and the used space on /dev/root partition is around 3.1GB.

The steps below are based on the blog post by Andrew Madsen with a little extra focus on the steps inside fdisk.

Get Ubuntu

Download an image of Ubuntu 14.04 for Raspberry Pi 2 from finnie.org and copy it onto the SD card. Boot the Raspberry Pi.

Change the partition

Log into the Raspberry Pi and change the partition size. The default size for /dev/root is 1.7G with 1.1G available. That is not enough.

$ df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/root       1.7G  540M  1.1G  35% /
devtmpfs        458M  4.0K  458M   1% /dev
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none             93M  228K   93M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            462M     0  462M   0% /run/shm
none            100M     0  100M   0% /run/user
/dev/mmcblk0p1   64M   20M   45M  31% /boot/firmware

Run fdisk

sudo fdisk /dev/mmcblk0

At the prompt enter p for 'print the partition table'. There are two partitions

/dev/mmcblk0p1   *        2048      133119       65536    c  W95 FAT32 (LBA)
/dev/mmcblk0p2          133120     3670015     1768448   83  Linux

When prompted, enter d (for delete), then 2. Then, recreate the partition by entering n, then p, then 2, then pressing enter at the next two prompts accepting the defaults.

Enter p again and see the second partition is now bigger, now all space on an 8GB card is used.

           Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1   *        2048      133119       65536    c  W95 FAT32 (LBA)
/dev/mmcblk0p2          133120    15523839     7695360   83  Linux

Enter w to write the changes to disk, then reboot

sudo reboot

Resize the partition

After the reboot, resize the partition’s file system by running

sudo resize2fs /dev/mmcblk0p2

Swap space

Setup a swap file by doing

sudo apt-get install dphys-swapfile

Install libicu-dev and clang-3.6

sudo apt-get install libicu-dev clang-3.6

Use update-alternatives to provide /usr/bin links for clang and clang++:

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

Then, add @iachievedit’s repository key:

wget -qO- http://dev.iachieved.it/iachievedit.gpg.key | sudo apt-key add -

Add the appropriate repository to sources.list:

echo "deb [arch=armhf] http://iachievedit-repos.s3.amazonaws.com/ trusty main" | sudo tee --append /etc/apt/sources.list

Run apt-get update:

sudo apt-get update

Install Swift

sudo apt-get install swift-2.2

After the installation is complete, you’re ready to compile Swift programs!

Write Swift

Open your favorite text editor, write a program, and save it (e.g. to 'hello.swift’):

let device = "Raspberry Pi 2!" 
print("Hello from Swift on \(device)")

Compile it

swiftc hello.swift

and run it:

./hello

Hello from Swift on Raspberry Pi 2!

That’s it! Swift running on Raspberry Pi

Stringendo answered 8/1, 2016 at 13:37 Comment(4)
Good to know there's a pre-compiled package already. Are/Were you able to confirm it's trustworthy? - dev.iachieved.it claims to own that bucket. +1 nevertheless.Triangular
I have decided to trust it for now :) On the above referenced blog it says: William Dillon (@hpux735) announced that he had gotten the Swift compiler to successfully build for armv7 systems such as the BeagleBone, Raspberry Pi 2, etc. A while later, @iachievedit had packaged William’s work up and made it available via apt-get for Debian and Ubuntu.Stringendo
An answer on how really cross-compile it would be nice (e.g. build on OS X, deploy on Rasperry Pi).Plenty
Excellent answer. Do you know if it's possible to install clang and Swift on Raspian, or does it only work with Ubuntu?Eindhoven
P
6

The Swift Package Manager got custom toolchain support via PR-1098 end of April 2017.

I wrote up detailed instructions on how to build a RaspberryPi toolchain over here: macOS -> RasPi Cross Compilation Toolchain and even the reverse (build macOS binaries on a RaspberryPi) for the fun of it. The same would work for Intel-Linux to ARM-Linux w/ minimal modifications. The SwiftPM repo contains an example script on how to do this for Intel-macOS to Intel-Ubuntu.

You can find a 2017-05-01 update on Swift-on-ARM over here: An Update on Swift 3.1.1 For Raspberry Pi Zero/1/2/3.

As a small summary, so that this answer isn't just links ;-), ARM status 2017-05-16:

  • you can compile Swift 3.1/3.1.1 on RaspberryPi Ubuntu
    • don't forget to setup swap, some minimal patches are required for 3.1.1. 8GB disk may be a bit to little.
  • compilation on Raspbian doesn't seem to work yet (last known version is 3.0.2)
  • you can cross-compile Swift using a custom toolchain, which is reasonably easy to setup
    • you need to grab a SwiftPM snapshot (Swift 4) for this (but the toolchain itself can be 3.1 or even 3.0.2 w/ minor changes)
  • you can also run (and compile) Swift via Docker, e.g. in HypriotOS.
  • there is a Slack group for Swift ARM: swift-arm
Pheasant answered 16/5, 2017 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.