-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
431 lines (373 loc) · 12 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
##############################################################################
# Project configuration
PACKAGE := redact
CABAL_FILE := $(PACKAGE).cabal
PROJECT := $(PACKAGE)-haskell
EXECUTABLES := redact
MODE ?= stack
DESTDIR ?=
PREFIX ?= /usr/local
DEB_CONTAINER ?= extremais/pkg-debian-stack:bookworm
RPM_CONTAINER ?= extremais/pkg-fedora-stack:41
MAINTAINER_NAME ?= Travis Cardwell
MAINTAINER_EMAIL ?= [email protected]
TEST_DEB_CONTAINER ?= debian:bookworm
TEST_DEB_ARCH ?= amd64
TEST_RPM_CONTAINER ?= fedora:41
TEST_RPM_OS ?= fc41
TEST_RPM_ARCH ?= x86_64
##############################################################################
# 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 := build
ifeq ($(MODE), cabal)
GHC_VERSION ?= $(shell ghc --version | sed 's/.* //')
CABAL_ARGS := --with-ghc ghc-$(GHC_VERSION)
ifneq ($(origin PROJECT_FILE), undefined)
CABAL_ARGS += "--project-file=$(PROJECT_FILE)"
else
PROJECT_FILE_AUTO := cabal-$(GHC_VERSION).project
ifneq (,$(wildcard $(PROJECT_FILE_AUTO)))
CABAL_ARGS += "--project-file=$(PROJECT_FILE_AUTO)"
endif
endif
else ifeq ($(MODE), stack)
STACK_ARGS :=
ifneq ($(origin CONFIG), undefined)
STACK_ARGS += --stack-yaml "$(CONFIG)"
endif
ifneq ($(origin RESOLVER), undefined)
STACK_ARGS += --resolver "$(RESOLVER)"
endif
else
$(error unknown MODE: $(MODE))
endif
BINDIR := $(DESTDIR)$(PREFIX)/bin
DATAROOTDIR := $(DESTDIR)$(PREFIX)/share
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 '^version:' $(if $(origin 1) == undefined,$(CABAL_FILE),$(1)) \
| sed 's/^version: *//')
endef
define hs_files
find . -not -path '*/\.*' -type f -name '*.hs'
endef
define newline
endef
##############################################################################
# Rules
build: hr
build: # build package *
ifeq ($(MODE), cabal)
> cabal v2-build $(CABAL_ARGS) --enable-tests --enable-benchmarks
else
> stack build $(STACK_ARGS) --test --bench --no-run-tests --no-run-benchmarks
endif
.PHONY: build
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
ifeq ($(MODE), cabal)
> @rm -rf dist-newstyle
else
> @stack clean
endif
.PHONY: clean
clean-all: clean
clean-all: # clean package and remove artifacts
> @rm -rf .stack-work
> @rm -rf build
> @rm -rf dist-newstyle
> @rm -f *.yaml.lock
> @rm -f cabal.project.local
> @rm -f result*
.PHONY: clean-all
coverage: hr
coverage: # run tests with code coverage *
ifeq ($(MODE), cabal)
> cabal v2-test $(CABAL_ARGS) \
> --enable-coverage --enable-tests --test-show-details=always
else
> stack test $(STACK_ARGS) --coverage
> stack hpc report .
endif
.PHONY: coverage
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-api: hr
doc-api: # build API documentation *
ifeq ($(MODE), cabal)
> cabal v2-haddock $(CABAL_ARGS)
else
> stack haddock $(STACK_ARGS)
endif
.PHONY: doc-api
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
> @if command -v column >/dev/null 2>&1 \
> ; then \
> grep '^[a-zA-Z0-9_-]\+:[^#]*# ' $(MAKEFILE_LIST) \
> | sed 's/^\([^:]\+\):[^#]*# \(.*\)/make \1\t\2/' \
> | column -t -s $$'\t' \
> ; else \
> grep '^[a-zA-Z0-9_-]\+:[^#]*# ' $(MAKEFILE_LIST) \
> | sed 's/^\([^:]\+\):[^#]*# \(.*\)/make \1\t\2/' \
> ; fi
> @echo
> @echo "Cabal mode (MODE=cabal)"
> @echo " * Set GHC_VERSION to specify a GHC version."
> @echo " * Set PROJECT_FILE to specify a cabal.project file."
> @echo
> @echo "Stack mode (MODE=stack)"
> @echo " * Set CONFIG to specify a stack.yaml file."
> @echo " * Set RESOLVER to specify a Stack resolver."
.PHONY: help
hlint: # run hlint on all Haskell source
> @$(call hs_files) | xargs hlint
.PHONY: hlint
hr: #internal# display a horizontal rule
> @command -v hr >/dev/null 2>&1 && hr -t || true
.PHONY: hr
hsgrep: # grep all Haskell source for expression E
> $(eval E := "")
> @test -n "$(E)" || $(call die,"usage: make hsgrep E=expression")
> @$(call hs_files) | xargs grep -Hn '$(E)' || true
.PHONY: hsgrep
hsrecent: # show N most recently modified Haskell files
> $(eval N := "10")
> @find . -not -path '*/\.*' -type f -name '*.hs' -printf '%T+ %p\n' \
> | sort --reverse \
> | head -n $(N)
.PHONY: hsrecent
hssloc: # count lines of Haskell source
> @$(call hs_files) | xargs wc -l | tail -n 1 | sed 's/^ *\([0-9]*\).*$$/\1/'
.PHONY: hssloc
ignored: # list files ignored by git
> @git ls-files . --ignored --exclude-standard --others
.PHONY: ignored
install: install-bin
install: install-man
install: install-doc
install: # install everything (*)
.PHONY: install
install-bin: build
install-bin: # install executable(s) (*)
> @mkdir -p "$(BINDIR)"
ifeq ($(MODE), cabal)
> $(foreach EXE,$(EXECUTABLES), \
@install -m 0755 \
"$(shell cabal list-bin $(CABAL_ARGS) $(EXE))" \
"$(BINDIR)/$(EXE)" $(newline) \
)
else
> $(eval LIROOT := $(shell stack path --local-install-root))
> $(foreach EXE,$(EXECUTABLES), \
@install -m 0755 "$(LIROOT)/bin/$(EXE)" "$(BINDIR)/$(EXE)" $(newline) \
)
endif
.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(s)
> @mkdir -p "$(MAN1DIR)"
> $(foreach EXE,$(EXECUTABLES), \
@install -m 0644 "doc/$(EXE).1" "$(MAN1DIR)" $(newline) \
@gzip "$(MAN1DIR)/$(EXE).1" $(newline) \
)
.PHONY: install-man
man: # build man page
> $(eval VERSION := $(call get_version))
> $(eval DATE := $(shell date --rfc-3339=date))
> $(foreach EXE,$(EXECUTABLES), \
@pandoc -s -t man -o doc/$(EXE).1 \
--variable header="$(EXE) Manual" \
--variable footer="$(PROJECT) $(VERSION) ($(DATE))" \
doc/$(EXE).1.md $(newline) \
)
.PHONY: man
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
repl: # enter a REPL *
ifeq ($(MODE), cabal)
> cabal repl $(CABAL_ARGS)
else
> stack exec $(STACK_ARGS) ghci
endif
.PHONY: repl
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
sdist: # create source tarball for Hackage
> $(eval BRANCH := $(shell git rev-parse --abbrev-ref HEAD))
> @test "${BRANCH}" = "main" || $(call die,"not in main branch")
ifeq ($(MODE), cabal)
> @cabal sdist
else
> @stack sdist
endif
.PHONY: sdist
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: # run tests, optionally for pattern P *
> $(eval P := "")
ifeq ($(MODE), cabal)
> @test -z "$(P)" \
> && cabal v2-test $(CABAL_ARGS) --enable-tests --test-show-details=always \
> || cabal v2-test $(CABAL_ARGS) --enable-tests --test-show-details=always \
> --test-option '--pattern=$(P)'
else
> @test -z "$(P)" \
> && stack test $(STACK_ARGS) \
> || stack test $(STACK_ARGS) --test-arguments '--pattern $(P)'
endif
.PHONY: test
test-all: # run all configured tests using MODE
> @./test-all.sh "$(MODE)"
.PHONY: test-all
test-bounds-lower: # test lower bounds (Cabal only)
ifeq ($(MODE), stack)
> $(error test-bounds-lower not supported in Stack mode)
endif
> @make test-build CABAL_ARGS="--project-file=cabal-bounds-lower.project"
.PHONY: test-bounds-lower
test-bounds-upper: # test upper bounds (Cabal only)
ifeq ($(MODE), stack)
> $(error test-bounds-upper not supported in Stack mode)
endif
> @make test-build CABAL_ARGS="--project-file=cabal-bounds-upper.project"
.PHONY: test-bounds-upper
test-build: hr
test-build: build
test-build: test
test-build: doc-api
test-build: # build, run tests, build API documentation *
.PHONY: test-build
test-nightly: # run tests for the latest Stackage nightly release (Stack only)
ifeq ($(MODE), cabal)
> $(error test-nightly not supported in Cabal mode)
endif
> @make test RESOLVER=nightly
.PHONY: test-nightly
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 "$(PROJECT) $(call get_version)"
.PHONY: version