Swift 3.0 on a BeagleBone Black

| | 0 Comments| 12:06 PM
Categories:

Swift 3.0 Swift 3.0 Swift 3.0

There are a number of people working to bring Swift 3.0 to as many ARM-based systems as possible. I’m personally interested in the BeagleBoard family of devices and try Swift builds on a BeagleBone Black as soon as they are available. If you’re interested in joining the community of folks working on getting Swift 3.0 for ARM devices come join our community.

We’re assuming here you have a BeagleBone Black running Debian Jessie and have at least 1G of free space available. The base .tgz distribution is nearly 200M, and around 600M extracted. If you’ve booted from a microSD card and taking advantage of its capacity we can show you how to get the most out of it.

The team working on Swift for ARM is using Jenkins to build natively on a BeagleBoard X15. You can find the latest build for BeagleBone Black here. Grab and extract the contents into a directory and set your PATH with something like:

Now, let’s give it a spin!

Create a file called helloWorld.swift:

You can use swift helloWorld.swift to execute the file like a script:

# swift helloWorld.swift
Hello, world!

Or, you can compile the file into an executable with swiftc if you have clang installed and configured properly:

# swiftc helloWorld.swift
# ./helloWorld
Hello world!

If swiftc failed with error: link command failed with exit code 127 there’s a good chance you don’t have clang installed and configured properly:

sudo apt-get update
sudo apt-get install libicu-dev
sudo apt-get install clang-3.6
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

Let’s look at a few other tidbits:

Importing Glibc works!

swiftcat.swift:

Compile (swiftc swiftcat.swift) and run (swiftcat)!

Bridging to C routines

Linking against compiled object files works!

escapetext.c:

escapetext.h:

escapeswift.swift:

Compile and link everything together:

# clang -c escapetext.c
# swiftc -c escapeswift.swift -import-objc-header escapetext.h
# swiftc escapeswift.o escapetext.o -o escapeswift -lcurl

And run:

# ./escapeswift "foo > bar"
Escaped text:  foo%20%3E%20bar

Swift Package Manager

Unless you enjoy writing makefiles and build scripts (and trust me, some do), the Swift Package Manager is here to help manage software package dependencies. We’ll be writing more about the SwiftPM available in Swift 3.0, but are excited to provide a version that works on armv7 devices. Try this out:

# mkdir finalCountdown && cd finalCountdown
# swift package init --type executable
Creating executable package: finalCountdown
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/main.swift
Creating Tests/

Replace the contents of Sources/main.swift with

Now, run swift build and your finalCountdown application:

# swift build
Compile Swift Module 'finalCountdown' (1 sources)
Linking .build/debug/finalCountdown
# .build/debug/finalCountdown
Entering thread
10...9...8...7...6...5...4...3...2...1...
Exiting thread
Done

moreswift

For a random smattering of Swift 3.0 applications that have been run on both x86 and armv7 systems, check out the moreswift swift-3.0 branch.

1 thought on “Swift 3.0 on a BeagleBone Black”

  1. Build #66 works well, but when I use the code in the finalCountdown Package Manager example, I got an error on line 3 of main.swift about Foundation because it uses NSThread, and I guess Swift-3.0 Foundation just calls it Thread.

    After fixing this, it works fine.

    Thanks again for all the hard work.

Leave a Reply

Your email address will not be published. Required fields are marked *