latex.mkの推敲
[makefiles.git] / latex_mk / latex.mk
index 19b15ad..bef47ce 100644 (file)
 # all: $(TEXTARGETS)
 #
 # include latex.mk
+
+#debug
+#SHELL = /bin/sh -x
+
 .PHONY: tex-warning tex-clean tex-distclean
 
 # シェルコマンド
@@ -46,6 +50,7 @@ DVIPDFMX := dvipdfmx
 EXTRACTBB := extractbb
 BIBTEX := pbibtex
 MENDEX := mendex
+KPSEWHICH := kpsewhich
 
 #LaTeXオプション
 LATEXFLAG ?=
@@ -60,128 +65,100 @@ tex-warning:
 # ファイル名から拡張子を除いた部分
 BASE = $(basename $<)
 
-# TeX中間ファイルの拡張子
-#   .aux: 相互参照
-#   .fls: tex -recorderで生成されるファイルリスト
+# .aux、.fls以外のTeX中間ファイルの拡張子
 #   .lof: 図リスト(\tableoffigures)
 #   .lot: 表リスト(\tableoftables)
 #   .out: hyperrefパッケージ
 #   .toc: 目次(\tableofcontents)
-#   .log: ログ
-TEX_INT := .aux .fls .lof .lot .out .toc .log
+TEX_INT := .lof .lot .out .toc
 # 索引中間ファイルの拡張子
 #   .idx: auxから作成
 #   .ind: idxから作成
-#   .ilg: 索引ログ
-IND_INT := .idx .ind .ilg
+IND_INT := .idx .ind
 # BiBTeX中間ファイルの拡張子
 #   .bbl: auxから作成
-#   .blg: BiBTeXログ
 BIB_INT := .bbl .blg
+#   .log: ログ
+#   .ilg: 索引ログ
+#   .blg: BiBTeXログ
+LOG := .log .ilg .blg
 
-ALL_INTERFILES = $(addprefix *,$(TEX_INT) $(IND_INT) $(BIB_INT) .d .*_prev)
+ALL_INTERFILES = $(addprefix *,.aux $(TEX_INT) $(IND_INT) $(BIB_INT) $(LOG) .fls .d .*_prev)
 
+# make完了後、中間ファイルを残す
 .SECONDARY: $(wildcard ALL_INTERFILES)
 
-# \tableofcontents命令をTeXファイルから検索する
-toc = \
-  $(shell \
-    $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
-    $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
-    $(SED) -n -e 's/.*\(\\tableofcontents\)/\1/p' \
-  )
-
-# \listoffigures命令をTeXファイルから検索する
-lof = \
-  $(shell \
-    $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
-    $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
-    $(SED) -n -e 's/.*\(\\listoffigures\)/\1/p' \
-  )
-
-# \listoftables命令をTeXファイルから検索する
-lot = \
-  $(shell \
-     $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
-     $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
-     $(SED) -n -e 's/.*\(\\listoftables\)/\1/p' \
-   )
+# ファイル名から拡張子を除いた部分
+BASE = $(basename $<)
 
-# \makeindex命令をTeXファイルから検索する
-makeindex = \
-  $(shell \
-    $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
-    $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
-    $(SED) -n -e 's/.*\(\\makeindex\)/\1/p' \
-  )
+# .flsファイルから、INPUTファイルを取得。ただし、$TEXMFDISTのファイルを除く
+# 取得は、1回のmake実行につき1回だけ行われる
+INPUTFILES = $(INPUTFILESre)
 
-# \bibliography命令で読み込まれる文献データベースファイルをTeXファイルから検索する
-bibdb = \
-  $(addsuffix .bib,$(basename $(strip $(shell \
-     $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
-     $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
-     $(SED) -n -e 's/\\bibliography\(\[[^]]*\]\)\{0,1\}{[^}]*}/&\n/pg' | \
-     $(SED) -n -e 's/.*{\([^}]*\)}$$/\1/p' | \
-     $(SED) -e 's/,/ /g' \
-   ))))
+INPUTFILESre = $(eval INPUTFILES := \
+  $(sort $(filter-out $(BASE).tex $(BASE).aux, $(shell \
+    $(SED) -n -e 's/^INPUT \(.\{1,\}\)/\1/p' $(BASE).fls | \
+    $(GREP) -v `$(KPSEWHICH) -expand-var '$$TEXMFROOT'` \
+  ))))
+
+# .flsファイルから、OUTPUTファイルを取得。ただし、$TEXMFDISTのファイルを除く
+# 取得は、1回のmake実行につき1回だけ行われる
+OUTPUTFILES =  $(OUTFILESre)
 
