-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
342 lines (296 loc) · 10.3 KB
/
Makefile
File metadata and controls
342 lines (296 loc) · 10.3 KB
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
##############################################################################
# Project configuration
PROJECT := base
TEST_CONTAINER := extremais/basetest
DESTDIR ?=
PREFIX ?= /usr/local
DEB_CONTAINER ?= extremais/pkg-debian:bullseye
RPM_CONTAINER ?= extremais/pkg-fedora:34
MAINTAINER_NAME ?= Travis Cardwell
MAINTAINER_EMAIL ?= travis.cardwell@extrema.is
TEST_DEB_CONTAINER ?= debian:bullseye
TEST_DEB_ARCH ?= all
TEST_RPM_CONTAINER ?= fedora:34
TEST_RPM_OS ?= fc34
TEST_RPM_ARCH ?= noarch
##############################################################################
# Make configuration
ifeq ($(origin .RECIPEPREFIX), undefined)
$(error GNU Make 4.0 or later required)
endif
.RECIPEPREFIX := >
SHELL := bash
.SHELLFLAGS := -o nounset -o errexit -o pipefail -c
MAKEFLAGS += --no-builtin-rules
MAKEFLAGS += --warn-undefined-variables
.DEFAULT_GOAL := help
BINDIR := $(DESTDIR)$(PREFIX)/bin
DATAROOTDIR := $(DESTDIR)$(PREFIX)/share
SHAREDIR := $(DATAROOTDIR)/$(PROJECT)
DOCDIR := $(DATAROOTDIR)/doc/$(PROJECT)
MAN1DIR := $(DATAROOTDIR)/man/man1
##############################################################################
# Functions
define all_files
find . -not -path '*/\.*' -type f
endef
define checksum_files
find . -maxdepth 1 -type f -not -path './*SUMS' | sed 's,^\./,,' | sort
endef
define die
(echo "error: $(1)" ; false)
endef
define get_version
$(shell grep ^BASE_VERSION $(if $(origin 1) == undefined,base.sh,$(1)) \
| sed -e 's/^[^"]*"//' -e 's/"$$//')
endef
##############################################################################
# Rules
checksums: # calculate checksums of build artifacts
> @cd build && $(call checksum_files) | xargs md5sum > MD5SUMS
> @cd build && $(call checksum_files) | xargs sha1sum > SHA1SUMS
> @cd build && $(call checksum_files) | xargs sha256sum > SHA256SUMS
> @cd build && $(call checksum_files) | xargs sha512sum > SHA512SUMS
.PHONY: checksums
clean: # clean package
.PHONY: clean
clean-all: clean
clean-all: # clean package and remove artifacts
> @rm -rf build
.PHONY: clean-all
deb: # build .deb package for VERSION in a Debian container
> $(eval VERSION := $(call get_version))
> $(eval SRC := $(PROJECT)-$(VERSION).tar.xz)
> @test -f build/$(SRC) || $(call die,"build/$(SRC) not found")
> @docker run --rm -it \
> -e DEBFULLNAME="$(MAINTAINER_NAME)" \
> -e DEBEMAIL="$(MAINTAINER_EMAIL)" \
> -v $(PWD)/build:/host \
> $(DEB_CONTAINER) \
> /home/docker/bin/make-deb.sh "$(SRC)"
.PHONY: deb
deb-test: # run a Debian container to test .deb package for VERSION
> $(eval VERSION := $(call get_version))
> $(eval PKG := "$(PROJECT)_$(VERSION)-1_$(TEST_DEB_ARCH).deb")
> @test -f build/$(PKG) || $(call die,"build/$(PKG) not found")
> @docker run --rm -it \
> -v $(PWD)/build/$(PKG):/tmp/$(PKG):ro \
> $(TEST_DEB_CONTAINER) \
> /bin/bash
.PHONY: deb-test
doc: # build script documentation
> $(eval VERSION := $(call get_version))
> @mkdir -p build
> @literatex --ignore-shebang \
> --input base.sh --output build/base-$(VERSION).md
> @pandoc \
> --from markdown --to html5 --standalone \
> --metadata title="base $(VERSION)" \
> --output build/base-$(VERSION).html \
> build/base-$(VERSION).md
> @literatex --ignore-shebang \
> --input base_activate.sh --output build/base_activate-$(VERSION).md
> @pandoc \
> --from markdown --to html5 --standalone \
> --metadata title="base $(VERSION)" \
> --output build/base_activate-$(VERSION).html \
> build/base_activate-$(VERSION).md
.PHONY: doc
grep: # grep all non-hidden files for expression E
> $(eval E:= "")
> @test -n "$(E)" || $(call die,"usage: make grep E=expression")
> @$(call all_files) | xargs grep -Hn '$(E)' || true
.PHONY: grep
help: # show this help
> @grep '^[a-zA-Z0-9_-]\+:[^#]*# ' $(MAKEFILE_LIST) \
> | sed 's/^\([^:]\+\):[^#]*# \(.*\)/make \1\t\2/' \
> | column -t -s $$'\t'
.PHONY: help
hr: #internal# display a horizontal rule
> @command -v hr >/dev/null 2>&1 && hr -t || true
.PHONY: hr
ignored: # list files ignored by git
> @git ls-files . --ignored --exclude-standard --others
.PHONY: ignored
install: install-bin
install: install-share
install: install-man
install: install-doc
install: # install everything
.PHONY: install
install-bin: # install scripts
> @mkdir -p "$(BINDIR)"
> @install -m 0755 base.sh "$(BINDIR)/base"
> @install -m 0755 base_activate.sh "$(BINDIR)/base_activate"
.PHONY: install-bin
install-doc: # install documentation
> @mkdir -p "$(DOCDIR)"
> @install -m 0644 README.md "$(DOCDIR)"
> @gzip "$(DOCDIR)/README.md"
> @install -m 0644 -T CHANGELOG.md "$(DOCDIR)/changelog"
> @gzip "$(DOCDIR)/changelog"
> @install -m 0644 LICENSE "$(DOCDIR)"
> @gzip "$(DOCDIR)/LICENSE"
.PHONY: install-doc
install-man: # install man page
> @mkdir -p "$(MAN1DIR)"
> @install -m 0644 doc/base.1 "$(MAN1DIR)"
> @gzip "$(MAN1DIR)/base.1"
.PHONY: install-man
install-share: # install share scripts
> @mkdir -p "$(SHAREDIR)"
> @install -m 0644 share/* "$(SHAREDIR)"
.PHONY: install-share
lint: hr
lint: shellcheck
lint: pycodestyle
lint: pylint
lint: # run shellcheck, pycodestyle, and pylint
.PHONY: lint
man: # build man page
> $(eval VERSION := $(call get_version))
> $(eval DATE := $(shell date --rfc-3339=date))
> @pandoc -s -t man -o doc/base.1 \
> --variable header="Base Manual" \
> --variable footer="Base $(VERSION) ($(DATE))" \
> doc/base.1.md
.PHONY: man
pycodestyle: hr
pycodestyle: # run pycodestyle on basetest.py
> @if command -v pycodestyle >/dev/null 2>&1; \
> then pycodestyle test/basetest.py; \
> else echo "WARNING: pycodestyle not found; skipping"; \
> fi
.PHONY: pycodestyle
pylint: hr
pylint: # run pylint on basetest.py
> @if command -v pylint >/dev/null 2>&1; \
> then pylint test/basetest.py; \
> else echo "WARNING: pylint not found; skipping"; \
> fi
.PHONY: pylint
recent: # show N most recently modified files
> $(eval N := "10")
> @find . -not -path '*/\.*' -type f -printf '%T+ %p\n' \
> | sort --reverse \
> | head -n $(N)
.PHONY: recent
rpm: # build .rpm package for VERSION in a Fedora container
> $(eval VERSION := $(call get_version))
> $(eval SRC := $(PROJECT)-$(VERSION).tar.xz)
> @test -f build/$(SRC) || $(call die,"build/$(SRC) not found")
> @docker run --rm -it \
> -e RPMFULLNAME="$(MAINTAINER_NAME)" \
> -e RPMEMAIL="$(MAINTAINER_EMAIL)" \
> -v $(PWD)/build:/host \
> $(RPM_CONTAINER) \
> /home/docker/bin/make-rpm.sh "$(SRC)"
.PHONY: rpm
rpm-test: # run a Fedora container to test .rpm package for VERSION
> $(eval VERSION := $(call get_version))
> $(eval PKG := "$(PROJECT)-$(VERSION)-1.$(TEST_RPM_OS).$(TEST_RPM_ARCH).rpm")
> @test -f build/$(PKG) || $(call die,"build/$(PKG) not found")
> @docker run --rm -it \
> -v $(PWD)/build/$(PKG):/tmp/$(PKG):ro \
> $(TEST_RPM_CONTAINER) \
> /bin/bash
.PHONY: rpm-test
shellcheck: hr
shellcheck: # run shellcheck on all shell scripts
> @if command -v shellcheck >/dev/null 2>&1; \
> then find . -type f -name '*.sh' | xargs shellcheck; \
> else echo "WARNING: shellcheck not found; skipping"; \
> fi
.PHONY: shellcheck
source-git: # create source tarball of git TREE
> $(eval TREE := "HEAD")
> $(eval BRANCH := $(shell git rev-parse --abbrev-ref $(TREE)))
> @test "$(BRANCH)" = "main" || echo "WARNING: Not in main branch!" >&2
> $(eval DIRTY := $(shell git diff --shortstat | wc -l))
> @test "$(DIRTY)" = "0" \
> || echo "WARNING: Not including non-committed changes!" >&2
> $(eval UNTRACKED := $(shell \
git ls-files --other --directory --no-empty-directory --exclude-standard \
| wc -l))
> @test "$(UNTRACKED)" = "0" \
> || echo "WARNING: Not including untracked files!" >&2
> $(eval VERSION := $(call get_version))
> @mkdir -p build
> @git archive --format=tar --prefix=$(PROJECT)-$(VERSION)/ $(TREE) \
> | xz \
> > build/$(PROJECT)-$(VERSION).tar.xz
.PHONY: source-git
source-tar: # create source tarball using tar
> $(eval DIRTY := $(shell git diff --shortstat | wc -l))
> @test "$(DIRTY)" = "0" \
> || echo "WARNING: Including non-committed changes!" >&2
> $(eval UNTRACKED := $(shell \
git ls-files --other --directory --no-empty-directory --exclude-standard \
| wc -l))
> @test "$(UNTRACKED)" = "0" \
> || echo "WARNING: Including untracked files!" >&2
> $(eval VERSION := $(call get_version))
> @mkdir -p build
> @sed -e 's,^/,./,' -e 's,/$$,,' .gitignore > build/.gitignore
> @tar \
> --exclude-vcs \
> --exclude-ignore-recursive=build/.gitignore \
> --transform "s,^\.,$(PROJECT)-$(VERSION)," \
> -Jcf build/$(PROJECT)-$(VERSION).tar.xz \
> .
> @rm -f build/.gitignore
.PHONY: source-tar
test: hr
test: test-image
test: # run all tests or test T in test container
> $(eval T := "")
> @test -z "$(T)" \
> && docker run --rm -it \
> --hostname "basetest" \
> -v "$(PWD)/base.sh:/usr/bin/base:ro" \
> -v "$(PWD)/base_activate.sh:/usr/bin/base_activate:ro" \
> -v "$(PWD)/share:/usr/share/base:ro" \
> -v "$(PWD)/test/basetest.py:/home/docker/basetest:ro" \
> "$(TEST_CONTAINER):latest" \
> /home/docker/basetest \
> || docker run --rm -it \
> --hostname "basetest" \
> -v "$(PWD)/base.sh:/usr/bin/base:ro" \
> -v "$(PWD)/base_activate.sh:/usr/bin/base_activate:ro" \
> -v "$(PWD)/share:/usr/share/base:ro" \
> -v "$(PWD)/test/basetest.py:/home/docker/basetest:ro" \
> "$(TEST_CONTAINER):latest" \
> /home/docker/basetest "TestBase.$(T)"
.PHONY: test
test-image: # build test image
> $(eval EXISTS := $(shell docker images --quiet $(TEST_CONTAINER):latest))
> @test -n "$(EXISTS)" || docker build \
> --build-arg "TERM=${TERM}" \
> --tag "$(TEST_CONTAINER):latest" \
> test
.PHONY: test-image
test-shell: test-image
test-shell: # run shell in test container
> @docker run --rm -it \
> --hostname "basetest" \
> -v "$(PWD)/base.sh:/usr/bin/base:ro" \
> -v "$(PWD)/base_activate.sh:/usr/bin/base_activate:ro" \
> -v "$(PWD)/share:/usr/share/base:ro" \
> -v "$(PWD)/test/basetest.py:/home/docker/basetest:ro" \
> "$(TEST_CONTAINER):latest" \
> /bin/bash
.PHONY: test-shell
todo: # search for TODO items
> @find . -type f \
> -not -path '*/\.*' \
> -not -path './build/*' \
> -not -path './project/*' \
> -not -path ./Makefile \
> | xargs grep -Hn TODO \
> | grep -v '^Binary file ' \
> || true
.PHONY: todo
version: # show current version
> @echo "base.sh $(call get_version, base.sh)"
> @echo "base_activate.sh $(call get_version, base_activate.sh)"
.PHONY: version