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 in terminal.
Having that said, working in an elaborate project. I don’t think that is a good way to go. I’ve come up with several ideas, researched and ended up with getting XCode handshaking with Raspberry Pi.
That is amazing for guys who use a MAC OS machine. We’re able take advantages of XCode, make development workflow more productivity, conveniently and much 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_programcd 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 compileswift 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!
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
Double click to open the 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.
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/id_rsa.pub | 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
Secondly, We need to create scheme for the target We has added. Follow these steps
Now, We need to set swish with the Raspberry user and ip address in the build target
Build
Note:
— 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)
Then click play Icon to compile, or use the shortcut key Cmd + B on MAC
After all, you should see “Build succeeded” message.
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
Come to this point, We’re all done.
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. This is a well-known, handy tool, it is strong enough to write a decent Swift program.
Lean more here -> Compile Swift for Raspberry Pi by Visual Studio Code
Published
Originally published at http://swiftreviewercom.wordpress.com on December 24, 2018.