-# hyperrefパッケージ読み込みをTeXファイルから検索する
-hyperref = \
-  $(shell \
-    $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
-    $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
-    $(SED) -n -e 's/.*\(\\usepackage\(\[[^]]*\]\)\{0,1\}{hyperref}\)/\1/p'\
-  )
+OUTFILESre = $(eval OUTPUTFILES := \
+  $(sort $(filter-out $(BASE).aux $(BASE).dvi $(BASE).log,$(shell \
+    $(SED) -n -e 's/^OUTPUT \(.\{1,\}\)/\1/p' $(BASE).fls | \
+    $(GREP) -v `$(KPSEWHICH) -expand-var '$$TEXMFROOT'` \
+  ))))
 
-# $(BASE).texで使われるLaTeX中間ファイル
+# $(BASE).texで読み込まれる中間ファイルを$(BASE).flsから取得する
+# .idxは、.indへ置換
 INTERFILES = \
-  $(strip \
-    $(if $(toc),$(BASE).toc) \
-    $(if $(lof),$(BASE).lof) \
-    $(if $(lot),$(BASE).lot) \
-    $(if $(makeindex),$(BASE).ind) \
-    $(if $(bibdb),$(BASE).bbl) \
-    $(if $(hyperref),$(BASE).out) \
-  )
+  $(sort $(subst .idx,.ind, \
+    $(filter $(addprefix $(BASE),$(TEX_INT) $(IND_INT) $(BIB_INT)),$(INPUTFILES) $(OUTPUTFILES)) \
+  ))
 
 INTERFILES_PREV = $(addsuffix _prev,$(INTERFILES))
 
-# \include命令で読み込まれるTeXファイル
-includetex = \
-  $(strip $(addsuffix .tex,$(shell \
-    $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex | \
-    $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
-    $(SED) -n -e 's/\\include\(\[[^]]*\]\)\{0,1\}{[^}]*}/&\n/pg' | \
-    $(SED) -n -e 's/.*{\([^}]*\)}$$/\1/p' \
-  )))
-
-# \input命令で読み込まれるTeXファイル
-define get_inputtex
-  $(strip $(shell \
-    $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $1 | \
-    $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
-    $(SED) -n -e 's/\\input\(\[[^]]*\]\)\{0,1\}{[^}]*}/&\n/pg' | \
-    $(SED) -n -e 's/.*{\([^}]*\)}$$/\1/p' \
-  ))
-endef
+# TeXファイル - .tex
+TEXFILES = $(filter %.tex,$(INPUTFILES))
 
-inputtex = $(call get_inputtex,$(BASE).tex $(includetex))
+# 対応する画像ファイルの拡張子
+GRAPHICSEXT := .pdf .eps .jpg .jpeg .png .bmp
 
-# \include命令または\input命令で読み込まれるTeXファイル
-intex = $(strip $(includetex) $(inputtex))
+# $(BASE).texで読み込まれる画像ファイルを取得する
+GRAPHICFILES = $(GRAPHICFILESre)
 
-# \includegraphics命令で読み込まれるグラフィックファイル
-ingraphics = \
-  $(strip $(shell \
-    $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(intex) | \
-    $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
-    $(SED) -n -e 's/\\includegraphics\(\[[^]]*\]\)\{0,1\}{[^}]*}/&\n/pg' | \
-    $(SED) -n -e 's/.*{\([^}]*\)}$$/\1/p' \
-  ))
+GRAPHICFILESre = $(eval GRAPHICFILES := \
+  $(sort \
+    $(shell \
+      $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(TEXFILES) | \
+      $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
+      $(SED) -n -e 's/\\includegraphics\(\[[^]]*\]\)\{0,1\}{[^}]*}/&\n/pg' | \
+      $(SED) -n -e 's/.*{\([^}]*\)}$$/\1/p' \
+    ) \
+    $(filter $(addprefix %,$(GRAPHICSEXT)),$(INPUTFILES)) \
+))
+
+# そのほかの読み込みファイル
+OTHERFILES = \
+  $(sort $(filter-out %.aux $(INTERFILES) $(TEXFILES) $(GRAPHICFILES),$(INPUTFILES)))
+
+# \bibliography命令で読み込まれる文献データベースファイルをTeXファイルから検索する
+BIBDB = $(BIBDBre)
+
+BIBDBre = $(eval BIBDB := \
+  $(addsuffix .bib,$(basename $(sort $(shell \
+     $(SED) -e '/^\s*%/d' -e 's/\([^\]\)\s*%.*/\1/g' $(BASE).tex $(TEXFILES) | \
+     $(SED) -e '/\\begin{verbatim}/,/\\end{verbatim}/d' | \
+     $(SED) -n -e 's/\\bibliography\(\[[^]]*\]\)\{0,1\}{[^}]*}/&\n/pg' | \
+     $(SED) -n -e 's/.*{\([^}]*\)}$$/\1/p' | \
+     $(SED) -e 's/,/ /g' \
+   )))))
 
 # LaTeX処理(コンパイル)
 LATEXCMD = $(LATEX) -interaction=batchmode $(LATEXFLAG) $(BASE).tex
