Makefile: Include some custom LaTeX setup.
[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 ## LaTeX configuration.
102 LATEX_INITCMDS = \errorcontextlines=\maxdimen
103
104 ###--------------------------------------------------------------------------
105 ### Project specific definitions.
106
107 include Project.mk
108 DISTFILES += Project.mk
109
110 ###--------------------------------------------------------------------------
111 ### Parse the `docstrip' installation files.
112
113 parse-names = $(shell \
114 sed -n '/^\\mdwgen/,$$ s:^.*\\$1 *{\([^}]*\)}.*$$:\1:p' $2 | sort -u)
115
116 define parse-insfile
117 $1_DTX := $$(call parse-names,from,$1)
118 $1_OUT := $$(call parse-names,mdwf,$1)
119 endef
120 $(foreach i,$(INS), $(eval $(call parse-insfile,$i)))
121
122 DTX += $(sort $(foreach i,$(INS), $($i_DTX)))
123 OUT += $(sort $(foreach i,$(INS), $($i_OUT)))
124
125 ###--------------------------------------------------------------------------
126 ### Extract output files from the `doc' files.
127
128 all:: $(OUT)
129 $(foreach i,$(INS), $(eval $(call output-targets,$$(TEX),TEX,$i,$($i_DTX))))
130
131 DISTFILES += $(INS)
132 DISTFILES += $(sort $(DTX) $(EXTRA))
133 CLEANFILES += $(addsuffix .log,$(basename $(INS)))
134 DISTFILES += $(OUT)
135 REALCLEANFILES += $(OUT)
136
137 ###--------------------------------------------------------------------------
138 ### Build the documentation.
139
140 ## The main rune for running LaTeX.
141 ##
142 ## We run LaTeX in a subdirectory to prevent the temporary files (e.g., the
143 ## `.aux' files) from interfering with each other. The `\jobname' isn't
144 ## enough, because we might be building DVI and PDF versions of the same
145 ## document at the same time.
146 define run-latex
147 rm -rf t.$@/ && mkdir t.$@/ && cd t.$@/ && \
148 TEXINPUTS=..:$$TEXINPUTS && export TEXINPUTS && \
149 $1 "\def\indexing{n} \nonstopmode $(LATEX_INITCMDS) \input $<" $(v_null) && \
150 $1 "\def\indexing{y} \nonstopmode $(LATEX_INITCMDS) \input $<" $(v_null) && \
151 makeindex $(v_quiet) -s gind.ist $*.idx $(v_null) && \
152 $1 "\def\indexing{n} \nonstopmode $(LATEX_INITCMDS) \input $<" $(v_null) && \
153 mv $@ $*.log ../ && cd ../ && rm -rf t.$@/
154 endef
155 CLEANFILES += *.log
156 clean::; rm -rf t.*/
157
158 ## Good old-fashioned DVI.
159 DVI = $(addsuffix .dvi,$(basename $(DTX)))
160 pkgdoc_FILES += $(DVI)
161 all:: $(DVI)
162 %.dvi: %.dtx $(OUT) $(EXTRA)
163 $(call v_tag,DVILATEX,$@)$(call run-latex,$(DVILATEX))
164 %.dvi: %.tex $(OUT) $(EXTRA)
165 $(call v_tag,DVILATEX,$@)$(call run-latex,$(DVILATEX))
166 DISTFILES += $(DVI)
167 REALCLEANFILES += $(DVI)
168
169 ## Build PostScript from the DVI.
170 PS = $(addsuffix .ps,$(basename $(DTX)))
171 pkgdoc_FILES += $(PS)
172 all:: $(PS)
173 %.ps: %.dvi
174 $(call v_tag,DVIPS,$@)$(DVIPS) $(v_quiet) -o $@ $<
175 DISTFILES += $(PS)
176 REALCLEANFILES += $(PS)
177
178 ## Build PDF using PDFTeX.
179 PDF = $(addsuffix .pdf,$(basename $(DTX)))
180 pkgdoc_FILES += $(PDF)
181 all:: $(PDF)
182 %.pdf: %.dtx $(OUT) $(EXTRA)
183 $(call v_tag,PDFLATEX,$@)$(call run-latex,$(PDFLATEX))
184 %.pdf: %.tex $(OUT) $(EXTRA)
185 $(call v_tag,PDFLATEX,$@)$(call run-latex,$(PDFLATEX))
186 DISTFILES += $(PDF)
187 REALCLEANFILES += $(PDF)
188
189 ###--------------------------------------------------------------------------
190 ### Installation.
191
192 install-targets = $(foreach d,$(INSTALLDIRS), install/$d)
193 install:: $(install-targets)
194 $(install-targets): install/%: $$($$*_FILES)
195 $(v_at)mkdir -p $(DESTDIR)$($*dir)
196 $(call v_tag,INSTALL,$($*dir))install -m$(or $($*_FILEMODE),644) \
197 $($*_FILES) $(DESTDIR)$($*dir)
198
199 uninstall-targets = $(foreach d,$(INSTALLDIRS), uninstall/$d)
200 uninstall:: $(uninstall-targets)
201 $(uninstall-targets): uninstall/%: $$($$*_FILES)
202 $(call v_tag,UNINSTALL,$($*dir))rm -f \
203 $(addprefix $(DESTDIR)$($*dir)/,$($*_FILES))
204
205 ###--------------------------------------------------------------------------
206 ### Distribution.
207
208 DISTFILES += COPYING $(wildcard README)
209 DISTFILES += Makefile
210
211 DISTFILES += debian/control debian/rules
212 DISTFILES += debian/copyright debian/changelog
213 DISTFILES += debian/compat
214
215 distdir = $(PACKAGE)-$(VERSION)
216 tarball = $(distdir).tar.gz
217
218 distdir:: recreate-distdir $(DISTFILES)
219 $(v_at)set -e; \
220 for i in $(DISTFILES); do \
221 case $$i in */*) mkdir -p $(distdir)/$${i%/*} ;; esac; \
222 ln $$i $(distdir)/$$i; \
223 done
224
225 dist: distdir
226 $(call v_tag,TAR,$(tarball))tar chzf $(tarball) $(distdir)
227 $(v_at)rm -rf $(distdir)/
228 .PHONY: dist
229 CLEANFILES += $(tarball)
230 clean::; rm -rf $(distdir)/
231
232 recreate-distdir:
233 $(v_at)rm -rf $(distdir)/
234 $(v_at)mkdir $(distdir)
235 .PHONY: recreate-distdir
236
237 distcheck: dist
238 rm -rf _distcheck/
239 mkdir _distcheck/ && cd _distcheck/ && \
240 tar xzf ../$(tarball) && cd $(distdir)/ && \
241 $(MAKE) check && \
242 $(MAKE) install texmfdir=../_install && \
243 $(MAKE) realclean && \
244 $(MAKE) install texmfdir=../_install && \
245 $(MAKE) uninstall texmfdir=../_install && \
246 $(MAKE) install DESTDIR=../_root && \
247 $(MAKE) uninstall DESTDIR=../_root && \
248 cd ../../ && \
249 find _distcheck/_install/ _distcheck/_root ! -type d | \
250 diff -u - /dev/null && \
251 rm -rf _distcheck
252 .PHONY: distcheck
253 clean::; rm -rf _distcheck/
254
255 ###----- That's all, folks --------------------------------------------------