]> gitweb.michael.orlitzky.com - mjotex.git/blob - GNUmakefile
GNUmakefile: update a bug reference in a comment.
[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 endif
67
68 # The first target is the default, so put the PDF document first.
69 #
70 # This voodoo is all designed to find a "fixed point" of calling
71 # $(LATEX). When you build a LaTeX document, it requires an unknown
72 # number of compilation passes. How do you know when to stop? Easy,
73 # stop when the output file stops changing! But how to encode that
74 # in a makefile?
75 #
76 # At the start of this target, we call $(LATEX) to compile $(PN).tex.
77 # If you ignore the "sed" for now, then the next step is to check for
78 # the existence of a "previous" file. If there isn't one, this is the
79 # first time that we've tried to build the PDF. In that case, take the
80 # PDF that we've just built and make *that* the previous file. Then
81 # start all over. If there is a previous file, then this is the second
82 # (or more) time that we've tried to build the PDF. We diff the PDF
83 # file that we've just built against the previous file; if they're the
84 # same, then we've succeeded and stop. Otherwise, we make the new PDF
85 # the previous file, and start all over. The end result is that we
86 # will loop until the newly-created PDF and the previous file are
87 # identical.
88 #
89 # But what about the "sed" call? By default, pdflatex will compile the
90 # creation date, modification date, and a unique ID into the output
91 # PDF. That means that two otherwise-identical documents, created
92 # seconds apart, will look different. We only need to know when the
93 # *contents* of the document are the same -- we don't care about the
94 # metadata -- so sed is used to remove those three nondeterministic
95 # pieces of information.
96 #
97 # The creation and modification dates should become optional in pdftex
98 # v1.40.17 thanks to Debian's SOURCE_DATE_EPOCH initiative. When that
99 # version of pdflatex makes it into TeX Live 2016, we can replace
100 # those two sed scripts with something smarter.
101 #
102 $(PN).pdf: $(SRCS) $(PN).bbl $(INDEX_DSTS)
103 $(LATEX) $(PN).tex
104
105 sed --in-place \
106 -e '/^\/ID \[<.*>\]/d' \
107 -e "s/^\/\(ModDate\) (.*)/\/\1 (D:19700101000000Z00'00')/" \
108 -e "s/^\/\(CreationDate\) (.*)/\/\\1 (D:19700101000000Z00'00')/" \
109 $@
110
111 if [ ! -f $@.previous ]; then \
112 mv $@ $@.previous; \
113 $(MAKE) $@; \
114 fi;
115
116 if cmp -s $@ $@.previous; then \
117 rm $@.previous; \
118 else \
119 mv $@ $@.previous; \
120 $(MAKE) $@; \
121 fi;
122
123
124 $(PN).aux: $(SRCS)
125 $(LATEX) $(PN).tex
126
127
128 ifdef INDICES
129 # We need to be able to build the index source files without involving
130 # the main $(PN).pdf rule, in order to avoid a chicken-and-egg problem.
131 # This is similar to the $(PN).aux rule above, except that an index is
132 # optional and there might be more than one of them.
133 $(INDEX_SRCS): $(PN).tex
134 $(LATEX) $(PN).tex
135 endif
136
137 ifdef INDICES
138 # Create real indices from source files by running "makeindex" on
139 # them. We depend on SRCS here because we *do* want to rebuild the
140 # index if the source document changes, but we use an order-only
141 # dependency (see the bbl rule below) on the idx files to prevent us
142 # from going into a rebuild loop when the idx files are regenerated.
143 %.ind: $(SRCS) | %.idx
144 makeindex $|
145 endif
146
147 # The pipe below indicates an "order-only dependency" on the aux file.
148 # Without it, every compilation of $(PN).tex would produce a new
149 # $(PN).aux, and thus $(PN).bbl would be rebuilt. This in turn causes
150 # $(PN).pdf to appear out-of-date, which leads to a recompilation of
151 # $(PN).tex... and so on. The order-only dependency means we won't
152 # rebuild $(PN).bbl if $(PN).aux changes.
153 #
154 # As a side effect, we now need to depend on $(SRCS) here, since we
155 # won't pick it up transitively from $(PN).aux.
156 #
157 # If the $BIBS variable is undefined, we presume that there are no
158 # references and create an empty bbl file. Otherwise, we risk trying
159 # to run biblatex on an aux file containing no citations. If you do
160 # define $BIBS but don't cite anything, you'll run into a similar
161 # problem. Don't do that.
162 #
163 $(PN).bbl: $(SRCS) | $(PN).aux
164 ifdef BIBS
165 bibtex $(PN).aux
166 else
167 echo -n '' > $@
168 endif
169
170 # If the output PDF exists but the log file does not, then an attempt
171 # to "build the log file" (i.e. build the PDF) would do nothing. Thus
172 # whenever the log file does not exist, we do a fresh build.
173 $(PN).log: $(SRCS)
174 $(MAKE) clean
175 $(MAKE)
176
177 # How do we convert a raw listing into something testable by sage? We
178 # append/prepend triple quotes to make the whole thing into a doctest.
179 sage_listings/%.py: sage_listings/%.listing
180 echo '"""' > $@ && cat $< >> $@ && echo '"""' >> $@
181
182 # Ensure that there are no overfull or underfull boxes in the output
183 # document by parsing the log for said warnings.
184 .PHONY: check-boxes
185 check-boxes: $(PN).log
186 @! grep -i 'overfull\|underfull' $<
187
188 # Run chktex to find silly mistakes. There is some exit code weirdness
189 # (Savannah bug 53129), so we just look for empty output.
190 .PHONY: check-chktex
191 CHKTEX = chktex --localrc .chktexrc --quiet --inputfiles=0
192 check-chktex:
193 @[ -z "$(shell $(CHKTEX) mjotex.sty)" ]
194
195 # Ensure that there are no undefined references in the document by
196 # parsing the log file for said warnings.
197 .PHONY: check-undefined
198 check-undefined: $(PN).log
199 @! grep -i 'undefined' $<
200
201 # Use sage to doctest any \sagelisting{}s in SAGE_LISTING_DSTS.
202 # The actuall command is ifdef'd so that we can comment out
203 # the definition of SAGE_LISTING_DSTS without breaking the
204 # default definition of the "check" target.
205 .PHONY: check-sage
206 check-sage: $(SAGE_LISTING_DSTS)
207 ifdef SAGE_LISTING_DSTS
208 PYTHONPATH="$(HOME)/src/sage.d" \
209 sage -t --timeout=0 --memlimit=0 \
210 $^
211 endif
212
213 # Run a suite of checks.
214 .PHONY: check
215 check: check-boxes check-chktex check-undefined check-sage
216
217 # Clean up leftover junk. This only looks overcomplicated because
218 # the *.{foo,bar} syntax supported by Bash is not POSIX, and Make
219 # will execute these commands using /bin/sh (which should be POSIX).
220 JUNK_EXTENSIONS = aux bbl bcf blg glo ilg ist listing lof log nav out pdf
221 JUNK_EXTENSIONS += snm spl toc xml
222 .PHONY: clean
223 clean:
224 for ext in $(JUNK_EXTENSIONS); do rm -f *.$$ext; done;
225 rm -rf dist/
226 rm -f $(SAGE_LISTING_DSTS) $(INDEX_SRCS) $(INDEX_DSTS)
227
228 # If this document will be published, the publisher isn't going to
229 # have your BibTeX database or your mjotex files. So, you need to
230 # package them up along with the code for your document. This target
231 # will create a "dist" directory and copy the necessary stuff there.
232 #
233 .PHONY: dist
234 dist: $(PN).bbl
235 mkdir -p dist
236 cp $(SRCS) $(PN).bbl $(BIBPATHS) $(MJOTEXPATHS) dist/