-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
1189 lines (901 loc) · 27.2 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# File: common-makefile/src/version.m4
MAKEFILE_VERSION = v1.6
MAKEFILE_DATE = 16-11-2018 14:34
MAKEFILE_AUTHOR = Alejandro Gallo
MAKEFILE_URL = https://github.com/alejandrogallo/latex-makefile
MAKEFILE_LICENSE = GPLv3
## <<HELP
#
# The ultimate
# _ ____ ___ ____ _ _ _ _ ____ _ _ ____ ____ _ _ ____
# | |__| | |___ \/ |\/| |__| |_/ |___ |___ | | |___
# |___ | | | |___ _/\_ | | | | | \_ |___ | | |___ |___
#
#
#
## HELP
# Local configuration
-include config.mk
# File: common-makefile/src/os.m4
# Recognise OS
ifeq ($(shell uname),Linux)
LINUX := 1
OSX :=
else
LINUX :=
OSX := 1
endif
# File: common-makefile/src/shell-utils.m4
# Shell used
SH ?= bash
# Alias for `SHELL'
SHELL ?= $(SH)
# Python interpreter
PY ?= python
# Alias for `PY'
PYTHON ?= $(PY)
# Perl command
PERL ?= perl
# Grep program version
GREP ?= grep
# Find utility
FIND ?= find
# `sed` program version
SED ?= $(if $(OSX),gsed,sed)
# `awk` program to use
AWK ?= $(if $(OSX),gawk,awk)
# For creating tags
CTAGS ?= ctags
# To get complete paths
READLINK ?= $(if $(OSX),greadlink,readlink)
# `xargs` program to use
XARGS ?= xargs
# `tr` program to use
TR ?= tr
# `git` version to use
GIT ?= git
# `which` program to use
WHICH ?= which
# `sort` program to use
SORT ?= sort
# `uniq` program to use
UNIQ ?= uniq
# `Makefile` binary
MAKE ?= $(or $(MAKE),make)
# `rm` command
RM ?= rm
# C++ compiler
CXX ?= g++
# C compiler
CC ?= gcc
# Fortran compiler
FC ?= gfortran
# M4 compiler
M4 ?= m4
# Folder to build the project
BUILD_DIR ?= .
# Shell utilities
LATEX ?= pdflatex
# Main pdflatex engine
PDFLATEX ?= pdflatex
# File: common-makefile/src/log.m4
# If secondary programs output is shown
QUIET ?= 0
# If the log messages should be also muted
QQUIET ?=
# If the commands issued should be printed write `DEBUG=1` if you want to see
# all commands.
DEBUG ?=
# For coloring
TPUT ?= $(shell $(WHICH) tput 2> /dev/null)
# If messages should have color
WITH_COLOR ?= 1
ifneq ($(strip $(QUIET)),0)
FD_OUTPUT = 2>&1 > /dev/null
else
FD_OUTPUT =
endif
ifdef DEBUG
DBG_FLAG =
DBG_FILE ?= .makefile-dbg
$(shell date | $(SED) "p; s/./=/g" > $(DBG_FILE))
else
DBG_FLAG = @
DBG_FILE =
endif
define log-debug
>> $(or $(DBG_FILE),/dev/null) echo
endef
# Print commands like [CMD]
define print-cmd-name
"[$(COLOR_LB) \
$(shell \
if test "$(1)" = g++; then \
echo -n GXX; \
elif test "$(1)" = gcc; then \
echo -n GCC; \
elif test "$(1)" = icc; then \
echo -n ICC; \
elif test "$(1)" = cc; then \
echo -n CC; \
elif test "$(1)" = povray; then \
echo -n POV; \
elif test "$(1)" = perl; then \
echo -n PL; \
elif test "$(1)" = perl5; then \
echo -n PL5; \
elif test "$(1)" = ruby; then \
echo -n RB; \
elif test "$(1)" = ruby2; then \
echo -n RB2; \
elif test "$(1)" = python; then \
echo -n PY; \
elif test "$(1)" = python2; then \
echo -n PY2; \
elif test "$(1)" = python3; then \
echo -n PY3; \
elif test "$(1)" = pdflatex; then \
echo -n pdfTeX; \
elif test "$(1)" = bash; then \
echo -n BASH; \
elif test "$(1)" = gnuplot; then \
echo -n GPT; \
elif test "$(1)" = mupdf; then \
echo -n muPDF; \
else \
echo -n "$(1)" | tr a-z A-Z ; \
fi
)\
$(COLOR_E)]"
endef
ifndef QQUIET
ifeq ($(strip $(WITH_COLOR)),1)
# Red
COLOR_R ?= $(if $(TPUT),$(shell $(TPUT) setaf 1),"\033[0;31m")
# Green
COLOR_G ?= $(if $(TPUT),$(shell $(TPUT) setaf 2),"\033[0;32m")
# Yellow
COLOR_Y ?= $(if $(TPUT),$(shell $(TPUT) setaf 3),"\033[0;33m")
# Dark blue
COLOR_DB ?= $(if $(TPUT),$(shell $(TPUT) setaf 4),"\033[0;34m")
# Lila
COLOR_L ?= $(if $(TPUT),$(shell $(TPUT) setaf 5),"\033[0;35m")
# Light blue
COLOR_LB ?= $(if $(TPUT),$(shell $(TPUT) setaf 6),"\033[0;36m")
# Empty color
COLOR_E ?= $(if $(TPUT),$(shell $(TPUT) sgr0),"\033[0m")
ARROW ?= @echo "$(COLOR_L)===>$(COLOR_E)"
else
ARROW ?= @echo "===>"
endif #WITH_COLOR
ECHO ?= @echo
else
ARROW := @ > /dev/null echo
ECHO := @ > /dev/null echo
endif #QQUIET
# Main texfile in the current directory
MAIN_SRC ?= $(call discoverMain)
# Format to build to
FMT ?= pdf
# If `BUILD_DOCUMENT` should be previewed after building
VIEW ?= 1
# Depth for discovering automatically included texfiles
INCLUDES_REC ?= 3
# Texfiles included in the main tex file
INCLUDES ?= $(call recursiveDiscoverIncludes,$(MAIN_SRC),$(INCLUDES_REC))
# All `texfiles` in the project
TEXFILES ?= $(MAIN_SRC) $(INCLUDES)
# Bibtex files in the current directory
BIBTEX_FILES ?= $(call discoverBibtexFiles,$(TEXFILES))
# Source directory
PREFIX ?= $(PWD)
.DEFAULT_GOAL := all
# File: libraries.m4
# File: build-dir.m4
# Folder to build the project
BUILD_DIR ?= .
# Build dir flag for latex.
# If `BUILD_DIR = .` then `BUILD_DIR_FLAG` is not defined,
# else `BUILD_DIR = -output-directory $(BUILD_DIR)`
BUILD_DIR_FLAG ?= $(if \
$(filter-out \
.,$(strip $(BUILD_DIR))),-output-directory $(BUILD_DIR))
$(BUILD_DIR):
$(ECHO) $(call print-cmd-name,mkdir) $@
$(DBG_FLAG)mkdir -p $@ $(FD_OUTPUT)
$(DBG_FLAG)for i in $(TEXFILES); do \
mkdir -p $@/$$(dirname $$i); \
done $(FD_OUTPUT)
# Tex libraries directory
PACKAGES_DIR ?= libtex
# Which files are tex libraries
PACKAGES_FILES ?= $(wildcard \
$(PACKAGES_DIR)/*.sty \
$(PACKAGES_DIR)/*.rtx \
$(PACKAGES_DIR)/*.cls \
$(PACKAGES_DIR)/*.bst \
$(PACKAGES_DIR)/*.tex \
$(PACKAGES_DIR)/*.clo \
)
$(BUILD_DIR)/%: $(PACKAGES_DIR)/%
$(ECHO) $(call print-cmd-name,CP) $@
$(DBG_FLAG)mkdir -p $(BUILD_DIR)
$(DBG_FLAG)cp $^ $@
# File: latex.m4
# Function to try to discover automatically the main latex document
define discoverMain
$(shell \
$(GREP) -H '\\begin{document}' *.tex 2>/dev/null \
| $(removeTexComments) \
| head -1 \
| $(AWK) -F ":" '{print $$1}' \
)
endef
# Remove comments from some file, this variables is intended to be put
# in a shell call for processing of TeX files
removeTexComments=$(SED) "s/\([^\\]\)%.*/\1/g; s/^%.*//g"
TEX_INCLUDES_REGEX = \in\(clude\|put\)\s*[{]\s*
define recursiveDiscoverIncludes
$(shell \
files=$(1);\
for i in $$(seq 1 $(2)); do \
files="$$(\
cat $$files 2> /dev/null\
| $(removeTexComments) \
| $(SED) 's/$(TEX_INCLUDES_REGEX)/\n&/g' \
| $(SED) -n '/$(TEX_INCLUDES_REGEX)/p' \
| $(SED) 's/$(TEX_INCLUDES_REGEX)//' \
| $(SED) 's/\.tex//g' \
| $(SED) 's/}.*//g' \
| $(SED) 's/\s*$$//g' \
| $(SED) 's/\(.*\)/\1.tex /' \
)"; \
$(log-debug) $$i th iteration includes; \
$(log-debug) $$files; \
test -n "$$files" || break; \
echo $$files; \
done \
)
endef
define hasToc
$(shell\
$(GREP) '\\tableofcontents' $(1) \
| $(removeTexComments) \
| $(SED) "s/ //g" \
)
endef
ifneq ($(strip $(MAIN_SRC)),) # Do this only if MAIN_SRC is defined
BUILD_DOCUMENT = $(patsubst %.tex,%.$(FMT),$(MAIN_SRC))
TOC_FILE = $(patsubst %.tex,$(BUILD_DIR)/%.toc,$(MAIN_SRC))
BIBITEM_FILES = $(patsubst %.tex,$(BUILD_DIR)/%.bbl,$(MAIN_SRC))
AUX_FILE = $(patsubst %.tex,$(BUILD_DIR)/%.aux,$(MAIN_SRC))
PYTHONTEX_FILE = $(patsubst %.tex,$(BUILD_DIR)/%.pytxcode,$(MAIN_SRC))
PDFPC_FILE = $(patsubst %.tex,%.pdfpc,$(MAIN_SRC))
PACKAGES_FILES_BUILD = $(patsubst $(PACKAGES_DIR)/%,$(BUILD_DIR)/%,$(PACKAGES_FILES))
endif #MAIN_SRC exists
$(AUX_FILE):
$(ECHO) $(call print-cmd-name,$(PDFLATEX)) $@
$(DBG_FLAG)$(PDFLATEX) $(BUILD_DIR_FLAG) $(MAIN_SRC) $(FD_OUTPUT)
# Default dependencies for `BUILD_DOCUMENT`
DEFAULT_DEPENDENCIES ?= \
$(BUILD_DIR) \
$(MAIN_SRC) \
$(INCLUDES) \
$(PACKAGES_FILES_BUILD) \
$(FIGURES) \
$(if $(call hasToc,$(MAIN_SRC)),$(TOC_FILE),$(AUX_FILE)) \
$(if $(wildcard $(BIBTEX_FILES)),$(BIBITEM_FILES)) \
$(if $(WITH_PYTHONTEX),$(PYTHONTEX_FILE)) \
$(if $(CHECK_SPELL),spelling) \
# General dependencies for `BUILD_DOCUMENT`
DEPENDENCIES ?= $(DEFAULT_DEPENDENCIES)
PURGE_SUFFIXES = %.aux %.bbl %.blg %.fdb_latexmk %.fls %.log %.out \
%.ilg %.toc %.nav %.snm
SUPPORTED_SUFFIXES = %.pdf %.div %.ps %.eps %.1 %.html
# File: deps.m4
# These files are to keep track of the dependencies for latex or pdf
# includes, table of contents generation or figure recognition
#
TOC_DEP ?= $(strip $(DEPS_DIR))/toc.d
FIGS_DEP ?= $(strip $(DEPS_DIR))/figs.d
# Folder to keep makefile dependencies
DEPS_DIR ?= .deps
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),help)
-include $(FIGS_DEP)
endif
endif
# Figures included in all texfiles
FIGURES ?=
$(TOC_FILE): $(TOC_DEP)
$(ECHO) $(call print-cmd-name,$(PDFLATEX)) $@
$(DBG_FLAG)mkdir -p $(BUILD_DIR)
$(DBG_FLAG)cd $(dir $(MAIN_SRC) ) && $(PDFLATEX) \
$(BUILD_DIR_FLAG) $(notdir $(MAIN_SRC) ) $(FD_OUTPUT)
$(TOC_DEP): $(TEXFILES)
$(ARROW) Writing table of contents into $(TOC_DEP)
$(DBG_FLAG)mkdir -p $(dir $@)
$(DBG_FLAG)$(GREP) -E \
'\\(section|subsection|subsubsection|chapter|part|subsubsubsection).' \
$(TEXFILES) \
| $(removeTexComments) \
| $(SED) 's/.*{\(.*\)}.*/\1/' > $@.control
$(DBG_FLAG)test -f "$@" && \
{ test diff $@ $@.control 2>&1 > /dev/null && mv $@.control $@; } || \
mv $@.control $@
$(FIGS_DEP): $(TEXFILES)
$(ARROW) Writing graphics dependencies into $(FIGS_DEP)
$(DBG_FLAG)mkdir -p $(dir $@)
$(DBG_FLAG)echo FIGURES = \\ > $@
$(DBG_FLAG)$(GREP) --no-filename -E '\\include(graphic|pdf).' $(TEXFILES) \
| $(removeTexComments) \
| $(SED) -n 's/.*{\([^{}]\+\)}.*/\1 \\/p' >> $@
figs: $(FIGURES) ## Make figures
deps: $(FIGS_DEP) ## Parse dependencies for the main texfile
# File: bibliography.m4
define discoverBibtexFiles
$(shell \
$(GREP) -E '\\bibliography\s*{' $(1) 2> /dev/null \
| $(removeTexComments) \
| $(SED) 's/.*\\bibliography//' \
| $(SED) 's/\.bib//g' \
| $(TR) "," "\n" \
| $(TR) -d "{}" \
| $(SED) 's/\s*$$/.bib /' \
| $(SORT) \
| $(UNIQ) \
)
endef
# For converting document formats
BIBTEX ?= bibtex
# =======================
# Bibliography generation
# =======================
#
# This generates a `bbl` file from a `bib` file For documents without a `bib`
# file, this will also be targeted, bit the '-' before the `$(BIBTEX)`
# ensures that the whole building doesn't fail because of it
#
$(BIBITEM_FILES): $(BIBTEX_FILES)
$(ARROW) "Compiling the bibliography"
-$(DBG_FLAG)test $(BUILD_DIR) = . || { \
for bibfile in $(BIBTEX_FILES); do \
mkdir -p $(BUILD_DIR)/$$(dirname $$bibfile); \
cp -u $$bibfile $(BUILD_DIR)/$$(dirname $$bibfile); \
done \
}
$(ECHO) $(call print-cmd-name,$(BIBTEX)) $@
$(DBG_FLAG)cd $(BUILD_DIR); $(BIBTEX) $(patsubst %.tex,%,$(MAIN_SRC)) $(FD_OUTPUT)
$(ARROW) Compiling again $(BUILD_DOCUMENT) to update refs
$(DBG_FLAG)$(MAKE) --no-print-directory force
.PHONY: view-pdf open-pdf $(PDF_VIEWER) todo help test force dist releases
pdf: FMT=pdf ## Create pdf file
html: FMT=html ## Create html file
revealjs: FMT=html ## Create a revealjs presentation
man: FMT=1 ## Create man file
$(FMT): $(BUILD_DOCUMENT)
all: $(FMT) $(if $(VIEW),view-$(FMT)) ## (Default) Create BUILD_DOCUMENT
$(BUILD_DOCUMENT): $(DEPENDENCIES)
# =================
# Force compilation
# =================
#
# This makefile only compiles the TeX document if it is strictly necessary, so
# sometimes to force compilation this target comes in handy.
#
force: ## Force creation of BUILD_DOCUMENT
$(DBG_FLAG)$(MAKE) --no-print-directory -W $(MAIN_SRC) $(BUILD_DOCUMENT)
# File: html.m4
BROWSER ?= firefox
view-html: $(BUILD_DOCUMENT)
$(DBG_FLAG)($(BROWSER) $(BUILD_DOCUMENT) &)&
# File: pythontex.m4
# If pythontex is being used
WITH_PYTHONTEX ?=
PYTHONTEX ?= pythontex
#FIXME: find a way of not having to compile the main document again
%.pytxcode: %.tex
$(ARROW) "Compiling latex for pythontex"
$(PDFLATEX) $<
$(ARROW) "Creating pythontex"
$(PYTHONTEX) $<
# File: figure-targets.m4
FIGS_SUFFIXES = %.pdf %.eps %.png %.jpg %.jpeg %.gif %.dvi %.bmp %.svg %.ps
# Eps to pdf converter
EPS2PDF ?= epstopdf
# For asymptote figures
ASYMPTOTE ?= asy
# Gnuplot interpreter
GNUPLOT ?= gnuplot
$(FIGS_SUFFIXES): %.asy
$(ECHO) $(call print-cmd-name,$(ASYMPTOTE)) $@
$(DBG_FLAG)cd $(dir $<) && $(ASYMPTOTE) -f \
$(shell echo $(suffix $@) | $(TR) -d "\.") $(notdir $< ) $(FD_OUTPUT)
$(FIGS_SUFFIXES): %.gnuplot
$(ECHO) $(call print-cmd-name,$(GNUPLOT)) $@
$(DBG_FLAG)cd $(dir $< ) && $(GNUPLOT) $(notdir $< ) $(FD_OUTPUT)
$(FIGS_SUFFIXES): %.sh
$(ECHO) $(call print-cmd-name,$(SH)) $@
$(DBG_FLAG)cd $(dir $< ) && $(SH) $(notdir $< ) $(FD_OUTPUT)
$(FIGS_SUFFIXES): %.py
$(ECHO) $(call print-cmd-name,$(PY)) $@
$(DBG_FLAG)cd $(dir $< ) && $(PY) $(notdir $< ) $(FD_OUTPUT)
$(FIGS_SUFFIXES): %.tex
$(ECHO) $(call print-cmd-name,$(PDFLATEX)) $@
$(DBG_FLAG)mkdir -p $(dir $<)/$(BUILD_DIR)
$(DBG_FLAG)cd $(dir $<) && $(PDFLATEX) \
$(BUILD_DIR_FLAG) $(notdir $*.tex ) $(FD_OUTPUT)
ifneq ($(strip $(BUILD_DIR)),.)
-$(DBG_FLAG)test ! "$@ = *.aux" || cp \
$(PWD)/$(dir $<)/$(BUILD_DIR)/$(notdir $@) $(PWD)/$(dir $<)/$(notdir $@)
endif
%.pdf: %.eps
$(ECHO) $(call print-cmd-name,$(EPS2PDF)) $@
$(DBG_FLAG)cd $(dir $< ) && $(EPS2PDF) $(notdir $< ) $(FD_OUTPUT)
# File: document-targets.m4
%.tex: %.sh
$(ECHO) $(call print-cmd-name,$(SH)) $@
$(DBG_FLAG)cd $(dir $<) && $(SH) $(notdir $<) $(FD_OUTPUT)
%.tex: %.py
$(ECHO) $(call print-cmd-name,$(PY)) $@
$(DBG_FLAG)cd $(dir $<) && $(PY) $(notdir $<) $(FD_OUTPUT)
%.tex: %.pl
$(ECHO) $(call print-cmd-name,$(PERL)) $@
$(DBG_FLAG)cd $(dir $<) && $(PERL) $(notdir $<) $(FD_OUTPUT)
# File: pdf-viewer.m4
# Recognise pdf viewer automagically
PDF_VIEWER ?= $(or \
$(shell $(WHICH) zathura 2> /dev/null),\
$(shell $(WHICH) mupdf 2> /dev/null),\
$(shell $(WHICH) mupdf-x11 2> /dev/null),\
$(shell $(WHICH) mupdf-gl 2> /dev/null),\
$(shell $(WHICH) evince 2> /dev/null),\
$(shell $(WHICH) okular 2> /dev/null),\
$(shell $(WHICH) xdg-open 2> /dev/null),\
$(shell $(WHICH) gnome-open 2> /dev/null),\
$(shell $(WHICH) kde-open 2> /dev/null),\
$(shell $(WHICH) open 2> /dev/null),\
)
# =============
# View document
# =============
#
# Open and refresh pdf.
#
view-pdf: $(PDF_VIEWER) open-pdf ## Refresh and open pdf
# ===============
# Open pdf viewer
# ===============
#
# Open a viewer if there is none open viewing `$(BUILD_DOCUMENT)`
#
open-pdf: $(BUILD_DOCUMENT) ## Open pdf build document
$(ECHO) $(call print-cmd-name,$(PDF_VIEWER)) $(BUILD_DOCUMENT)
-$(DBG_FLAG)ps aux | $(GREP) -v $(GREP) \
| $(GREP) "$(PDF_VIEWER)" \
| $(GREP) -q "$(BUILD_DOCUMENT)" \
|| $(PDF_VIEWER) "$(BUILD_DOCUMENT)" 2>&1 > /dev/null &
# =============
# Refresh mupdf
# =============
#
# If the opened document is being viewed with `mupdf` this target uses the
# mupdf signal API to refresh the document.
#
mupdf /usr/bin/mupdf: ## Refresh mupdf
-$(DBG_FLAG)ps aux \
| $(GREP) -v $(GREP) \
| $(GREP) "$(PDF_VIEWER)" \
| $(GREP) "$(BUILD_DOCUMENT)" \
| $(AWK) '{print $$2}'\
| { read pid; test -z "$$pid" || kill -s HUP $$pid; }
# File: os.m4
# Recognise OS
ifeq ($(shell uname),Linux)
LINUX := 1
OSX :=
else
LINUX :=
OSX := 1
endif
# File: clean.m4
# Remove command
RM ?= rm
RM_FLAGS ?= -rf
# Default clean file to be cleaned
DEFAULT_CLEAN_FILES = \
$(wildcard $(PACKAGES_FILES_BUILD)) \
$(wildcard $(PYTHONTEX_FILE)) \
$(wildcard $(BUILD_DOCUMENT)) \
$(wildcard $(subst %,*,$(PURGE_SUFFIXES))) \
$(wildcard $(subst %,$(patsubst %.tex,%,$(MAIN_SRC)),$(SUPPORTED_SUFFIXES))) \
$(wildcard $(DEPS_DIR)) \
$(wildcard $(DBG_FILE)) \
$(wildcard $(PDFPC_FILE)) \
$(wildcard $(DIST_DIR)) \
$(wildcard $(DIFF_BUILD_DIR_MAIN)) \
$(wildcard $(DIFF_SRC_NAME)) \
$(if $(filter-out .,$(strip $(BUILD_DIR))),$(wildcard $(BUILD_DIR))) \
# Files to be cleaned
CLEAN_FILES ?= $(DEFAULT_CLEAN_FILES)
# =============
# Main cleaning
# =============
#
# This does a main cleaning of the produced auxiliary files. Before using it
# check which files are going to be cleaned up.
#
clean: ## Remove build and temporary files
$(ARROW) Cleaning up...
$(DBG_FLAG) {\
for file in $(CLEAN_FILES); do \
test -e $$file && { \
$(RM) $(RM_FLAGS) $$file && \
echo $(call print-cmd-name,RM) "$$file";\
} || : ; \
done \
}
# File: todo.m4
todo: $(TEXFILES) ## Print the todos from the main document
$(ARROW) Parsing \\TODO{} in $(MAIN_SRC)
$(DBG_FLAG)$(SED) -n "/\\TODO{/,/}/\
{\
s/.TODO/===/; \
s/[{]//g; \
s/[}]/===/g; \
p\
}" $(TEXFILES)
# File: pandoc.m4
#PANDOC CONVERSIONS
###################
PANDOC ?= pandoc
# FIXME: It doesn't work out of the box
#
# ======================
# Reveal.js presentation
# ======================
#
# This creates a revealjs presentation using the the pandoc program stored in
# the make variable PANDOC.
#
revealjs: reveal.js $(TEXFILES)
$(ARROW) Creating revealjs presentation...
$(DBG_FLAG)$(PANDOC) \
--mathjax -s \
-f latex -t revealjs \
--section-divs \
--variable theme="$(REVEALJS_THEME)" \
--variable transition="$(REVEALJS_TRANSITION)" \
$(MAIN_SRC) -o $(BUILD_DOCUMENT)
reveal.js:
$(ARROW) Gettin revealjs from $(REVEALJS_SRC)
$(DBG_FLAG)$(GIT) clone --depth=1 $(REVEALJS_SRC) && \
rm -rf reveal.js/.git && \
cp reveal.js/js/reveal.js reveal.js/js/reveal.min.js && \
cp reveal.js/css/reveal.css reveal.js/css/reveal.min.css
# (beige black blood league moon night serif simple sky solarized white)
REVEALJS_THEME ?= solarized
REVEALJS_TRANSITION ?= linear
REVEALJS_SRC ?= https://github.com/hakimel/reveal.js/
# =================
# Unix man document
# =================
#
# This creates a man page using `pandoc`.
#
man: $(MAIN_SRC)
$(ARROW) $(call print-cmd-name,$(PANDOC)) $(BUILD_DOCUMENT)
$(PANDOC) -s -f latex -t man $(MAIN_SRC) -o $(BUILD_DOCUMENT)
# =============
# HTML document
# =============
#
# This creates an html page using `pandoc`.
#
html: $(MAIN_SRC)
$(ARROW) $(call print-cmd-name,$(PANDOC)) $(BUILD_DOCUMENT)
$(PANDOC) --mathjax -s -f latex -t html5 $(MAIN_SRC) -o $(BUILD_DOCUMENT)
# File: pdfpc.m4
# ===========================
# Presenter console generator
# ===========================
#
# `pdfpc` is a nice program for presenting beamer presentations with notes
# and a speaker clock. This target implements a simple script to convert
# the standard `\notes{ }` beamer command into `pdfpc` compatible files, so
# that you can also see your beamer notes inside the `pdfpc` program.
#
pdf-presenter-console: $(PDFPC_FILE) ## Create annotations file for the pdfpc program
$(PDFPC_FILE): $(TEXFILES)
echo "[file]" > $@
echo "$(PDF_DOCUMENT)" >> $@
echo "[notes]" >> $@
cat $(MAIN_SRC) | $(AWK) '\
BEGIN { frame = 0; initialized = 0; } \
/(\\begin{frame}|\\frame{)/ { \
if(!/[%]/) { \
frame++; print "###",frame \
} \
} \
/\\note{/,/^\s*}\s*$$/ { \
if(!/\\note{/ && !/^[ ]*}[ ]*$$/) {\
if (frame == 0 && initialized == 0){ \
frame++; \
print "###",frame; \
initialized = 1; \
} \
print $$0 ; \
}\
} \
END { print frame } \
' | tee -a $@
# File: release.m4
RELEASES_DIR=releases
RELEASES_FMT=tar
releases: $(BUILD_DOCUMENT) ## Create all releases (according to tags)
$(ARROW) Copying releases to $(RELEASES_DIR) folder in $(RELEASES_FMT) format
$(DBG_FLAG)mkdir -p $(RELEASES_DIR)
$(DBG_FLAG)for tag in $$($(GIT) tag); do\
echo "Processing $$tag"; \
$(GIT) archive --format=$(RELEASES_FMT) \
--prefix=$$tag/ $$tag > $(RELEASES_DIR)/$$tag.$(RELEASES_FMT); \
done
# File: dist.m4
# Distribution directory
DIST_DIR ?= dist
# ============
# Distribution
# ============
#
# Create a distribution folder wit the bare minimum to compile your project.
# For example it will consider the files in the DEPENDENCIES variable, so make
# sure to update or add DEPENDENCIES to it in the config.mk per user
# configuration.
#
dist: $(BUILD_DOCUMENT) ## Create a dist folder with the bare minimum to compile
$(ECHO) $(call print-cmd-name,mkdir) $(DIST_DIR)
$(DBG_FLAG)mkdir -p $(DIST_DIR)
$(ECHO) $(call print-cmd-name,cp) $(DIST_DIR)/Makefile
$(DBG_FLAG)cp Makefile $(DIST_DIR)/
$(ECHO) $(call print-cmd-name,cp) $(DIST_DIR)/$(BUILD_DOCUMENT)
$(DBG_FLAG)cp $(BUILD_DOCUMENT) $(DIST_DIR)/
$(ARROW) "Copying bib files"
$(DBG_FLAG)test -n "$(BIBTEX_FILES)" && {\
for bibfile in $(BIBTEX_FILES); do \
mkdir -p $(DIST_DIR)/$$(dirname $$bibfile); \
cp -u $$bibfile $(DIST_DIR)/$$(dirname $$bibfile); \
done \
} || echo "No bibfiles"
$(ARROW) "Creating folder for dependencies"
$(DBG_FLAG)echo $(DEPENDENCIES)\
| $(XARGS) -n1 dirname\
| $(XARGS) -n1 -I FF mkdir -p $(DIST_DIR)/FF
$(ARROW) "Copying dependencies"
-$(DBG_FLAG)echo $(DEPENDENCIES)\
| $(TR) " " "\n" \
| $(XARGS) -n1 -I FF cp -r FF $(DIST_DIR)/FF
ifneq ($(strip $(PACKAGES_FILES)),)
$(ARROW) "Creating folder for latex libraries"
$(DBG_FLAG)test -n "$(PACKAGES_FILES)" && echo $(PACKAGES_FILES)\
| $(XARGS) -n1 dirname\
| $(XARGS) -n1 -I FF mkdir -p $(DIST_DIR)/FF
$(ARROW) "Copying latex libraries"
$(DBG_FLAG)test -n "$(PACKAGES_FILES)" && echo $(PACKAGES_FILES)\
| $(TR) " " "\n" \
| $(XARGS) -n1 -I FF cp FF $(DIST_DIR)/FF
endif
# ==================
# Distribution clean
# ==================
#
# Clean distribution files
#
dist-clean: CLEAN_FILES=$(DIST_DIR) ## Clean distribution files
dist-clean: clean
# File: merge.m4
# Name of the merged file
MERGE_FILE = merged.tex
# =====
# Merge
# =====
#
# Merge all include files into one single tex file
#
merge: $(MERGE_FILE) ## Create a merged file
$(MERGE_FILE): $(TEXFILES)
$(ECHO) $(call print-cmd-name,CP) $@
$(DBG_FLAG)cp $(MAIN_SRC) $@
$(ECHO) $(call print-cmd-name,m4) $(@)
$(DBG_FLAG)$(FD_OUTPUT)for texfile in $(TEXFILES); do\
cat $@ | \
$(removeTexComments) | \
$(SED) "s/[\\]in\(put\|clude\)\s*{\(.*\)}/include(\2)/" | \
m4 | tee $@; \
done
# Directory for merged distribution
MERGE_DIST_DIR = merged_$(DIST_DIR)
# ===================
# Merged distribution
# ===================
#
# Create a distribution with only a tex file
#
merge-dist: merge ## Create a merged file distribution
$(DBG_FLAG)$(MAKE) --no-print-directory \
dist MAIN_SRC=$(MERGE_FILE) DIST_DIR=$(MERGE_DIST_DIR)
# File: diff.m4
.PHONY: diff
# For creating differences in a repository
LATEXDIFF ?= latexdiff-git
# Commits to compute the difference from
DIFF ?=HEAD HEAD~1
NEW_COMMIT = $(word 1,$(DIFF))
OLD_COMMIT = $(word 2,$(DIFF))
DIFF_BUILD_DIR_MAIN ?= diffs
DIFF_BUILD_DIR ?= $(DIFF_BUILD_DIR_MAIN)/$(NEW_COMMIT)_$(OLD_COMMIT)
DIFF_SRC_NAME ?= diff.tex
# ====
# Diff
# ====
#
# This target creates differences between older versions of the main latex file
# by means of [GIT](https://git-scm.com/). You have to specify the commits that
# you want to compare by doing
#
# ```bash
# make DIFF="HEAD HEAD~3" diff
# ```
# If you want to compare the HEAD commit with the commit three times older than
# HEAD. You can also provide a *commit hash*. The default value is `HEAD HEAD~1`.
#
# The target creates a distribution folder located in the variable
# `DIFF_BUILD_DIR`.
diff: ## Create a latexdiff using git versions
$(ARROW) Creating diff between $(NEW_COMMIT) and $(OLD_COMMIT)
$(DBG_FLAG){ \
temp=$$(mktemp -d); \
$(LATEXDIFF) \
-r $(NEW_COMMIT) \
-r $(OLD_COMMIT) $(MAIN_SRC) -d $${temp} $(FD_OUTPUT); \
cp $${temp}/$(MAIN_SRC) $(DIFF_SRC_NAME); \
} $(FD_OUTPUT)
$(ARROW) Building in $(DIFF_BUILD_DIR)
$(DBG_FLAG)$(MAKE) dist \
BUILD_DIR=$(DIFF_BUILD_DIR) \
MAIN_SRC=$(DIFF_SRC_NAME) \
DIST_DIR=$(DIFF_BUILD_DIR)
# File: spelling.m4
# Speller program to use
SPELLER ?= aspell
# Directory to store spelling related information
SPELL_DIR ?= .spell
# Language for the spelling program
SPELL_LANG ?= en
# Wether or not spelling should be checked