diff --git a/.github/workflows/go-checks.yaml b/.github/workflows/go-checks.yaml index 4c81ce8..ff3b083 100644 --- a/.github/workflows/go-checks.yaml +++ b/.github/workflows/go-checks.yaml @@ -65,8 +65,8 @@ jobs: uses: actions/cache@v3 with: path: | - bin/gomodcache - bin/gocache + .go/gomodcache + .go/gocache key: ${{ runner.os }}-gobuildcontainer-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-gobuildcontainer- diff --git a/.github/workflows/release-binary.yaml b/.github/workflows/release-binary.yaml index ce5ff18..2948119 100644 --- a/.github/workflows/release-binary.yaml +++ b/.github/workflows/release-binary.yaml @@ -30,8 +30,8 @@ jobs: uses: actions/cache@v3 with: path: | - bin/gomodcache - bin/gocache + .go/gomodcache + .go/gocache key: ${{ runner.os }}-gobuildcontainer-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-gobuildcontainer- @@ -46,7 +46,7 @@ jobs: echo ::set-output name=VERSION::${VERSION} - name: Build Binaries for All Platforms - run: make trigger-all-build + run: make trigger-all-package - name: Attach Binaries to Release uses: softprops/action-gh-release@v1 diff --git a/.github/workflows/release-image.yaml b/.github/workflows/release-image.yaml index 8e4a06a..4bf97eb 100644 --- a/.github/workflows/release-image.yaml +++ b/.github/workflows/release-image.yaml @@ -20,8 +20,8 @@ jobs: uses: actions/cache@v3 with: path: | - bin/gomodcache - bin/gocache + .go/gomodcache + .go/gocache key: ${{ runner.os }}-gobuildcontainer-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-gobuildcontainer- @@ -69,8 +69,8 @@ jobs: uses: actions/cache@v3 with: path: | - bin/gomodcache - bin/gocache + .go/gomodcache + .go/gocache key: ${{ runner.os }}-gobuildcontainer-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-gobuildcontainer- diff --git a/.github/workflows/upload-test-binary.yaml b/.github/workflows/upload-test-binary.yaml index b1de6fc..0deb601 100644 --- a/.github/workflows/upload-test-binary.yaml +++ b/.github/workflows/upload-test-binary.yaml @@ -64,8 +64,8 @@ jobs: uses: actions/cache@v3 with: path: | - bin/gomodcache - bin/gocache + .go/gomodcache + .go/gocache key: ${{ runner.os }}-gobuildcontainer-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-gobuildcontainer- diff --git a/.gitignore b/.gitignore index 215a7a3..4704ae5 100644 --- a/.gitignore +++ b/.gitignore @@ -56,6 +56,7 @@ cscope.* # Build output bin +.go # etcd default.etcd diff --git a/Makefile b/Makefile index 409a1fd..e51030d 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ $(foreach p,$(SUBPROJS),$(eval \ $(p)-%: mk-%.$(p); \ )) -# Common targets for subprojects +# Common targets for subprojects, will be executed on all subprojects TARGETS := build \ all-build \ package \ @@ -42,18 +42,21 @@ TARGETS := build \ container-push \ all-container-build-push \ clean \ - cleanall \ + all-clean \ version \ imageversion \ binaryname \ variables \ help -# Targets to run on all subprojects +# Run common targets on all subprojects $(foreach t,$(TARGETS),$(eval \ $(t): $(addprefix mk-$(t).,$(SUBPROJS)); \ )) +# `shell' only needs to be executed once, not on every subproject +shell: $(addprefix mk-shell.,$(word 1,$(SUBPROJS))); + mk-%: $(MAKE) -f $(lastword $(subst ., ,$*)).mk $(firstword $(subst ., ,$*)) diff --git a/makefiles/consts.mk b/makefiles/consts.mk index d651c92..133fb41 100644 --- a/makefiles/consts.mk +++ b/makefiles/consts.mk @@ -68,8 +68,10 @@ ifeq ($(OS), windows) PKG_FULLNAME := $(subst .exe,,$(if $(FULL_NAME),$(BIN_VERBOSE_BASE),$(BIN_BASENAME))).zip endif -# This holds build output, cache, and helper tools +# This holds build output and helper tools DIST := bin +# This holds build cache +GOCACHE := .go BIN_VERBOSE_DIR := $(DIST)/$(BIN)-$(VERSION) # Full output path OUTPUT := $(if $(FULL_NAME),$(BIN_VERBOSE_DIR)/$(BIN_FULLNAME),$(DIST)/$(BIN_FULLNAME)) diff --git a/makefiles/targets.mk b/makefiles/targets.mk index d6797ad..8d01b88 100644 --- a/makefiles/targets.mk +++ b/makefiles/targets.mk @@ -14,34 +14,34 @@ all: build -# Build cache of the build container -BUILDCACHE ?= $$(pwd)/$(DIST) - # ===== BUILD ===== +build-dirs: + mkdir -p "$(GOCACHE)/gocache" \ + "$(GOCACHE)/gomodcache" \ + "$(DIST)" + build: # @HELP build binary for current platform -build: gen-dockerignore - mkdir -p "$(BUILDCACHE)/gocache" "$(BUILDCACHE)/gomodcache" && \ - docker run \ - -i \ - --rm \ - -u $$(id -u):$$(id -g) \ - -v $$(pwd):/src \ - -w /src \ - -v $(BUILDCACHE)/gocache:/gocache \ - -v $(BUILDCACHE)/gomodcache:/gomodcache \ - --env GOCACHE="/gocache" \ - --env GOMODCACHE="/gomodcache" \ - --env ARCH="$(ARCH)" \ - --env OS="$(OS)" \ - --env VERSION="$(VERSION)" \ - --env DEBUG="$(DEBUG)" \ - --env OUTPUT="$(OUTPUT)" \ - --env GOFLAGS="$(GOFLAGS)" \ - --env GOPROXY="$(GOPROXY)" \ - --env HTTP_PROXY="$(HTTP_PROXY)" \ - --env HTTPS_PROXY="$(HTTPS_PROXY)" \ - $(BUILD_IMAGE) \ +build: gen-dockerignore build-dirs + docker run \ + -i \ + --rm \ + -u $$(id -u):$$(id -g) \ + -v $$(pwd):/src \ + -w /src \ + -v $$(pwd)/$(GOCACHE):/cache \ + --env GOCACHE="/cache/gocache" \ + --env GOMODCACHE="/cache/gomodcache" \ + --env ARCH="$(ARCH)" \ + --env OS="$(OS)" \ + --env VERSION="$(VERSION)" \ + --env DEBUG="$(DEBUG)" \ + --env OUTPUT="$(OUTPUT)" \ + --env GOFLAGS="$(GOFLAGS)" \ + --env GOPROXY="$(GOPROXY)" \ + --env HTTP_PROXY="$(HTTP_PROXY)" \ + --env HTTPS_PROXY="$(HTTPS_PROXY)" \ + $(BUILD_IMAGE) \ ./build/build.sh $(ENTRY) # INTERNAL: build-_ to build for a specific platform @@ -92,27 +92,22 @@ all-package: $(addprefix package-, $(subst /,_, $(BIN_PLATFORMS))) # ===== CONTAINERS ===== container-build: # @HELP build container image for current platform -container-build: build-$(OS)_$(ARCH) - printf "# CONTAINER repotags: %s\ttarget: %s/%s\tbinaryversion: %s\n" "$(IMAGE_REPO_TAGS)" "$(OS)" "$(ARCH)" "$(VERSION)" +container-build: build-linux_$(ARCH) + printf "# CONTAINER repotags: %s\ttarget: %s/%s\tbinaryversion: %s\n" "$(IMAGE_REPO_TAGS)" "linux" "$(ARCH)" "$(VERSION)" + if [ "$(OS)" != "linux" ]; then \ + echo "# CONTAINER warning: you have set target os to $(OS), but container target os will always be linux"; \ + fi; \ TMPFILE=Dockerfile && \ sed 's/$${BIN}/$(BIN)/g' Dockerfile.in > $${TMPFILE} && \ DOCKER_BUILDKIT=1 \ docker build \ -f $${TMPFILE} \ --build-arg "ARCH=$(ARCH)" \ - --build-arg "OS=$(OS)" \ + --build-arg "OS=linux" \ --build-arg "VERSION=$(VERSION)" \ --build-arg "BASE_IMAGE=$(BASE_IMAGE)" \ $(addprefix -t ,$(IMAGE_REPO_TAGS)) . -# INTERNAL: container-build-_ to build container image for a specific platform -container-build-%: - $(MAKE) -f $(firstword $(MAKEFILE_LIST)) \ - docker-build \ - --no-print-directory \ - GOOS=$(firstword $(subst _, ,$*)) \ - GOARCH=$(lastword $(subst _, ,$*)) - container-push: # @HELP push built container image to all repos container-push: $(addprefix container-push-, $(subst :,=, $(subst /,_, $(IMAGE_REPO_TAGS)))) @@ -138,6 +133,34 @@ all-container-build-push: $(addprefix build-, $(subst /,_, $(IMAGE_PLATFORMS))) # ===== MISC ===== +# Optional variable to pass arguments to sh +# Example: make shell CMD="-c 'date'" +CMD ?= + +shell: # @HELP launches a shell in the containerized build environment +shell: build-dirs + echo "# launching a shell in the containerized build environment" + docker run \ + -it \ + --rm \ + -u $$(id -u):$$(id -g) \ + -v $$(pwd):/src \ + -w /src \ + -v $$(pwd)/$(GOCACHE):/cache \ + --env GOCACHE="/cache/gocache" \ + --env GOMODCACHE="/cache/gomodcache" \ + --env ARCH="$(ARCH)" \ + --env OS="$(OS)" \ + --env VERSION="$(VERSION)" \ + --env DEBUG="$(DEBUG)" \ + --env OUTPUT="$(OUTPUT)" \ + --env GOFLAGS="$(GOFLAGS)" \ + --env GOPROXY="$(GOPROXY)" \ + --env HTTP_PROXY="$(HTTP_PROXY)" \ + --env HTTPS_PROXY="$(HTTPS_PROXY)" \ + $(BUILD_IMAGE) \ + /bin/sh $(CMD) + # Generate a dockerignore file to ignore everything except # current build output directory. This is useful because # when building a container, we only need the final binary. @@ -150,9 +173,10 @@ clean: # @HELP clean built binaries clean: rm -rf $(DIST)/$(BIN)* -cleanall: # @HELP clean built binaries, build cache, and helper tools -cleanall: clean - rm -rf $(DIST) +all-clean: # @HELP clean built binaries, build cache, and helper tools +all-clean: clean + test -d $(GOCACHE) && chmod -R u+w $(GOCACHE) || true + rm -rf $(GOCACHE) $(DIST) version: # @HELP output the version string version: @@ -186,7 +210,7 @@ help: # @HELP print this message help: variables echo "TARGETS:" grep -E '^.*: *# *@HELP' $(MAKEFILE_LIST) \ - | sed --expression='s_.*.mk:__g' \ + | sed -E 's_.*.mk:__g' \ | awk ' \ BEGIN {FS = ": *# *@HELP"}; \ { printf " %-25s %s\n", $$1, $$2 }; \