Skip to content

Commit

Permalink
Makefile: mutualize local and Dockerfile build opts
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Aug 23, 2022
1 parent 157d38a commit 2ce30f4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 37 deletions.
11 changes: 3 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ RUN apk add --no-cache \
docker \
file \
git \
make \
protoc \
protobuf-dev
WORKDIR /src
Expand Down Expand Up @@ -68,20 +69,14 @@ RUN --mount=type=bind,target=.,rw <<EOT
fi
EOT

FROM base AS version
RUN --mount=target=. \
PKG=github.com/docker/compose/v2 VERSION=$(git describe --match 'v[0-9]*' --dirty='.m' --always --tags); \
echo "-X ${PKG}/internal.Version=${VERSION}" | tee /tmp/.ldflags; \
echo -n "${VERSION}" | tee /tmp/.version;

FROM build-base AS build
ARG BUILD_TAGS
ARG TARGETPLATFORM
RUN xx-go --wrap
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/root/.cache \
--mount=type=cache,target=/go/pkg/mod \
--mount=type=bind,source=/tmp/.ldflags,target=/tmp/.ldflags,from=version \
set -x; xx-go build -trimpath -tags "$BUILD_TAGS" -ldflags "$(cat /tmp/.ldflags) -w -s" -o /usr/bin/docker-compose ./cmd && \
make build CGO_ENABLED=$CGO_ENABLED GO_BUILDTAGS="$BUILD_TAGS" DESTDIR=/usr/bin && \
xx-verify --static /usr/bin/docker-compose

FROM build-base AS lint
Expand Down
75 changes: 46 additions & 29 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

ifneq (, $(BUILDX_BIN))
export BUILDX_CMD = $(BUILDX_BIN)
else ifneq (, $(shell docker buildx version))
export BUILDX_CMD = docker buildx
else ifneq (, $(shell which buildx))
export BUILDX_CMD = $(which buildx)
else
$(error "Buildx is required: https://github.com/docker/buildx#installing")
endif
PKG := github.com/docker/compose/v2
VERSION ?= $(shell git describe --match 'v[0-9]*' --dirty='.m' --always --tags)

CGO_ENABLED ?= 0
GO111MODULE ?= off
GO_LDFLAGS ?= -s -w -X ${PKG}/internal.Version=${VERSION}
GO_BUILDTAGS ?= e2e,kube

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
Expand All @@ -30,23 +28,27 @@ ifeq ($(UNAME_S),Darwin)
MOBY_DOCKER=/Applications/Docker.app/Contents/Resources/bin/docker
endif

BINARY_FOLDER=$(shell pwd)/bin
GIT_TAG?=$(shell git describe --tags --match "v[0-9]*")
TEST_FLAGS?=
E2E_TEST?=
ifeq ($(E2E_TEST),)
else
TEST_FLAGS=-run $(E2E_TEST)
endif

all: compose-plugin
all: build

.PHONY: build ## Build the compose cli-plugin
build:
$(eval DESTDIR ?= $(shell pwd)/bin/build)
mkdir -p "$(DESTDIR)"
CGO_ENABLED=$(CGO_ENABLED) GO111MODULE=$(GO111MODULE) go build -trimpath -tags "$(GO_BUILDTAGS)" -ldflags "$(GO_LDFLAGS)" -o "$(DESTDIR)/docker-compose" ./cmd

.PHONY: compose-plugin
compose-plugin: ## Compile the compose cli-plugin
.PHONY: binary
binary: buildx-cmd
$(BUILDX_CMD) bake binary

.PHONY: install
install: compose-plugin
install: binary
mkdir -p ~/.docker/cli-plugins
install bin/build/docker-compose ~/.docker/cli-plugins/docker-compose

Expand All @@ -61,10 +63,10 @@ e2e-compose-standalone: ## Run End to end local tests in standalone mode. Set E2
go test $(TEST_FLAGS) -v -count=1 -parallel=1 --tags=standalone ./pkg/e2e

.PHONY: build-and-e2e-compose
build-and-e2e-compose: compose-plugin e2e-compose ## Compile the compose cli-plugin and run end to end local tests in plugin mode. Set E2E_TEST=TestName to run a single test
build-and-e2e-compose: build e2e-compose ## Compile the compose cli-plugin and run end to end local tests in plugin mode. Set E2E_TEST=TestName to run a single test

