不是说多牛逼,也是从网上各种地方抄来的,就是自己写代码够用了,需要的可以参考,好的优化建议也提提,力求简单明了。
APP:=xxx
PACKAGE:=github.com/xxx/xxx
GO_VERSION:=$(shell go version)
BUILD_TIME:=$(shell date +%Y-%m-%dT%H:%M:%S%z)
COMMIT_HASH:=$(shell git rev-parse --short=8 HEAD || echo unknown)
GIT_BRANCH:=$(shell git rev-parse --abbrev-ref HEAD)
GIT_TAG:=$(shell git tag)
GO_LDFLAGS += -X '$(PACKAGE)/version.BUILD_TIME=$(BUILD_TIME)'
GO_LDFLAGS += -X '$(PACKAGE)/version.GO_VERSION=$(GO_VERSION)'
GO_LDFLAGS += -X '$(PACKAGE)/version.COMMIT_HASH=$(COMMIT_HASH)'
GO_LDFLAGS += -X '$(PACKAGE)/version.GIT_BRANCH=$(GIT_BRANCH)'
GO_LDFLAGS += -X '$(PACKAGE)/version.PROJECT_VERSION=$(GIT_TAG)'
GO_LDFLAGS += -s -w
.PHONY: default darwin linux clean check
default: linux ## Build the default binary file (default: linux)
check: ## Check working tree is clean or not
ifneq ($(shell git status -s),)
$(error You must run git commit)
endif
linux: check ## Build the ELF binary file
GOOS=linux GOARCH=amd64 CGO_ENABLE=0 go build -mod=readonly -ldflags "$(GO_LDFLAGS)" -o $(APP) .
darwin: check ## Build the Mach-O binary file
GOOS=darwin GOARCH=amd64 CGO_ENABLE=0 go build -mod=readonly -ldflags "$(GO_LDFLAGS)" -o $(APP) .
clean: ## Remove previous build
go clean
help: ## Display this help screen
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'