From 02cda510cc1dda9dbeb9ce018595828739c2af41 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Thu, 26 Aug 2021 11:45:40 +0200 Subject: [PATCH] build.make: support binaries outside of cmd, with optional go.mod Setting CMDS_DIR in the top-level Makefile before including build.make will ensure that binaries are picked up from that other directory. This is useful for lib-volume-populator where an example populator is provided under "example". The commands are no longer expected to share the go.mod file with the project. Entering the command directory and using `go build .` will pick up a local go.mod if it exists and otherwise fall back to the project's go.mod. --- build.make | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/build.make b/build.make index 0ed51e80..6caf64fd 100644 --- a/build.make +++ b/build.make @@ -21,6 +21,10 @@ SHELL := /bin/bash # set in main Makefile of a repository. # CMDS= +# Normally, commands are expected in "cmd". That can be changed for a +# repository to something else by setting CMDS_DIR before including build.make. +CMDS_DIR ?= cmd + # This is the default. It can be overridden in the main Makefile after # including build.make. REGISTRY_NAME?=quay.io/k8scsi @@ -90,7 +94,7 @@ $(CMDS:%=build-%): build-%: check-go-version-go if ! [ $${#os_arch_seen_pre} = $${#os_arch_seen} ]; then \ continue; \ fi; \ - if ! (set -x; CGO_ENABLED=0 GOOS="$$os" GOARCH="$$arch" go build $(GOFLAGS_VENDOR) -a -ldflags '$(FULL_LDFLAGS)' -o "./bin/$*$$suffix" ./cmd/$*); then \ + if ! (set -x; cd ./$(CMDS_DIR)/$* && CGO_ENABLED=0 GOOS="$$os" GOARCH="$$arch" go build $(GOFLAGS_VENDOR) -a -ldflags '$(FULL_LDFLAGS)' -o "$(abspath ./bin)/$*$$suffix" .); then \ echo "Building $* for GOOS=$$os GOARCH=$$arch failed, see error(s) above."; \ exit 1; \ fi; \ @@ -98,7 +102,7 @@ $(CMDS:%=build-%): build-%: check-go-version-go done $(CMDS:%=container-%): container-%: build-% - docker build -t $*:latest -f $(shell if [ -e ./cmd/$*/Dockerfile ]; then echo ./cmd/$*/Dockerfile; else echo Dockerfile; fi) --label revision=$(REV) . + docker build -t $*:latest -f $(shell if [ -e ./$(CMDS_DIR)/$*/Dockerfile ]; then echo ./$(CMDS_DIR)/$*/Dockerfile; else echo Dockerfile; fi) --label revision=$(REV) . $(CMDS:%=push-%): push-%: container-% set -ex; \ @@ -133,7 +137,7 @@ DOCKER_BUILDX_CREATE_ARGS ?= # This target builds a multiarch image for one command using Moby BuildKit builder toolkit. # Docker Buildx is included in Docker 19.03. # -# ./cmd//Dockerfile[.Windows] is used if found, otherwise Dockerfile[.Windows]. +# ./$(CMDS_DIR)//Dockerfile[.Windows] is used if found, otherwise Dockerfile[.Windows]. # It is currently optional: if no such file exists, Windows images are not included, # even when Windows is listed in BUILD_PLATFORMS. That way, projects can test that # Windows binaries can be built before adding a Dockerfile for it. @@ -146,8 +150,8 @@ $(CMDS:%=push-multiarch-%): push-multiarch-%: check-pull-base-ref build-% export DOCKER_CLI_EXPERIMENTAL=enabled; \ docker buildx create $(DOCKER_BUILDX_CREATE_ARGS) --use --name multiarchimage-buildertest; \ trap "docker buildx rm multiarchimage-buildertest" EXIT; \ - dockerfile_linux=$$(if [ -e ./cmd/$*/Dockerfile ]; then echo ./cmd/$*/Dockerfile; else echo Dockerfile; fi); \ - dockerfile_windows=$$(if [ -e ./cmd/$*/Dockerfile.Windows ]; then echo ./cmd/$*/Dockerfile.Windows; else echo Dockerfile.Windows; fi); \ + dockerfile_linux=$$(if [ -e ./$(CMDS_DIR)/$*/Dockerfile ]; then echo ./$(CMDS_DIR)/$*/Dockerfile; else echo Dockerfile; fi); \ + dockerfile_windows=$$(if [ -e ./$(CMDS_DIR)/$*/Dockerfile.Windows ]; then echo ./$(CMDS_DIR)/$*/Dockerfile.Windows; else echo Dockerfile.Windows; fi); \ if [ '$(BUILD_PLATFORMS)' ]; then build_platforms='$(BUILD_PLATFORMS)'; else build_platforms="linux amd64"; fi; \ if ! [ -f "$$dockerfile_windows" ]; then \ build_platforms="$$(echo "$$build_platforms" | sed -e 's/windows *[^ ]* *.exe *[^ ]* *[^ ]*//g' -e 's/; *;/;/g' -e 's/;[ ]*$$//')"; \