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