Skip to content

Commit 30c64b0

Browse files
committed
Modernize rpi-5 build
Updates the rpi-5-blink example to link using SwiftPM and use a toolset.json.
1 parent e487219 commit 30c64b0

File tree

6 files changed

+65
-39
lines changed

6 files changed

+65
-39
lines changed

Tools/Toolsets/rpi-5-elf.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"schemaVersion": "1.0",
3+
"swiftCompiler": {
4+
"extraCLIOptions": [
5+
"-Xfrontend", "-disable-stack-protector",
6+
"-Xfrontend", "-function-sections",
7+
"-enable-experimental-feature", "Embedded",
8+
"-Xfrontend", "-mergeable-symbols",
9+
"-Xclang-linker", "-nostdlib"
10+
]
11+
},
12+
"linker": {
13+
"extraCLIOptions": [
14+
"-T", "Sources/Support/linkerscript.ld",
15+
"--unresolved-symbols=ignore-in-object-files"
16+
]
17+
}
18+
}

rpi-4b-blink/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SWIFT_EXEC ?= $(shell if [ "$(shell uname)" = "Darwin" ]; then xcrun -f swift; else which swift; fi)
1+
SWIFT_EXEC ?= swift
22
CLANG ?= $(shell if [ "$(shell uname)" = "Darwin" ]; then xcrun -f clang; else which clang; fi)
33
LLVM_OBJCOPY ?= $(shell if [ "$(shell uname)" = "Darwin" ]; then xcrun -f llvm-objcopy; else which llvm-objcopy; fi)
44

@@ -16,7 +16,7 @@ kernel8.img: kernel8.elf
1616

1717
kernel8.elf: $(BUILDROOT)/libMainApp.a $(BUILDROOT)/Support.build/boot.S.o link.ld
1818
@echo "🔗 Linking with clang..."
19-
$(CLANG) --target=aarch64-elf -o kernel8.elf $< $^ -fuse-ld=lld -nostdlib -Wl,--unresolved-symbols=ignore-in-object-files -Wl,-T ./link.ld
19+
$(CLANG) --target=aarch64-elf -o kernel8.elf $< $^ -fuse-ld=lld -nostdlib -Wl,--unresolved-symbols=ignore-in-object-files -Wl,-T ./link.ld -###
2020
@echo ""
2121

2222
$(BUILDROOT)/libMainApp.a $(BUILDROOT)/Support.build/boot.S.o:

rpi-5-blink/Makefile

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
1-
SWIFT_EXEC ?= $(shell if [ "$(shell uname)" = "Darwin" ]; then xcrun -f swift; else which swift; fi)
2-
CLANG ?= $(shell if [ "$(shell uname)" = "Darwin" ]; then xcrun -f clang; else which clang; fi)
3-
LLVM_OBJCOPY ?= $(shell if [ "$(shell uname)" = "Darwin" ]; then xcrun -f llvm-objcopy; else which llvm-objcopy; fi)
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the Swift open source project
4+
##
5+
## Copyright (c) 2025 Apple Inc. and the Swift project authors.
6+
## Licensed under Apache License v2.0 with Runtime Library Exception
7+
##
8+
## See https://swift.org/LICENSE.txt for license information
9+
##
10+
##===----------------------------------------------------------------------===##
411

5-
BUILDROOT := $(shell $(SWIFT_EXEC) build --triple aarch64-none-none-elf -Xswiftc -Xfrontend -Xswiftc -disable-stack-protector --show-bin-path)
12+
# Paths
13+
REPOROOT := $(shell git rev-parse --show-toplevel)
14+
TOOLSROOT := $(REPOROOT)/Tools
15+
TOOLSET := $(TOOLSROOT)/Toolsets/rpi-5-elf.json
16+
LLVM_OBJCOPY := llvm-objcopy
17+
SWIFT_BUILD := swift build
618

7-
.PHONY: all clean
19+
# Flags
20+
ARCH := aarch64
21+
TARGET := $(ARCH)-none-none-elf
22+
SWIFT_BUILD_ARGS := \
23+
--configuration release \
24+
--triple $(TARGET) \
25+
--toolset $(TOOLSET) \
26+
--disable-local-rpath
27+
BUILDROOT := $(shell $(SWIFT_BUILD) $(SWIFT_BUILD_ARGS) --show-bin-path)
828

9-
all: kernel8.img
29+
.PHONY: build
30+
build:
31+
@echo "building..."
32+
$(SWIFT_BUILD) \
33+
$(SWIFT_BUILD_ARGS) \
34+
--verbose
1035

11-
kernel8.img: kernel8.elf
12-
@echo "💾 Converting to binary kernel image with llvm-objcopy..."
13-
$(LLVM_OBJCOPY) -O binary kernel8.elf kernel8.img
14-
@echo ""
15-
@echo "🥳 Done! kernel8.img was saved to this directory."
36+
@echo "extracting binary..."
37+
$(LLVM_OBJCOPY) \
38+
-O binary \
39+
"$(BUILDROOT)/Application" \
40+
"$(BUILDROOT)/Application.bin" \
1641

17-
kernel8.elf: $(BUILDROOT)/libMainApp.a $(BUILDROOT)/Support.build/boot.S.o link.ld
18-
@echo "🔗 Linking with clang..."
19-
$(CLANG) --target=aarch64-elf -o kernel8.elf $< $^ -fuse-ld=lld -nostdlib -Wl,--unresolved-symbols=ignore-in-object-files -Wl,-T ./link.ld
20-
@echo ""
21-
22-
$(BUILDROOT)/libMainApp.a $(BUILDROOT)/Support.build/boot.S.o:
23-
@echo "🛠️ Building with Swift Package Manager..."
24-
$(SWIFT_EXEC) build --triple aarch64-none-none-elf -Xswiftc -Xfrontend -Xswiftc -disable-stack-protector
25-
@echo ""
2642

43+
.PHONY: clean
2744
clean:
28-
rm -rf kernel8.elf kernel8.img .build
45+
@echo "cleaning..."
46+
@swift package clean
47+
@rm -rf .build

rpi-5-blink/Package.swift

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,19 @@ let package = Package(
88
.macOS(.v14)
99
],
1010
products: [
11-
.library(
12-
name: "MainApp",
13-
type: .static,
14-
targets: ["MainApp"])
11+
.executable(name: "Application", targets: ["Application"])
1512
],
1613
dependencies: [
1714
.package(
1815
url: "https://github.com/apple/swift-mmio.git",
1916
branch: "swift-embedded-examples")
2017
],
2118
targets: [
22-
.target(
23-
name: "MainApp",
19+
.executableTarget(
20+
name: "Application",
2421
dependencies: [
2522
.product(name: "MMIO", package: "swift-mmio")
26-
],
27-
swiftSettings: [
28-
.enableExperimentalFeature("Embedded"),
29-
.unsafeFlags(["-Xfrontend", "-function-sections"]),
30-
]
31-
),
23+
]),
3224
.target(name: "Support"),
33-
3425
]
3526
)

rpi-5-blink/Sources/MainApp/MainApp.swift renamed to rpi-5-blink/Sources/Application/Application.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@ func ledOff() {
4141
}
4242

4343
@main
44-
struct Main {
45-
44+
struct Application {
4645
static func main() {
4746
setLedOutput()
48-
4947
while true {
5048
ledOn()
5149
for _ in 1..<100000 {} // just a delay
File renamed without changes.

0 commit comments

Comments
 (0)