Merged revisions 1675-1736 via svnmerge from
[scons.git] / src / engine / SCons / Tool / tex.py
1 """SCons.Tool.tex
2
3 Tool-specific initialization for TeX.
4
5 There normally shouldn't be any need to import this module directly.
6 It will usually be imported through the generic SCons.Tool.Tool()
7 selection method.
8
9 """
10
11 #
12 # __COPYRIGHT__
13 #
14 # Permission is hereby granted, free of charge, to any person obtaining
15 # a copy of this software and associated documentation files (the
16 # "Software"), to deal in the Software without restriction, including
17 # without limitation the rights to use, copy, modify, merge, publish,
18 # distribute, sublicense, and/or sell copies of the Software, and to
19 # permit persons to whom the Software is furnished to do so, subject to
20 # the following conditions:
21 #
22 # The above copyright notice and this permission notice shall be included
23 # in all copies or substantial portions of the Software.
24 #
25 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
26 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
27 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 #
33
34 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
35
36 import os.path
37 import re
38 import string
39
40 import SCons.Action
41 import SCons.Node
42 import SCons.Node.FS
43 import SCons.Util
44
45 warning_rerun_re = re.compile("^LaTeX Warning:.*Rerun", re.MULTILINE)
46
47 rerun_citations_str = "^LaTeX Warning:.*\n.*Rerun to get citations correct"
48 rerun_citations_re = re.compile(rerun_citations_str, re.MULTILINE)
49
50 undefined_references_str = '(^LaTeX Warning:.*undefined references)|(^Package \w+ Warning:.*undefined citations)'
51 undefined_references_re = re.compile(undefined_references_str, re.MULTILINE)
52
53 openout_aux_re = re.compile(r"\\openout.*`(.*\.aux)'")
54
55 # An Action sufficient to build any generic tex file.
56 TeXAction = None
57
58 # An action to build a latex file.  This action might be needed more
59 # than once if we are dealing with labels and bibtex.
60 LaTeXAction = None
61
62 # An action to run BibTeX on a file.
63 BibTeXAction = None
64
65 # An action to run MakeIndex on a file.
66 MakeIndexAction = None
67
68 def InternalLaTeXAuxAction(XXXLaTeXAction, target = None, source= None, env=None):
69     """A builder for LaTeX files that checks the output in the aux file
70     and decides how many times to use LaTeXAction, and BibTeXAction."""
71
72     basename = SCons.Util.splitext(str(source[0]))[0]
73     basedir = os.path.split(str(source[0]))[0]
74
75     # Run LaTeX once to generate a new aux file.
76     XXXLaTeXAction(target, source, env)
77
78     # Decide if various things need to be run, or run again.  We check
79     # for the existence of files before opening them--even ones like the
80     # aux file that TeX always creates--to make it possible to write tests
81     # with stubs that don't necessarily generate all of the same files.
82
83     # Read the log file to find all .aux files
84     logfilename = basename + '.log'
85     auxfiles = []
86     if os.path.exists(logfilename):
87         content = open(logfilename, "rb").read()
88         auxfiles = openout_aux_re.findall(content)
89
90     # Now decide if bibtex will need to be run.
91     for auxfilename in auxfiles:
92         if os.path.exists(os.path.join(basedir, auxfilename)):
93             content = open(os.path.join(basedir, auxfilename), "rb").read()
94             if string.find(content, "bibdata") != -1:
95                 bibfile = env.fs.File(basename)
96                 BibTeXAction(bibfile, bibfile, env)
97                 break
98
99     # Now decide if makeindex will need to be run.
100     idxfilename = basename + '.idx'
101     if os.path.exists(idxfilename):
102         idxfile = env.fs.File(basename)
103         # TODO: if ( idxfile has changed) ...
104         MakeIndexAction(idxfile, idxfile, env)
105         XXXLaTeXAction(target, source, env)
106
107     # Now decide if latex will need to be run again due to table of contents.
108     tocfilename = basename + '.toc'
109     if os.path.exists(tocfilename):
110         # TODO: if ( tocfilename has changed) ...
111         XXXLaTeXAction(target, source, env)
112
113     # Now decide if latex needs to be run yet again.
114     logfilename = basename + '.log'
115     for trial in range(int(env.subst('$LATEXRETRIES'))):
116         if not os.path.exists(logfilename):
117             break
118         content = open(logfilename, "rb").read()
119         if not warning_rerun_re.search(content) and \
120            not rerun_citations_re.search(content) and \
121            not undefined_references_re.search(content):
122             break
123         XXXLaTeXAction(target, source, env)
124     return 0
125
126 def LaTeXAuxAction(target = None, source= None, env=None):
127     InternalLaTeXAuxAction( LaTeXAction, target, source, env )
128
129 LaTeX_re = re.compile("\\\\document(style|class)")
130
131 def is_LaTeX(flist):
132     # Scan a file list to decide if it's TeX- or LaTeX-flavored.
133     for f in flist:
134         content = f.get_contents()
135         if LaTeX_re.search(content):
136             return 1
137     return 0
138
139 def TeXLaTeXFunction(target = None, source= None, env=None):
140     """A builder for TeX and LaTeX that scans the source file to
141     decide the "flavor" of the source and then executes the appropriate
142     program."""
143     if is_LaTeX(source):
144         LaTeXAuxAction(target,source,env)
145     else:
146         TeXAction(target,source,env)
147     return 0
148
149 def tex_emitter(target, source, env):
150     base = SCons.Util.splitext(str(source[0]))[0]
151     target.append(base + '.aux')
152     env.Precious(base + '.aux')
153     target.append(base + '.log')
154     for f in source:
155         content = f.get_contents()
156         if string.find(content, r'\tableofcontents') != -1:
157             target.append(base + '.toc')
158         if string.find(content, r'\makeindex') != -1:
159             target.append(base + '.ilg')
160             target.append(base + '.ind')
161             target.append(base + '.idx')
162         if string.find(content, r'\bibliography') != -1:
163             target.append(base + '.bbl')
164             target.append(base + '.blg')
165
166     # read log file to get all .aux files
167     logfilename = base + '.log'
168     if os.path.exists(logfilename):
169         content = open(logfilename, "rb").read()
170         aux_files = openout_aux_re.findall(content)
171         aux_files = filter(lambda f, b=base+'.aux': f != b, aux_files)
172         dir = os.path.split(base)[0]
173         aux_files = map(lambda f, d=dir: d+os.sep+f, aux_files)
174         target.extend(aux_files)
175
176     return (target, source)
177
178 TeXLaTeXAction = None
179
180 def generate(env):
181     """Add Builders and construction variables for TeX to an Environment."""
182
183     # A generic tex file Action, sufficient for all tex files.
184     global TeXAction
185     if TeXAction is None:
186         TeXAction = SCons.Action.Action("$TEXCOM", "$TEXCOMSTR")
187
188     # An Action to build a latex file.  This might be needed more
189     # than once if we are dealing with labels and bibtex.
190     global LaTeXAction
191     if LaTeXAction is None:
192         LaTeXAction = SCons.Action.Action("$LATEXCOM", "$LATEXCOMSTR")
193
194     # Define an action to run BibTeX on a file.
195     global BibTeXAction
196     if BibTeXAction is None:
197         BibTeXAction = SCons.Action.Action("$BIBTEXCOM", "$BIBTEXCOMSTR")
198
199     # Define an action to run MakeIndex on a file.
200     global MakeIndexAction
201     if MakeIndexAction is None:
202         MakeIndexAction = SCons.Action.Action("$MAKEINDEXCOM", "$MAKEINDEXCOMSTR")
203
204     global TeXLaTeXAction
205     if TeXLaTeXAction is None:
206         TeXLaTeXAction = SCons.Action.Action(TeXLaTeXFunction, strfunction=None)
207
208     import dvi
209     dvi.generate(env)
210
211     bld = env['BUILDERS']['DVI']
212     bld.add_action('.tex', TeXLaTeXAction)
213     bld.add_emitter('.tex', tex_emitter)
214
215     env['TEX']      = 'tex'
216     env['TEXFLAGS'] = SCons.Util.CLVar('')
217     env['TEXCOM']   = 'cd ${TARGET.dir} && $TEX $TEXFLAGS ${SOURCE.file}'
218
219     # Duplicate from latex.py.  If latex.py goes away, then this is still OK.
220     env['LATEX']        = 'latex'
221     env['LATEXFLAGS']   = SCons.Util.CLVar('')
222     env['LATEXCOM']     = 'cd ${TARGET.dir} && $LATEX $LATEXFLAGS ${SOURCE.file}'
223     env['LATEXRETRIES'] = 3
224
225     env['BIBTEX']      = 'bibtex'
226     env['BIBTEXFLAGS'] = SCons.Util.CLVar('')
227     env['BIBTEXCOM']   = 'cd ${TARGET.dir} && $BIBTEX $BIBTEXFLAGS ${SOURCE.filebase}'
228
229     env['MAKEINDEX']      = 'makeindex'
230     env['MAKEINDEXFLAGS'] = SCons.Util.CLVar('')
231     env['MAKEINDEXCOM']   = 'cd ${TARGET.dir} && $MAKEINDEX $MAKEINDEXFLAGS ${SOURCE.file}'
232
233 def exists(env):
234     return env.Detect('tex')