f56e5fd28a72d574b5b7ba4d15a109ab5b256624
[mdwtools] / Makefile
1 ### -*-makefile-*-
2 ###
3 ### Build script for TeX packages
4 ###
5 ### (c) 2020 Mark Wooding
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10
11 ###--------------------------------------------------------------------------
12 ### Extension points.
13
14 all::
15 .PHONY: all
16
17 install::
18 .PHONY: install
19
20 uninstall::
21 .PHONY: uninstall
22
23 check::
24 .PHONY: check
25
26 clean::; rm -f $(CLEANFILES)
27 .PHONY: clean
28
29 realclean:: clean; rm -f $(REALCLEANFILES)
30 .PHONY: clean
31
32 distdir:: recreate-distdir
33 .PHONY: distdir
34
35 ###--------------------------------------------------------------------------
36 ### Project identification.
37
38 VERSION := $(shell ./auto-version)
39 DISTFILES += auto-version
40
41 distdir:: recreate-distdir
42 $(v_at)echo "$(VERSION)" >$(distdir)/RELEASE
43
44 ###--------------------------------------------------------------------------
45 ### Tools.
46
47 TEX = tex
48 DVILATEX = latex
49 PDFLATEX = pdflatex
50 DVIPS = dvips
51
52 ###--------------------------------------------------------------------------
53 ### Installation directories.
54
55 ## Determine a sensible TeX base directory.
56 prefix = /usr/local
57 texmfdir := $(shell \
58 for d in $(prefix)/share/texmf $(prefix)/lib/texmf $(prefix)/texmf; do \
59 if [ -d $$d ]; then echo $$d; exit 0; fi; \
60 done; \
61 echo /invalid/)
62
63 ifeq ($(texmfdir),/invalid/)
64 $(error "Failed to choose a TeX installation directory.")
65 endif
66
67 INSTALLDIRS += pkglatex
68 pkglatexdir = $(texmfdir)/tex/latex/$(PACKAGE)
69
70 INSTALLDIRS += pkgdoc
71 pkgdocdir = $(texmfdir)/doc/latex/$(PACKAGE)
72
73 ###--------------------------------------------------------------------------
74 ### Preliminary definitions.
75
76 .SECONDEXPANSION: # sorry
77
78 ## Silent-rules.
79 V = 0
80
81 v_at = $(v_at_$V)
82 v_at_0 = @
83
84 v_tag = $(call v_tag_$V,$1,$2)
85 v_tag_0 = @printf " %-8s %s\n" "$1" "$2";
86
87 v_null = $(v_null_$V)
88 v_null_0 = >/dev/null
89
90 v_quiet = $(v_quiet_$V)
91 v_quiet_0 = -q
92
93 ## Runes for building rules.
94 define output-targets
95 $$(firstword $$($3_OUT)): $3 $4
96 $$(call v_tag,$2,$$<)$1 $$< $$(v_null)
97 $$(wordlist 2,$$(words $$($3_OUT)),$$($3_OUT)): \
98 $$(firstword $$($3_OUT))
99 endef
100
101 ###--------------------------------------------------------------------------
102 ### Project specific definitions.
103
104 include Project.mk
105 DISTFILES += Project.mk
106
107 ###--------------------------------------------------------------------------
108 ### Parse the `docstrip' installation files.
109
110 parse-names = $(shell \
111 sed -n '/^\\mdwgen/,$$ s:^.*\\$1 *{\([^}]*\)}.*$$:\1:p' $2 | sort -u)
112
113 define parse-insfile
114 $1_DTX := $$(call parse-names,from,$1)
115 $1_OUT := $$(call parse-names,mdwf,$1)
116 endef
117 $(foreach i,$(INS), $(eval $(call parse-insfile,$i)))
118
119 DTX += $(sort $(foreach i,$(INS), $($i_DTX)))
120 OUT += $(sort $(foreach i,$(INS), $($i_OUT)))
121
122 ###--------------------------------------------------------------------------
123 ### Extract output files from the `doc' files.
124
125 all:: $(OUT)
126 $(foreach i,$(INS), $(eval $(call output-targets,$$(TEX),TEX,$i,$($i_DTX))))
127
128 DISTFILES += $(INS)
129 DISTFILES += $(sort $(DTX) $(EXTRA))
130 CLEANFILES += $(addsuffix .log,$(basename $(INS)))
131 DISTFILES += $(OUT)
132 REALCLEANFILES += $(OUT)
133
134 ###--------------------------------------------------------------------------
135 ### Build the documentation.
136
137 ## The main rune for running LaTeX.
138 ##
139 ## We run LaTeX in a subdirectory to prevent the temporary files (e.g., the
140 ## `.aux' files) from interfering with each other. The `\jobname' isn't
141 ## enough, because we might be building DVI and PDF versions of the same
142 ## document at the same time.
143 define run-latex
144 rm -rf t.$@/ && mkdir t.$@/ && cd t.$@/ && \
145 TEXINPUTS=..:$$TEXINPUTS && export TEXINPUTS && \
146 $1 "\def\indexing{n} \nonstopmode \input $<" $(v_null) && \
147 $1 "\def\indexing{y} \nonstopmode \input $<" $(v_null) && \
148 makeindex $(v_quiet) -s gind.ist $*.idx $(v_null) && \
149 $1 "\def\indexing{n} \nonstopmode \input $<" $(v_null) && \
150 mv $@ $*.log ../ && cd ../ && rm -rf t.$@/
151 endef
152 clean::; rm -rf t.*/
153
154 ## Good old-fashioned DVI.
155 DVI = $(addsuffix .dvi,$(basename $(DTX)))
156 pkgdoc_FILES += $(DVI)
157 all:: $(DVI)
158 %.dvi: %.dtx $(OUT) $(EXTRA)
159 $(call v_tag,DVILATEX,$@)$(call run-latex,$(DVILATEX))
160 %.dvi: %.tex $(OUT) $(EXTRA)
161 $(call v_tag,DVILATEX,$@)$(call run-latex,$(DVILATEX))
162 DISTFILES += $(DVI)
163 REALCLEANFILES += $(DVI)
164
165 ## Build PostScript from the DVI.
166 PS = $(addsuffix .ps,$(basename $(DTX)))
167 pkgdoc_FILES += $(PS)
168 all:: $(PS)
169 %.ps: %.dvi
170 $(call v_tag,DVIPS,$@)$(DVIPS) $(v_quiet) -o $@ $<
171 DISTFILES += $(PS)
172 REALCLEANFILES += $(PS)
173
174 ## Build PDF using PDFTeX.
175 PDF = $(addsuffix .pdf,$(basename $(DTX)))
176 pkgdoc_FILES += $(PDF)
177 all:: $(PDF)
178 %.pdf: %.dtx $(OUT) $(EXTRA)
179 $(call v_tag,PDFLATEX,$@)$(call run-latex,$(PDFLATEX))
180 %.pdf: %.tex $(OUT) $(EXTRA)
181 $(call v_tag,PDFLATEX,$@)$(call run-latex,$(PDFLATEX))
182 DISTFILES += $(PDF)
183 REALCLEANFILES += $(PDF)
184
185 ###--------------------------------------------------------------------------
186 ### Installation.
187
188 install-targets = $(foreach d,$(INSTALLDIRS), install/$d)
189 install:: $(install-targets)
190 $(install-targets): install/%: $$($$*_FILES)
191 $(v_at)mkdir -p $(DESTDIR)$($*dir)
192 $(call v_tag,INSTALL,$($*dir))install -m$(or $($*_FILEMODE),644) \
193 $($*_FILES) $(DESTDIR)$($*dir)
194
195 uninstall-targets = $(foreach d,$(INSTALLDIRS), uninstall/$d)
196 uninstall:: $(uninstall-targets)
197 $(uninstall-targets): uninstall/%: $$($$*_FILES)
198 $(call v_tag,UNINSTALL,$($*dir))rm -f \
199 $(addprefix $(DESTDIR)$($*dir)/,$($*_FILES))
200
201 ###--------------------------------------------------------------------------
202 ### Distribution.
203
204 DISTFILES += COPYING $(wildcard README)
205 DISTFILES += Makefile
206
207 DISTFILES += debian/control debian/rules
208 DISTFILES += debian/copyright debian/changelog
209 DISTFILES += debian/compat
210
211 distdir = $(PACKAGE)-$(VERSION)
212 tarball = $(distdir).tar.gz
213
214 distdir:: recreate-distdir $(DISTFILES)
215 $(v_at)set -e; \
216 for i in $(DISTFILES); do \
217 case $$i in */*) mkdir -p $(distdir)/$${i%/*} ;; esac; \
218 ln $$i $(distdir)/$$i; \
219 done
220
221 dist: distdir
222 $(call v_tag,TAR,$(tarball))tar chzf $(tarball) $(distdir)
223 $(v_at)rm -rf $(distdir)/
224 .PHONY: dist
225 CLEANFILES += $(tarball)
226 clean::; rm -rf $(distdir)/
227
228 recreate-distdir:
229 $(v_at)rm -rf $(distdir)/
230 $(v_at)mkdir $(distdir)
231 .PHONY: recreate-distdir
232
233 distcheck: dist
234 rm -rf _distcheck/
235 mkdir _distcheck/ && cd _distcheck/ && \
236 tar xzf ../$(tarball) && cd $(distdir)/ && \
237 $(MAKE) check && \
238 $(MAKE) install texmfdir=../_install && \
239 $(MAKE) realclean && \
240 $(MAKE) install texmfdir=../_install && \
241 $(MAKE) uninstall texmfdir=../_install && \
242 $(MAKE) install DESTDIR=../_root && \
243 $(MAKE) uninstall DESTDIR=../_root && \
244 cd ../../ && \
245 find _distcheck/_install/ _distcheck/_root ! -type d | \
246 diff -u - /dev/null && \
247 rm -rf _distcheck
248 .PHONY: distcheck
249 clean::; rm -rf _distcheck/
250
251 ###----- That's all, folks --------------------------------------------------