-
Notifications
You must be signed in to change notification settings - Fork 45
/
Makefile
236 lines (201 loc) · 3.88 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
.PHONY: all
all: btfhub
#
# make
#
.ONESHELL:
SHELL = /bin/sh
PARALLEL = $(shell $(CMD_GREP) -c ^processor /proc/cpuinfo)
MAKE = make
MAKEFLAGS += --no-print-directory
#
# tools
#
CMD_TR ?= tr
CMD_CUT ?= cut
CMD_AWK ?= awk
CMD_SED ?= sed
CMD_GIT ?= git
CMD_RM ?= rm
CMD_MKDIR ?= mkdir
CMD_TOUCH ?= touch
CMD_GO ?= go
CMD_GREP ?= grep
CMD_CAT ?= cat
CMD_MD5 ?= md5sum
CMD_RSYNC ?= rsync
CMD_CLANG ?= clang
CMD_STATICCHECK ?= staticcheck
.check_%:
#
@command -v $* >/dev/null
if [ $$? -ne 0 ]; then
echo "missing required tool $*"
exit 1
else
touch $@ # avoid target rebuilds due to inexistent file
fi
#
# tools version
#
GO_VERSION = $(shell $(CMD_GO) version 2>/dev/null | $(CMD_AWK) '{print $$3}' | $(CMD_SED) 's:go::g' | $(CMD_CUT) -d. -f1,2)
GO_VERSION_MAJ = $(shell echo $(GO_VERSION) | $(CMD_CUT) -d'.' -f1)
GO_VERSION_MIN = $(shell echo $(GO_VERSION) | $(CMD_CUT) -d'.' -f2)
.checkver_$(CMD_GO): \
| .check_$(CMD_GO)
#
@if [ ${GO_VERSION_MAJ} -eq 1 ]; then
if [ ${GO_VERSION_MIN} -lt 18 ]; then
echo -n "you MUST use golang 1.18 or newer, "
echo "your current golang version is ${GO_VERSION}"
exit 1
fi
fi
touch $@
#
# version
#
LAST_GIT_TAG ?= $(shell $(CMD_GIT) describe --tags --match 'v*' 2>/dev/null)
VERSION ?= $(if $(RELEASE_TAG),$(RELEASE_TAG),$(LAST_GIT_TAG))
#
# environment
#
DEBUG ?= 0
UNAME_M := $(shell uname -m)
UNAME_R := $(shell uname -r)
ifeq ($(DEBUG),1)
GO_DEBUG_FLAG =
else
GO_DEBUG_FLAG = -w
endif
ifeq ($(UNAME_M),x86_64)
ARCH = x86_64
LINUX_ARCH = x86
GO_ARCH = amd64
endif
ifeq ($(UNAME_M),aarch64)
ARCH = arm64
LINUX_ARCH = arm64
GO_ARCH = arm64
endif
#
# variables
#
PROGRAM ?= btfhub
#
# btfhub tool
#
STATIC ?= 0
GO_TAGS =
ifeq ($(STATIC), 1)
GO_TAGS := $(GO_TAGS),netgo
endif
GO_ENV =
GO_ENV += GOOS=linux
GO_ENV += CC=$(CMD_CLANG)
GO_ENV += GOARCH=$(GO_ARCH)
SRC_DIRS = ./cmd/ ./pkg/
SRC = $(shell find $(SRC_DIRS) -type f -name '*.go' ! -name '*_test.go')
$(PROGRAM): \
$(SRC) \
| .check_$(CMD_GO) \
.checkver_$(CMD_GO)
#
$(GO_ENV) $(CMD_GO) build \
-tags $(GO_TAGS) \
-ldflags="$(GO_DEBUG_FLAG) \
-X main.version=\"$(VERSION)\" \
" \
-v -o $@ \
./cmd/btfhub/
#
# btfhub tests
#
.PHONY: test-unit
test-unit: \
$(SRC) \
| .check_$(CMD_GO) \
.checkver_$(CMD_GO)
#
$(GO_ENV) \
$(CMD_GO) test \
-short \
-race \
-v \
./cmd/... \
./pkg/...
#
# code checkers
#
.PHONY: check-vet
check-vet: \
| .check_$(CMD_GO) \
.checkver_$(CMD_GO) \
#
$(GO_ENV) \
$(CMD_GO) vet \
-tags $(GO_TAGS) \
./cmd/... \
./pkg/...
.PHONY: check-staticcheck
check-staticcheck: \
| .check_$(CMD_GO) \
.checkver_$(CMD_GO) \
#
$(GO_ENV) \
$(CMD_STATICCHECK) -f stylish \
-tags $(GO_TAGS) \
./cmd/... \
./pkg/...
#
# repository
#
BTFHUB_ARCHIVE_DIR ?= ../btfhub-archive
LOCAL_ARCHIVE_DIR ?= ./archive
.PHONY: bring
bring: \
| .check_$(CMD_RSYNC)
#
@echo ""
@if [ ! -d $(BTFHUB_ARCHIVE_DIR) ]; then
echo "ERROR: make sure to have the btfhub-archive repository at $(BTFHUB_ARCHIVE_DIR)"
echo ""
exit 1
fi
echo -n "WARNING: this will delete all the files in $(LOCAL_ARCHIVE_DIR), press enter to continue"
echo -n " ... "
read nop
echo ""
$(CMD_RSYNC) -av --delete --exclude=.git* $(BTFHUB_ARCHIVE_DIR)/ $(LOCAL_ARCHIVE_DIR)/
echo ""
.PHONY: take
take: \
| .check_$(CMD_RSYNC)
#
@echo ""
if [ ! -d $(BTFHUB_ARCHIVE_DIR) ]; then
echo "ERROR: make sure to have the btfhub-archive repository at $(BTFHUB_ARCHIVE_DIR)"
echo ""
exit 1
fi
echo -n "WARNING: this will take files from $(LOCAL_ARCHIVE_DIR) into $(BTFHUB_ARCHIVE_DIR), press enter to continue"
echo -n " ... "
echo ""
read nop
$(CMD_RSYNC) -av \
--exclude=.git* \
--exclude=*.deb \
--exclude=*.ddeb \
--exclude=*.rpm \
$(LOCAL_ARCHIVE_DIR)/ $(BTFHUB_ARCHIVE_DIR)/
echo ""
echo "INFO: now goto $(BTFHUB_ARCHIVE_DIR) and commit the changes"
echo ""
#
# clean
#
.PHONY: clean
clean:
#
$(CMD_RM) -rf $(PROGRAM)
$(CMD_RM) -f .check*