-COMPILE.tex = $(ECHO) $(LATEXCMD); $(LATEXCMD) >/dev/null 2>&1 || ($(CAT) $(BASE).log; exit 1)
+COMPILE.tex = $(ECHO) $(LATEXCMD); $(LATEXCMD) >/dev/null 2>&1 || ($(SED) -n -e '/^!/,/^$$/p' $(BASE).log; exit 1)
 
 # 相互参照未定義の警告
 WARN_UNDEFREF := 'There were undefined references\.'
@@ -202,7 +179,7 @@ COMPILES.tex = \
 # DVI -> PDF
 # 出力結果は.logファイルへ出力
 DVIPDFCMD = $(DVIPDFMX) $(DVIPDFMXFLAG) $(BASE).dvi
-COMPILE.dvi = $(ECHO) $(DVIPDFCMD); $(DVIPDFCMD) >>$(BASE).log 2>&1 || ($(CAT) $(BASE).log; exit 1)
+COMPILE.dvi = $(ECHO) $(DVIPDFCMD); $(DVIPDFCMD) >>$(BASE).log 2>&1 || ($(SED) -n -e '/^Output written on toc_hyperref.dvi/,$$p' $(BASE).log; exit 1)
 
 # 索引中間ファイル(.ind)作成
 MENDEXCMD = $(MENDEX) $(MENDEXFLAG) $(BASE).idx
