Merge pull request #1433 from crazy-max/makefile-build-opts

hack: mutualize build opts in Makefile and Dockerfile
This commit is contained in:
CrazyMax 2022-12-07 05:04:44 +01:00 committed by GitHub
commit fe8d5627e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 16 deletions

View File

@ -19,20 +19,24 @@ ENV CGO_ENABLED=0
WORKDIR /src WORKDIR /src
FROM gobase AS buildx-version FROM gobase AS buildx-version
RUN --mount=target=. \ RUN --mount=type=bind,target=. <<EOT
PKG=github.com/docker/buildx VERSION=$(git describe --match 'v[0-9]*' --dirty='.m' --always --tags) REVISION=$(git rev-parse HEAD)$(if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi); \ set -e
echo "-X ${PKG}/version.Version=${VERSION} -X ${PKG}/version.Revision=${REVISION} -X ${PKG}/version.Package=${PKG}" | tee /tmp/.ldflags; \ mkdir /buildx-version
echo -n "${VERSION}" | tee /tmp/.version; echo -n "$(./hack/git-meta version)" | tee /buildx-version/version
echo -n "$(./hack/git-meta revision)" | tee /buildx-version/revision
EOT
FROM gobase AS buildx-build FROM gobase AS buildx-build
ARG LDFLAGS="-w -s"
ARG TARGETPLATFORM ARG TARGETPLATFORM
RUN --mount=type=bind,target=. \ RUN --mount=type=bind,target=. \
--mount=type=cache,target=/root/.cache \ --mount=type=cache,target=/root/.cache \
--mount=type=cache,target=/go/pkg/mod \ --mount=type=cache,target=/go/pkg/mod \
--mount=type=bind,source=/tmp/.ldflags,target=/tmp/.ldflags,from=buildx-version \ --mount=type=bind,from=buildx-version,source=/buildx-version,target=/buildx-version <<EOT
set -x; xx-go build -ldflags "$(cat /tmp/.ldflags) ${LDFLAGS}" -o /usr/bin/buildx ./cmd/buildx && \ set -e
xx-verify --static /usr/bin/buildx xx-go --wrap
DESTDIR=/usr/bin VERSION=$(cat /buildx-version/version) REVISION=$(cat /buildx-version/revision) GO_EXTRA_LDFLAGS="-s -w" ./hack/build
xx-verify --static /usr/bin/docker-buildx
EOT
FROM gobase AS test FROM gobase AS test
RUN --mount=type=bind,target=. \ RUN --mount=type=bind,target=. \
@ -45,13 +49,13 @@ FROM scratch AS test-coverage
COPY --from=test /tmp/coverage.txt /coverage.txt COPY --from=test /tmp/coverage.txt /coverage.txt
FROM scratch AS binaries-unix FROM scratch AS binaries-unix
COPY --link --from=buildx-build /usr/bin/buildx / COPY --link --from=buildx-build /usr/bin/docker-buildx /buildx
FROM binaries-unix AS binaries-darwin FROM binaries-unix AS binaries-darwin
FROM binaries-unix AS binaries-linux FROM binaries-unix AS binaries-linux
FROM scratch AS binaries-windows FROM scratch AS binaries-windows
COPY --link --from=buildx-build /usr/bin/buildx /buildx.exe COPY --link --from=buildx-build /usr/bin/docker-buildx /buildx.exe
FROM binaries-$TARGETOS AS binaries FROM binaries-$TARGETOS AS binaries
@ -60,8 +64,11 @@ FROM --platform=$BUILDPLATFORM alpine AS releaser
WORKDIR /work WORKDIR /work
ARG TARGETPLATFORM ARG TARGETPLATFORM
RUN --mount=from=binaries \ RUN --mount=from=binaries \
--mount=type=bind,source=/tmp/.version,target=/tmp/.version,from=buildx-version \ --mount=type=bind,from=buildx-version,source=/buildx-version,target=/buildx-version <<EOT
mkdir -p /out && cp buildx* "/out/buildx-$(cat /tmp/.version).$(echo $TARGETPLATFORM | sed 's/\//-/g')$(ls buildx* | sed -e 's/^buildx//')" set -e
mkdir -p /out
cp buildx* "/out/buildx-$(cat /buildx-version/version).$(echo $TARGETPLATFORM | sed 's/\//-/g')$(ls buildx* | sed -e 's/^buildx//')"
EOT
FROM scratch AS release FROM scratch AS release
COPY --from=releaser /out/ / COPY --from=releaser /out/ /

