Swift running on bare metal Raspberry Pi
APACHE-2.0 License
Hleeb (Ukrainian "хліб" for "bread") is collection of sample code allowing to run trivial Swift functions on bare metal Raspberry Pi 2, aspiring to become an OS kernel in some distant future.
But Swift already runs on Raspberry Pi, what's the big deal?
Existing port of Swift to Raspberry Pi runs in Linux user space, relying on the Linux kernel for memory management and I/O. With Hleeb we're trying to implement foundations of an OS kernel from scratch, utilizing Swift for it as much as possible.
Hleeb relies on uSwift as a minimalistic replacement for the Swift standard library. Additionally, a patch enabling bare metal targets is required for the Swift toolchain to compile Hleeb.
On macOS, build requirements can be installed with Homebrew by executing brew bundle
in the root
directory of a clone of this repository.
Make sure you have at least 20-50 GB of free storage for the toolchain build. 8 GB is the minimal amount of RAM, 16 GB is recommended. This was tested on macOS Big Sur with Xcode 13.2, earlier versions may work but weren't tested. Building on Linux may work, but you'll need to adjust these steps accordingly.
Create a swift-source
directory for the toolchain build in your working directory and navigate to it:
mkdir swift-source
cd swift-source
maxd/baremetal
branch of this Swift toolchain fork.git clone https://github.com/swiftwasm/swift.git
cd swift
git checkout maxd/baremetal
./utils/webassembly/ci.sh && cd ../..
git clone https://github.com/compnerd/uswift.git
git clone https://github.com/MaxDesiatov/Hleeb.git
LLVM_BIN
pointing to LLVM installed on M1, you may need to adjust this pathSWIFT_BIN
to the path of your freshly built Swift toolchaincmake -B build -DLLVM_BIN=/opt/homebrew/opt/llvm/bin/ \
-DSWIFT_BIN=<your_bare_metal_swift_toolchain_bin_path> \
-DCMAKE_TOOLCHAIN_FILE=./toolchain-arm-none-eabi.cmake \
-DCMAKE_BUILD_TYPE=Release \
-G Ninja -S . && \
ninja -C build
Run ./test.sh
after a successful build. This will execute the newly built "kernel" in QEMU emulating Raspberry Pi 2.
You should see the following output:
42
Hello, kernel World!
Number 42 is computed with Swift code as a sum of numbers 40 and 2, called from the C code. Eventually we'd like to rewrite remaining C code that controls UART in Swift, as soon as pointer types are fully working in uSwift.
For now we're leaving testing procedure on real RPi hardware as an exercise for the reader. See this tutorial for more details on USB serial output, which is currently the only way to get output from Hleeb when running on real hardware.
Where not stated otherwise, Hleeb is available under the Apache 2.0 license. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the LICENSE file for more info.
This repository also incorporates code available under BSD-3 and MIT licenses, which is specified in headers of corresponding files.