-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
239 lines (197 loc) · 7.67 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
########################################################################
# jqt management
########################################################################
# Parameters:
# prefix
# bindir
# datadir
# mandir
PROJECT := jqt
prefix ?= /usr/local
bindir ?= $(prefix)/bin
datadir ?= $(prefix)/share
mandir ?= $(prefix)/share/man
########################################################################
# Prerequisites
########################################################################
# We are using some of the newest GNU Make features... so require GNU
# Make version >= 3.82
version_test := $(filter 3.82,$(firstword $(sort $(MAKE_VERSION) 3.82)))
ifndef version_test
$(error GNU Make version $(MAKE_VERSION); version >= 3.82 is needed)
endif
ifeq (,$(filter install uninstall,$(MAKECMDGOALS)))
ifeq (0,$(shell id --user))
$(error Root only can make "(un)install" targets)
endif
else
ifneq (0,$(shell id --user))
$(error Only root can make "(un)install" targets)
endif
endif
########################################################################
# Configuration
########################################################################
# Disable builtins.
MAKEFLAGS += --no-builtin-rules
MAKEFLAGS += --no-builtin-variables
# Warn when an undefined variable is referenced.
MAKEFLAGS += --warn-undefined-variables
# Make will not print the recipe used to remake files.
.SILENT:
# Eliminate use of the built-in implicit rules. Also clear out the
# default list of suffixes for suffix rules.
.SUFFIXES:
# Sets the default goal to be used if no targets were specified on the
# command line.
.DEFAULT_GOAL := all
# When it is time to consider phony targets, make will run its recipe
# unconditionally, regardless of whether a file with that name exists or
# what its last-modification time is.
.PHONY: all
# When a target is built all lines of the recipe will be given to a
# single invocation of the shell.
# !!!Does not work in make <= 3.82 event it is documented!!!
#.ONESHELL:
# Default shell: if we require GNU Make, why not require Bash?
SHELL := /bin/bash
# The argument(s) passed to the shell are taken from the variable
# .SHELLFLAGS.
.SHELLFLAGS := -o errexit -o pipefail -o nounset -c
# Make will delete the target of a rule if it has changed and its recipe
# exits with a nonzero exit status.
.DELETE_ON_ERROR:
########################################################################
# Rules
########################################################################
# Warning: only `dnf`! Use this rule as template to your own script.
setup:
@rpm -q --quiet general-purpose-preprocessor || sudo dnf -y install general-purpose-preprocessor
@test -e /usr/bin/jq || test -e /usr/local/bin/jq || sudo dnf -y install jq
@rpm -q --quiet pandoc || sudo dnf -y install pandoc
@rpm -q --quiet python2-pyyaml || sudo dnf -y install python2-pyyaml
@echo Done!
# Default target
all: check
########################################################################
# Utilities
########################################################################
.PHONY: clean clobber install uninstall
clean::
rm -f tests/*/generated/* jqt.1.gz
clobber: clean
install:
[[ -e jqt.1.gz ]] || { cd docs && make ../jqt.1.gz; }
test -d $(bindir) || mkdir --verbose --parents $(bindir)
test -d $(datadir)/$(PROJECT) || mkdir --verbose --parents $(datadir)/$(PROJECT)
test -d $(mandir)/man1 || mkdir --verbose --parents $(mandir)/man1
install --verbose --compare --mode 555 bin/* $(bindir)
install --verbose --compare --mode 644 share/* $(datadir)/$(PROJECT)
install --verbose --compare --mode 644 jqt.1.gz $(mandir)/man1
sed -i -e "s#DATADIR='.*'#DATADIR='$(datadir)'#" $(bindir)/jqt
uninstall:
rm --verbose --force -- $(addprefix $(prefix)/,$(wildcard bin/*))
rm --verbose --force -- $(mandir)/man1/jqt.1.gz
test -d $(datadir)/$(PROJECT) \
&& rm --verbose --force --recursive $(datadir)/$(PROJECT) \
|| true
# Show targets
.PHONY: help
help:
echo 'Usage: make TARGET [parameter=value...]'
echo 'Targets:'; \
$(MAKE) --print-data-base --just-print 2>&1 \
| grep -v '^[mM]akefile' \
| awk '/^[^ \t.%][-A-Za-z0-9_]*:/ { print $$1 }' \
| sort --unique \
| sed 's/:\+$$//' \
| pr --omit-pagination --indent=4 --width=80 --columns=4
echo 'Default parameters:'; \
echo ' prefix = /usr/local'; \
echo ' bindir = /usr/local/bin'; \
echo ' datadir = /usr/local/share'; \
echo ' mandir = /usr/local/share/man'
########################################################################
# Generate help text
########################################################################
# Independent target: helps generating text for `jqt -h`
# Needs explicit call: `make /tmp/help`
/tmp/help: docs/content/help.text
$(info ==> $@)
jqt -P MarkDown -Idocs < $< \
| pandoc --from markdown --to plain - \
| sed '1,7b;/^$$/d;s/_\([A-Z]\+\)_/\1/g;/^[^A-Z]/s/^/ /' \
> $@
clean::
@rm -f /tmp/help
########################################################################
# Tests
########################################################################
.PHONY: check
check: test-jqt test-expand test-format
#
# Test JQT
#
.PHONY: test-jqt test-cond test-expr test-loop test-macros test-syntax
test-jqt: test-cond test-expr test-loop test-macros test-syntax
define TestJQT
# Run one example
test-$(1)-%.jqt:
echo "==> jqt: $$(subst test-,,$$(basename $$@))"
if test -e tests/jqt/$$(subst test-,,$$(basename $$@)).json; then \
jqt -ifilters -Ltests/jqt/filters -Mtop:tests/jqt/$$(subst test-,,$$(basename $$@)).json -w tests/jqt/$$(subst test-,,$$@) tests/jqt/generated/$$(subst test-,,$$(basename $$@)).txt; \
else \
jqt -ifilters -Ltests/jqt/filters -w tests/jqt/$$(subst test-,,$$@) tests/jqt/generated/$$(subst test-,,$$(basename $$@)).txt; \
fi
diff tests/jqt/expected/$$(subst test-,,$$(basename $$@)).txt tests/jqt/generated/$$(subst test-,,$$(basename $$@)).txt
# Run one example named without file suffix
test-$(1)-%: test-$(1)-%.jqt ;
# Run all tests
test-$(1): $(sort $(subst tests/jqt/,test-,$(wildcard tests/jqt/$(1)-[0-9][0-9].jqt)))
endef
$(eval $(call TestJQT,cond))
$(eval $(call TestJQT,expr))
$(eval $(call TestJQT,loop))
$(eval $(call TestJQT,macros))
$(eval $(call TestJQT,syntax))
#
# Test macro expansion
#
.PHONY: test-expand test-mpjqt test-mpmd test-mpjson test-mpcss
test-expand: test-mpjqt test-mpmd test-mpjson test-mpcss
define TestMacroExpand
# Run one example
test-$(2)-%.$(1):
echo "==> expand: $$(subst test-,,$$(basename $$@))"
jqt -P $(1) tests/expand/$$(subst test-,,$$@) > tests/expand/generated/$$(subst test-,,$$@)
diff tests/expand/expected/$$(subst test-,,$$@) tests/expand/generated/$$(subst test-,,$$@)
# Run one example named without file suffix
test-$(2)-%: test-$(2)-%.$(1) ;
# Run all tests
test-$(2): $(sort $(subst tests/expand/,test-,$(wildcard tests/expand/$(2)-[0-9][0-9].$(1))))
endef
$(eval $(call TestMacroExpand,jqt,mpjqt))
$(eval $(call TestMacroExpand,md,mpmd))
$(eval $(call TestMacroExpand,json,mpjson))
$(eval $(call TestMacroExpand,css,mpcss))
#
# Test file format conversions
#
.PHONY: test-format test-csv test-yaml
test-format: test-csv test-yaml
define TestFileFormat
# Run one example
test-$(1)-%.sh:
echo "==> format: $$(subst test-,,$$(basename $$@))"
$(SHELL) tests/format/$$(subst test-,,$$@)
# Run one example named without file suffix
test-$(1)-%: test-$(1)-%.sh ;
# Run all tests
test-$(1): $(sort $(subst tests/format/,test-,$(wildcard tests/format/$(1)-[0-9][0-9].sh)))
# Check output of all filters is empty for empty input
test -z "$$$$(for f in bin/$(1)2* bin/*2$(1); do echo | $$$$f; done)" \
|| { echo 1>&2 'EMPTY-FAILED'; false; }
endef
$(eval $(call TestFileFormat,csv))
$(eval $(call TestFileFormat,yaml))
# vim:ai:sw=8:ts=8:noet:syntax=make