]> gitweb.michael.orlitzky.com - mjotex.git/blob - GNUmakefile
GNUmakefile: update the cleanup rules.
[mjotex.git] / GNUmakefile
1 #
2 # Example makefile using mjotex and a BibTeX references database.
3 #
4
5 # The latex compiler.
6 LATEX = pdflatex -file-line-error -halt-on-error
7
8 # The name of this document.
9 #
10 # For example, to use the name of our parent directory:
11 #
12 # PN = $(notdir $(realpath .))
13 #
14 PN = examples
15
16 # A space-separated list of bib files. These must all belong to paths
17 # contained in your $BIBINPUTS environment variable.
18 #
19 # Leave commented if you don't use a bibliography database.
20 #
21 BIBS = local-references.bib
22
23 # A space-separated list of the mjotex files that you use. The path to
24 # mjotex must be contain in your $TEXINPUTS environment variable.
25 #
26 # MJOTEX = mjotex.sty
27 #
28 MJOTEX = mjo-algebra.tex mjo-algorithm.tex mjo-arrow.tex mjo-calculus.tex
29 MJOTEX += mjo-common.tex mjo-complex.tex mjo-cone.tex mjo-convex.tex
30 MJOTEX += mjo-eja.tex mjo-font.tex mjo-linear_algebra.tex mjo-listing.tex
31 MJOTEX += mjo-misc.tex mjo-proof_by_cases.tex mjo-theorem.tex
32 MJOTEX += mjo-theorem-star.tex mjo-topology.tex mjo.bst
33
34 # Compile a list of raw source code listings (*.listing) and their
35 # associated output files (*.py) that will be tested by check-sage.
36 SAGE_LISTING_SRCS = $(wildcard sage_listings/*.listing)
37 SAGE_LISTING_DSTS = $(patsubst %.listing,%.py,$(SAGE_LISTING_SRCS))
38
39 # A space-separated list of indices (just their names). Usually you'll
40 # have just one, and it will be named the same thing as your document,
41 # because that's what the makeidx package does.
42 #
43 # Leave commented if you don't use an index.
44 #
45 INDICES = $(PN)
46
47 # Use kpsewhich (from the kpathsea suite) to find the absolute paths
48 # of the bibtex/mjotex files listed in in $(BIBS)/$(MJOTEX). The SRCS
49 # variable should contain all (Bib)TeX source files for the document.
50 SRCS = $(PN).tex
51 ifdef BIBS
52 BIBPATHS = $(shell kpsewhich $(BIBS))
53 SRCS += $(BIBPATHS)
54 endif
55 ifdef MJOTEX
56 MJOTEXPATHS = $(shell kpsewhich $(MJOTEX))
57 SRCS += $(MJOTEXPATHS)
58 endif
59 ifdef SAGE_LISTING_DSTS
60 SRCS += $(SAGE_LISTING_DSTS)
61 endif
62
63 ifdef INDICES
64 INDEX_SRCS = $(addsuffix .idx,$(INDICES))
65 INDEX_DSTS = $(addsuffix .ind,$(INDICES))
66 SRCS += $(INDEX_DSTS)
67 endif
68
69 # The first target is the default, so put the PDF document first.
70 #
71 # This voodoo is all designed to find a "fixed point" of calling
72 # $(LATEX). When you build a LaTeX document, it requires an unknown
73 # number of compilation passes. How do you know when to stop? Easy,
74 # stop when the output file stops changing! But how to encode that
75 # in a makefile?
76 #
77 # At the start of this target, we call $(LATEX) to compile $(PN).tex.
78 # If you ignore the "sed" for now, then the next step is to check for
79 # the existence of a "previous" file. If there isn't one, this is the
80 # first time that we've tried to build the PDF. In that case, take the
81 # PDF that we've just built and make *that* the previous file. Then
82 # start all over. If there is a previous file, then this is the second
83 # (or more) time that we've tried to build the PDF. We diff the PDF
84 # file that we've just built against the previous file; if they're the
85 # same, then we've succeeded and stop. Otherwise, we make the new PDF
86 # the previous file, and start all over. The end result is that we
87 # will loop until the newly-created PDF and the previous file are
88 # identical.
89 #
90 # But what about the "sed" call? By default, pdflatex will compile the
91 # creation date, modification date, and a unique ID into the output
92 # PDF. That means that two otherwise-identical documents, created
93 # seconds apart, will look different. We only need to know when the
94 # *contents* of the document are the same -- we don't care about the
95 # metadata -- so sed is used to remove those three nondeterministic
96 # pieces of information.
97 #
98 # The creation and modification dates should become optional in pdftex
99 # v1.40.17 thanks to Debian's SOURCE_DATE_EPOCH initiative. When that
100 # version of pdflatex makes it into TeX Live 2016, we can replace
101 # those two sed scripts with something smarter.
102 #
103 $(PN).pdf: $(SRCS) $(PN).bbl
104 $(LATEX) $(PN).tex
105
106 sed --in-place \
107 -e '/^\/ID \[<.*>\]/d' \
108 -e "s/^\/\(ModDate\) (.*)/\/\1 (D:19700101000000Z00'00')/" \
109 -e "s/^\/\(CreationDate\) (.*)/\/\\1 (D:19700101000000Z00'00')/" \
110 $@
111
112 if [ ! -f $@.previous ]; then \
113 mv $@ $@.previous; \
114 $(MAKE) $@; \
115 fi;
116
117 if cmp -s $@ $@.previous; then \
118 rm $@.previous; \
119 else \
120 mv $@ $@.previous; \
121 $(MAKE) $@; \
122 fi;
123
124
125 $(PN).aux: $(SRCS)
126 $(LATEX) $(PN).tex
127
128
129 ifdef INDICES
130 # We need to be able to build the index source files without involving
131 # the main $(PN).pdf rule, in order to avoid a chicken-and-egg problem.
132 # This is similar to the $(PN).aux rule above, except that an index is
133 # optional and there might be more than one of them.
134 $(INDEX_SRCS): $(PN).tex
135 $(LATEX) $(PN).tex
136 endif
137
138 ifdef INDICES
139 # Create real indices from source files by running "makeindex" on them.
140 %.ind: %.idx
141 makeindex $<
142 endif
143
144 # The pipe below indicates an "order-only dependency" on the aux file.
145 # Without it, every compilation of $(PN).tex would produce a new
146 # $(PN).aux, and thus $(PN).bbl would be rebuilt. This in turn causes
147 # $(PN).pdf to appear out-of-date, which leads to a recompilation of
148 # $(PN).tex... and so on. The order-only dependency means we won't
149 # rebuild $(PN).bbl if $(PN).aux changes.
150 #
151 # As a side effect, we now need to depend on $(SRCS) here, since we
152 # won't pick it up transitively from $(PN).aux.
153 #
154 # If the $BIBS variable is undefined, we presume that there are no
155 # references and create an empty bbl file. Otherwise, we risk trying
156 # to run biblatex on an aux file containing no citations. If you do
157 # define $BIBS but don't cite anything, you'll run into a similar
158 # problem. Don't do that.
159 #
160 $(PN).bbl: $(SRCS) | $(PN).aux
161 ifdef BIBS
162 bibtex $(PN).aux
163 else
164 echo -n '' > $@
165 endif
166
167 # If the output PDF exists but the log file does not, then an attempt
168 # to "build the log file" (i.e. build the PDF) would do nothing. Thus
169 # whenever the log file does not exist, we do a fresh build.
170 $(PN).log: $(SRCS)
171 $(MAKE) clean
172 $(MAKE)
173
174 # How do we convert a raw listing into something testable by sage? We
175 # append/prepend triple quotes to make the whole thing into a doctest.
176 sage_listings/%.py: sage_listings/%.listing
177 echo '"""' > $@ && cat $< >> $@ && echo '"""' >> $@
178
179 # Ensure that there are no overfull or underfull boxes in the output
180 # document by parsing the log for said warnings.
181 .PHONY: check-boxes
182 check-boxes: $(PN).log
183 @! grep -i 'overfull\|underfull' $<
184
185 # Run chktex to find silly mistakes. There is some exit code weirdness
186 # (Savannah bug 45979), so we just look for empty output.
187 .PHONY: check-chktex
188 CHKTEX = chktex --localrc .chktexrc --quiet --inputfiles=0
189 check-chktex:
190 @[ -z "$(shell $(CHKTEX) mjotex.sty)" ]
191
192 # Ensure that there are no undefined references in the document by
193 # parsing the log file for said warnings.
194 .PHONY: check-undefined
195 check-undefined: $(PN).log
196 @! grep -i 'undefined' $<
197
198 # Use sage to doctest any \sagelisting{}s in SAGE_LISTING_DSTS.
199 # The actuall command is ifdef'd so that we can comment out
200 # the definition of SAGE_LISTING_DSTS without breaking the
201 # default definition of the "check" target.
202 .PHONY: check-sage
203 check-sage: $(SAGE_LISTING_DSTS)
204 ifdef SAGE_LISTING_DSTS
205 PYTHONPATH="$(HOME)/src/sage.d" \
206 sage -t --timeout=0 --memlimit=0 \
207 $^
208 endif
209
210 # Run a suite of checks.
211 .PHONY: check
212 check: check-boxes check-chktex check-undefined check-sage
213
214 # Clean up leftover junk. This only looks overcomplicated because
215 # the *.{foo,bar} syntax supported by Bash is not POSIX, and Make
216 # will execute these commands using /bin/sh (which should be POSIX).
217 JUNK_EXTENSIONS = aux bbl bcf blg glo ilg ist listing lof log nav out pdf
218 JUNK_EXTENSIONS += snm spl toc xml
219 .PHONY: clean
220 clean:
221 for ext in $(JUNK_EXTENSIONS); do rm -f *.$$ext; done;
222 rm -rf dist/
223 rm -f $(SAGE_LISTING_DSTS) $(INDEX_SRCS) $(INDEX_DSTS)
224
225 # If this document will be published, the publisher isn't going to
226 # have your BibTeX database or your mjotex files. So, you need to
227 # package them up along with the code for your document. This target
228 # will create a "dist" directory and copy the necessary stuff there.
229 #
230 .PHONY: dist
231 dist: $(PN).bbl
232 mkdir -p dist
233 cp $(SRCS) $(PN).bbl $(BIBPATHS) $(MJOTEXPATHS) dist/