-
Notifications
You must be signed in to change notification settings - Fork 3
/
Makefile
173 lines (158 loc) · 4.82 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
SHELL := /bin/bash
MODULE := $(shell sed -nr 's/^module ([a-z\-]+)$$/\1/p' go.mod)
GO_FILE := $(MODULE).go
ifeq ($(output),)
OUT_FILE := dist/$(MODULE).bin
else
OUT_FILE := $(output)
endif
default:
@{\
set -e ;\
os_release_id=$$(grep -E '^ID(_LIKE)?=' /etc/os-release | sort | head -n 1 | sed -r 's/ID(_LIKE)?=//' || true) ;\
if [ "$$os_release_id" = "arch" ]; then \
make --no-print-directory release type=dynamic ;\
else \
make --no-print-directory release type=static ;\
fi ;\
}
start:
go run $(GO_FILE)
clean:
go clean 2>/dev/null || true
rm -rf dist/*
rm -f "$(OUT_FILE)"
# Build development binary
.PHONY: build
build:
go build -v -o $(OUT_FILE) $(GO_FILE)
@ls -lh "$(OUT_FILE)"
release:
@{\
set -e ;\
if [ "$(type)" != "static" ] && [ "$(type)" != "dynamic" ]; then \
echo "The type parameter must be \"static\" or \"dynamic\"" ;\
exit 1 ;\
fi ;\
echo "Building $(type) binary (GOARCH: $(GOARCH) GOARM: $(GOARM))..." ;\
if [ -z "$${skip_clean}" ]; then make --no-print-directory clean; fi ;\
export VERSION_FLAGS="-X main.Version=$$(make --no-print-directory version) -X main.CommitDate=$$(make --no-print-directory commit-date)" ;\
case "$(type)" in \
static) \
make --no-print-directory release-static ;\
;;\
dynamic) \
make --no-print-directory release-dynamic ;\
;;\
esac ;\
}
@make --no-print-directory postbuild
# Build statically linked production binary
release-static:
@{\
set -e ;\
args=(-a -trimpath -mod=readonly -modcacherw -ldflags "-w -s $${VERSION_FLAGS}") ;\
if [ "$${GOARCH}" != "arm" ]; then \
args+=("-buildmode=pie") ;\
fi ;\
CGO_ENABLED=0 go build "$${args[@]}" -o "$(OUT_FILE)" $(GO_FILE) ;\
}
# Build dynamically linked production binary
release-dynamic:
@{\
set -e ;\
export CGO_CPPFLAGS="$${CPPFLAGS}" ;\
export CGO_CFLAGS="$${CFLAGS}" ;\
export CGO_CXXFLAGS="$${CXXFLAGS}" ;\
export CGO_LDFLAGS="$${LDFLAGS}" ;\
args=(-a -trimpath -mod=readonly -modcacherw -ldflags "-linkmode external -w -s $${VERSION_FLAGS}") ;\
if [ "$${GOARCH}" != "arm" ]; then \
args+=("-buildmode=pie") ;\
fi ;\
go build "$${args[@]}" -o "$(OUT_FILE)" $(GO_FILE) ;\
}
postbuild:
@{\
set -e ;\
if [ ! -z "$${make_tgz}" ]; then \
tgz_file="$(OUT_FILE).tar.gz" ;\
echo "Creating \"$${tgz_file}\"..." ;\
tar -C "$$(dirname "$(OUT_FILE)")" \
-cz -f "$$tgz_file" \
"$$(basename "$(OUT_FILE)")" ;\
fi ;\
if [ ! -z "$${make_deb}" ]; then \
echo "Creating deb ($${DEB_ARCH}) file..." ;\
CONFIG_FILE=build/package/debian.yml \
ARCH=$${DEB_ARCH} \
PREFIX="$${PREFIX}" \
BIN_FILE="$(OUT_FILE)" \
VERSION=$${VERSION} \
RELEASE=$${RELEASE} \
./scripts/mkdeb.py ;\
rm -rf "$${PREFIX}" ;\
fi ;\
}
@ls -lh "$(OUT_FILE)"*
docker:
@{\
set -e ;\
image_name=$(MODULE)_$$(openssl rand -hex 8) ;\
container_name=$(MODULE)_$$(openssl rand -hex 8) ;\
docker build \
--file build/Dockerfile \
--build-arg "CURRENT_TAG=$${CURRENT_TAG}" \
--build-arg "COMMIT_DATE=$$(make --no-print-directory commit-date)" \
--tag $${image_name} . ;\
docker run -d --rm --name $${container_name} --entrypoint tail $${image_name} -f /dev/null ;\
make --no-print-directory clean ;\
docker cp $${container_name}:/go/src/app/dist . ;\
docker stop -t 0 $${container_name} ;\
docker rmi --no-prune $${image_name} ;\
}
# Print the value of the VERSION variable if available, otherwise get version
# based on the latest git tag
version:
@{\
TAG=$$(git describe --tags 2>/dev/null | sed -r 's/^v([0-9]+\.[0-9]+\.[0-9]+).*/\1/') ;\
VER=$${VERSION:-$${TAG}} ;\
if [ ! -z "$$VER" ]; then \
echo "$$VER" ;\
exit 0 ;\
fi ;\
echo Unknown ;\
}
# Print the value of the COMMIT_DATE variable if available, otherwise get commit
# date from the last git commit
commit-date:
@{\
set -e ;\
if [ ! -z "$$COMMIT_DATE" ]; then \
echo "$$COMMIT_DATE" ;\
exit 0 ;\
fi ;\
git log -1 --no-merges --format=%cI ;\
}
# Run unit tests on all packages
test:
go test -v ./...
# Update Go version in go.mod file, keep in mind that -go must contain a major
# and a minor version number (i.e. not the last one)
update:
go mod edit -go=$$(go version | sed -r 's/.*go([1-9]+\.[1-9]+).*/\1/')
go mod tidy
install:
install -Dm755 "$(OUT_FILE)" "$(PREFIX)/usr/bin/$(MODULE)"
install -Dm644 LICENSE -t "$(PREFIX)/usr/share/licenses/$(MODULE)/"
install -Dm644 build/package/$(MODULE).default "$(PREFIX)/etc/default/$(MODULE)"
install -Dm644 build/package/$(MODULE).service -t "$(PREFIX)/usr/lib/systemd/system/"
uninstall:
@{\
if [ -f "$(PREFIX)/usr/lib/systemd/system/$(MODULE).service" ]; then \
systemctl disable --now $(MODULE).service ;\
fi ;\
}
rm -f "$(PREFIX)/usr/lib/systemd/system/$(MODULE).service"
rm -f "$(PREFIX)/etc/default/$(MODULE)"
rm -rf "$(PREFIX)/usr/share/licenses/$(MODULE)/"
rm -f "$(PREFIX)/usr/bin/$(MODULE)"