Skip to content

Commit 9ff3496

Browse files
committed
feat: enable cross-compilation of Linux binaries using Swift static SDK
1 parent ece6a10 commit 9ff3496

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

.github/workflows/release.yml

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,26 @@ jobs:
3131
retention-days: 5
3232

3333
linux-amd64:
34-
name: Build Linux AMD64 binary
3534
runs-on: ubuntu-latest
3635
steps:
37-
- name: Checkout
38-
uses: actions/checkout@v4
39-
40-
- name: Set up Docker Buildx
41-
uses: docker/setup-buildx-action@v3
42-
43-
- name: Build AMD64 binary
44-
uses: docker/build-push-action@v5
36+
- uses: actions/checkout@v4
37+
- uses: docker/setup-buildx-action@v3
38+
- uses: docker/build-push-action@v5
4539
with:
4640
context: .
4741
target: output
42+
platforms: linux/amd64
43+
build-args: |
44+
SWIFT_SDK_ID=x86_64-swift-linux-musl
4845
outputs: type=local,dest=artifacts
49-
46+
- name: Assert x86_64 binary
47+
run: |
48+
file artifacts/subtree
49+
file artifacts/subtree | grep -q 'x86-64' # fail if not x86_64
5050
- name: Strip and prepare binary
5151
run: |
5252
strip artifacts/subtree
5353
mv artifacts/subtree subtree_linux
54-
5554
- name: Upload AMD64 Artifact
5655
uses: actions/upload-artifact@v4
5756
with:
@@ -60,27 +59,26 @@ jobs:
6059
retention-days: 5
6160

6261
linux-arm64:
63-
name: Build Linux ARM64 binary
6462
runs-on: ubuntu-24.04-arm
6563
steps:
66-
- name: Checkout
67-
uses: actions/checkout@v4
68-
69-
- name: Set up Docker Buildx
70-
uses: docker/setup-buildx-action@v3
71-
72-
- name: Build ARM64 binary
73-
uses: docker/build-push-action@v5
64+
- uses: actions/checkout@v4
65+
- uses: docker/setup-buildx-action@v3
66+
- uses: docker/build-push-action@v5
7467
with:
7568
context: .
7669
target: output
70+
platforms: linux/arm64
71+
build-args: |
72+
SWIFT_SDK_ID=aarch64-swift-linux-musl
7773
outputs: type=local,dest=artifacts
78-
74+
- name: Assert aarch64 binary
75+
run: |
76+
file artifacts/subtree
77+
file artifacts/subtree | grep -q 'ARM aarch64'
7978
- name: Strip and prepare binary
8079
run: |
8180
strip artifacts/subtree
8281
mv artifacts/subtree subtree_linux_aarch64
83-
8482
- name: Upload ARM64 Artifact
8583
uses: actions/upload-artifact@v4
8684
with:

Dockerfile

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# syntax=docker/dockerfile:1
22

3-
# Base image and static SDK have to be updated together.
4-
# Native runner approach: Each job uses its own native architecture
5-
FROM swift:6.1 AS builder
3+
FROM --platform=$BUILDPLATFORM swift:6.1 AS builder
4+
ARG TARGETPLATFORM
5+
ARG SWIFT_SDK_ID
66
WORKDIR /workspace
77

8-
# Install Swift static SDK for better portability
8+
# Install Swift static SDK (version must match base toolchain)
99
RUN swift sdk install \
10-
https://download.swift.org/swift-6.1-release/static-sdk/swift-6.1-RELEASE/swift-6.1-RELEASE_static-linux-0.0.1.artifactbundle.tar.gz \
11-
--checksum 111c6f7d280a651208b8c74c0521dd99365d785c1976a6e23162f55f65379ac6
10+
https://download.swift.org/swift-6.1-release/static-sdk/swift-6.1-RELEASE/swift-6.1-RELEASE_static-linux-0.0.1.artifactbundle.tar.gz \
11+
--checksum 111c6f7d280a651208b8c74c0521dd99365d785c1976a6e23162f55f65379ac6
1212

13-
# Copy source files
1413
COPY . /workspace
1514

16-
# Build with static SDK - will compile for native architecture
17-
RUN swift build -c release --swift-sdk swift-6.1-RELEASE_static-linux-0.0.1 && \
18-
find /workspace/.build -name "subtree" -type f -executable -exec cp {} /workspace/subtree \;
15+
# Install `file` utility
16+
RUN apt-get update && apt-get install -y --no-install-recommends file \
17+
&& rm -rf /var/lib/apt/lists/*
18+
19+
# Map platform -> correct SDK ID if not provided
20+
RUN if [ -z "$SWIFT_SDK_ID" ]; then \
21+
case "$TARGETPLATFORM" in \
22+
linux/amd64) SWIFT_SDK_ID=x86_64-swift-linux-musl ;; \
23+
linux/arm64) SWIFT_SDK_ID=aarch64-swift-linux-musl ;; \
24+
*) echo "Unsupported TARGETPLATFORM: $TARGETPLATFORM" && exit 1 ;; \
25+
esac; \
26+
fi && \
27+
echo "Using Swift SDK: $SWIFT_SDK_ID" && \
28+
swift build -c release --swift-sdk "$SWIFT_SDK_ID" -Xswiftc -Osize && \
29+
cp ".build/$SWIFT_SDK_ID/release/subtree" /workspace/subtree && \
30+
file /workspace/subtree
1931

20-
# Final minimal runtime image
2132
FROM scratch AS runner
2233
COPY --from=builder /workspace/subtree /usr/bin/subtree
23-
ENTRYPOINT [ "/usr/bin/subtree" ]
34+
ENTRYPOINT ["/usr/bin/subtree"]
2435
CMD ["--help"]
2536

26-
# Output stage for CI extraction
2737
FROM scratch AS output
28-
COPY --from=builder /workspace/subtree /subtree
38+
COPY --from=builder /workspace/subtree /subtree

0 commit comments

Comments
 (0)