-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
160 lines (130 loc) · 6.28 KB
/
Makefile
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Copyright 2022 Tetrate
# Licensed under the Apache License, Version 2.0 (the "License")
include Tools.mk
name := istio-cost-analyzer
# VERSION is used in release artifacts names. This should be in <major>.<minor>.<patch> (without v
# prefix). When generating actual release assets this can be resolved using "git describe --tags --long".
VERSION ?= dev
# Root dir returns absolute path of current directory. It has a trailing "/".
root_dir := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
# Currently we resolve it using which. But more sophisticated approach is to use infer GOROOT.
go := $(shell which go)
goarch := $(shell $(go) env GOARCH)
goexe := $(shell $(go) env GOEXE)
goos := $(shell $(go) env GOOS)
# Local cache directory.
CACHE_DIR ?= $(root_dir).cache
all_go_sources := $(wildcard cmd/*.go pkg/*/*.go)
main_go_sources := $(wildcard $(filter-out %_test.go,$(all_go_sources)))
# Go tools directory holds the binaries of Go-based tools.
go_tools_dir := $(CACHE_DIR)/tools/go
export PATH := $(go_tools_dir):$(PATH)
current_binary_path := build/$(name)_$(goos)_$(goarch)
current_binary := $(current_binary_path)/$(name)$(goexe)
linux_platforms := linux_amd64 linux_arm64
non_windows_platforms := darwin_amd64 darwin_arm64 $(linux_platforms)
windows_platforms := windows_amd64
archives := $(non_windows_platforms:%=dist/$(name)_$(VERSION)_%.tar.gz) $(windows_platforms:%=dist/$(name)_$(VERSION)_%.zip)
checksums := dist/$(name)_$(VERSION)_checksums.txt
# Go-based tools.
addlicense := $(go_tools_dir)/addlicense
goimports := $(go_tools_dir)/goimports
golangci-lint := $(go_tools_dir)/golangci-lint
# This is adopted from https://github.com/tetratelabs/func-e/blob/3df66c9593e827d67b330b7355d577f91cdcb722/Makefile#L60-L76.
# ANSI escape codes. f_ means foreground, b_ background.
# See https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters.
f_black := $(shell printf "\33[30m")
b_black := $(shell printf "\33[40m")
f_white := $(shell printf "\33[97m")
f_gray := $(shell printf "\33[37m")
f_dark_gray := $(shell printf "\33[90m")
f_bright_cyan := $(shell printf "\33[96m")
b_bright_cyan := $(shell printf "\33[106m")
ansi_reset := $(shell printf "\33[0m")
ansi_$(name) := $(b_black)$(f_black)$(b_bright_cyan)$(name)$(ansi_reset)
ansi_format_dark := $(f_gray)$(f_bright_cyan)%-10s$(ansi_reset) $(f_dark_gray)%s$(ansi_reset)\n
ansi_format_bright := $(f_white)$(f_bright_cyan)%-10s$(ansi_reset) $(f_black)$(b_bright_cyan)%s$(ansi_reset)\n
# This formats help statements in ANSI colors. To hide a target from help, don't comment it with a trailing '##'.
help: ## Describe how to use each target
@printf "$(ansi_$(name))$(f_white)\n"
@awk 'BEGIN {FS = ":.*?## "} /^[0-9a-zA-Z_-]+:.*?## / {sub("\\\\n",sprintf("\n%22c"," "), $$2);printf "$(ansi_format_dark)", $$1, $$2}' $(MAKEFILE_LIST)
.PHONY: test
test: ## Run all unit tests
@$(go) test ./...
.PHONY: build
build: $(current_binary)
# @echo "uy"## Build the binary
# This generates the assets that attach to a release.
dist: $(archives) $(checksums) ## Generate release assets
license_files := cmd pkg
license: $(addlicense) ## Add license to files
@$(addlicense) -c "Tetrate" $(license_files) 1>/dev/null 2>&1
# Override lint cache directory so we can cache it on CI. Reference: https://golangci-lint.run/usage/configuration/#cache.
# TODO(dio): we need to have .golangci.yml so we can control how linter lints our code.
export GOLANGCI_LINT_CACHE=$(CACHE_DIR)/golangci-lint
lint: $(all_go_sources) $(golangci-lint) ## Lint all Go sources
@printf "$(ansi_format_dark)" $@ "linting files"
@$(golangci-lint) run --timeout 5m
@printf "$(ansi_format_bright)" $@ "ok"
format: go.mod $(all_go_sources) $(goimports) ## Format all Go sources
@printf "$(ansi_format_dark)" $@ "formatting files"
@$(go) mod tidy
@$(go)fmt -s -w $(all_go_sources)
# Workaround inconsistent goimports grouping with awk until golang/go#20818 or incu6us/goimports-reviser#50
@for f in $(all_go_sources); do \
awk '/^import \($$/,/^\)$$/{if($$0=="")next}{print}' $$f > /tmp/fmt; \
mv /tmp/fmt $$f; \
done
@$(goimports) -local $$(sed -ne 's/^module //gp' go.mod) -w $(all_go_sources)
@printf "$(ansi_format_bright)" $@ "ok"
check: ## Verify contents of last commit
@$(MAKE) format
@$(MAKE) lint
@$(MAKE) license
@if [ ! -z "`git status -s`" ]; then \
echo "The following differences will fail CI until committed:"; \
git diff --exit-code; \
fi
clean: ## Ensure a clean build
@printf "$(ansi_format_dark)" $@ "deleting temporary files"
@rm -rf coverage.txt
@rm -rf build
@rm -rf dist
@$(go) clean -testcache
@printf "$(ansi_format_bright)" $@ "ok"
build/$(name)_%/$(name): $(main_go_sources)
$(call go-build,$@,$<)
build/$(name)_%/$(name).exe: $(main_go_sources)
$(call go-build,$@,$<)
dist/$(name)_$(VERSION)_%.tar.gz: build/$(name)_%/$(name)
@printf "$(ansi_format_dark)" tar.gz "tarring $@"
@mkdir -p $(@D)
@tar -C $(<D) -cpzf $@ $(<F)
@printf "$(ansi_format_bright)" tar.gz "ok"
# TODO(dio): Archive the signed binary instead of the unsigned one.
dist/$(name)_$(VERSION)_%.zip: build/$(name)_%/$(name).exe
@printf "$(ansi_format_dark)" zip "zipping $@"
@mkdir -p $(@D)
@zip -qj $@ $<
@printf "$(ansi_format_bright)" zip "ok"
# Darwin doesn't have sha256sum. See https://github.com/actions/virtual-environments/issues/90
sha256sum := $(if $(findstring darwin,$(goos)),shasum -a 256,sha256sum)
$(checksums): $(archives)
@printf "$(ansi_format_dark)" sha256sum "generating $@"
@$(sha256sum) $^ > $@
@printf "$(ansi_format_bright)" sha256sum "ok"
go_link := -X main.version=$(VERSION) -X main.commit=$(shell git rev-parse --short HEAD) -X main.date=$(shell date '+%Y-%m-%d')
go-arch = $(if $(findstring amd64,$1),amd64,arm64)
go-os = $(if $(findstring .exe,$1),windows,$(if $(findstring linux,$1),linux,darwin))
define go-build
@printf "$(ansi_format_dark)" build "building $1"
@CGO_ENABLED=0 GOOS=$(call go-os,$1) GOARCH=$(call go-arch,$1) $(go) build \
# -ldflags "-s -w $(go_link)" \
-o $1 $2
@printf "$(ansi_format_bright)" build "ok"
endef
# Catch all rules for Go-based tools.
$(go_tools_dir)/%:
@printf "$(ansi_format_dark)" tools "installing $($(notdir $@)@v)..."
@GOBIN=$(go_tools_dir) go install $($(notdir $@)@v)
@printf "$(ansi_format_bright)" tools "ok"