View File

@ -4,56 +4,77 @@ else ifneq (, $(shell docker buildx version))
export BUILDX_CMD = docker buildx export BUILDX_CMD = docker buildx
else ifneq (, $(shell which buildx)) else ifneq (, $(shell which buildx))
export BUILDX_CMD = $(which buildx) export BUILDX_CMD = $(which buildx)
else
$(error "Buildx is required: https://github.com/docker/buildx#installing")
endif endif
export BUILDX_CMD ?= docker buildx
.PHONY: all
all: binaries
.PHONY: build
build:
./hack/build
.PHONY: shell
shell: shell:
./hack/shell ./hack/shell
.PHONY: binaries
binaries: binaries:
$(BUILDX_CMD) bake binaries $(BUILDX_CMD) bake binaries
.PHONY: binaries-cross
binaries-cross: binaries-cross:
$(BUILDX_CMD) bake binaries-cross $(BUILDX_CMD) bake binaries-cross
.PHONY: install
install: binaries install: binaries
mkdir -p ~/.docker/cli-plugins mkdir -p ~/.docker/cli-plugins
install bin/build/buildx ~/.docker/cli-plugins/docker-buildx install bin/build/buildx ~/.docker/cli-plugins/docker-buildx
.PHONY: release
release: release:
./hack/release ./hack/release
.PHONY: validate-all
validate-all: lint test validate-vendor validate-docs validate-all: lint test validate-vendor validate-docs
.PHONY: lint
lint: lint:
$(BUILDX_CMD) bake lint $(BUILDX_CMD) bake lint
.PHONY: test
test: test:
$(BUILDX_CMD) bake test $(BUILDX_CMD) bake test
.PHONY: validate-vendor
validate-vendor: validate-vendor:
$(BUILDX_CMD) bake validate-vendor $(BUILDX_CMD) bake validate-vendor
.PHONY: validate-docs
validate-docs: validate-docs:
$(BUILDX_CMD) bake validate-docs $(BUILDX_CMD) bake validate-docs
.PHONY: validate-authors
validate-authors: validate-authors:
$(BUILDX_CMD) bake validate-authors $(BUILDX_CMD) bake validate-authors
.PHONY: test-driver
test-driver: test-driver:
./hack/test-driver ./hack/test-driver
.PHONY: vendor
vendor: vendor:
./hack/update-vendor ./hack/update-vendor
.PHONY: docs
docs: docs:
./hack/update-docs ./hack/update-docs
.PHONY: authors
authors: authors:
$(BUILDX_CMD) bake update-authors $(BUILDX_CMD) bake update-authors
.PHONY: mod-outdated
mod-outdated: mod-outdated:
$(BUILDX_CMD) bake mod-outdated $(BUILDX_CMD) bake mod-outdated
.PHONY: shell binaries binaries-cross install release validate-all lint validate-vendor validate-docs validate-authors vendor docs authors

16
hack/build Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env sh
set -e
: "${DESTDIR=./bin/build}"
: "${PACKAGE=github.com/docker/buildx}"
: "${VERSION=$(./hack/git-meta version)}"
: "${REVISION=$(./hack/git-meta revision)}"
: "${CGO_ENABLED=0}"
: "${GO_PKG=github.com/docker/buildx}"
: "${GO_LDFLAGS=-X ${GO_PKG}/version.Version=${VERSION} -X ${GO_PKG}/version.Revision=${REVISION} -X ${GO_PKG}/version.Package=${PACKAGE}}"
: "${GO_EXTRA_LDFLAGS=}"
set -x
CGO_ENABLED=$CGO_ENABLED go build -mod vendor -trimpath -ldflags "${GO_LDFLAGS} ${GO_EXTRA_LDFLAGS}" -o "${DESTDIR}/docker-buildx" ./cmd/buildx

16
hack/git-meta Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env sh
set -e
case $1 in
"version")
git describe --match 'v[0-9]*' --dirty='.m' --always --tags
;;
"revision")
echo "$(git rev-parse HEAD)$(if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi)"
;;
*)
echo "usage: ./hack/git-meta <version|revision>"
exit 1
;;
esac