-
Notifications
You must be signed in to change notification settings - Fork 173
/
golang.mk
110 lines (91 loc) · 3.63 KB
/
golang.mk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright 2021 Layotto Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
GO := go
GO_FMT := gofmt
GO_IMPORTS := goimports
GO_MODULE := github.com/mosn.io/layotto
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') \
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
.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)/$(OS)/$(ARCH)
@CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) $(GO) build -o $(OUTPUT_DIR)/$(OS)/$(ARCH)/$(COMMAND) -ldflags "$(GO_LDFLAGS)" $(ROOT_PACKAGE)/cmd/$(COMMAND)
.PHONY: go.build
go.build: $(addprefix go.build., $(addprefix $(PLATFORM)., $(BINS)))
.PHONY: go.build.multiarch
go.build.multiarch: $(foreach p,$(PLATFORMS),$(addprefix go.build., $(addprefix $(p)., $(BINS))))
.PHONY: go.clean
go.clean:
@echo "===========> Cleaning all build output"
@rm -rf $(OUTPUT_DIR)
@rm -rf $(ROOT_DIR)/cover.out
.PHONY: go.lint.verify
go.lint.verify:
ifeq (,$(shell which golangci-lint))
@echo "===========> Installing golangci lint"
@curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(${GO} env GOPATH)/bin
endif
.PHONY: go.lint
go.lint: go.lint.verify
@echo "===========> Run golangci to lint source codes"
@golangci-lint run
.PHONY: go.test.verify
go.test.verify:
ifeq ($(shell which go-junit-report), )
@echo "===========> Installing go-junit-report"
@GO111MODULE=off $(GO) get -u github.com/jstemmer/go-junit-report
endif
.PHONY: go.test
go.test: go.test.verify
@echo "===========> Run unit test in cmd"
$(GO) test -count=1 -timeout=10m -short -v `go list ./cmd/...`
@echo "===========> Run unit test in diagnostics"
$(GO) test -count=1 -timeout=10m -short -v `go list ./diagnostics/...`
@echo "===========> Run unit test in pkg"
$(GO) test -count=1 -timeout=10m -short -v `go list ./pkg/...`
.PHONY: go.style
go.style:
@echo "===========> Running go style check"
$(GO) fmt ./... && git status && [[ -z `git status -s` ]]
.PHONY: go.format.verify
go.format.verify:
ifeq ($(shell which goimports), )
@echo "===========> Installing missing goimports"
@GO111MODULE=off $(GO) get -u golang.org/x/tools/cmd/goimports
endif
.PHONY: go.format
go.format: go.format.verify
@echo "===========> Running go codes format"
$(GO_FMT) -s -w .
$(GO_IMPORTS) -w -local $(GO_MODULE) .
$(GO) mod tidy