-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
167 lines (131 loc) · 3.76 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
161
162
163
164
165
166
167
# Required for globs to work correctly
SHELL := /usr/bin/env bash
MODNAME := $(shell sed -nE 's/^module\s+(.*)\b/\1/p' go.mod)
BINDIR := bin
BINS ?= $(shell find cmd -name 'main.go' | cut -d/ -f2)
export GOCACHE := $(CURDIR)/build
ARCHS ?= amd64 arm
TAGS ?=
TESTS := .
TESTFLAGS := -race -v
LDFLAGS :=
ifeq ($(RELEASE),1)
LDFLAGS += -s -w
else
TAGS += debug
endif
GOFLAGS :=
ifneq ($(wildcard $(CURDIR)/.env/.),)
TAGS += envishere
endif
SRCDIRS := $(wildcard $(CURDIR)/src $(CURDIR)/internal $(CURDIR)/pkg)
SRC := $(shell find $(SRCDIRS) -type f -iname '*.go' -print)
CMD_SRC = $(shell find $(CURDIR)/cmd/$(notdir $@) -type f -iname '*.go' -print)
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null)
GIT_SHA := $(shell git rev-parse HEAD 2>/dev/null)
GIT_TAG := $(shell git describe --tags --abbrev=0 --match='v*' --candidates=1 2>/dev/null)
GIT_STATUS := $(shell test -n "`git status --porcelain 2>/dev/null`" && echo "dirty" || echo "clean")
ifeq ($(GIT_SHA),)
GIT_STATUS := invalid
endif
ifneq ($(GIT_TAG),)
VERSION ?= $(GIT_TAG:v%=%)
else
VERSION ?= dev
endif
VARMODULE := $(MODNAME)/internal/config/vars
LDFLAGS += -X $(VARMODULE).Version=$(VERSION)
LDFLAGS += -X $(VARMODULE).Commit=$(GIT_COMMIT)
LDFLAGS += -X $(VARMODULE).CommitSHA=$(GIT_SHA)
ifeq ($(RELEASE),1)
LDFLAGS += -X $(VARMODULE)._build=release
else
LDFLAGS += -X $(VARMODULE)._build=debug
endif
MULTIARCH:=
ifneq ($(words $(ARCHS)),0)
ifneq ($(words $(ARCHS)),1)
MULTIARCH:=1
endif
endif
ifndef MULTIARCH
OUTPUTS=$(BINS:%=$(BINDIR)/%)
else
OUTPUTS:=$(addprefix $(BINDIR)/,$(foreach arch,$(ARCHS),$(foreach bin,$(BINS),$(arch)/$(bin))))
endif
###################################
# build
.PHONY: all
all: build
ifneq ($(wildcard $(CURDIR)/api/docs/swagger.json),)
.PHONY: swagger
swagger:
@swag init -g api/swagger.go -o api/docs
endif
.PHONY: build
build: $(OUTPUTS)
$(OUTPUTS):%: $(SRC) $(CMD_SRC)
ifdef MULTIARCH
@$(eval ARCH_ENV:=GOARCH=$(notdir $(patsubst %/,%,$(dir $@))))
else
@$(eval ARCH_ENV:=)
endif
@$(eval APPNAME_LDFLAGS:=-X $(VARMODULE).AppName=$(notdir $@))
@$(ARCH_ENV) go build $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS) $(APPNAME_LDFLAGS)' -o '$@' $(CURDIR)/cmd/$(notdir $@)
@echo + built: $@
###################################
# tests
.PHONY: test
test: build
#test: test-style
test: test-unit
.PHONY: test-unit
test-unit:
@go test $(GOFLAGS) -run $(TESTS) ./... $(TESTFLAGS)
###################################
# clean
.PHONY: clean
clean:
@rm -rf '$(BINDIR)'
.PHONY: mrpopper
mrpopper: clean
@rm -rf '$(GOCACHE)'
###################################
# build info
.PHONY: info
info:
@echo "Version: $(VERSION)"
@echo "Git Tag: $(GIT_TAG)"
@echo "Git Commit: $(GIT_COMMIT)"
@echo "Git Tree Status: $(GIT_STATUS)"
###################################
# Makefile variables
dump:
$(foreach v, \
$(shell echo "$(filter-out .VARIABLES,$(.VARIABLES))" | tr ' ' '\n' | sort), \
$(if $(filter file,$(origin $(v))), $(info $(shell printf "%-20s" "$(v)")= $(value $(v)))) \
)
dump-expand:
$(foreach v, \
$(shell echo "$(filter-out .VARIABLES,$(.VARIABLES))" | tr ' ' '\n' | sort), \
$(if $(filter file,$(origin $(v))), $(info $(shell printf "%-20s" "$(v)")= $($(v)))) \
)
dump-all:
$(foreach v, \
$(shell echo "$(filter-out .VARIABLES,$(.VARIABLES))" | tr ' ' '\n' | sort), \
$(info $(shell printf "%-20s" "$(v)")= $(value $(v))) \
)
###################################
release:
make clean
#make tox
#make clean
make push
make bump V=$(V)
make push
make clean
bump:
bumpversion $(if $(V),$(V),minor)
push:
git push
git push --tags