-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
271 lines (224 loc) · 6.64 KB
/
Makefile
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# vim:ft=make:
-include config.mk
BIBTEX ?= bibtex
PYTHON ?= python
ASYMPTOTE ?= asy
GNUPLOT ?= gnuplot
MAXIMA ?= maxima -b
TEXENGINE ?= pdflatex
MAIN_SRC ?= main.tex
SOURCES ?=
PACKAGES ?=
BUILD_DIR ?= build
VIEWER ?= xdg-open
MAIN_NAME = $(patsubst %.tex,%,$(MAIN_SRC))
MAIN_TARGET = $(BUILD_DIR)/$(patsubst %.tex,%.pdf,$(MAIN_SRC))
BIBITEM_FILE = $(BUILD_DIR)/$(patsubst %.tex,%.bbl,$(MAIN_SRC))
BIBITEM_REDO_FILE = $(BUILD_DIR)/$(patsubst %.tex,%.REDO.bbl,$(MAIN_SRC))
OBJECTS = $(patsubst %.tex,$(BUILD_DIR)/%.o,$(SOURCES))
OBJECTS_BIBFILES = $(patsubst %.tex,$(BUILD_DIR)/%.obib,$(SOURCES))
OBJECTS_BIBITEM = $(patsubst %.tex,$(BUILD_DIR)/%.obibitem,$(SOURCES))
OBJECTS_SOURCES = $(patsubst %.tex,$(BUILD_DIR)/%.osrc,$(SOURCES))
OBJECTS_FIGURES = $(patsubst %.tex,$(BUILD_DIR)/%.ofig,$(SOURCES))
OBJECTS_TOC = $(patsubst %.tex,$(BUILD_DIR)/%.otoc,$(SOURCES))
DEP_FILE = $(BUILD_DIR)/$(patsubst %.tex,%.dependency_list,$(MAIN_SRC))
TOC_DEP = $(BUILD_DIR)/toc.d
TOC_DEP_NEW = $(BUILD_DIR)/toc.d.new
FIGS_SUFFIXES = %.pdf %.eps %.png %.jpg %.jpeg %.gif %.dvi %.bmp %.svg %.ps
SCRIPT_SUFFIXES = $(FIGS_SUFFIXES) %.tex
ifdef COLOR
COLOR_R = [0;31m
COLOR_G = [0;32m
COLOR_Y = [0;33m
COLOR_E = [0m
endif
ifdef QUIET
STDERROUT = 2> /dev/null > /dev/null
endif
ifneq ($(MAKECMDGOALS),clean)
-include $(DEP_FILE)
endif
.DEFAULT_GOAL := all
all: $(MAIN_TARGET)
$(MAIN_TARGET): $(TOC_DEP_NEW) $(BIBITEM_REDO_FILE) $(PACKAGES)
$(MAIN_TARGET):
$(_creating)
$(tex-command)
view: $(MAIN_TARGET)
$(VIEWER) $<
$(BIBITEM_FILE): $(shell cat /dev/null $(OBJECTS_BIBFILES))
.NOTPARALLEL:
$(BIBITEM_FILE): $(OBJECTS_BIBITEM)
$(_creating)
if test -z $^; then \
touch $@; \
else \
{ for i in $^; do set -x; test -e $$i && cp $$i $(BUILD_DIR)/$$i; done }; \
( cd $(BUILD_DIR) ; $(BIBTEX) $(patsubst %.tex,%,$(MAIN_SRC));); \
fi
$(BIBITEM_REDO_FILE): $(BIBITEM_FILE)
$(_creating)
$(tex-command)
$(tex-command)
touch $@
REV ?=
ifeq ($(MAKECMDGOALS),diff)
ifeq ($(REV),)
$(error set the value of REV to a valid revision name)
endif
endif
DIFF_TARGET = $(MAIN_NAME)-diff$(REV).pdf
DIFF_SOURCE = $(MAIN_NAME)-diff$(REV).tex
diff: $(DIFF_TARGET)
$(DIFF_TARGET): $(DIFF_SOURCE)
$(_creating)
tectonic $<
$(DIFF_SOURCE): $(MAIN_SRC)
$(_creating)
latexdiff-vc --git -r $(REV) $<
dist: $(shell cat /dev/null \
$(OBJECTS_SOURCES) \
$(OBJECTS_FIGURES) \
$(OBJECTS_BIBFILES) $(PACKAGES) )
$(_creating)
@mkdir -p $@
@$(MAKE) \
dist/$(MAIN_SRC) \
$(patsubst %,dist/%,$^) \
dist/%: %
$(_creating)
@mkdir -p $(dir $@)
@cp $< $@
$(TOC_DEP_NEW): $(OBJECTS_TOC)
$(_creating)
@cat $^ > $@
@if ! diff $(TOC_DEP) $(TOC_DEP_NEW) > /dev/null 2>&1; then $(tex-command); fi
@cp $(TOC_DEP_NEW) $(TOC_DEP)
$(BUILD_DIR)/%.o: %.tex
$(_creating)
@mkdir -p $(dir $@)
@python3 -c "$$python_get_all_dependencies" $? > $@
%.osrc: %.o
$(_creating)
@echo > $@
@awk -F'^input +' '{print $$2}' $< | sed "/^\s*$$/d" | sort -u >> $@
@awk -F'^include +' '{print $$2}' $< | sed "/^\s*$$/d" | sort -u >> $@
%.obib: %.o
$(_creating)
@echo > $@
@awk -F'^bibliography +' '{print $$2}' $< | sed "/^\s*$$/d" | sed "s/,/ /g" |\
sort -u >> $@
%.obibitem: %.o
$(_creating)
@echo > $@
@awk -F'cite +' '{print $$2}' $< | sed "/^\s*$$/d" | sort -u >> $@
%.ofig: %.o
$(_creating)
@echo > $@
@awk -F'^includegraphics +' '{print $$2}' $< | sed "/^\s*$$/d" | sort -u >> $@
@awk -F'^includepdf +' '{print $$2}' $< | sed "/^\s*$$/d" | sort -u >> $@
%.otoc: %.o
$(_creating)
@echo > $@
@awk -F'^part ' '{print $$2}' $< | sed "/^\s*$$/d" | sort -u >> $@
@awk -F'^section ' '{print $$2}' $< | sed "/^\s*$$/d" | sort -u >> $@
@awk -F'^chapter ' '{print $$2}' $< | sed "/^\s*$$/d" | sort -u >> $@
@awk -F'^subsection ' '{print $$2}' $< | sed "/^\s*$$/d" | sort -u >> $@
@awk -F'^subsubsection ' '{print $$2}' $< | sed "/^\s*$$/d" | sort -u >> $@
$(DEP_FILE): $(OBJECTS_SOURCES) $(OBJECTS_FIGURES) $(OBJECTS_BIBFILES)
$(_creating)
@echo '$(MAIN_TARGET): \' > $@
@cat $^ | sed "/^\s*$$/d" | sed 's_$$_ \\_' >> $@
@echo '' >> $@
$(BUILD_DIR):
mkdir -p $@
$(SCRIPT_SUFFIXES): %.mac
$(_creating)
@cd $(dir $<) && $(MAXIMA) $(notdir $<)
$(SCRIPT_SUFFIXES): %.py
$(_creating)
@cd $(dir $<) && $(PYTHON) $(notdir $<)
$(SCRIPT_SUFFIXES): %.sh
$(_creating)
@cd $(dir $<) && $(SHELL) $(notdir $<)
$(FIGS_SUFFIXES): %.asy
$(_creating)
@cd $(dir $<) && $(ASYMPTOTE) -f $$(echo $(suffix $@) | tr -d "\.") $(notdir $< )
$(FIGS_SUFFIXES): %.gnuplot
$(_creating)
@cd $(dir $<) && $(GNUPLOT) $(notdir $< )
$(FIGS_SUFFIXES): %.tex
$(_creating)
@cd $(dir $<) && $(TEXENGINE) $(notdir $< )
PURGE_SUFFIXES = .aux .bbl .blg .fdb_latexmk .fls .log out \
.ilg .toc .nav .snm .run.xml .glo .ist -blx.bib
clean:
@rm -v $(wildcard \
$(DEP_FILE) $(OBJECTS) $(OBJECTS_SOURCES) \
$(OBJECTS_BIBFILES) \
$(OBJECTS_FIGURES) $(TOC_DEP) $(TOC_DEP_NEW) $(MAIN_TARGET) \
$(OBJECTS_TOC) $(patsubst %,$(BUILD_DIR)/*%,$(PURGE_SUFFIXES)) \
) 2> /dev/null
config.mk:
@echo "Creating a default $@"
$(file >$@,$(_default_config))
define _default_config
MAIN_SRC = main.tex
BUILD_DIR = build
# TeX files
SOURCES = $$(MAIN_SRC) $$(wildcard *.tex)
PACKAGES = $$(wildcard *.sty)
VIEW = 1
VIEWER = xdg-open
BIBTEX = bibtex
PYTHON = python3
TEXENGINE = pdflatex
# use color for output
COLOR = 1
endef
define python_get_all_dependencies
import re
import sys
keywords = [ "includegraphics"
, "includepdf"
, "cite"
, "citep"
, "ref"
, "refeq"
, "label"
, "input"
, "include"
, "bibliography"
, "part"
, "chapter"
, "section"
, "subsection"
, "subsubsection"
]
command_regex = r"\\(%s)%s%s" % ( "|".join(keywords)
, "(\[[^{}]+\])?"
, "{([^{}]+)}"
)
re_command_regex = re.compile(command_regex)
def remove_comments(text):
return re.sub(r"%.*[\n]", "", text)
def get_parsed_keywords(text):
parsed = re_command_regex.findall(remove_comments(text))
return sum([[(name, value)
for value in re.sub(r"\s", "", values).split(",")]
for name, _, values in parsed], [])
if __name__ == "__main__":
input_file = sys.argv[1]
with open(input_file) as f:
text = f.read()
for s in get_parsed_keywords(text):
print("{0} {1}".format(s[0], s[1]))
endef
export python_get_all_dependencies
define _creating
$(info $(COLOR_G)$@$(COLOR_E) from $(COLOR_Y)$<$(COLOR_E))
endef
define tex-command
$(TEXENGINE) -output-directory=$(BUILD_DIR) $(MAIN_SRC) $(STDERROUT)
endef
.PHONY: clean diff