Skip to content

Commit 0883be4

Browse files
authored
Merge pull request #171 from pohly/example-commands
build.make: support binaries outside of cmd, with optional go.mod
2 parents 65922ea + 02cda51 commit 0883be4

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

build.make

+9-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ SHELL := /bin/bash
2121
# set in main Makefile of a repository.
2222
# CMDS=
2323

24+
# Normally, commands are expected in "cmd". That can be changed for a
25+
# repository to something else by setting CMDS_DIR before including build.make.
26+
CMDS_DIR ?= cmd
27+
2428
# This is the default. It can be overridden in the main Makefile after
2529
# including build.make.
2630
REGISTRY_NAME?=quay.io/k8scsi
@@ -90,15 +94,15 @@ $(CMDS:%=build-%): build-%: check-go-version-go
9094
if ! [ $${#os_arch_seen_pre} = $${#os_arch_seen} ]; then \
9195
continue; \
9296
fi; \
93-
if ! (set -x; CGO_ENABLED=0 GOOS="$$os" GOARCH="$$arch" go build $(GOFLAGS_VENDOR) -a -ldflags '$(FULL_LDFLAGS)' -o "./bin/$*$$suffix" ./cmd/$*); then \
97+
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 \
9498
echo "Building $* for GOOS=$$os GOARCH=$$arch failed, see error(s) above."; \
9599
exit 1; \
96100
fi; \
97101
os_arch_seen+=";$$os-$$arch"; \
98102
done
99103

100104
$(CMDS:%=container-%): container-%: build-%
101-
docker build -t $*:latest -f $(shell if [ -e ./cmd/$*/Dockerfile ]; then echo ./cmd/$*/Dockerfile; else echo Dockerfile; fi) --label revision=$(REV) .
105+
docker build -t $*:latest -f $(shell if [ -e ./$(CMDS_DIR)/$*/Dockerfile ]; then echo ./$(CMDS_DIR)/$*/Dockerfile; else echo Dockerfile; fi) --label revision=$(REV) .
102106

103107
$(CMDS:%=push-%): push-%: container-%
104108
set -ex; \
@@ -133,7 +137,7 @@ DOCKER_BUILDX_CREATE_ARGS ?=
133137
# This target builds a multiarch image for one command using Moby BuildKit builder toolkit.
134138
# Docker Buildx is included in Docker 19.03.
135139
#
136-
# ./cmd/<command>/Dockerfile[.Windows] is used if found, otherwise Dockerfile[.Windows].
140+
# ./$(CMDS_DIR)/<command>/Dockerfile[.Windows] is used if found, otherwise Dockerfile[.Windows].
137141
# It is currently optional: if no such file exists, Windows images are not included,
138142
# even when Windows is listed in BUILD_PLATFORMS. That way, projects can test that
139143
# 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-%
146150
export DOCKER_CLI_EXPERIMENTAL=enabled; \
147151
docker buildx create $(DOCKER_BUILDX_CREATE_ARGS) --use --name multiarchimage-buildertest; \
148152
trap "docker buildx rm multiarchimage-buildertest" EXIT; \
149-
dockerfile_linux=$$(if [ -e ./cmd/$*/Dockerfile ]; then echo ./cmd/$*/Dockerfile; else echo Dockerfile; fi); \
150-
dockerfile_windows=$$(if [ -e ./cmd/$*/Dockerfile.Windows ]; then echo ./cmd/$*/Dockerfile.Windows; else echo Dockerfile.Windows; fi); \
153+
dockerfile_linux=$$(if [ -e ./$(CMDS_DIR)/$*/Dockerfile ]; then echo ./$(CMDS_DIR)/$*/Dockerfile; else echo Dockerfile; fi); \
154+
dockerfile_windows=$$(if [ -e ./$(CMDS_DIR)/$*/Dockerfile.Windows ]; then echo ./$(CMDS_DIR)/$*/Dockerfile.Windows; else echo Dockerfile.Windows; fi); \
151155
if [ '$(BUILD_PLATFORMS)' ]; then build_platforms='$(BUILD_PLATFORMS)'; else build_platforms="linux amd64"; fi; \
152156
if ! [ -f "$$dockerfile_windows" ]; then \
153157
build_platforms="$$(echo "$$build_platforms" | sed -e 's/windows *[^ ]* *.exe *[^ ]* *[^ ]*//g' -e 's/; *;/;/g' -e 's/;[ ]*$$//')"; \

0 commit comments

Comments
 (0)