简体   繁体   中英

(Cross-)Compiling Swift for Raspberry PI

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?

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 (eg 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

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 , eg in HypriotOS.
  • there is a Slack group for Swift ARM: swift-arm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM