Skip to content

Commit

Permalink
dockerfile: use osxcross toolchain on debian
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Jul 25, 2022
1 parent 04f4be8 commit 75dabed
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
53 changes: 41 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ ARG XX_VERSION=1.1.1
ARG GOVERSIONINFO_VERSION=v1.3.0
ARG GOTESTSUM_VERSION=v1.7.0
ARG BUILDX_VERSION=0.8.2
ARG OSXCROSS_VERSION=11.3-r7

FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx

FROM crazymax/osxcross:${OSXCROSS_VERSION}-alpine AS osxcross-alpine
FROM --platform=$BUILDPLATFORM crazymax/osxcross:${OSXCROSS_VERSION}-debian AS osxcross-bullseye
FROM osxcross-${BASE_VARIANT} AS osxcross

FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_VARIANT} AS build-base-alpine
COPY --from=xx / /
RUN apk add --no-cache bash clang lld llvm file git
Expand Down Expand Up @@ -42,6 +47,7 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
&& /out/gotestsum --version

FROM build-${BASE_VARIANT} AS build
ARG BASE_VARIANT
# GO_LINKMODE defines if static or dynamic binary should be produced
ARG GO_LINKMODE=static
# GO_BUILDTAGS defines additional build tags
Expand All @@ -57,15 +63,27 @@ ARG PACKAGER_NAME
COPY --from=goversioninfo /out/goversioninfo /usr/bin/goversioninfo
# in bullseye arm64 target does not link with lld so configure it to use ld instead
RUN [ ! -f /etc/alpine-release ] && xx-info is-cross && [ "$(xx-info arch)" = "arm64" ] && XX_CC_PREFER_LINKER=ld xx-clang --setup-target-triple || true
RUN --mount=type=bind,target=.,ro \
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/root/.cache \
--mount=from=dockercore/golang-cross:xx-sdk-extras,target=/xx-sdk,src=/xx-sdk \
--mount=type=tmpfs,target=cli/winresources \
# override the default behavior of go with xx-go
xx-go --wrap && \
# export GOCACHE=$(go env GOCACHE)/$(xx-info)$([ -f /etc/alpine-release ] && echo "alpine") && \
TARGET=/out ./scripts/build/binary && \
xx-verify $([ "$GO_LINKMODE" = "static" ] && echo "--static") /out/docker
--mount=from=osxcross,src=/osxcross,target=/osxcross \
--mount=from=osxcross,src=/osxsdk,target=/xx-sdk \
--mount=type=tmpfs,target=cli/winresources <<EOT
set -e
if [ "$BASE_VARIANT" != "alpine" ] && [ "$(xx-info os)" = "darwin" ]; then
# on debian, use osxcross toolchain that includes the llvm-dsymutil tool
# and right minimal llvm version required
# more info: https://github.com/docker/cli/pull/3717
export PATH="/osxcross/bin:$PATH"
export LD_LIBRARY_PATH="/osxcross/lib:$LD_LIBRARY_PATH"
export CC="o64-clang"
export CXX="o64-clang++"
fi
# override the default behavior of go with xx-go
xx-go --wrap
# export GOCACHE=$(go env GOCACHE)/$(xx-info)$([ -f /etc/alpine-release ] && echo "alpine")
TARGET=/out ./scripts/build/binary
xx-verify $([ "$GO_LINKMODE" = "static" ] && echo "--static") /out/docker
EOT

FROM build-${BASE_VARIANT} AS test
COPY --from=gotestsum /out/gotestsum /usr/bin/gotestsum
Expand All @@ -79,15 +97,26 @@ FROM scratch AS test-coverage
COPY --from=test /tmp/coverage.txt /coverage.txt

FROM build-${BASE_VARIANT} AS build-plugins
ARG BASE_VARIANT
ARG GO_LINKMODE=static
ARG GO_BUILDTAGS
ARG GO_STRIP
ARG CGO_ENABLED
ARG VERSION
RUN --mount=ro --mount=type=cache,target=/root/.cache \
--mount=from=dockercore/golang-cross:xx-sdk-extras,target=/xx-sdk,src=/xx-sdk \
xx-go --wrap && \
TARGET=/out ./scripts/build/plugins e2e/cli-plugins/plugins/*
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/root/.cache \
--mount=from=osxcross,src=/osxcross,target=/osxcross \
--mount=from=osxcross,src=/osxsdk,target=/xx-sdk <<EOT
set -e
if [ "$BASE_VARIANT" != "alpine" ] && [ "$(xx-info os)" = "darwin" ]; then
export PATH="/osxcross/bin:$PATH"
export LD_LIBRARY_PATH="/osxcross/lib:$LD_LIBRARY_PATH"
export CC="o64-clang"
export CXX="o64-clang++"
fi
xx-go --wrap
TARGET=/out ./scripts/build/plugins e2e/cli-plugins/plugins/*
EOT

FROM build-base-alpine AS e2e-base-alpine
RUN apk add --no-cache build-base curl docker-compose openssl openssh-client
Expand Down
4 changes: 4 additions & 0 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ target "_common" {
GO_VERSION = GO_VERSION
BUILDKIT_CONTEXT_KEEP_GIT_DIR = 1
}
no-cache-filter = ["osxcross"]
}

target "_platforms" {
Expand Down Expand Up @@ -141,16 +142,19 @@ target "update-authors" {
}

target "test" {
inherits = ["_common"]
target = "test"
output = ["type=cacheonly"]
}

target "test-coverage" {
inherits = ["_common"]
target = "test-coverage"
output = ["build/coverage"]
}

target "e2e-image" {
inherits = ["_common"]
target = "e2e"
output = ["type=docker"]
tags = ["${IMAGE_NAME}"]
Expand Down

0 comments on commit 75dabed

Please sign in to comment.