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