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