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
This commit is contained in:
Djordje Lukic 2020-05-04 22:31:59 +02:00 committed by Djordje Lukic
parent 900d82ced0
commit ba8c824436
4 changed files with 32 additions and 22 deletions

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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