-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathMakefile
105 lines (92 loc) · 3.33 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
CODENAME = themis
PACKAGE = github/jdcloud-bri/$(CODENAME)
DATE ?= $(shell date +%Y%m%d)
TIME ?= $(shell date +%H%M%S)
VERSION ?= $(shell git describe --tags --always --match=v* | awk -F\- '{print $$1}' | sed -e 's/^v//')
RELEASE ?= $(shell git describe --tags --always --match=v* | awk -F\- '{print $$2}')
COMMITID ?= $(shell git describe --tags --always --match=v* | awk -F\- '{print $$3}' | sed -e 's/^g//')
GOPATH = $(CURDIR)/build
BIN = $(GOPATH)/bin
BASE = $(GOPATH)/src/$(PACKAGE)
RPMBUILD = $(GOPATH)/rpmbuild
PKGS = $(or $(PKG),$(shell cd $(BASE) && env GOPATH=$(GOPATH) $(GO) list ./... | grep "^$(PACKAGE)/cmd/"))
TESTPKGS = $(shell env GOPATH=$(GOPATH) $(GO) list -f '{{ if or .TestGoFiles .XTestGoFiles }}{{ .ImportPath }}{{ end }}' $(PKGS))
export GOPATH
GO = go
GODOC = godoc
GOFMT = gofmt
TIMEOUT = 15
V = 0
Q = $(if $(filter 1,$V),,@)
M = $(shell printf "\033[34;1m▶\033[0m")
# All
.PHONY: all
all: fmt | build
# Build
.PHONY: build
build: $(BASE) ; $(info $(M) building executable ...) @ ## Build program binary
$Q cd $(BASE) && \
release=$(RELEASE).$(DATE).$(COMMITID); \
if [[ "$(RELEASE)" -eq "" ]] ; then \
release=0.$(DATE)$(TIME); \
fi && \
$(GO) install \
-tags release \
-ldflags "-X main.versionNumber=$(VERSION)-$$release -X main.buildTime=$(DATE)-$(TIME)" \
$(PKGS) ; \
cp -rdp config/*.conf $(GOPATH)/config ; \
$(BASE): ; $(info $(M) setting GOPATH ...)
@mkdir -p $(dir $@)
@mkdir -p $(GOPATH)/config
@ln -sf $(CURDIR) $@
# Tools
$(BIN):
@mkdir -p $@
$(BIN)/%: $(BIN) | $(BASE) ; $(info $(M) building $(REPOSITORY) ...)
$Q tmp=$$(mktemp -d); \
(GOPATH=$$tmp go get $(REPOSITORY) && cp $$tmp/bin/* $(BIN)/.) || ret=$$?; \
rm -rf $$tmp ; exit $$ret
.PHONY: fmt
fmt: ; $(info $(M) running gofmt ...) @ ## Run gofmt on all source files
@ret=0 && for d in $$($(GO) list -f '{{.Dir}}' ./... | grep -v /vendor/); do \
$(GOFMT) -l -w $$d/*.go || ret=$$? ; \
done ; exit $$ret
# Packages
.PHONY: rpm
rpm: build ; $(info $(M) building rpm ...) @ ## Building rpm packages
@mkdir -p $(GOPATH)/rpms
@mkdir -p $(RPMBUILD)/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
@ret=0 && for s in $$(ls spec); do \
cd $(BASE) ; \
release=$(RELEASE).$(DATE).$(COMMITID); \
if [[ "$(RELEASE)" -eq "" ]] ; then \
release=0.$(DATE)$(TIME); \
fi; \
name=$${s%%.*} ; \
sed -e "s#GIT_VERSION#$(VERSION)#g" -e "s#GIT_RELEASE#$$release#g" spec/$$s > $(RPMBUILD)/SPECS/$$name.spec ;\
cd $(GOPATH) ; \
cp -rdp $(BASE)/config/*.conf config/ ; \
tar zcfhP $(RPMBUILD)/SOURCES/$(CODENAME).tar.gz $(GOPATH)/bin $(GOPATH)/config --transform s=$(GOPATH)=$(CODENAME)= ; \
rpmbuild --define '_topdir $(GOPATH)/rpmbuild' -bb $(RPMBUILD)/SPECS/$$name.spec > /dev/null 2>&1 ; \
echo "$(M) package $$name done ..." ; \
done && \
find $(RPMBUILD) -name '*.rpm' | xargs -i cp {} $(GOPATH)/rpms/ && \
rm -rf $(RPMBUILD) || ret=$$? ; exit $$ret
# Misc
.PHONY: clean
clean: ; $(info $(M) cleaning ...) @ ## Cleanup everything
@rm -rf $(GOPATH)
@rm -rf bin
@rm -rf test/tests.* test/coverage.*
.PHONY: help
help:
@grep -E '^[ a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}'
.PHONY: version
version:
@ret=0 && \
release=$(RELEASE).$(DATE).$(COMMITID); \
if [[ "$(RELEASE)" -eq "" ]] ; then \
release=0.$(DATE); \
fi; \
echo $(VERSION)-$$release