/
Makefile
189 lines (160 loc) · 4.9 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Copyright (c) 2017 Equim.
#
# 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.
PKG := .
#####################################################################
# basic envs
SHELL := /bin/bash
MAKE := make --no-print-directory
# conditions check
HAVE_UPX := $(shell hash upx 2> /dev/null && echo 1)
# build commands and flags
GC := go build
# determine whether enable pie or not
CGO_ENABLED := 0
ifeq ($(shell test $(shell go env GOOS) = linux -o $(shell go env GOOS)/$(shell go env GOARCH) = darwin/amd64; echo $$?),0)
# native build?
ifeq ($(shell env GOOS=$(GOOS) go env GOOS),$(shell go env GOOS))
ifeq ($(shell env GOARCH=$(GOARCH) go env GOARCH),$(shell go env GOARCH))
ifeq ($(shell env GOARM=$(GOARM) go env GOARM),$(shell go env GOARM))
FLAGS += --buildmode pie
CGO_ENABLED := 1
endif
endif
endif
endif
# just in case we get a cygwin path on windows
GOPATH := $(shell go env GOPATH)
ifdef DEBUG
GCFLAGS += -N -l
ifdef RACE
FLAGS += --race
endif
else ifdef RELEASE
# in release mode, GOPATH should be wiped from the built binary
GCFLAGS += --trimpath $(GOPATH)
ASMFLAGS += --trimpath $(GOPATH)
LDFLAGS += -s -w
endif
VERSION := $(shell git describe --dirty --always --tags 2> /dev/null || date +"%y%m%d")
GIT_HASH := $(shell git rev-parse --short HEAD 2>/dev/null)
BUILD_DATE := $(shell date +"%y-%m-%d")
LDFLAGS += -X main.Version=$(VERSION) -X main.GitHash=$(GIT_HASH) -X main.BuildDate=$(BUILD_DATE)
# determine the name of the default target `BIN'
DIRNAME := $(shell basename $(shell pwd))
TITLE := $(DIRNAME)
ifdef SUFFIX
TITLE := $(DIRNAME)-$(SUFFIX)
else ifdef GOOS
ifdef GOARCH
ifdef GOARM
TITLE := $(DIRNAME)-$(GOOS)-$(GOARCH)$(GOARM)
else
TITLE := $(DIRNAME)-$(GOOS)-$(GOARCH)
endif
endif
endif
GOEXE := $(shell env GOOS=$(GOOS) go env GOEXE)
BIN := $(TITLE)$(GOEXE)
# prefix for target `install'
PREFIX := $(GOPATH)/bin
define magic
SUFFIX=$1 $(shell test "$(RELEASE)" && echo "RELEASE=1 pack")
endef
ifdef V
VERBOSE := 1
endif
ifndef VERBOSE
AT := @
endif
#####################################################################
SRC := $(shell find . -path ./vendor -prune -o -name '*.go') Makefile
PLATFORMS := linux-64 linux-32 windows-64 windows-32 macos
PREPARE := vendor resource_windows_386.syso resource_windows_amd64.syso
$(BIN): $(PREPARE) $(SRC)
@test "$(GOOS)" -o "$(GOARCH)" -o "$(GOARM)" && echo -ne " "; \
test "$(GOOS)" && echo -ne "\x1b[32mGOOS=$(GOOS) "; \
test "$(GOARCH)" && echo -ne "\x1b[33mGOARCH=$(GOARCH) "; \
test "$(GOARM)" && echo -ne "\x1b[35mGOARM=$(GOARM) "; \
test "$(GOOS)" -o "$(GOARCH)" -o "$(GOARM)" && echo -ne "\n"; \
echo -e "\x1b[0m - \x1b[1;36mGC\x1b[0m"
$(AT)$(GC) -o $@ $(FLAGS) --gcflags "$(GCFLAGS)" --asmflags "$(ASMFLAGS)" --ldflags "$(LDFLAGS)" $(PKG)
vendor: Gopkg.lock Gopkg.toml
dep ensure -v
resource_windows_386.syso resource_windows_amd64.syso: versioninfo.json icon.ico
goversioninfo --platform-specific --icon icon.ico versioninfo.json
doc/%.1.html: doc/%.1.adoc
asciidoctor -o $@ -b manpage $<
man:
mkdir -p $@
man/%.1: doc/%.1.adoc man
asciidoctor -o - -b manpage $< | \
sed "s@$(shell date +"%Y-%m-%d")@$(shell date +"%B %Y")@" | \
sed 's/\&.br/\.br/' \
> $@
.PHONY: pack
pack: $(TITLE)-$(VERSION).tar.xz
# this target won't remove its dependancy
$(TITLE)-$(VERSION).tar.xz: $(BIN) LICENSE
ifdef HAVE_UPX
@echo " - UPX"
ifdef VERBOSE
upx -9 $<
else
@upx -q9 $< > /dev/null
endif
endif
@echo " - TAR | XZ"
$(AT)tar -cf - --mode="a+x" $^ | xz -T0 -c9 - > $@
# cross-building
.PHONY: all
all: $(PLATFORMS)
.PHONY: $(PLATFORMS)
linux-64:
@$(MAKE) GOOS=linux GOARCH=amd64 $(call magic,$@)
linux-32:
@$(MAKE) GOOS=linux GOARCH=386 $(call magic,$@)
windows-64:
@$(MAKE) GOOS=windows GOARCH=amd64 $(call magic,$@)
windows-32:
@$(MAKE) GOOS=windows GOARCH=386 $(call magic,$@)
macos:
@$(MAKE) GOOS=darwin GOARCH=amd64 $(call magic,$@)
.PHONY: release
release: RELEASE=1
release: all
@echo -e "\n\x1b[35m - SHA256 > $(DIRNAME)-$(VERSION).sha256\x1b[0m"
$(AT)sha256sum *.tar.xz > $(DIRNAME)-$(VERSION).sha256
.PHONY: install
install: $(BIN)
cp -t $(PREFIX) $^
.PHONY: uninstall
uninstall:
$(RM) $(PREFIX)/$(BIN)
.PHONY: test
test: $(PREPARE)
go test ./...
.PHONY: bench
bench: $(PREPARE)
go test --bench . ./...
.PHONY: clean
clean:
go clean
$(RM) -r man
$(RM) \
$(BIN) \
*.syso \
doc/*.html \
$(DIRNAME).tar.xz \
$(foreach p,$(PLATFORMS),$(DIRNAME)-$(p)*) \
$(DIRNAME)-*.sha256