@@ -215,48 +192,62 @@ COMPILE.bib = $(ECHO) $(BIBTEXCMD); $(BIBTEXCMD) >/dev/null 2>&1 || ($(CAT) $(BA
 # ターゲットファイルと必須ファイルを比較し、内容が異なる場合はターゲットファイルの内容を必須ファイルに置き換える
 CMPPREV = $(CMP) $@ $< || $(CP) -p -v $< $@
 
+# $(BASE).flsファイルの作成
+FLSCMD = $(LATEX) -interaction=nonstopmode -recorder $(BASE).tex
+CREATE.fls = \
+  $(FLSCMD) 1>/dev/null 2>&1; \
+  test -e $(BASE).fls && $(ECHO) '$(BASE).fls is created.'; \
+  $(RM) $(addprefix $(BASE),.aux .dvi .log $(TEX_INT) $(IND_INT) $(BIB_INT))
+
 # 依存関係を.dファイルに書き出す
-%.d: %.tex
-       @$(ECHO) '$@ is created by scanning $<.'
-# .dファイルの依存関係
-       @$(ECHO) '$(BASE).d: $(BASE).tex' >$@
-# 中間ファイルの依存関係
-       $(if $(INTERFILES),@( \
+%.d: %.fls
+       @$(ECHO) '$@ is created by scanning $(BASE).tex and $(BASE).fls.'
+    # .dファイルの依存関係
+       @$(ECHO) '$(BASE).d: $(BASE).tex $(BASE).fls' >$@
+    # \includeまたは\input命令で読み込まれるTeXファイルの依存関係
+       $(if $(TEXFILES),@( \
       $(ECHO); \
-      $(ECHO) '# LaTeX Intermediate Files'; \
-      $(ECHO) '$(BASE).dvi:: $(INTERFILES_PREV)'; \
-      $(ECHO) '        @$$(COMPILE.tex)'; \
+      $(ECHO) '# Files called from \include or \input - .tex'; \
+      $(ECHO) '$(BASE).aux: $(TEXFILES)'; \
+    ) >>$@)
+    # そのほかのファイル(TEXMFROOT以外にあるスタイルファイルなど)の依存関係
+       $(if $(OTHERFILES),@( \
       $(ECHO); \
-      $(ECHO) '$(BASE).dvi:: $(BASE).aux'; \
-      $(ECHO) '        @$$(COMPILES.tex)'; \
+      $(ECHO) '# Other files'; \
+      $(ECHO) '$(BASE).aux: $(OTHERFILES)'; \
     ) >>$@)
-# 画像ファイルの依存関係
-       $(if $(ingraphics),@( \
+    # 画像ファイルの依存関係
+       $(if $(GRAPHICFILES),@( \
       $(ECHO); \
       $(ECHO) '# IncludeGraphic Files - .pdf, .eps, .jpeg/.jpg, .png'; \
       $(ECHO) '#           .xbb Files - .pdf, .jpeg/.jpg, .png'; \
-      $(ECHO) '$(BASE).aux: $(ingraphics)'; \
-      $(if $(filter-out %.eps,$(ingraphics)), \
+      $(ECHO) '$(BASE).aux: $(GRAPHICFILES)'; \
+      $(if $(filter-out %.eps,$(GRAPHICFILES)), \
         $(ECHO); \
-        $(ECHO) '$(BASE).aux: $(addsuffix .xbb,$(basename $(filter-out %.eps,$(ingraphics))))'; \
+        $(ECHO) '$(BASE).aux: $(addsuffix .xbb,$(basename $(filter-out %.eps,$(GRAPHICFILES))))'; \
       ) \
     ) >>$@)
-       $(if $(intex),@( \
+    # 文献処理用ファイルの依存関係
+       $(if $(BIBDB),@( \
+        $(ECHO); \
+        $(ECHO) '# Bibliography files: .aux, BIBDB -> .bbl -> .div'; \
+        $(ECHO) '$(BASE).bbl: $(BIBDB) $(BASE).tex'; \
+      ) >>$@)
+    # 中間ファイルの依存関係
+       $(if $(sort $(INTERFILES) $(BIBDB)),@( \
       $(ECHO); \
-      $(ECHO) '# Files called from \include or \input - .tex'; \
-      $(ECHO) '$(BASE).aux: $(intex)'; \
-    ) >>$@)
-# 文献処理用ファイルの依存関係
-       $(if $(bibdb),@( \
+      $(ECHO) '# LaTeX Intermediate Files'; \
+      $(ECHO) '$(BASE).dvi:: $(sort $(INTERFILES_PREV) $(if $(BIBDB),$(BASE).bbl_prev))'; \
+      $(ECHO) '        @$$(COMPILE.tex)'; \
       $(ECHO); \
-      $(ECHO) '# Bibliography files: .aux, BIBDB -> .bbl -> .div'; \
-      $(ECHO) '$(BASE).bbl: $(bibdb) $(BASE).tex'; \
+      $(ECHO) '$(BASE).dvi:: $(BASE).aux'; \
+      $(ECHO) '        @$$(COMPILES.tex)'; \
     ) >>$@)
 
 # 変数TEXTARGETSで指定されたターゲットファイルに対応する
 # .dファイルをインクルードし、依存関係を取得する
 # ターゲット末尾に clean、.xbb、.tex、.d が含まれている場合は除く
-ifeq (,$(filter %clean %.xbb %.tex %.d,$(MAKECMDGOALS)))
+ifeq (,$(filter %clean %.xbb %.tex %.d %.fls %.fls_prev,$(MAKECMDGOALS)))
   -include $(addsuffix .d,$(basename $(TEXTARGETS)))
 endif
 
@@ -277,6 +268,9 @@ endif
 
 # バウンディング情報ファイル作成
 # pdf、jpeg/jpg、pngファイルに対応
+extractbb:
+       $(MAKE) -s $(addsuffix .xbb,$(basename $(wildcard $(addprefix *,.pdf .jpg .jpeg .png))))
+
 %.xbb: %.pdf
        $(EXTRACTBB) $(EXTRACTBBFLAGS) $<
 
@@ -289,6 +283,13 @@ endif
 %.xbb: %.png
        $(EXTRACTBB) $(EXTRACTBBFLAGS) $<
 
+# ファイル一覧作成
+%.fls: %.tex
+       @-$(CREATE.fls)
+
+%.fls_prev: %.fls
+       @$(CMPPREV)
+
 # 目次中間ファイル作成
 %.toc: %.tex
        @$(MAKE) -s $(BASE).aux