-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
272 lines (220 loc) · 8.56 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
########################################################################
# 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 clean
# Default shell: if we require GNU Make, why not require Bash?
SHELL := /bin/bash
# Argument(s) passed to the shell.
.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:
# Debug utility
print-%: ; @echo $* = $($*)
########################################################################
# Targets and variables
########################################################################
# The only "productive" target...
ManPages := jqt.1.gz sake.1.gz
# Dependencies from documentations files
DOCS := docs
CONTENT := $(DOCS)/content
########################################################################
# Install `jqt` dependencies.
#
# Warning: only with `dnf`!
# Modify this rule as a template to your own script.
########################################################################
.PHONY: setup
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!
########################################################################
# Install scripts and data
########################################################################
.PHONY: install uninstall
install: all
install --directory $(bindir) $(datadir) $(mandir)/man1 $(datadir)/$(PROJECT)/{sake.d,milligram}
install --verbose --compare --mode 555 bin/* $(bindir)
install --verbose --compare --mode 644 share/*.m $(datadir)/$(PROJECT)
install --verbose --compare --mode 644 $(ManPages) $(mandir)/man1
install --verbose --compare --mode 644 share/sake.d/*.* $(datadir)/$(PROJECT)/sake.d
install --verbose --compare --mode 644 share/milligram/*.* $(datadir)/$(PROJECT)/milligram
sed -i -e "s#DATADIR='.*'#DATADIR='$(datadir)'#" $(bindir)/jqt
sed -i -e "s#JQTLIB='.*'#JQTLIB='$(datadir)/jqt'#" $(bindir)/sake
uninstall:
rm --verbose --force -- $(addprefix $(prefix)/,$(wildcard bin/*))
rm --verbose --force -- $(addprefix $(mandir)/man1/,$(ManPages))
rm --verbose --force -- $(mandir)/man1/$(ManPages)
test -d $(datadir)/$(PROJECT) \
&& rm --verbose --force --recursive $(datadir)/$(PROJECT) \
|| true
########################################################################
# Show targets
########################################################################
.PHONY: list
list:
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 = $(prefix)'; \
echo ' bindir = $(bindir)'; \
echo ' datadir = $(datadir)'; \
echo ' mandir = $(mandir)'
########################################################################
# Generate man page for jqt
########################################################################
# gpp for the man page (to be built without calling jqt!)
GPP_MD := gpp \
-U '<%' '>' '\B' '\B' '\W>' '<' '>' '$$' '' \
-M '<%' '>' '\B' '\B' '\W>' '<' '>' \
+sccc '&\n' '' '' \
+sccc '<\#' '\#>' '' \
+siqi "'" "'" '\' \
+siQi '"' '"' '\' \
+ssss '<!--' '-->' '' \
+ssss '`' '`' '' \
+ssss '\n```' '\n```' '' \
+ssss '\n~~~' '\n~~~' '' \
-I${datadir}/jqt \
--nostdinc \
--include lib.md.m
# Man page: jqt(1) sake(1)
jqt.1.gz: $(CONTENT)/opt/*.txt
$(ManPages): %.1.gz : $(CONTENT)/%.1.text
$(info ==> $@)
@$(GPP_MD) -I$(DOCS) < $< \
| pandoc --standalone --from markdown --to man \
| gzip > $@
# Default target
all: $(ManPages)
# Add prerequisites and recipes to common targets
clean:: ; @rm -f $(ManPages)
########################################################################
# Tests
########################################################################
.PHONY: check
check: test-jqt test-expand # BUG!!!!!!!!! test-format
clean:: ; rm -f tests/*/generated/*
#
# 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