|
1 | 1 | ARG BASE_VARIANT=alpine |
| 2 | +ARG GO_VERSION=1.17.6 |
| 3 | +ARG XX_VERSION=1.1.0 |
2 | 4 |
|
3 | 5 | ARG LIBGIT2_IMG |
4 | 6 | ARG LIBGIT2_TAG |
5 | 7 |
|
6 | | -FROM --platform=linux/amd64 ${LIBGIT2_IMG}:${LIBGIT2_TAG} as build-amd64 |
7 | | -FROM --platform=linux/arm64 ${LIBGIT2_IMG}:${LIBGIT2_TAG} as build-arm64 |
8 | | -FROM --platform=linux/arm/v7 ${LIBGIT2_IMG}:${LIBGIT2_TAG} as build-armv7 |
| 8 | +FROM ${LIBGIT2_IMG}:${LIBGIT2_TAG} AS build-deps |
9 | 9 |
|
10 | | -FROM --platform=$BUILDPLATFORM build-$TARGETARCH$TARGETVARIANT AS build |
| 10 | +FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx |
11 | 11 |
|
12 | | -RUN apk add clang lld pkgconfig ca-certificates |
| 12 | +FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_VARIANT} as gostable |
13 | 13 |
|
14 | | -ARG TARGETPLATFORM |
| 14 | +FROM gostable AS go-linux |
15 | 15 |
|
16 | | -# Some dependencies have to installed |
17 | | -# for the target platform: https://github.com/tonistiigi/xx#go--cgo |
18 | | -RUN xx-apk add --no-cache \ |
19 | | - musl-dev gcc lld binutils-gold |
| 16 | +# Build-base consists of build platform dependencies and xx. |
| 17 | +# These will be used at current arch to yield execute the cross compilations. |
| 18 | +FROM go-${TARGETOS} AS build-base |
| 19 | + |
| 20 | +RUN apk add clang lld pkgconfig |
| 21 | + |
| 22 | +COPY --from=xx / / |
| 23 | + |
| 24 | +# build-go-mod can still be cached at build platform architecture. |
| 25 | +FROM build-base as build-go-mod |
20 | 26 |
|
21 | 27 | WORKDIR /root/smoketest |
22 | 28 | COPY tests/smoketest/go.mod . |
23 | 29 | COPY tests/smoketest/go.sum . |
24 | 30 | RUN go mod download |
25 | 31 |
|
| 32 | +# Build stage install per target platform |
| 33 | +# dependency and effectively cross compile the application. |
| 34 | +FROM build-go-mod as build |
| 35 | + |
| 36 | +ARG TARGETPLATFORM |
| 37 | + |
| 38 | +# Some dependencies have to installed |
| 39 | +# for the target platform: https://github.com/tonistiigi/xx#go--cgo |
| 40 | +RUN xx-apk add musl-dev gcc clang lld |
| 41 | + |
| 42 | +WORKDIR /root/smoketest |
| 43 | + |
26 | 44 | COPY tests/smoketest/main.go . |
| 45 | +COPY --from=build-deps /usr/local/ /usr/local/ |
27 | 46 |
|
28 | 47 | ENV CGO_ENABLED=1 |
29 | | -RUN export LIBRARY_PATH="/usr/local/$(xx-info triple)/lib:/usr/local/$(xx-info triple)/lib64:${LIBRARY_PATH}" && \ |
| 48 | +RUN export LIBRARY_PATH="/usr/local/$(xx-info triple):/usr/local/$(xx-info triple)/lib64" && \ |
30 | 49 | export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig:/usr/local/$(xx-info triple)/lib64/pkgconfig" && \ |
31 | | - export FLAGS="$(pkg-config --static --libs --cflags libssh2 openssl libgit2)" && \ |
32 | | - CGO_LDFLAGS="${FLAGS} -static" \ |
33 | | - xx-go build \ |
| 50 | + export FLAGS="$(pkg-config --static --libs --cflags libssh2 openssl libgit2)" && \ |
| 51 | + export CGO_LDFLAGS="${FLAGS} -static" && \ |
| 52 | + xx-go build \ |
34 | 53 | -ldflags "-s -w" \ |
35 | 54 | -tags 'netgo,osusergo,static_build' \ |
36 | | - -o static-test-runner -trimpath main.go; |
| 55 | + -o static-test-runner -trimpath main.go |
| 56 | + |
37 | 57 |
|
38 | 58 | # Ensure that the generated binary is valid for the target platform |
39 | 59 | RUN xx-verify --static static-test-runner |
|
0 commit comments