Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Cross-)Compiling Swift for Raspberry PI

Tags:

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?

like image 577
Lupurus Avatar asked Dec 05 '15 16:12

Lupurus


People also ask

Can you program a Raspberry Pi with Swift?

Since Raspberry Pi is limited in RAM and CPU power, using a language like Swift is perfect for maximizing the potential of the hardware.


1 Answers

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

like image 187
Olaf Avatar answered Oct 14 '22 07:10

Olaf