Branch for User's Guide changes.
[scons.git] / doc / SConscript
1 #
2 # SConscript file for building SCons documentation.
3 #
4
5 #
6 # Copyright (c) 2001, 2002, 2003 Steven Knight
7 #
8 # Permission is hereby granted, free of charge, to any person obtaining
9 # a copy of this software and associated documentation files (the
10 # "Software"), to deal in the Software without restriction, including
11 # without limitation the rights to use, copy, modify, merge, publish,
12 # distribute, sublicense, and/or sell copies of the Software, and to
13 # permit persons to whom the Software is furnished to do so, subject to
14 # the following conditions:
15 #
16 # The above copyright notice and this permission notice shall be included
17 # in all copies or substantial portions of the Software.
18 #
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
20 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
21 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #
27
28 import os.path
29 import re
30 import string
31
32 Import('env', 'whereis')
33
34 build = os.path.join('#build', 'doc')
35
36 #
37 #
38 #
39 doc_tar_gz = os.path.join('#build',
40                           'dist',
41                           'scons-doc-%s.tar.gz' % env.Dictionary('VERSION'))
42
43 #
44 # We'll only try to build text files (for some documents)
45 # if lynx is available to do the dump.
46 #
47 fig2dev = whereis('fig2dev')
48 groff = whereis('groff')
49 lynx = whereis('lynx')
50 man2html = whereis('man2html')
51 jade = whereis('jade')
52 jadetex = whereis('jadetex')
53 pdfjadetex = whereis('pdfjadetex')
54 jw = whereis('jw')
55 tidy = whereis('tidy')
56
57 tar_deps = []
58 tar_list = []
59
60 entity_re = re.compile(r'<!entity\s+(?:%\s+)?(?:\S+)\s+SYSTEM\s+"([^"]*)">', re.I)
61 format_re = re.compile(r'<(?:graphic|imagedata)\s+fileref="([^"]*)"(?:\s+format="([^"]*)")?')
62
63 #
64 # Find internal dependencies in .sgml files:
65 #
66 #   <!entity bground SYSTEM "bground.sgml">
67 #   <graphic fileref="file.jpg">
68 #   <imagedata fileref="file.jpg">
69 #
70 # This only finds one per line, and assumes that anything
71 # defined as a SYSTEM entity is, in fact, a file included
72 # somewhere in the document.
73 #
74 def scansgml(node, env, target):
75     includes = []
76
77     contents = node.get_contents()
78
79     includes.extend(entity_re.findall(contents))
80
81     matches = format_re.findall(contents)
82     for m in matches:
83         file, format = m
84         if format and file[-len(format):] != format:
85             file = file + format
86         if not os.path.isabs(file):
87             a = []
88             f = file
89             while 1:
90                 f, tail = os.path.split(f)
91                 if tail == 'doc':
92                     break
93                 a = [tail] + a
94             file = apply(os.path.join, a, {})
95         includes.append(file)
96
97     return includes
98
99 s = Scanner(name = 'sgml', function = scansgml, skeys = ['.sgml', '.mod'])
100 env = env.Copy(SCANNERS = [s])
101
102 if jw:
103     #
104     # Always create a version.sgml file containing the version information
105     # for this run.  Ignore it for dependency purposes so we don't
106     # rebuild all the docs every time just because the date changes.
107     #
108     date, ver, rev = env.Dictionary('DATE', 'VERSION', 'REVISION')
109     #version_sgml = File(os.path.join(build, "version.sgml"))
110     version_sgml = File("version.sgml")
111     verfile = str(version_sgml)
112     try:
113         os.unlink(verfile)
114     except OSError:
115         pass    # okay if the file didn't exist
116     dir, f = os.path.split(verfile)
117     try:
118         os.makedirs(dir)
119     except OSError:
120         pass    # okay if the directory already exists
121     open(verfile, "w").write("""<!--
122 THIS IS AN AUTOMATICALLY-GENERATED FILE.  DO NOT EDIT.
123 -->
124 <!ENTITY builddate "%s">
125 <!ENTITY buildversion "%s">
126 <!ENTITY buildrevision "%s">
127 """ % (date, ver, rev))
128
129     #
130     # Each document will live in its own subdirectory.  List them here
131     # as hash keys, with a hash of the info to control its build.
132     #
133     docs = {
134         'design' : {
135                 'htmlindex' : 'book1.html',
136                 'ps'        : 1,
137                 'pdf'       : 1,
138                 'text'      : 0,
139         },
140         'python10' : {
141                 'htmlindex' : 't1.html',
142                 'html'      : 1,
143                 'ps'        : 1,
144                 'pdf'       : 0,
145                 'text'      : 0,
146                 'graphics'  : [ 'arch', 'builder', 'job-task', 'node', 'scanner', 'sig' ],
147         },
148         'reference' : {
149                 'htmlindex' : 'book1.html',
150                 'html'      : 1,
151                 'ps'        : 1,
152                 'pdf'       : 1,
153                 'text'      : 0,
154         },
155         'user' : {
156                 'htmlindex' : 'book1.html',
157                 'html'      : 1,
158                 'ps'        : 1,
159                 'pdf'       : 1,
160                 'text'      : 0,
161         },
162     }
163
164     #
165     # We have to tell SCons to scan the top-level SGML files which
166     # get included by the document SGML files in the subdirectories.
167     #
168     included_sgml = [
169         'scons.mod',
170         'copyright.sgml',
171     ]
172
173     #
174     # For each document, build the document itself in HTML, Postscript,
175     # and PDF formats.
176     #
177     for doc in docs.keys():
178         main = os.path.join(doc, 'main.sgml')
179         out = 'main.out'
180
181         # Hard-coding the scons-src path is a bit of a hack.  This can
182         # be reworked when a better solution presents itself.
183         scons_src_main = os.path.join('#build', 'scons-src', 'doc', main)
184         env.Ignore(scons_src_main, version_sgml)
185
186         htmldir = os.path.join(build, 'HTML', 'scons-%s' % doc)
187         htmlindex = os.path.join(htmldir, docs[doc]['htmlindex'])
188         html = os.path.join(build, 'HTML', 'scons-%s.html' % doc)
189         ps = os.path.join(build, 'PS', 'scons-%s.ps' % doc)
190         pdf = os.path.join(build, 'PDF', 'scons-%s.pdf' % doc)
191         text = os.path.join(build, 'TEXT', 'scons-%s.txt' % doc)
192
193         if docs[doc].get('html') and jade:
194             cmds = [
195                 "rm -f ${TARGET.dir}/*.html",
196                 "jw -b html -o ${TARGET.dir} $SOURCES",
197                 "mv -v ${TARGET.dir}/index.html $TARGET || true",
198             ]
199             if tidy:
200                 cmds.append("tidy -m -q $TARGET || true")
201             env.Command(htmlindex, main, cmds)
202             Local(htmlindex)
203
204             cmds = [
205                 "rm -f ${TARGET.dir}/main.html",
206                 "jw -u -b html -o ${TARGET.dir} $SOURCES",
207                 "mv -v ${TARGET.dir}/main.html $TARGET || true",
208             ]
209             if tidy:
210                 cmds.append("tidy -m -q $TARGET || true")
211             env.Command(html, main, cmds)
212             Local(html)
213
214             env.Ignore([html, htmlindex], version_sgml)
215
216             tar_deps.extend([html, htmlindex])
217             tar_list.extend([html, htmldir])
218
219             if fig2dev:
220                 for g in docs[doc].get('graphics', []):
221                     fig = os.path.join(doc, '%s.fig' % g)
222                     jpg = os.path.join(htmldir, '%s.jpg' % g)
223                     env.Command(jpg, fig,
224                                 "%s -L jpeg -q 100 $SOURCES $TARGET" % fig2dev)
225                     env.Depends(html, jpg)
226                     Local(jpg)
227
228         if docs[doc].get('ps') and jadetex:
229             env.Command(ps, main, [
230                 "rm -f ${TARGET.dir}/%s" % out,
231                 "jw -b ps -o ${TARGET.dir} $SOURCES",
232                 "mv ${TARGET.dir}/main.ps $TARGET",
233                 "rm -f ${TARGET.dir}/%s" % out,
234             ])
235             Local(ps)
236
237             env.Ignore(ps, version_sgml)
238
239             tar_deps.append(ps)
240             tar_list.append(ps)
241
242             if fig2dev:
243                 for g in docs[doc].get('graphics', []):
244                     fig = os.path.join(doc, '%s.fig' % g)
245                     eps = os.path.join(build, 'PS', '%s.eps' % g)
246                     env.Command(eps, fig, "%s -L eps $SOURCES $TARGET" % fig2dev)
247                     env.Depends(ps, eps)
248                     Local(eps)
249
250         if docs[doc].get('pdf') and pdfjadetex:
251             env.Command(pdf, main, [
252                 "rm -f ${TARGET.dir}/%s" % out,
253                 "jw -b pdf -o ${TARGET.dir} $SOURCES",
254                 "mv ${TARGET.dir}/main.pdf $TARGET",
255                 "rm -f ${TARGET.dir}/out",
256             ])
257             Local(pdf)
258
259             env.Ignore(pdf, version_sgml)
260
261             tar_deps.append(pdf)
262             tar_list.append(pdf)
263
264         if docs[doc].get('text') and jade and lynx:
265             env.Command(text, html, "lynx -dump ${SOURCE.abspath} > $TARGET")
266             Local(text)
267
268             env.Ignore(text, version_sgml)
269
270             tar_deps.append(text)
271             tar_list.append(text)
272
273 #
274 # Man page(s), in good ol' troff format.
275 #
276 man_page_list = ['scons', 'sconsign']
277
278 for man in man_page_list:
279     man_1 = os.path.join('man', '%s.1' % man)
280
281     if groff:
282         ps = os.path.join(build, 'PS', '%s-man.ps' % man)
283         text = os.path.join(build, 'TEXT', '%s-man.txt' % man)
284
285         env.Command(ps, man_1, "groff -man -Tps $SOURCES > $TARGET")
286         Local(ps)
287
288         env.Command(text, man_1, "groff -man -Tascii $SOURCES > $TARGET")
289         Local(text)
290
291         tar_deps.extend([ps, text])
292         tar_list.extend([ps, text])
293
294     if man2html:
295         html = os.path.join(build, 'HTML' , '%s-man.html' % man)
296
297         cmds = [ "man2html $SOURCES > $TARGET" ]
298         if tidy:
299             cmds.append("tidy -m -q $TARGET || true")
300         env.Command(html, man_1, cmds)
301         Local(html)
302
303         tar_deps.append(html)
304         tar_list.append(html)
305
306 #
307 # Now actually create the tar file of the documentation,
308 # for easy distribution to the web site.
309 #
310 if tar_deps:
311     tar_list = map(lambda x: x[11:], tar_list)
312     env.Command(doc_tar_gz, tar_deps,
313                 "tar cf${TAR_HFLAG} - -C build/doc %s | gzip > $TARGET" % tar_list)
314     Local(doc_tar_gz)