.PHONY: build-and-e2e-compose-standalone
build-and-e2e-compose-standalone: compose-plugin e2e-compose-standalone ## Compile the compose cli-plugin and run End to end local tests in standalone mode. Set E2E_TEST=TestName to run a single test
build-and-e2e-compose-standalone: build e2e-compose-standalone ## Compile the compose cli-plugin and run End to end local tests in standalone mode. Set E2E_TEST=TestName to run a single test

.PHONY: mocks
mocks:
Expand All @@ -76,55 +78,70 @@ mocks:
e2e: e2e-compose e2e-compose-standalone ## Run end to end local tests in both modes. Set E2E_TEST=TestName to run a single test

.PHONY: build-and-e2e
build-and-e2e: compose-plugin e2e-compose e2e-compose-standalone ## Compile the compose cli-plugin and run end to end local tests in both modes. Set E2E_TEST=TestName to run a single test
build-and-e2e: build e2e-compose e2e-compose-standalone ## Compile the compose cli-plugin and run end to end local tests in both modes. Set E2E_TEST=TestName to run a single test

.PHONY: cross
cross: ## Compile the CLI for linux, darwin and windows
$(BUILDX_CMD) bake binary
cross: buildx-cmd ## Compile the CLI for linux, darwin and windows
$(BUILDX_CMD) bake binary-cross

.PHONY: test
test: ## Run unit tests
test: buildx-cmd ## Run unit tests
$(BUILDX_CMD) bake test

.PHONY: cache-clear
cache-clear: ## Clear the builder cache
cache-clear: buildx-cmd ## Clear the builder cache
$(BUILDX_CMD) prune --force --filter type=exec.cachemount --filter=unused-for=24h

.PHONY: lint
lint: ## run linter(s)
lint: buildx-cmd ## run linter(s)
$(BUILDX_CMD) bake lint

.PHONY: docs
docs: ## generate documentation
docs: buildx-cmd ## generate documentation
$(eval $@_TMP_OUT := $(shell mktemp -d -t compose-output.XXXXXXXXXX))
$(BUILDX_CMD) bake --set "*.output=type=local,dest=$($@_TMP_OUT)" docs-update
rm -rf ./docs/internal
cp -R "$($@_TMP_OUT)"/out/* ./docs/
rm -rf "$($@_TMP_OUT)"/*

.PHONY: validate-docs
validate-docs: ## validate the doc does not change
validate-docs: buildx-cmd ## validate the doc does not change
$(BUILDX_CMD) bake docs-validate

.PHONY: check-dependencies
check-dependencies: ## check dependency updates
go list -u -m -f '{{if not .Indirect}}{{if .Update}}{{.}}{{end}}{{end}}' all

.PHONY: validate-headers
validate-headers: ## Check license header for all files
validate-headers: buildx-cmd ## Check license header for all files
$(BUILDX_CMD) bake license-validate

.PHONY: go-mod-tidy
go-mod-tidy: ## Run go mod tidy in a container and output resulting go.mod and go.sum
go-mod-tidy: buildx-cmd ## Run go mod tidy in a container and output resulting go.mod and go.sum
$(BUILDX_CMD) bake vendor-update

.PHONY: validate-go-mod
validate-go-mod: ## Validate go.mod and go.sum are up-to-date
validate-go-mod: buildx-cmd ## Validate go.mod and go.sum are up-to-date
$(BUILDX_CMD) bake vendor-validate

validate: validate-go-mod validate-headers validate-docs ## Validate sources

pre-commit: validate check-dependencies lint compose-plugin test e2e-compose
pre-commit: validate check-dependencies lint build test e2e-compose

.PHONY: buildx-cmd
ifneq (, $(BUILDX_BIN))
buildx-cmd:
$(eval BUILDX_CMD = $(BUILDX_BIN))
else ifneq (, $(shell docker buildx version 2>/dev/null))
buildx-cmd:
$(eval BUILDX_CMD = docker buildx)
else ifneq (, $(shell which buildx 2>/dev/null))
buildx-cmd:
$(eval BUILDX_CMD = $(shell which buildx))
else
buildx-cmd:
$(error "Buildx is required: https://github.com/docker/buildx#installing")
endif

help: ## Show help
@echo Please specify a build target. The choices are:
Expand Down

0 comments on commit 2ce30f4

Please sign in to comment.