Skip to content

Commit a1112f1

Browse files
authored
Convert stm32-blink to SwiftPM (#157)
Replaces bespoke shell scripts with a SwiftPM based build for the stm32-blink example. Adds CI for MachO based examples. Future: enable ELF variants of all MachO STM32 examples.
1 parent ea331f9 commit a1112f1

26 files changed

+4298
-545
lines changed

.github/actions/install-swift/action.yml

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ runs:
77
- name: Setup Environment
88
shell: bash
99
run: |
10+
mkdir -p "$HOME/.local/share"
11+
1012
export SWIFTLY_TOOLCHAINS_DIR="$HOME/.local/share/swiftly/toolchains"
1113
echo "SWIFTLY_TOOLCHAINS_DIR=$SWIFTLY_TOOLCHAINS_DIR" >> $GITHUB_ENV
1214
echo "SWIFTLY_TOOLCHAINS_DIR=$SWIFTLY_TOOLCHAINS_DIR" >> $HOME/.bashrc
@@ -22,15 +24,15 @@ runs:
2224
echo "PATH=$SWIFTLY_BIN_DIR:$PATH" >> $GITHUB_ENV
2325
echo "PATH=\$SWIFTLY_BIN_DIR:\$PATH" >> $HOME/.bashrc
2426
25-
- name: "Restore: Swift"
27+
- name: Restore Swift
2628
uses: actions/cache/restore@v4
2729
id: cache-swift
2830
with:
2931
path: "~/.local/share/swiftly"
30-
key: swift-${{ hashFiles('**/.swift-version') }}
32+
key: swift-${{ runner.os }}-${{ hashFiles('**/.swift-version', '.github/actions/install-swift/action.yml') }}
3133

3234
- name: Install `apt` Dependencies
33-
if: steps.cache-swift.outputs.cache-hit != 'true'
35+
if: runner.os == 'Linux' && steps.cache-swift.outputs.cache-hit != 'true'
3436
shell: bash
3537
run: |
3638
SUDO=$(if [[ $EUID -ne 0 ]]; then echo sudo; fi)
@@ -40,29 +42,47 @@ runs:
4042
DEBIAN_FRONTEND: noninteractive
4143

4244
- name: Install Swiftly
43-
if: steps.cache-swift.outputs.cache-hit != 'true'
45+
if: runner.os == 'Linux' && steps.cache-swift.outputs.cache-hit != 'true'
4446
shell: bash
4547
run: |
48+
SWIFTLY_VERSION=1.0.1
4649
UNAME=$(uname -m)
47-
curl -O "https://download.swift.org/swiftly/linux/swiftly-$UNAME.tar.gz"
48-
tar zxf "swiftly-$UNAME.tar.gz"
50+
SWIFTLY_TGZ=swiftly-$SWIFTLY_VERSION-$UNAME.tar.gz
51+
curl -O "https://download.swift.org/swiftly/linux/$SWIFTLY_TGZ"
52+
tar zxf "$SWIFTLY_TGZ"
4953
./swiftly init \
5054
--skip-install \
5155
--assume-yes \
5256
--quiet-shell-followup \
5357
--no-modify-profile
5458
59+
- name: Install Swiftly
60+
if: runner.os == 'macOS' && steps.cache-swift.outputs.cache-hit != 'true'
61+
shell: bash
62+
run: |
63+
SWIFTLY_VERSION=1.0.1
64+
SWIFTLY_PKG=swiftly-$SWIFTLY_VERSION.pkg
65+
curl -O "https://download.swift.org/swiftly/darwin/$SWIFTLY_PKG"
66+
pkgutil --check-signature $SWIFTLY_PKG
67+
pkgutil --verbose --expand $SWIFTLY_PKG $SWIFTLY_HOME_DIR
68+
tar -C $SWIFTLY_HOME_DIR -xvf $SWIFTLY_HOME_DIR/swiftly-*/Payload
69+
"$SWIFTLY_BIN_DIR/swiftly" init \
70+
--skip-install \
71+
--assume-yes \
72+
--quiet-shell-followup \
73+
--no-modify-profile
74+
5575
- name: Install Swift
5676
if: steps.cache-swift.outputs.cache-hit != 'true'
5777
shell: bash
5878
run: swiftly install --post-install-file ./out.sh
5979

60-
- name: "Save: Swift"
80+
- name: Save Swift
6181
if: steps.cache-swift.outputs.cache-hit != 'true'
6282
uses: actions/cache/save@v4
6383
with:
6484
path: "~/.local/share/swiftly"
65-
key: swift-${{ hashFiles('**/.swift-version') }}
85+
key: swift-${{ runner.os }}-${{ hashFiles('**/.swift-version', '.github/actions/install-swift/action.yml') }}
6686

6787
- name: Print Swift Version
6888
shell: bash
Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: STM32
1+
name: STM32 ELF
22

33
on:
44
push:
@@ -15,16 +15,12 @@ jobs:
1515
strategy:
1616
fail-fast: false
1717
matrix:
18-
example: [stm32-blink, stm32-lvgl]
18+
example: [stm32-lvgl]
1919

2020
steps:
2121
- name: Checkout repo
2222
uses: actions/checkout@v4
2323

24-
- name: Fixup for running locally in act
25-
if: ${{ env.ACT }}
26-
run: echo /opt/acttoolcache/node/18.20.8/x64/bin >> $GITHUB_PATH
27-
2824
- name: Set up Python
2925
uses: actions/setup-python@v5
3026
with:
@@ -36,21 +32,11 @@ jobs:
3632
- name: Install Swift
3733
uses: ./.github/actions/install-swift
3834

39-
- name: Set environment variables
40-
run: |
41-
echo "STM_BOARD=STM32F746G_DISCOVERY" >> $GITHUB_ENV
42-
4335
- name: Build ${{ matrix.example }}
4436
working-directory: ${{ matrix.example }}
4537
run: |
4638
if [[ -f ./fetch-dependencies.sh ]]; then
4739
./fetch-dependencies.sh
4840
fi
4941
50-
if [[ -f ./build-elf.sh ]]; then
51-
./build-elf.sh
52-
fi
53-
54-
if [[ -f Makefile ]]; then
55-
make
56-
fi
42+
make
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: STM32 Macho
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
pull_request:
7+
types: [opened, reopened, synchronize, ready_for_review]
8+
workflow_dispatch:
9+
10+
jobs:
11+
build:
12+
name: Build
13+
runs-on: [self-hosted, macos, sequoia, ARM64]
14+
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
example: [stm32-blink, stm32-lcd-logo, stm32-neopixel, stm32-uart-echo]
19+
20+
steps:
21+
- name: Checkout repo
22+
uses: actions/checkout@v4
23+
24+
- name: Install uv
25+
uses: astral-sh/setup-uv@v5
26+
with:
27+
python-version: 3.11
28+
29+
- name: Install Swift
30+
uses: ./.github/actions/install-swift
31+
32+
- name: Build ${{ matrix.example }}
33+
working-directory: ${{ matrix.example }}
34+
run: make

.swiftformatignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
./harmony/*
2-
./stm32-lvgl/*
2+
./stm32-blink/Sources/STM32F7X6/*
33
./stm32-lcd-logo/Sources/STM32F7X6/*
4+
./stm32-lvgl/*
45
./stm32-lvgl/Sources/Registers/*
56
./stm32-neopixel/Sources/STM32F7X6/*
67
./stm32-uart-echo/Sources/STM32F7X6/*

stm32-blink/Board.swift

Lines changed: 0 additions & 115 deletions
This file was deleted.

stm32-blink/Main.swift

Lines changed: 0 additions & 42 deletions
This file was deleted.

stm32-blink/Makefile

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
##===----------------------------------------------------------------------===##
11+
12+
# Paths
13+
REPOROOT := $(shell git rev-parse --show-toplevel)
14+
TOOLSROOT := $(REPOROOT)/Tools
15+
TOOLSET := $(TOOLSROOT)/Toolsets/stm32f74x.json
16+
MACHO2BIN := $(TOOLSROOT)/macho2bin.py
17+
SWIFT_BUILD := swift build
18+
19+
# Flags
20+
ARCH := armv7em
21+
TARGET := $(ARCH)-apple-none-macho
22+
SWIFT_BUILD_ARGS := \
23+
--configuration release \
24+
--triple $(TARGET) \
25+
--toolset $(TOOLSET)
26+
BUILDROOT := $(shell $(SWIFT_BUILD) $(SWIFT_BUILD_ARGS) --show-bin-path)
27+
28+
.PHONY: build
29+
build:
30+
@echo "building..."
31+
$(SWIFT_BUILD) \
32+
$(SWIFT_BUILD_ARGS) \
33+
-Xlinker -map -Xlinker $(BUILDROOT)/Application.mangled.map \
34+
--verbose
35+
36+
@echo "demangling linker map..."
37+
cat $(BUILDROOT)/Application.mangled.map \
38+
| c++filt | swift demangle > $(BUILDROOT)/Application.map
39+
40+
@echo "disassembling..."
41+
otool \
42+
-arch $(ARCH) -v -V -d -t \
43+
$(BUILDROOT)/Application \
44+
| c++filt | swift demangle > $(BUILDROOT)/Application.disassembly
45+
46+
@echo "extracting binary..."
47+
$(MACHO2BIN) \
48+
$(BUILDROOT)/Application \
49+
$(BUILDROOT)/Application.bin \
50+
--base-address 0x20010000 \
51+
--segments '__TEXT,__DATA,__VECTORS'
52+
53+
.PHONY: clean
54+
clean:
55+
@echo "cleaning..."
56+
@swift package clean
57+
@rm -rf .build

0 commit comments

Comments
 (0)