-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile.include
153 lines (133 loc) · 4.73 KB
/
Makefile.include
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
###
### generic GNU make Makefile for .tex -> .pdf.
### ransford at cs.washington.edu
### http://github.com/ransford/pdflatex-makefile
###
### Recommended usage:
### 1. echo 'include Makefile.include' > Makefile
### 2. Optional: Edit the Makefile to override $(TARGET)
### and anything else (e.g., PDFVIEWER, AFTERALL)
###
### Final result:
### % cat Makefile
### TARGET=mypaper
### PDFVIEWER=open -a 'Adobe Acrobat Professional'
### AFTERALL=mypostprocessingstep
### include Makefile.include
###
### mypostprocessingstep:
### # do something...
###
PDFLATEX ?= pdflatex -halt-on-error -file-line-error -shell-escape
BIBTEX ?= bibtex
ifneq ($(QUIET),)
PDFLATEX += -interaction=batchmode
ERRFILTER := > /dev/null || (egrep ':[[:digit:]]+:' *.log && false)
BIBTEX += -terse
else
PDFLATEX += -interaction=nonstopmode
ERRFILTER=
endif
## Action for 'make view'
OS=$(shell uname -s)
ifeq ($(OS),Darwin)
PDFVIEWER ?= open
else
PDFVIEWER ?= xdg-open
endif
## Name of the target file, minus .pdf: e.g., TARGET=mypaper causes this
## Makefile to turn mypaper.tex into mypaper.pdf.
TARGETS += $(TARGET)
TEXTARGETS = $(TARGETS:=.tex)
PDFTARGETS = $(TARGETS:=.pdf)
AUXFILES = $(TARGETS:=.aux)
LOGFILES = $(TARGETS:=.log)
## If $(TARGET).tex refers to .bib files like \bibliography{foo,bar}, then
## $(BIBFILES) will contain foo.bib and bar.bib, and both files will be added as
## dependencies to $(PDFTARGETS).
## Effect: updating a .bib file will trigger re-typesetting.
BIBFILES = $(patsubst %,%.bib,\
$(shell grep '^[^%]*\\bibliography{' $(TEXTARGETS) | \
grep -o '\\bibliography{[^}]\+}' | \
sed -e 's/^[^%]*\\bibliography{\([^}]*\)}.*/\1/' \
-e 's/, */ /g'))
## Add \input'ed or \include'd files to $(PDFTARGETS) dependencies.
INCLUDEDTEX = $(patsubst %,%.tex,\
$(shell grep '^[^%]*\\\(input\|include\){' $(TEXTARGETS) | \
grep -o '\\\(input\|include\){[^}]\+}' | \
sed -e 's/^.*{\([^}]*\)}.*/\1/'))
AUXFILES += $(INCLUDEDTEX:.tex=.aux)
## grab a version number from the repository (if any) that stores this.
## * REVISION is the current revision number (short form, for inclusion in text)
## * VCSTURD is a file that gets touched after a repo update
SPACE = $(empty) $(empty)
ifeq ($(shell git status >/dev/null 2>&1 && echo USING_GIT),USING_GIT)
ifeq ($(shell git svn info >/dev/null 2>&1 && echo USING_GIT_SVN),USING_GIT_SVN)
# git-svn
REVISION := $(shell git svn find-rev git-svn)
VCSTURD := $(subst $(SPACE),\ ,$(shell git rev-parse --show-toplevel)/.git/refs/remotes/git-svn)
else
# plain git
REVISION := $(shell git rev-parse --short HEAD)
GIT_BRANCH := $(shell git symbolic-ref HEAD 2>/dev/null)
VCSTURD := $(subst $(SPACE),\ ,$(shell git rev-parse --show-toplevel)/.git/$(GIT_BRANCH))
endif
else ifeq ($(shell hg root >/dev/null 2>&1 && echo USING_HG),USING_HG)
# mercurial
REVISION := $(shell hg id -i)
VCSTURD := $(subst $(SPACE),\ ,$(shell hg root)/.hg/dirstate)
else ifneq ($(wildcard .svn/entries),)
# subversion
REVISION := $(shell svnversion -n)
VCSTURD := .svn/entries
endif
# .PHONY names all targets that aren't filenames
.PHONY: all clean pdf view snapshot
all: pdf $(AFTERALL)
pdf: $(PDFTARGETS)
view: $(PDFTARGETS)
$(PDFVIEWER) $(PDFTARGETS)
# define a \Revision{} command you can include in your document's preamble.
# especially useful with e.g. drafthead.sty or fancyhdr.
# usage: \input{revision}
# ... \Revision{}
ifneq ($(REVISION),)
REVDEPS += revision.tex
revision.tex: $(VCSTURD)
/bin/echo '\newcommand{\Revision}'"{$(REVISION)}" > $@
AUXFILES += revision.aux
endif
# to generate aux but not pdf from pdflatex, use -draftmode
.INTERMEDIATE: $(AUXFILES)
%.aux: %.tex $(REVDEPS)
$(PDFLATEX) -draftmode $* $(ERRFILTER)
# introduce BibTeX dependency if we found a \bibliography
ifneq ($(strip $(BIBFILES)),)
BIBDEPS = %.bbl
%.bbl: %.aux $(BIBFILES)
$(BIBTEX) $*
endif
$(PDFTARGETS): %.pdf: %.tex %.aux $(BIBDEPS) $(INCLUDEDTEX) $(REVDEPS)
$(PDFLATEX) $* $(ERRFILTER)
ifneq ($(strip $(BIBFILES)),)
@if grep -q "undefined references" $*.log; then \
$(BIBTEX) $* && $(PDFLATEX) $* $(ERRFILTER); fi
endif
@while grep -q "Rerun to" $*.log; do \
$(PDFLATEX) $* $(ERRFILTER); done
DRAFTS := $(PDFTARGETS:.pdf=-$(REVISION).pdf)
$(DRAFTS): %-$(REVISION).pdf: %.pdf
cp $< $@
snapshot: $(DRAFTS)
%.distilled.pdf: %.pdf
gs -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$@ \
-dCompatibilityLevel=1.5 -dPDFSETTINGS=/prepress -c .setpdfwrite -f $<
exiftool -overwrite_original -Title="" -Creator="" -CreatorTool="" $@
distill: $(PDFTARGETS:.pdf=.distilled.pdf)
clean:
$(RM) $(foreach T,$(TARGETS), \
$(T).out $(T).pdf $(T).blg $(T).bbl \
$(T).lof $(T).lot $(T).toc $(T).idx \
$(T).nav $(T).snm $(T).distilled.pdf) \
$(REVDEPS) $(AUXFILES) $(LOGFILES) \
$(EXTRACLEAN)