# Copyright 2020 Lingfei Kong . All rights reserved. # Use of this source code is governed by a MIT style # license that can be found in the LICENSE file. # ============================================================================== # Makefile helper functions for golang # GO := go GO_SUPPORTED_VERSIONS ?= 1.13|1.14|1.15|1.16|1.17|1.18|1.19|1.20 GO_LDFLAGS += -X $(VERSION_PACKAGE).GitVersion=$(VERSION) \ -X $(VERSION_PACKAGE).GitCommit=$(GIT_COMMIT) \ -X $(VERSION_PACKAGE).GitTreeState=$(GIT_TREE_STATE) \ -X $(VERSION_PACKAGE).BuildDate=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ') ifneq ($(DLV),) GO_BUILD_FLAGS += -gcflags "all=-N -l" LDFLAGS = "" endif GO_BUILD_FLAGS += -ldflags "$(GO_LDFLAGS)" ifeq ($(GOOS),windows) GO_OUT_EXT := .exe endif ifeq ($(ROOT_PACKAGE),) $(error the variable ROOT_PACKAGE must be set prior to including golang.mk) endif GOPATH := $(shell go env GOPATH) ifeq ($(origin GOBIN), undefined) GOBIN := $(GOPATH)/bin endif COMMANDS ?= $(filter-out %.md, $(wildcard ${ROOT_DIR}/cmd/*)) BINS ?= $(foreach cmd,${COMMANDS},$(notdir ${cmd})) ifeq (${COMMANDS},) $(error Could not determine COMMANDS, set ROOT_DIR or run in source dir) endif ifeq (${BINS},) $(error Could not determine BINS, set ROOT_DIR or run in source dir) endif EXCLUDE_TESTS=github.com/marmotedu/iam/test github.com/marmotedu/iam/pkg/log github.com/marmotedu/iam/third_party github.com/marmotedu/iam/internal/pump/storage github.com/marmotedu/iam/internal/pump github.com/marmotedu/iam/internal/pkg/logger .PHONY: go.build.verify go.build.verify: ifneq ($(shell $(GO) version | grep -q -E '\bgo($(GO_SUPPORTED_VERSIONS))\b' && echo 0 || echo 1), 0) $(error unsupported go version. Please make install one of the following supported version: '$(GO_SUPPORTED_VERSIONS)') endif .PHONY: go.build.% go.build.%: $(eval COMMAND := $(word 2,$(subst ., ,$*))) $(eval PLATFORM := $(word 1,$(subst ., ,$*))) $(eval OS := $(word 1,$(subst _, ,$(PLATFORM)))) $(eval ARCH := $(word 2,$(subst _, ,$(PLATFORM)))) @echo "===========> Building binary $(COMMAND) $(VERSION) for $(OS) $(ARCH)" @mkdir -p $(OUTPUT_DIR)/platforms/$(OS)/$(ARCH) @CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) $(GO) build $(GO_BUILD_FLAGS) -o $(OUTPUT_DIR)/platforms/$(OS)/$(ARCH)/$(COMMAND)$(GO_OUT_EXT) $(ROOT_PACKAGE)/cmd/$(COMMAND) .PHONY: go.build go.build: go.build.verify $(addprefix go.build., $(addprefix $(PLATFORM)., $(BINS))) .PHONY: go.build.multiarch go.build.multiarch: go.build.verify $(foreach p,$(PLATFORMS),$(addprefix go.build., $(addprefix $(p)., $(BINS)))) .PHONY: go.clean go.clean: @echo "===========> Cleaning all build output" @-rm -vrf $(OUTPUT_DIR) .PHONY: go.lint go.lint: tools.verify.golangci-lint @echo "===========> Run golangci to lint source codes" @golangci-lint run -c $(ROOT_DIR)/.golangci.yaml $(ROOT_DIR)/... .PHONY: go.test go.test: tools.verify.go-junit-report @echo "===========> Run unit test" @set -o pipefail;$(GO) test -race -cover -coverprofile=$(OUTPUT_DIR)/coverage.out \ -timeout=10m -shuffle=on -short -v `go list ./...|\ egrep -v $(subst $(SPACE),'|',$(sort $(EXCLUDE_TESTS)))` 2>&1 | \ tee >(go-junit-report --set-exit-code >$(OUTPUT_DIR)/report.xml) @sed -i '/mock_.*.go/d' $(OUTPUT_DIR)/coverage.out # remove mock_.*.go files from test coverage @$(GO) tool cover -html=$(OUTPUT_DIR)/coverage.out -o $(OUTPUT_DIR)/coverage.html .PHONY: go.test.cover go.test.cover: go.test @$(GO) tool cover -func=$(OUTPUT_DIR)/coverage.out | \ awk -v target=$(COVERAGE) -f $(ROOT_DIR)/scripts/coverage.awk .PHONY: go.updates go.updates: tools.verify.go-mod-outdated @$(GO) list -u -m -json all | go-mod-outdated -update -direct