Skip to content

Commit 2a0fc80

Browse files
authored
Merge pull request #1019 from crazy-max/xx-1.9.0
dockerfile: zig support with xx 1.9.0
2 parents 155d3e6 + e824d93 commit 2a0fc80

1 file changed

Lines changed: 14 additions & 37 deletions

File tree

Dockerfile

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
ARG GO_VERSION="1.25.5"
44
ARG ALPINE_VERSION="3.22"
5+
ARG XX_VERSION="1.9.0"
56

67
# xx is a helper for cross-compilation
7-
FROM --platform=$BUILDPLATFORM tonistiigi/xx:1.7.0 AS xx
8+
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
89

910
# osxcross contains the MacOSX cross toolchain for xx
1011
FROM crazymax/osxcross:15.5-debian AS osxcross
@@ -18,53 +19,29 @@ RUN --mount=type=cache,target=/go/pkg/mod \
1819
go mod download
1920
ENV CGO_ENABLED=1
2021

21-
22-
ARG GIT_TAG
23-
ARG GIT_COMMIT
22+
FROM builder-base AS builder
23+
RUN apk add clang zig
24+
COPY . ./
2425
ARG TARGETPLATFORM
2526
ARG TARGETOS
2627
ARG TARGETARCH
27-
28-
FROM builder-base AS builder-darwin
29-
RUN apk add clang
30-
COPY . ./
28+
RUN xx-apk add musl-dev
29+
ARG GIT_TAG
30+
ARG GIT_COMMIT
3131
RUN --mount=type=bind,from=osxcross,src=/osxsdk,target=/xx-sdk \
3232
--mount=type=cache,target=/root/.cache,id=docker-ai-$TARGETPLATFORM \
3333
--mount=type=cache,target=/go/pkg/mod <<EOT
3434
set -ex
35+
if [ "$TARGETOS" != "darwin" ]; then
36+
export XX_GO_PREFER_C_COMPILER=zig
37+
fi
3538
xx-go build -trimpath -ldflags "-s -w -X 'github.com/docker/cagent/pkg/version.Version=$GIT_TAG' -X 'github.com/docker/cagent/pkg/version.Commit=$GIT_COMMIT'" -o /binaries/cagent-$TARGETOS-$TARGETARCH .
36-
xx-verify --static /binaries/cagent-darwin-$TARGETARCH
37-
EOT
38-
39-
FROM builder-base AS builder-linux
40-
RUN apk add clang
41-
RUN xx-apk add musl-dev gcc
42-
COPY . ./
43-
RUN --mount=type=cache,target=/root/.cache,id=docker-ai-$TARGETPLATFORM \
44-
--mount=type=cache,target=/go/pkg/mod <<EOT
45-
set -ex
46-
xx-go build -trimpath -ldflags "-s -w -linkmode=external -extldflags '-static' -X 'github.com/docker/cagent/pkg/version.Version=$GIT_TAG' -X 'github.com/docker/cagent/pkg/version.Commit=$GIT_COMMIT'" -o /binaries/cagent-$TARGETOS-$TARGETARCH .
47-
xx-verify --static /binaries/cagent-linux-$TARGETARCH
48-
EOT
49-
50-
FROM builder-base AS builder-windows
51-
RUN apk add zig build-base
52-
COPY . ./
53-
RUN --mount=type=cache,target=/root/.cache,id=docker-ai-$TARGETPLATFORM \
54-
--mount=type=cache,target=/go/pkg/mod <<EOT
55-
set -ex
56-
if [ "$TARGETARCH" = "arm64" ]; then
57-
ZIG_TARGET="aarch64-windows-gnu"
58-
else
59-
ZIG_TARGET="x86_64-windows-gnu"
39+
xx-verify --static /binaries/cagent-$TARGETOS-$TARGETARCH
40+
if [ "$TARGETOS" = "windows" ]; then
41+
mv /binaries/cagent-$TARGETOS-$TARGETARCH /binaries/cagent-$TARGETOS-$TARGETARCH.exe
6042
fi
61-
CC="zig cc -target $ZIG_TARGET" CXX="zig c++ -target $ZIG_TARGET" xx-go build -trimpath -ldflags "-s -w -X 'github.com/docker/cagent/pkg/version.Version=$GIT_TAG' -X 'github.com/docker/cagent/pkg/version.Commit=$GIT_COMMIT'" -o /binaries/cagent-$TARGETOS-$TARGETARCH .
62-
mv /binaries/cagent-$TARGETOS-$TARGETARCH /binaries/cagent-$TARGETOS-$TARGETARCH.exe
63-
xx-verify --static /binaries/cagent-windows-$TARGETARCH.exe
6443
EOT
6544

66-
FROM builder-$TARGETOS AS builder
67-
6845
FROM scratch AS local
6946
ARG TARGETOS TARGETARCH
7047
COPY --from=builder /binaries/cagent-$TARGETOS-$TARGETARCH* cagent

0 commit comments

Comments
 (0)