From e9c191c6a0ca51b65c249ac75855c1e0a60bc83d Mon Sep 17 00:00:00 2001 From: Benedek Kozma Date: Sun, 27 Oct 2024 15:19:36 +0100 Subject: [PATCH] Use static Linux SDK in Dockerfile (#1915) --- .dockerignore | 35 ++++++++++++++++++++++++++++++++++ .github/workflows/docker.yml | 7 ++----- CommandLineTool/main.swift | 4 +++- Dockerfile | 22 ++++++++++++--------- Scripts/build-linux-release.sh | 26 +++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 .dockerignore create mode 100755 Scripts/build-linux-release.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..e7125ec55 --- /dev/null +++ b/.dockerignore @@ -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 diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index fb9fe6159..b6991ecb6 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -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 @@ -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 diff --git a/CommandLineTool/main.swift b/CommandLineTool/main.swift index 1a68379d9..55d619730 100644 --- a/CommandLineTool/main.swift +++ b/CommandLineTool/main.swift @@ -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 diff --git a/Dockerfile b/Dockerfile index 5bfb69658..44ebeecee 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 ["."] diff --git a/Scripts/build-linux-release.sh b/Scripts/build-linux-release.sh new file mode 100755 index 000000000..8d256f338 --- /dev/null +++ b/Scripts/build-linux-release.sh @@ -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[@]}"