From ba8c824436a767fed48b9222c5aa8b9e6dce694c Mon Sep 17 00:00:00 2001 From: Djordje Lukic Date: Mon, 4 May 2020 22:31:59 +0200 Subject: [PATCH] Cleanup Makefiles * add `make help` target * remove unused variables * add .exe to the binary name when on windows * add ldflags to go build to strip the binary (smaller binary size) * `make protos` must be executed manually when proto files change --- Dockerfile | 4 ++-- Makefile | 19 +++++++++---------- README.md | 8 +++++--- builder.Makefile | 23 ++++++++++++++++------- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/Dockerfile b/Dockerfile index e55b0f66a..8d83d1937 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,13 +21,13 @@ ADD . ${PWD} FROM fs AS make-protos RUN make -f builder.Makefile protos -FROM make-protos AS make-cli +FROM fs AS make-cli RUN --mount=type=cache,target=/root/.cache/go-build \ GOOS=${TARGET_OS} \ GOARCH=${TARGET_ARCH} \ make -f builder.Makefile cli -FROM make-protos AS make-cross +FROM fs AS make-cross RUN --mount=type=cache,target=/root/.cache/go-build \ make -f builder.Makefile cross diff --git a/Makefile b/Makefile index 06e2c2b9d..34210b538 100644 --- a/Makefile +++ b/Makefile @@ -23,39 +23,38 @@ # ARISING FROM, OUT OF OR IN CONNECTION WITH # THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -GIT_COMMIT=$(shell git rev-parse --short HEAD) GOOS ?= $(shell go env GOOS) GOARCH ?= $(shell go env GOARCH) -PROTOS=$(shell find . -name \*.proto) - export DOCKER_BUILDKIT=1 all: cli -protos: +protos: ## Generate go code from .proto files @docker build . \ --target protos -cli: +cli: ## Compile the cli @docker build . \ --output type=local,dest=./bin \ --build-arg TARGET_OS=${GOOS} \ --build-arg TARGET_ARCH=${GOARCH} \ --target cli -cross: +cross: ## Compile the CLI for linux, darwin and windows @docker build . \ --output type=local,dest=./bin \ --target cross -test: +test: ## Run unit tests @docker build . \ --target test -cache-clear: +cache-clear: # Clear the builder cache @docker builder prune --force --filter type=exec.cachemount --filter=unused-for=24h -FORCE: +help: ## Show help + @echo Please specify a build target. The choices are: + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' -.PHONY: all protos cli cross +.PHONY: all protos cli cross test cache-clear help diff --git a/README.md b/README.md index bb049448b..3e08364d6 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,12 @@ ## Dev Setup -To setup a development machine to update the API protobufs, first run the `./setup-dev.sh` script to install the correct version of protobufs on your system and get the protobuild binary. +Make sure you have Docker installed and running. -## Building the API Project +## Building the project ```bash -> make +$ make ``` + +If you make changes to the `.proto` files, make sure to `make protos` to generate go code. diff --git a/builder.Makefile b/builder.Makefile index 759c7931d..a6532cb10 100644 --- a/builder.Makefile +++ b/builder.Makefile @@ -23,13 +23,22 @@ # ARISING FROM, OUT OF OR IN CONNECTION WITH # THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -GIT_COMMIT=$(shell git rev-parse --short HEAD) GOOS ?= $(shell go env GOOS) GOARCH ?= $(shell go env GOARCH) PROTOS=$(shell find . -name \*.proto) -export DOCKER_BUILDKIT=1 +EXTENSION := +ifeq ($(GOOS),windows) + EXTENSION := .exe +endif + +STATIC_FLAGS= CGO_ENABLED=0 +LDFLAGS := "-s -w" +GO_BUILD = $(STATIC_FLAGS) go build -trimpath -ldflags=$(LDFLAGS) + +BINARY=bin/docker +BINARY_WITH_EXTENSION=$(BINARY)$(EXTENSION) all: cli @@ -38,16 +47,16 @@ protos: @goimports -w -local github.com/docker/api . cli: - GOOS=${GOOS} GOARCH=${GOARCH} go build -v -o bin/docker ./cli + GOOS=${GOOS} GOARCH=${GOARCH} $(GO_BUILD) -o $(BINARY_WITH_EXTENSION) ./cli cross: - @GOOS=linux GOARCH=amd64 go build -v -o bin/docker-linux-amd64 ./cli - @GOOS=darwin GOARCH=amd64 go build -v -o bin/docker-darwin-amd64 ./cli - @GOOS=windows GOARCH=amd64 go build -v -o bin/docker-windows-amd64.exe ./cli + @GOOS=linux GOARCH=amd64 $(GO_BUILD) -o $(BINARY)-linux-amd64 ./cli + @GOOS=darwin GOARCH=amd64 $(GO_BUILD) -o $(BINARY)-darwin-amd64 ./cli + @GOOS=windows GOARCH=amd64 $(GO_BUILD) -o $(BINARY)-windows-amd64.exe ./cli test: @gotestsum ./... FORCE: -.PHONY: all protos cli cross +.PHONY: all protos cli cross test