1- # This Dockerfile tests the hack/Makefile output against git2go.
21ARG BASE_VARIANT=alpine
3- ARG GO_VERSION=1.17.6
2+ ARG GO_VERSION=1.17
43ARG XX_VERSION=1.1.0
54
6- FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
7-
8- FROM golang:${GO_VERSION}-${BASE_VARIANT} as gostable
9-
10- FROM gostable AS go-linux
11-
12- FROM --platform=$BUILDPLATFORM ${BASE_VARIANT} AS build-deps
13-
14- RUN apk add --no-cache \
15- bash \
16- curl \
17- build-base \
18- linux-headers \
19- perl \
20- cmake \
21- pkgconfig \
22- gcc \
23- musl-dev \
24- clang \
25- lld
26-
27- COPY --from=xx / /
5+ ARG LIBGIT2_IMG
6+ ARG LIBGIT2_TAG
287
29- ARG TARGETPLATFORM
30-
31- RUN xx-apk add --no-cache \
32- xx-c-essentials
33-
34- RUN xx-apk add --no-cache \
35- xx-cxx-essentials
36-
37- ARG TARGETPLATFORM
38- RUN xx-apk add --no-cache \
39- build-base \
40- pkgconfig \
41- gcc \
42- musl-dev \
43- clang \
44- lld \
45- llvm \
46- linux-headers
47-
48- WORKDIR /build
49- COPY hack/static.sh .
50-
51- ARG TARGETPLATFORM
52- ENV CC=xx-clang
53- ENV CXX=xx-clang++
8+ FROM ${LIBGIT2_IMG}:${LIBGIT2_TAG} AS build-deps
549
55- RUN CHOST=$(xx-clang --print-target-triple) \
56- ./static.sh build_libz
57-
58- RUN CHOST=$(xx-clang --print-target-triple) \
59- ./static.sh build_openssl
10+ FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
6011
61- RUN export LIBRARY_PATH="/usr/local/$(xx-info triple)/lib:/usr/local/$(xx-info triple)/lib64:${LIBRARY_PATH}" && \
62- export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig:/usr/local/$(xx-info triple)/lib64/pkgconfig" && \
63- export OPENSSL_ROOT_DIR="/usr/local/$(xx-info triple)" && \
64- export OPENSSL_CRYPTO_LIBRARY="/usr/local/$(xx-info triple)/lib64" && \
65- export OPENSSL_INCLUDE_DIR="/usr/local/$(xx-info triple)/include/openssl"
12+ FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_VARIANT} as gostable
6613
67- RUN ./static.sh build_libssh2
68- RUN ./static.sh build_libgit2
14+ FROM gostable AS go-linux
6915
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
7019
71- FROM go-${TARGETOS} AS build
20+ RUN apk add clang lld pkgconfig
7221
73- # Copy cross-compilation tools
7422COPY --from=xx / /
75- # Copy compiled libraries
76- COPY --from=build-deps /usr/local/ /usr/local/
7723
78- RUN apk add clang lld pkgconfig
24+ # build-go-mod can still be cached at build platform architecture.
25+ FROM build-base as build-go-mod
7926
8027WORKDIR /root/smoketest
8128COPY tests/smoketest/go.mod .
8229COPY tests/smoketest/go.sum .
8330RUN go mod download
8431
32+ # Build stage install per target platform
33+ # dependency and effectively cross compile the application.
34+ FROM build-go-mod as build
35+
8536ARG TARGETPLATFORM
8637
8738# Some dependencies have to installed
8839# for the target platform: https://github.com/tonistiigi/xx#go--cgo
89- RUN xx-apk add --no-cache \
90- musl-dev \
91- gcc
40+ RUN xx-apk add musl-dev gcc clang lld
9241
42+ WORKDIR /root/smoketest
9343
9444COPY tests/smoketest/main.go .
45+ COPY --from=build-deps /usr/local/ /usr/local/
9546
9647ENV CGO_ENABLED=1
97- 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" && \
9849 export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig:/usr/local/$(xx-info triple)/lib64/pkgconfig" && \
99- export FLAGS="$(pkg-config --static --libs --cflags libssh2 openssl libgit2)" && \
100- CGO_LDFLAGS="${FLAGS} -static" \
101- xx-go build \
50+ export FLAGS="$(pkg-config --static --libs --cflags libssh2 openssl libgit2)" && \
51+ export CGO_LDFLAGS="${FLAGS} -static" && \
52+ xx-go build \
10253 -ldflags "-s -w" \
10354 -tags 'netgo,osusergo,static_build' \
104- -o static-test-runner -trimpath main.go;
55+ -o static-test-runner -trimpath main.go
56+
10557
10658# Ensure that the generated binary is valid for the target platform
10759RUN xx-verify --static static-test-runner
@@ -119,11 +71,4 @@ COPY --from=build \
11971
12072ENV SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
12173
122- # To do docker run instead, replace the RUN statement with:
123- # ENTRYPOINT [ "/root/smoketest/static-test-runner" ]
124-
125- # The approach below was preferred as it provides a way to
126- # assert the functionality across the supported architectures
127- # without any extra steps.
128-
12974RUN /root/smoketest/static-test-runner
0 commit comments