-
Notifications
You must be signed in to change notification settings - Fork 7
/
Makefile
352 lines (293 loc) · 14.1 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
DIST_DIR ?= dist/
GOOS ?= $(shell uname -s | tr "[:upper:]" "[:lower:]")
GOARCH ?= $(shell uname -m | sed -E 's/x86_64/amd64/')
BUILDINFOSDET ?=
UDP_PROXY_2020_ARGS ?=
PROJECT_VERSION := 0.1.0
DOCKER_REPO := synfinatic
PROJECT_NAME := udp-proxy-2020
PROJECT_TAG := $(shell git describe --tags 2>/dev/null $(git rev-list --tags --max-count=1))
ifeq ($(PROJECT_TAG),)
PROJECT_TAG := NO-TAG
endif
PROJECT_COMMIT := $(shell git rev-parse HEAD)
ifeq ($(PROJECT_COMMIT),)
PROJECT_COMMIT := NO-CommitID
endif
PROJECT_DELTA := $(shell DELTA_LINES=$$(git diff | wc -l); if [ $${DELTA_LINES} -ne 0 ]; then echo $${DELTA_LINES} ; else echo "''" ; fi)
VERSION_PKG := $(shell echo $(PROJECT_VERSION) | sed 's/^v//g')
LICENSE := GPLv3
URL := https://github.com/$(DOCKER_REPO)/$(PROJECT_NAME)
DESCRIPTION := UDP Proxy 2020: A bad hack for a stupid problem
BUILDINFOS := $(shell date +%FT%T%z)$(BUILDINFOSDET)
HOSTNAME := $(shell hostname)
LDFLAGS := -X "main.Version=$(PROJECT_VERSION)" -X "main.Delta=$(PROJECT_DELTA)"
LDFLAGS += -X "main.Buildinfos=$(BUILDINFOS)" -X "main.Tag=$(PROJECT_TAG)"
LDFLAGS += -X "main.CommitID=$(PROJECT_COMMIT)" -s -w
OUTPUT_NAME := $(DIST_DIR)$(PROJECT_NAME)-$(GOOS)-$(GOARCH)
DOCKER_VERSION ?= v$(PROJECT_VERSION)
ALL: $(OUTPUT_NAME)
include help.mk # place after ALL target and before all other targets
release: build-release ## Build and sign official release
cd dist && shasum -a 256 udp-proxy-2020* | gpg --clear-sign >release.sig
@echo "Now run `make docker-release`?"
build-release: clean linux-amd64 linux-mips64 linux-arm darwin-amd64 freebsd docker package ## Build our release binaries
tags: ./cmd/udp-proxy-2020/*.go ## Create tags file for vim, etc
@echo Make sure you have Universal Ctags installed: https://github.com/universal-ctags/ctags
ctags -R
.PHONY: run
run: ./cmd/udp-proxy-2020/*.go ## build and run udp-proxy-2020 using $UDP_PROXY_2020_ARGS
sudo go run ./cmd/udp-proxy-2020/... $(UDP_PROXY_2020_ARGS)
clean-all: freebsd-clean docker-clean clean ## Clean _everything_
clean: ## Remove all binaries in dist
rm -f dist/*
clean-go: ## Clean Go cache
go clean -i -r -cache -modcache
$(OUTPUT_NAME): ./cmd/udp-proxy-2020/*.go .prepare
go build -ldflags='$(LDFLAGS)' -o $(OUTPUT_NAME) ./cmd/udp-proxy-2020/...
@echo "Created: $(OUTPUT_NAME)"
.PHONY: build-race
build-race: .prepare ## Build race detection binary
go build -race -ldflags='$(LDFLAGS)' -o $(OUTPUT_NAME) ./cmd/udp-proxy-2020/...
debug: .prepare ## Run debug in dlv
dlv debug ./cmd
.PHONY: unittest
unittest: ## Run go unit tests
go test ./...
.PHONY: test-race
test-race: ## Run `go test -race` on the code
@echo checking code for races...
go test -race ./...
.PHONY: vet
vet: ## Run `go vet` on the code
@echo checking code is vetted...
go vet $(shell go list ./...)
test: vet unittest ## Run all tests
.prepare: $(DIST_DIR)
$(DIST_DIR):
@mkdir -p $(DIST_DIR)
.PHONY: fmt
fmt: ## Format Go code
@go fmt ./cmd/...
.PHONY: test-fmt
test-fmt: fmt ## Test to make sure code if formatted correctly
@if test `git diff ./cmd | wc -l` -gt 0; then \
echo "Code changes detected when running 'go fmt':" ; \
git diff -Xfiles ; \
exit -1 ; \
fi
.PHONY: test-tidy
test-tidy: ## Test to make sure go.mod is tidy
@go mod tidy
@if test `git diff go.mod | wc -l` -gt 0; then \
echo "Need to run 'go mod tidy' to clean up go.mod" ; \
exit -1 ; \
fi
precheck: test test-fmt test-tidy lint ## Run all tests that happen in a PR
lint: ## Run golangci-lint
golangci-lint run
######################################################################
# Linux targets for building Linux in Docker
######################################################################
LINUX_AMD64_S_NAME := $(DIST_DIR)$(PROJECT_NAME)-$(PROJECT_VERSION)-linux-amd64
AMD64_IMAGE := $(DOCKER_REPO)/$(PROJECT_NAME)-builder-amd64:$(DOCKER_VERSION)
.PHONY: linux-amd64
linux-amd64: ## Build static Linux/x86_64 binary using Docker
docker build -t $(AMD64_IMAGE) -f Dockerfile.amd64 .
docker run --rm \
--volume $(shell pwd)/dist:/build/$(PROJECT_NAME)/dist \
$(AMD64_IMAGE)
.PHONY: linux-amd64-shell
linux-amd64-shell: ## Get a shell in Linux/x86_64 Docker container
docker run -it --rm \
--volume $(shell pwd)/dist:/build/$(PROJECT_NAME)/dist \
$(AMD64_IMAGE) /bin/bash
.linux-amd64: $(LINUX_AMD64_S_NAME)
$(LINUX_AMD64_S_NAME): .prepare
LDFLAGS='-l/usr/lib/libpcap.a' CGO_ENABLED=1 \
go build -ldflags '$(LDFLAGS) -linkmode external -extldflags -static' \
-o $(LINUX_AMD64_S_NAME) ./cmd/udp-proxy-2020/...
@echo "Created: $(LINUX_AMD64_S_NAME)"
######################################################################
# Vagrant targets for building for FreeBSD/pfSense
######################################################################
.PHONY: .vagrant-check
.vagrant-check:
@which vagrant >/dev/null || "Please install Vagrant: https://www.vagrantup.com"
@which VBoxManage >/dev/null || "Please install VirtualBox: https://www.virtualbox.org"
freebsd: .vagrant-check ## Build all FreeBSD/pfSense binaries using Vagrant VM
vagrant provision && vagrant up && vagrant ssh-config >.vagrant-ssh && \
scp -F .vagrant-ssh default:$(PROJECT_NAME)/dist/*freebsd* dist/
freebsd-shell: ## Get a shell in FreeBSD Vagrant VM
vagrant ssh
freebsd-clean: ## Destroy FreeBSD Vagrant VM
vagrant destroy -f || true
rm -f .vagrant-ssh
ifeq ($(GOOS),freebsd)
# FreeBSD aarch64 and armv7 targets only work inside of FreeBSD Vagrant VM
FREEBSD_AMD64_S_NAME := $(DIST_DIR)$(PROJECT_NAME)-$(PROJECT_VERSION)-freebsd-amd64
FREEBSD_ARM64_S_NAME := $(DIST_DIR)$(PROJECT_NAME)-$(PROJECT_VERSION)-freebsd-arm64
FREEBSD_ARMV7_S_NAME := $(DIST_DIR)$(PROJECT_NAME)-$(PROJECT_VERSION)-freebsd-armv7
freebsd-binaries: freebsd-amd64 freebsd-arm64 freebsd-armv7 ## no-help
freebsd-amd64: $(FREEBSD_AMD64_S_NAME) ## no-help
freebsd-arm64: $(FREEBSD_ARM64_S_NAME) ## no-help
freebsd-armv7: $(FREEBSD_ARMV7_S_NAME) ## no-help
# Seems to be a bug with CGO & Clang where it always wants to use the host arch
# linker and it doesn't seem to honor the LD ENV var :(
.PHONY: .freebsd-arm-cross .freebsd-amd64-cross .freebsd-aarch64-cross
.freebsd-aarch64-cross:
@cd /usr/local/bin && \
if test ! -f x86_64-unknown-freebsd12.2-ld.bfd.bak ; then \
mv x86_64-unknown-freebsd12.2-ld.bfd x86_64-unknown-freebsd12.2-ld.bfd.bak ; \
ln -s aarch64-unknown-freebsd12.2-ld.bfd x86_64-unknown-freebsd12.2-ld.bfd ; \
fi
.freebsd-arm-cross:
@cd /usr/local/bin && \
if test ! -f x86_64-unknown-freebsd12.2-ld.bfd.bak ; then \
mv x86_64-unknown-freebsd12.2-ld.bfd x86_64-unknown-freebsd12.2-ld.bfd.bak ; \
ln -s arm-gnueabi-freebsd12.2-ld.bfd x86_64-unknown-freebsd12.2-ld.bfd ; \
fi
.freebsd-amd64-cross:
@cd /usr/local/bin && \
if test -f x86_64-unknown-freebsd12.2-ld.bfd.bak ; then \
rm x86_64-unknown-freebsd12.2-ld.bfd ; \
mv x86_64-unknown-freebsd12.2-ld.bfd.bak x86_64-unknown-freebsd12.2-ld.bfd ;\
fi
$(FREEBSD_AMD64_S_NAME): .freebsd-amd64-cross
GOOS=freebsd GOARCH=amd64 CGO_ENABLED=1 \
CGO_LDFLAGS='-libverbs' \
go build -ldflags '$(LDFLAGS) -linkmode external -extldflags -static' \
-o $(FREEBSD_AMD64_S_NAME) ./cmd/udp-proxy-2020/...
@echo "Created: $(FREEBSD_AMD64_S_NAME)"
$(FREEBSD_ARM64_S_NAME): .freebsd-aarch64-cross
GOOS=freebsd GOARCH=arm64 CGO_ENABLED=1 \
CGO_LDFLAGS='--sysroot=/usr/local/freebsd-sysroot/aarch64 -libverbs' \
CGO_CFLAGS='-I/usr/local/freebsd-sysroot/aarch64/usr/include' \
CC=/usr/local/freebsd-sysroot/aarch64/bin/cc \
PKG_CONFIG_PATH=/usr/local/freebsd-sysroot/aarch64/usr/libdata/pkgconfig \
go build -ldflags '$(LDFLAGS) -linkmode external -extldflags -static' \
-o $(FREEBSD_ARM64_S_NAME) ./cmd/udp-proxy-2020/...
@echo "Created: $(FREEBSD_ARM64_S_NAME)"
$(FREEBSD_ARMV7_S_NAME): .freebsd-arm-cross
GOOS=freebsd GOARCH=arm GOARM=7 CGO_ENABLED=1 \
CGO_LDFLAGS='--sysroot=/usr/local/freebsd-sysroot/armv7 -libverbs' \
CGO_CFLAGS='-I/usr/local/freebsd-sysroot/armv7/usr/include' \
CC=/usr/local/freebsd-sysroot/armv7/bin/cc \
PKG_CONFIG_PATH=/usr/local/freebsd-sysroot/armv7/usr/libdata/pkgconfig \
go build -ldflags '$(LDFLAGS) -linkmode external -extldflags -static' \
-o $(FREEBSD_ARMV7_S_NAME) ./cmd/udp-proxy-2020/...
@echo "Created: $(FREEBSD_ARMV7_S_NAME)"
endif
######################################################################
# MIPS64 targets for building for Ubiquiti USG/Edgerouter
######################################################################
LINUX_MIPS64_S_NAME := $(DIST_DIR)$(PROJECT_NAME)-$(PROJECT_VERSION)-linux-mips64
MIPS64_IMAGE := $(DOCKER_REPO)/$(PROJECT_NAME)-builder-mips64:$(DOCKER_VERSION)
.PHONY: linux-mips64
linux-mips64: .prepare ## Build Linux/MIPS64 static binary in Docker container
docker build -t $(MIPS64_IMAGE) -f Dockerfile.mips64 .
docker run --rm \
--volume $(shell pwd):/build/udp-proxy-2020 \
$(MIPS64_IMAGE)
.PHONY: linux-mips64-shell
linux-mips64-shell: .prepare ## Get a shell in Linux/MIPS64 build Docker container
docker run -it --rm \
--volume $(shell pwd):/build/udp-proxy-2020 \
--entrypoint /bin/bash $(MIPS64_IMAGE)
.linux-mips64: $(LINUX_MIPS64_S_NAME)
$(LINUX_MIPS64_S_NAME): .prepare
LDFLAGS='-l/usr/mips64-linux-gnuabi64/lib/libpcap.a' \
GOOS=linux GOARCH=mips64 CGO_ENABLED=1 CC=mips64-linux-gnuabi64-gcc \
PKG_CONFIG_PATH=/usr/mips64-linux-gnuabi64/lib/pkgconfig \
go build -ldflags '$(LDFLAGS) -linkmode external -extldflags -static' \
-o $(LINUX_MIPS64_S_NAME) ./cmd/udp-proxy-2020/...
@echo "Created: $(LINUX_MIPS64_S_NAME)"
######################################################################
# Targets for building for Linux/ARM32 no hardware floating point
######################################################################
LINUX_ARMV5_S_NAME := $(DIST_DIR)$(PROJECT_NAME)-$(PROJECT_VERSION)-linux-armv5
LINUX_ARMV6_S_NAME := $(DIST_DIR)$(PROJECT_NAME)-$(PROJECT_VERSION)-linux-armv6
LINUX_ARMV7_S_NAME := $(DIST_DIR)$(PROJECT_NAME)-$(PROJECT_VERSION)-linux-armv7
LINUX_ARM64_S_NAME := $(DIST_DIR)$(PROJECT_NAME)-$(PROJECT_VERSION)-linux-arm64
ARM_IMAGE := $(DOCKER_REPO)/$(PROJECT_NAME)-builder-arm:$(DOCKER_VERSION)
.PHONY: linux-arm
linux-arm: .prepare ## Build Linux/arm static binaries in Docker container
docker build -t $(ARM_IMAGE) -f Dockerfile.arm .
docker run --rm \
--volume $(shell pwd):/build/udp-proxy-2020 \
$(ARM_IMAGE)
.PHONY: linux-arm-shell
linux-arm-shell: .prepare ## Get a shell in Linux/arm build Docker container
docker run -it --rm \
--volume $(shell pwd):/build/udp-proxy-2020 \
--entrypoint /bin/bash $(ARM_IMAGE)
.linux-arm: $(LINUX_ARMV5_S_NAME) $(LINUX_ARMV6_S_NAME) $(LINUX_ARMV7_S_NAME) $(LINUX_ARM64_S_NAME)
$(LINUX_ARMV5_S_NAME): .prepare
LDFLAGS='-l/usr/arm-linux-gnueabi/lib/libpcap.a' \
GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=1 CC=arm-linux-gnueabi-gcc-11 \
PKG_CONFIG_PATH=/usr/arm-linux-gnueabi/lib/pkgconfig \
go build -ldflags '$(LDFLAGS) -linkmode external -extldflags -static' \
-o $(LINUX_ARMV5_S_NAME) ./cmd/udp-proxy-2020/...
@echo "Created: $(LINUX_ARMV5_S_NAME)"
$(LINUX_ARMV6_S_NAME): .prepare
LDFLAGS='-l/usr/arm-linux-gnueabi/lib/libpcap.a' \
GOOS=linux GOARCH=arm GOARM=6 CGO_ENABLED=1 CC=arm-linux-gnueabihf-gcc-11 \
PKG_CONFIG_PATH=/usr/arm-linux-gnueabihf/lib/pkgconfig \
go build -ldflags '$(LDFLAGS) -linkmode external -extldflags -static' \
-o $(LINUX_ARMV6_S_NAME) ./cmd/udp-proxy-2020/...
@echo "Created: $(LINUX_ARMV6_S_NAME)"
$(LINUX_ARMV7_S_NAME): .prepare
LDFLAGS='-l/usr/arm-linux-gnueabi/lib/libpcap.a' \
GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 CC=arm-linux-gnueabihf-gcc-11 \
PKG_CONFIG_PATH=/usr/arm-linux-gnueabihf/lib/pkgconfig \
go build -ldflags '$(LDFLAGS) -linkmode external -extldflags -static' \
-o $(LINUX_ARMV7_S_NAME) ./cmd/udp-proxy-2020/...
@echo "Created: $(LINUX_ARMV7_S_NAME)"
$(LINUX_ARM64_S_NAME): .prepare
LDFLAGS='-l/usr/aarch64-linux-gnu/lib/libpcap.a' \
GOOS=linux GOARCH=arm64 CGO_ENABLED=1 CC=aarch64-linux-gnu-gcc-11 \
PKG_CONFIG_PATH=/usr/aarch64-linux-gnu/lib/pkgconfig \
go build -ldflags '$(LDFLAGS) -linkmode external -extldflags -static' \
-o $(LINUX_ARM64_S_NAME) ./cmd/udp-proxy-2020/...
@echo "Created: $(LINUX_ARM64_S_NAME)"
######################################################################
# Targets for building macOS/Darwin (only valid on macOS)
######################################################################
ifeq ($(GOOS),darwin)
DARWIN_AMD64_S_NAME := $(DIST_DIR)$(PROJECT_NAME)-$(PROJECT_VERSION)-darwin-amd64
darwin-amd64: $(DARWIN_AMD64_S_NAME) ## Build macOS/amd64 binary
$(DARWIN_AMD64_S_NAME): ./cmd/udp-proxy-2020/*.go .prepare
GOOS=darwin GOARCH=amd64 go build -ldflags='$(LDFLAGS)' \
-o $(DARWIN_AMD64_S_NAME) ./cmd/udp-proxy-2020/...
@echo "Created: $(DARWIN_AMD64_S_NAME)"
endif
######################################################################
# Docker image for running in docker container for UDM Pro/etc
######################################################################
.PHONY: docker docker-clean .docker
docker: ## Build docker image for Linux/amd64
docker build \
-t $(DOCKER_REPO)/$(PROJECT_NAME):$(DOCKER_VERSION) \
--build-arg VERSION=$(DOCKER_VERSION) \
-f Dockerfile .
.docker:
CGO_ENABLED=1 \
go build -ldflags '$(LDFLAGS)' -o dist/udp-proxy-2020 ./cmd/udp-proxy-2020/...
docker-shell: ## Get a shell in the docker image
docker run --rm -it --network=host \
$(DOCKER_REPO)/$(PROJECT_NAME):$(DOCKER_VERSION) \
/bin/sh
docker-release: ## Tag and push docker images Linux AMD64/ARM64
docker buildx build \
-t $(DOCKER_REPO)/$(PROJECT_NAME):$(DOCKER_VERSION) \
-t $(DOCKER_REPO)/$(PROJECT_NAME):latest \
--build-arg VERSION=$(DOCKER_VERSION) \
--platform linux/arm64,linux/amd64 \
--push -f Dockerfile .
docker-clean: ## Remove all docker build images
docker image rm $(ARM_IMAGE) $(AMD64_IMAGE) $(MIPS64_IMAGE) || true
package: linux-amd64 linux-arm ## Build deb/rpm packages
docker build -t udp-proxy-2020-builder:latest -f Dockerfile.package .
docker run --rm \
-v $$(pwd)/dist:/root/dist \
-e VERSION=$(PROJECT_VERSION) udp-proxy-2020-builder:latest