Compile Swift for Raspberry Pi by XCode

First and foremost, I hope you guys have the Swift environment in place for running and programming. If not, check out these following posts:
* Step 1 – set up Raspbian OS
Step 2 – Set up Swift environment on Raspbian

There are many ways to write a program for Raspberry Pi.

The simplest way is you will have a keyboard, a mouse, a monitor plug directly in the Raspberry board, or just simply connect it over SSH remotely. Use command-line editors like nano, vim, whatever. Create a swift file, code it, then compile and run the program. Everything can be done on terminal.

stone man

But I don’t like to be the man in stone of age. 🙂

In order to develop a complex project. I don’t think that is a good way to go. I have come up with several ideas, researched and ended up with getting XCode handshaking with Rasberry Pi.

That is amazing for guys who use a MAC. We’re able take advantage of XCode, make development more productivity, convienient and more fun. This is the goal I like to share in this post.

Note: The tutorial was done on the following environment.
* MAC: MAC OS Mojave 10.14.12, XCode 10.1
* Raspberry: Raspberry Pi 3B+, Raspbian Strech Lite OS, Swift 4.1.1

Create a project

First of all, We should know how to create a project with the help of Swift tools. I’m just showing you the steps on MAC.

On your MAC, open the terminal. Make a directory, and jump in it

mkdir swift_program
cd swift_program

Create the swift project

swift package init --type executable

Executable type creates a executable program, you can try types like empty|library|executable|system-module later

Ok, the project has been created. Now compile and run it.

swift compile
swift run

You can call for compile and run separately, or just call swift run. It will automatically compile and run right after

Or you can just run the program which has been compiled by adding this option –skip-run

After all, you should see Hello World!

make new swift project on raspberry
Create swift project on Raspbian

To keep this post short, I’m not gonna explain the directory structure which has been generated. You can learn more here

XCode project file

In order to open the project, We need a XCode project file which has .xcodeproj extension.

Simply generate the project file by the following command

swift package generate-xcodeproj

generate swift xcode file swift xcode file

Double click to open the project in Xcode.

open project in xcode

How to compile?

Basically, XCode cannot compile program for Raspberry on MAC OS, because of CPU arch, OS kernel, libraries and stuff. What We do here is ask Raspberry to compile code.

The mechanism includes 2 phases:

  • Phase 1: Sync the source files. XCode call the command rsync. This command automatically syncs up everything to Raspberry Pi include source code and relevant files in the project folder.
  • Phase 2: XCode send compiling command swift build over ssh, then Raspberry Pi takes care of building the source. In case of failure or success, the message will be sent back to XCode’s console.

XCode compile Swift for Raspberry 2

In order to achieve these, We need a script to do this. I need to say thank Thomas Paulmann , He has the idea implemented and package it perfectly. It works like a boss. Check it out his project here Swish for more detail.

Install Swish

——> See the full setup guide of Swish here

Run this command to install Swish on your MAC

brew install thomaspaulmann/homebrew-formulae/swish

Then you need to pair your MAC with Raspberry, so that everytime you call ssh, it will not ask you again for password.

Firstly, Generate public key

ssh-keygen -t rsa

Put it into Raspberry

cat ~/.ssh/ | ssh @ "mkdir -p ~/.ssh && cat >>  ~/.ssh/authorized_keys"

Check ssh pairing again.

ssh user@hostname

Try this twice. In the second time, it should not ask you for the password.

Config Swish in XCode

In this part, We integrates Swish into XCode

Firstly, We need to add an external build system target to project. Follow steps in the following pictures

This slideshow requires JavaScript.

Secondly, We need to create scheme for the target We has added. Follow these steps


This slideshow requires JavaScript.

Now, We need to set swish with the Raspberry user and ip address in the build target

set swish in xcode.png
replace <user> and <ip address> by the appropriate raspberry user and ip address


– The Swift version on Raspberry is Swift 4.1.1. So make sure the swift-tools-version in Package.swift is 4.1
– The Raspberry Pi must be on, and accessible via the given user/ip address. (Use ssh to check accessibility)

When everything is in place, to build the project. You need to select the target “RemoteBuild”, choose the platform as a MAC (In fact, It triggers the build script to build on Raspberry Pi)

Select target to build on xcode.png

Then click play Icon to compile, or use the shortcut key Cmd + B on MAC

Remote build console output.png

After all, you should see “Build succeeded” message.

project synced on raspberry pi.png

Now, you connect to the Raspberry Pi by ssh.

The project locates at /home/pi/Swish/swift_program

Change to directory the project directory, and use the following command to run the  project

swift run --skip-build

That is it.

What’s next

At this time, I see XCode is the best candidate for Swift development. However, XCode can be so advanced for developers who are not familiar. There is another option, Visual Studio Code. I will share this next time, so stay tuned

Reference resources:

3 thoughts on “Compile Swift for Raspberry Pi by XCode

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s