Makefile: Include some custom LaTeX setup.
[mdwtools] / Makefile
CommitLineData
5598cad4
MW
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
14all::
15.PHONY: all
16
17install::
18.PHONY: install
19
20uninstall::
21.PHONY: uninstall
22
23check::
24.PHONY: check
25
26clean::; rm -f $(CLEANFILES)
27.PHONY: clean
28
29realclean:: clean; rm -f $(REALCLEANFILES)
30.PHONY: clean
31
32distdir:: recreate-distdir
33.PHONY: distdir
34
35###--------------------------------------------------------------------------
36### Project identification.
37
38VERSION := $(shell ./auto-version)
39DISTFILES += auto-version
40
41distdir:: recreate-distdir
42 $(v_at)echo "$(VERSION)" >$(distdir)/RELEASE
43
44###--------------------------------------------------------------------------
45### Tools.
46
47TEX = tex
48DVILATEX = latex
49PDFLATEX = pdflatex
50DVIPS = dvips
51
52###--------------------------------------------------------------------------
53### Installation directories.
54
55## Determine a sensible TeX base directory.
56prefix = /usr/local
57texmfdir := $(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
63ifeq ($(texmfdir),/invalid/)
64 $(error "Failed to choose a TeX installation directory.")
65endif
66
67INSTALLDIRS += pkglatex
68pkglatexdir = $(texmfdir)/tex/latex/$(PACKAGE)
69
70INSTALLDIRS += pkgdoc
71pkgdocdir = $(texmfdir)/doc/latex/$(PACKAGE)
72
73###--------------------------------------------------------------------------
74### Preliminary definitions.
75
76.SECONDEXPANSION: # sorry
77
78## Silent-rules.
79V = 0
80
81v_at = $(v_at_$V)
82v_at_0 = @
83
84v_tag = $(call v_tag_$V,$1,$2)
85v_tag_0 = @printf " %-8s %s\n" "$1" "$2";
86
87v_null = $(v_null_$V)
88v_null_0 = >/dev/null
89
90v_quiet = $(v_quiet_$V)
91v_quiet_0 = -q
92
93## Runes for building rules.
94define 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))
99endef
100
9cb5c64c
MW
101## LaTeX configuration.
102LATEX_INITCMDS = \errorcontextlines=\maxdimen
103
5598cad4
MW
104###--------------------------------------------------------------------------
105### Project specific definitions.
106
107include Project.mk
108DISTFILES += Project.mk
109
110###--------------------------------------------------------------------------
111### Parse the `docstrip' installation files.
112
113parse-names = $(shell \
114 sed -n '/^\\mdwgen/,$$ s:^.*\\$1 *{\([^}]*\)}.*$$:\1:p' $2 | sort -u)
115
116define parse-insfile
117$1_DTX := $$(call parse-names,from,$1)
118$1_OUT := $$(call parse-names,mdwf,$1)
119endef
120$(foreach i,$(INS), $(eval $(call parse-insfile,$i)))
121
122DTX += $(sort $(foreach i,$(INS), $($i_DTX)))
123OUT += $(sort $(foreach i,$(INS), $($i_OUT)))
124
125###--------------------------------------------------------------------------
126### Extract output files from the `doc' files.
127
128all:: $(OUT)
129$(foreach i,$(INS), $(eval $(call output-targets,$$(TEX),TEX,$i,$($i_DTX))))
130
131DISTFILES += $(INS)
132DISTFILES += $(sort $(DTX) $(EXTRA))
133CLEANFILES += $(addsuffix .log,$(basename $(INS)))
134DISTFILES += $(OUT)
135REALCLEANFILES += $(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.
54b70d06
MW
146define run-latex
147rm -rf t.$@/ && mkdir t.$@/ && cd t.$@/ && \
148TEXINPUTS=..:$$TEXINPUTS && export TEXINPUTS && \
9cb5c64c
MW
149$1 "\def\indexing{n} \nonstopmode $(LATEX_INITCMDS) \input $<" $(v_null) && \
150$1 "\def\indexing{y} \nonstopmode $(LATEX_INITCMDS) \input $<" $(v_null) && \
54b70d06 151makeindex $(v_quiet) -s gind.ist $*.idx $(v_null) && \
9cb5c64c 152$1 "\def\indexing{n} \nonstopmode $(LATEX_INITCMDS) \input $<" $(v_null) && \
54b70d06
MW
153mv $@ $*.log ../ && cd ../ && rm -rf t.$@/
154endef
3f40da71 155CLEANFILES += *.log
5598cad4
MW
156clean::; rm -rf t.*/
157
158## Good old-fashioned DVI.
159DVI = $(addsuffix .dvi,$(basename $(DTX)))
160pkgdoc_FILES += $(DVI)
161all:: $(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))
166DISTFILES += $(DVI)
167REALCLEANFILES += $(DVI)
168
169## Build PostScript from the DVI.
170PS = $(addsuffix .ps,$(basename $(DTX)))
171pkgdoc_FILES += $(PS)
172all:: $(PS)
173%.ps: %.dvi
174 $(call v_tag,DVIPS,$@)$(DVIPS) $(v_quiet) -o $@ $<
175DISTFILES += $(PS)
176REALCLEANFILES += $(PS)
177
178## Build PDF using PDFTeX.
179PDF = $(addsuffix .pdf,$(basename $(DTX)))
180pkgdoc_FILES += $(PDF)
181all:: $(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))
186DISTFILES += $(PDF)
187REALCLEANFILES += $(PDF)
188
189###--------------------------------------------------------------------------
190### Installation.
191
192install-targets = $(foreach d,$(INSTALLDIRS), install/$d)
193install:: $(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
199uninstall-targets = $(foreach d,$(INSTALLDIRS), uninstall/$d)
200uninstall:: $(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
208DISTFILES += COPYING $(wildcard README)
209DISTFILES += Makefile
210
211DISTFILES += debian/control debian/rules
212DISTFILES += debian/copyright debian/changelog
213DISTFILES += debian/compat
214
215distdir = $(PACKAGE)-$(VERSION)
216tarball = $(distdir).tar.gz
217
218distdir:: 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
225dist: distdir
226 $(call v_tag,TAR,$(tarball))tar chzf $(tarball) $(distdir)
227 $(v_at)rm -rf $(distdir)/
228.PHONY: dist
229CLEANFILES += $(tarball)
230clean::; rm -rf $(distdir)/
231
232recreate-distdir:
233 $(v_at)rm -rf $(distdir)/
234 $(v_at)mkdir $(distdir)
235.PHONY: recreate-distdir
236
237distcheck: 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
253clean::; rm -rf _distcheck/
254
255###----- That's all, folks --------------------------------------------------