Skip to content

Commit

Permalink
Use static Linux SDK in Dockerfile (#1915)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyberbeni authored and nicklockwood committed Oct 27, 2024
1 parent 31319a9 commit dba3bef
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 15 deletions.
35 changes: 35 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Dockerfile
.dockerignore
.git
.github
.gitignore

# Everything from .gitignore
# OS X
.DS_Store

# Xcode
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.dot
.build
.swiftpm

/Snapshots/Private
swiftformat.artifactbundle.zip

# AppCode
.idea
7 changes: 2 additions & 5 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ jobs:
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Set up QEMU
uses: docker/setup-qemu-action@v3
with:
platforms: arm64

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3
Expand All @@ -52,6 +47,8 @@ jobs:
with:
context: .
platforms: linux/amd64, linux/arm64
no-cache: true
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
provenance: false
4 changes: 3 additions & 1 deletion CommandLineTool/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ import Foundation
import Darwin.POSIX
#elseif os(Windows)
import ucrt
#else
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#endif

#if SWIFT_PACKAGE
Expand Down
22 changes: 13 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# syntax=docker/dockerfile:1

# Swift official image with arm64 support
# https://hub.docker.com/r/arm64v8/swift/
ARG SWIFT_IMAGE=swift:focal
# Base image and static SDK have to be updated together.
FROM --platform=$BUILDPLATFORM swift:6.0.1 AS builder
WORKDIR /workspace
RUN swift sdk install \
https://download.swift.org/swift-6.0.1-release/static-sdk/swift-6.0.1-RELEASE/swift-6.0.1-RELEASE_static-linux-0.0.1.artifactbundle.tar.gz \
--checksum d4f46ba40e11e697387468e18987ee622908bc350310d8af54eb5e17c2ff5481

FROM $SWIFT_IMAGE AS builder
COPY . /workspace
WORKDIR /workspace
RUN swift build -c release && mv `swift build -c release --show-bin-path`/swiftformat /workspace
ARG TARGETPLATFORM
RUN --mount=type=cache,target=/workspace/.build,id=build-$TARGETPLATFORM \
./Scripts/build-linux-release.sh && \
cp /workspace/.build/release/swiftformat /workspace

FROM $SWIFT_IMAGE-slim AS runner
COPY --from=builder /workspace/swiftformat /usr/bin
ENTRYPOINT [ "swiftformat" ]
FROM scratch AS runner
COPY --from=builder /workspace/swiftformat /usr/bin/swiftformat
ENTRYPOINT [ "/usr/bin/swiftformat" ]
CMD ["."]
26 changes: 26 additions & 0 deletions Scripts/build-linux-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

set -eo pipefail

pushd "$(dirname "${BASH_SOURCE[0]}")/.." > /dev/null

BUILD_ARGS=(
--product swiftformat
--configuration release
)

if [[ -z "$TARGETPLATFORM" ]]; then
ARCH="$(uname -m)"
else
if [[ "$TARGETPLATFORM" = "linux/amd64" ]]; then
ARCH="x86_64"
elif [[ "$TARGETPLATFORM" = "linux/arm64" ]]; then
ARCH="aarch64"
else
echo "Unsupported target platform: $TARGETPLATFORM"
exit 1
fi
fi
BUILD_ARGS+=(--swift-sdk "${ARCH}-swift-linux-musl")

swift build "${BUILD_ARGS[@]}"

0 comments on commit dba3bef

Please sign in to comment.