From 1c374dab1fe8c1795b56056657b1d48dd2ee096a Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Mon, 28 Jan 2019 10:53:16 -0800 Subject: [PATCH] Update Makefile to not repeat build when possible --- Makefile | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 748e3fa..5f0b3be 100644 --- a/Makefile +++ b/Makefile @@ -87,24 +87,33 @@ all-push: $(addprefix push-, $(ALL_PLATFORMS)) build: bin/$(OS)_$(ARCH)/$(BIN) -BUILD_DIRS := bin/$(OS)_$(ARCH) .go/src/$(PKG) .go/pkg .go/bin .go/std/$(OS)_$(ARCH) .go/cache +BUILD_DIRS := \ + bin/$(OS)_$(ARCH) \ + .go/src/$(PKG) \ + .go/pkg \ + .go/bin/$(OS)_$(ARCH) \ + .go/std/$(OS)_$(ARCH) \ + .go/cache -# TODO: This is .PHONY because building Go code uses a compiler-internal DAG, -# so we have to run the go tool. Unfortunately, go always touches the binary -# during `go install` even if it didn't change anything (as per md5sum). This -# makes make unhappy. Better would be to run go, see that the result did not -# change, and then bypass further processing. Sadly not possible for now. -.PHONY: bin/$(OS)_$(ARCH)/$(BIN) -bin/$(OS)_$(ARCH)/$(BIN): $(BUILD_DIRS) - @echo "building: $@" +# The following structure defeats Go's (intentional) behavior to always touch +# result files, even if they have not changed. This will still run `go` but +# will not trigger further work if nothing has actually changed. +OUTBIN = bin/$(OS)_$(ARCH)/$(BIN) +$(OUTBIN): .go/$(OUTBIN).stamp + @true + +# This will build the binary under ./.go and update the real binary iff needed. +.PHONY: .go/$(OUTBIN).stamp +.go/$(OUTBIN).stamp: $(BUILD_DIRS) + @echo "making $(OUTBIN)" @docker run \ -ti \ --rm \ -u $$(id -u):$$(id -g) \ - -v $$(pwd)/.go:/go \ -v $$(pwd):/go/src/$(PKG) \ - -v $$(pwd)/bin/$(OS)_$(ARCH):/go/bin \ - -v $$(pwd)/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \ + -v $$(pwd)/.go:/go \ + -v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin \ + -v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \ -v $$(pwd)/.go/std/$(OS)_$(ARCH):/usr/local/go/pkg/$(OS)_$(ARCH)_static \ -v $$(pwd)/.go/cache:/.cache \ -w /go/src/$(PKG) \ @@ -118,6 +127,10 @@ bin/$(OS)_$(ARCH)/$(BIN): $(BUILD_DIRS) PKG=$(PKG) \ ./build/build.sh \ " + @if ! cmp -s .go/$(OUTBIN) $(OUTBIN); then \ + cat .go/$(OUTBIN) > $(OUTBIN); \ + date >$@; \ + fi DOTFILE_IMAGE = $(subst /,_,$(IMAGE))-$(TAG) @@ -165,9 +178,10 @@ test: $(BUILD_DIRS) @docker run \ -ti \ -u $$(id -u):$$(id -g) \ - -v $$(pwd)/.go:/go \ -v $$(pwd):/go/src/$(PKG) \ - -v $$(pwd)/bin/$(OS)_$(ARCH):/go/bin \ + -v $$(pwd)/.go:/go \ + -v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin \ + -v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \ -v $$(pwd)/.go/std/$(OS)_$(ARCH):/usr/local/go/pkg/$(OS)_$(ARCH)_static \ -v $$(pwd)/.go/cache:/.cache \ --env HTTP_PROXY=$(HTTP_PROXY) \