-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
159 lines (124 loc) · 3.29 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
# project specific definitions
SRCDIR = cmd
BINDIR = bin
PACKAGE = pdf-ws
# go commands
GOCMD = go
GOBLD = $(GOCMD) build
GOCLN = $(GOCMD) clean
GOTST = $(GOCMD) test
GOVET = $(GOCMD) vet
GOFMT = $(GOCMD) fmt
GOGET = $(GOCMD) get
GOMOD = $(GOCMD) mod
GOVER = $(GOCMD) version
GOLNT = golint
GOBIN = $(HOME)/go/bin
# default build target is host machine architecture
MACHINE = $(shell uname -s | tr '[A-Z]' '[a-z]')
TARGET = $(MACHINE)
# git commit used for this build, either passed to make via Dockerfile or determined from local directory
ifeq ($(GIT_COMMIT),)
GIT_COMMIT = $(shell \
commit="$$(git rev-list -1 HEAD 2>/dev/null)" ; \
if [ "$${commit}" != "" ] ; then \
postfix="" ; \
git diff --quiet || postfix="-modified" ; \
echo "$${commit}$${postfix}" ; \
fi \
)
endif
# darwin-specific definitions
GOENV_darwin =
GOFLAGS_darwin =
GOLINK_darwin =
# linux-specific definitions
GOENV_linux =
GOFLAGS_linux =
GOLINK_linux =
# extra flags
GOENV_EXTRA = GOARCH=amd64
GOFLAGS_EXTRA =
GOLINK_EXTRA = -X main.gitCommit=$(GIT_COMMIT)
# default target:
build: go-vars compile symlink
go-vars:
$(eval GOENV = GOOS=$(TARGET) $(GOENV_$(TARGET)) $(GOENV_EXTRA))
$(eval GOFLAGS = $(GOFLAGS_$(TARGET)) $(GOFLAGS_EXTRA))
$(eval GOLINK = -ldflags '$(GOLINK_$(TARGET)) $(GOLINK_EXTRA)')
compile:
@ \
echo "building [$(PACKAGE)] for target: [$(TARGET)]" ; \
echo ; \
$(GOVER) ; \
echo ; \
printf "compile: %-6s env: [%s] flags: [%s] link: [%s]\n" "$(PACKAGE)" "$(GOENV)" "$(GOFLAGS)" "$(GOLINK)" ; \
$(GOENV) $(GOBLD) $(GOFLAGS) $(GOLINK) -o "$(BINDIR)/$(PACKAGE).$(TARGET)" "$(SRCDIR)"/*.go || exit 1
symlink:
@ \
echo ; \
echo "symlink: $(BINDIR)/$(PACKAGE) -> $(PACKAGE).$(TARGET)" ; \
ln -sf "$(PACKAGE).$(TARGET)" "$(BINDIR)/$(PACKAGE)" || exit 1
darwin: target-darwin build
target-darwin:
$(eval TARGET = darwin)
linux: target-linux build
target-linux:
$(eval TARGET = linux)
rebuild: flag build
flag:
$(eval GOFLAGS_EXTRA += -a)
rebuild-darwin: target-darwin rebuild
rebuild-linux: target-linux rebuild
# docker: make sure binary is linux and truly static
docker-vars:
$(eval GOENV_EXTRA += CGO_ENABLED=0)
$(eval GOLINK_EXTRA += -extldflags "-static")
docker: docker-vars linux
rebuild-docker: docker-vars rebuild-linux
# maintenance rules
fmt:
@ \
echo "[FMT] $(PACKAGE)" ; \
(cd "$(SRCDIR)" && $(GOFMT))
vet:
@ \
echo "[VET] $(PACKAGE)" ; \
(cd "$(SRCDIR)" && $(GOVET))
lint:
@ \
echo "[LINT] $(PACKAGE)" ; \
(cd "$(SRCDIR)" && $(GOLNT))
clean:
@ \
echo "[PURGE] $(BINDIR)/" ; \
rm -rf $(BINDIR) ; \
echo "[CLEAN] $(PACKAGE)" ; \
(cd "$(SRCDIR)" && $(GOCLN))
tidy:
@ \
echo "[MOD] $(GOMOD) tidy" ; \
$(GOMOD) tidy
verify:
@ \
echo "[MOD] $(GOMOD) verify" ; \
$(GOMOD) verify
dep:
@ \
echo "[MOD] GOPROXY=$(GOPROXY) $(GOGET)" ; \
GOPROXY=$(GOPROXY) $(GOGET) -u ./$(SRCDIR)/...
DEP: goproxy-direct dep
goproxy-direct:
$(eval GOPROXY = direct)
check-static:
@ \
echo "[CHECK] static checks" ; \
go install honnef.co/go/tools/cmd/staticcheck ; \
$(GOBIN)/staticcheck -checks all,-S1002,-ST1003 -fail all,-U1000 ./$(SRCDIR)/...
check-shadow:
@ \
echo "[CHECK] shadowed variables" ; \
go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow ; \
go vet -vettool=$(GOBIN)/shadow ./$(SRCDIR)/...
check: check-shadow check-static
sure: check tidy verify fmt vet lint