X-Git-Url: http://gitweb.michael.orlitzky.com/?p=mjotex.git;a=blobdiff_plain;f=makefile;h=eabe908839945305a694fe7552ebbb25fb9ed5dc;hp=1486a26fbff7d5d1b46f01dcf19f8f744c0987c1;hb=59e4fa4c0281befce512b366f66e0e92c1740e29;hpb=6442db810af2ba37e6ff4f3639543510a79aafa3 diff --git a/makefile b/makefile index 1486a26..eabe908 100644 --- a/makefile +++ b/makefile @@ -6,11 +6,14 @@ LATEX = pdflatex -file-line-error -halt-on-error # The name of this document. -PN := paper +PN = examples # A space-separated list of bib files. These must all belong to paths # contained in your $BIBINPUTS environment variable. -BIBS = references.bib +# +# Leave commented if you don't use a bibliography file. +# +#BIBS = references.bib # A space-separated list of the mjotex files that you use. The path to # mjotex must be contain in your $TEXINPUTS environment variable. @@ -22,49 +25,109 @@ MJOTEX += mjo-theorem-star.tex mjo-topology.tex # Use kpsewhich (from the kpathsea suite) to find the absolute paths # of the bibtex/mjotex files listed in in $(BIBS)/$(MJOTEX). The SRCS # variable should contain all (Bib)TeX source files for the document. -SRCS = $(PN).tex $(shell kpsewhich $(BIBS)) $(shell kpsewhich $(MJOTEX)) +SRCS = $(PN).tex +ifdef BIBS +SRCS += $(shell kpsewhich $(BIBS)) +endif +ifdef MJOTEX +SRCS += $(shell kpsewhich $(MJOTEX)) +endif # The first target is the default, so put the PDF document first. # -# The voodoo here is to avoid rebuilding the project when nothing has -# changed. Each compilation pass with pdflatex will produce a new aux -# file, even if nothing has changed. That cases Make to think that we -# need to regenerate the bbl file, which in turn means we need to -# rebuild the whole project... +# This voodoo is all designed to find a "fixed point" of calling +# $(LATEX). When you build a LaTeX document, it requires an unknown +# number of compilation passes. How do you know when to stop? Easy, +# stop when the output file stops changing! But how to encode that +# in a makefile? # -# To avoid that, we save a copy of the current aux file and compare it -# to the new one afterwards. If the files are the same, great, put the -# old one (with the old timestamp) back. If not, we need to compile -# again, so we just invoke $(MAKE) again on this target. +# At the start of this target, we call $(LATEX) to compile $(PN).tex. +# If you ignore the "sed" for now, then the next step is to check for +# the existence of a "previous" file. If there isn't one, this is the +# first time that we've tried to build the PDF. In that case, take the +# PDF that we've just built and make *that* the previous file. Then +# start all over. If there is a previous file, then this is the second +# (or more) time that we've tried to build the PDF. We diff the PDF +# file that we've just built against the previous file; if they're the +# same, then we've succeeded and stop. Otherwise, we make the new PDF +# the previous file, and start all over. The end result is that we +# will loop until the newly-created PDF and the previous file are +# identical. # -# The process is kind of like finding a fixpoint of `make` with -# respect to the contents of the aux file. +# But what about the "sed" call? By default, pdflatex will compile the +# creation date, modification date, and a unique ID into the output +# PDF. That means that two otherwise-identical documents, created +# seconds apart, will look different. We only need to know when the +# *contents* of the document are the same -- we don't care about the +# metadata -- so sed is used to remove those three nondeterministic +# pieces of information. # -$(PN).pdf: $(SRCS) $(PN).aux $(PN).bbl - mv $(PN).aux $(PN).aux.bak - $(LATEX) $< && $(LATEX) $< - if cmp -s $(PN).aux $(PN).aux.bak; then \ - mv $(PN).aux.bak $(PN).aux; \ +# The creation and modification dates should become optional in pdftex +# v1.40.17 thanks to Debian's SOURCE_DATE_EPOCH initiative. When that +# version of pdflatex makes it into TeX Live 2016, we can replace +# those two sed scripts with something smarter. +# +$(PN).pdf: $(SRCS) $(PN).bbl + $(LATEX) $(PN).tex + + sed --in-place \ + -e '/^\/ID \[<.*>\]/d' \ + -e "s/^\/\(ModDate\) (.*)/\/\1 (D:19700101000000Z00'00')/" \ + -e "s/^\/\(CreationDate\) (.*)/\/\\1 (D:19700101000000Z00'00')/" \ + $@ + + if [ ! -f $@.previous ]; then \ + mv $@ $@.previous; \ + $(MAKE) $@; \ + fi; + + if cmp -s $@ $@.previous; then \ + rm $@.previous; \ else \ + mv $@ $@.previous; \ $(MAKE) $@; \ fi; + $(PN).aux: $(SRCS) - $(LATEX) $< + $(LATEX) $(PN).tex -$(PN).bbl: $(PN).aux - bibtex $< +# The pipe below indicates an "order-only dependency" on the aux file. +# Without it, every compilation of $(PN).tex would produce a new +# $(PN).aux, and thus $(PN).bbl would be rebuild. This in turn causes +# $(PN).pdf to appear out-of-date, which leads to a recompilation of +# $(PN).tex... and so on. The order-only dependency means we won't +# rebuild $(PN).bbl if $(PN).aux changes. +# +# As a side effect, we now need to depend on $(SRCS) here, since we +# won't pick it up transitively from $(PN).aux. +# +# If the $BIBS variable is undefined, we presume that there are no +# references and create an empty bbl file. Otherwise, we risk trying +# to run biblatex on an aux file containing no citations. +# +$(PN).bbl: $(SRCS) | $(PN).aux +ifdef BIBS + bibtex $(PN).aux +else + echo -n '' > $@ +endif + +# Run chktex to find silly mistakes. There is some exit code weirdness +# (Savannah bug 45979), so we just look for empty output. +.PHONY: check +check: + @[ -z "$(shell chktex --quiet mjotex.sty)" ] # Clean up leftover junk. .PHONY: clean clean: - rm -f *.{out,nav,snm,toc,aux,log,pdf,bcf,xml,bbl,blg,bib} + rm -f *.{aux,bbl,bcf,bib,blg,listing,lof,log} + rm -f *.{nav,out,pdf,snm,spl,toc,xml} rm -rf dist/ - - # If this document will be published, the publisher isn't going to # have your BibTeX database or your mjotex files. So, you need to # package them up along with the code for your document. This target