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