Build: don't use an intermediate scons-doc.tar file.
[scons.git] / doc / SConscript
1 #
2 # SConscript file for building SCons documentation.
3 #
4 # THIS IS NOT READY YET.  DO NOT TRY TO BUILD SCons WITH ITSELF YET.
5 #
6
7 #
8 # Copyright (c) 2001, 2002 Steven Knight
9 #
10 # Permission is hereby granted, free of charge, to any person obtaining
11 # a copy of this software and associated documentation files (the
12 # "Software"), to deal in the Software without restriction, including
13 # without limitation the rights to use, copy, modify, merge, publish,
14 # distribute, sublicense, and/or sell copies of the Software, and to
15 # permit persons to whom the Software is furnished to do so, subject to
16 # the following conditions:
17 #
18 # The above copyright notice and this permission notice shall be included
19 # in all copies or substantial portions of the Software.
20 #
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
22 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
23 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #
29
30 import os.path
31 import re
32 import string
33
34 Import('env', 'whereis')
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     verfile = str(File("version.sgml"))
110     try:
111         os.unlink(verfile)
112     except:
113         pass
114     open(verfile, "w").write("""<!--
115 THIS IS AN AUTOMATICALLY-GENERATED FILE.  DO NOT EDIT.
116 -->
117 <!ENTITY builddate "%s">
118 <!ENTITY buildversion "%s">
119 <!ENTITY buildrevision "%s">
120 """ % (date, ver, rev))
121
122     #
123     # Each document will live in its own subdirectory.  List them here
124     # as hash keys, with a hash of the info to control its build.
125     #
126     docs = {
127         'design' : {
128                 'htmlindex' : 'book1.html',
129                 'ps'        : 1,
130                 'pdf'       : 1,
131                 'text'      : 0,
132         },
133         'python10' : {
134                 'htmlindex' : 't1.html',
135                 'html'      : 1,
136                 'ps'        : 1,
137                 'pdf'       : 0,
138                 'text'      : 0,
139                 'graphics'  : [ 'arch', 'builder', 'job-task', 'node', 'scanner', 'sig' ],
140         },
141         'user' : {
142                 'htmlindex' : 'book1.html',
143                 'html'      : 1,
144                 'ps'        : 1,
145                 'pdf'       : 1,
146                 'text'      : 0,
147         },
148     }
149
150     #
151     # We have to tell Cons to QuickScan the top-level SGML files which
152     # get included by the document SGML files in the subdirectories.
153     #
154     included_sgml = [
155         'scons.mod',
156         'copyright.sgml',
157     ]
158
159     #
160     # For each document, build the document itself in HTML, Postscript,
161     # and PDF formats.
162     #
163     for doc in docs.keys():
164         main = os.path.join(doc, 'main.sgml')
165         out = 'main.out'
166
167         htmldir = os.path.join('HTML', 'scons-%s' % doc)
168         htmlindex = os.path.join(htmldir, docs[doc]['htmlindex'])
169         html = os.path.join('HTML', 'scons-%s.html' % doc)
170         ps = os.path.join('PS', 'scons-%s.ps' % doc)
171         pdf = os.path.join('PDF', 'scons-%s.pdf' % doc)
172         text = os.path.join('TEXT', 'scons-%s.txt' % doc)
173
174         if docs[doc].get('html') and jade:
175             cmds = [
176                 "rm -f ${TARGET.dir}/*.html",
177                 "jw -b html -o ${TARGET.dir} $SOURCES",
178                 "mv -v ${TARGET.dir}/index.html $TARGET || true",
179             ]
180             if tidy:
181                 cmds.append("tidy -m -q $TARGET || true")
182             env.Command(htmlindex, main, cmds)
183
184             cmds = [
185                 "rm -f ${TARGET.dir}/main.html",
186                 "jw -u -b html -o ${TARGET.dir} $SOURCES",
187                 "mv -v ${TARGET.dir}/main.html $TARGET || true",
188             ]
189             if tidy:
190                 cmds.append("tidy -m -q $TARGET || true")
191             env.Command(html, main, cmds)
192
193             env.Ignore([html, htmlindex], "version.sgml")
194
195             tar_deps.extend([html, htmlindex])
196             tar_list = string.join([tar_list, html, htmldir], " ")
197
198             if fig2dev:
199                 for g in docs[doc].get('graphics', []):
200                     fig = os.path.join(doc, '%s.fig' % g)
201                     jpg = os.path.join(htmldir, '%s.jpg' % g)
202                     env.Command(jpg, fig,
203                                 "%s -L jpeg -q 100 $SOURCES $TARGET" % fig2dev)
204                     env.Depends(ps, jpg)
205
206         if docs[doc].get('ps') and jadetex:
207             env.Command(ps, main, [
208                 "rm -f ${TARGET.dir}/%s" % out,
209                 "jw -b ps -o ${TARGET.dir} $SOURCES",
210                 "mv ${TARGET.dir}/main.ps $TARGET",
211                 "rm -f ${TARGET.dir}/%s" % out,
212             ])
213
214             env.Ignore(ps, "version.sgml")
215
216             tar_deps.append(ps)
217             tar_list = tar_list + " " + ps
218
219             if fig2dev:
220                 for g in docs[doc].get('graphics', []):
221                     fig = os.path.join(doc, '%s.fig' % g)
222                     eps = os.path.join('PS', '%s.eps' % g)
223                     env.Command(eps, fig, "%s -L eps $SOURCES $TARGET" % fig2dev)
224                     env.Depends(ps, eps)
225
226         if docs[doc].get('pdf') and pdfjadetex:
227             env.Command(pdf, main, [
228                 "rm -f ${TARGET.dir}/%s" % out,
229                 "jw -b pdf -o ${TARGET.dir} $SOURCES",
230                 "mv ${TARGET.dir}/main.pdf $TARGET",
231                 "rm -f ${TARGET.dir}/out",
232             ])
233
234             env.Ignore(pdf, "version.sgml")
235
236             tar_deps.append(pdf)
237             tar_list = tar_list + " " + pdf
238
239         if docs[doc].get('text') and jade and lynx:
240             env.Command(text, html, "lynx -dump ${SOURCE.abspath} > $TARGET")
241
242             env.Ignore(text, "version.sgml")
243
244             tar_deps.append(text)
245             tar_list = tar_list + " " + text
246
247 #
248 # Man page(s), in good ol' troff format.
249 #
250 scons_1 = os.path.join('man', 'scons.1')
251
252 if groff:
253     ps = os.path.join('PS', 'scons-man.ps')
254     text = os.path.join('TEXT', 'scons-man.txt')
255
256     env.Command(ps, scons_1, "groff -man -Tps $SOURCES > $TARGET")
257
258     env.Command(text, scons_1, "groff -man -Tascii $SOURCES > $TARGET")
259
260     tar_deps.extend([ps, text])
261     tar_list = string.join([tar_list, ps, text], " ")
262
263 if man2html:
264     html = os.path.join('HTML' , 'scons-man.html')
265
266     cmds = [ "man2html $SOURCES > $TARGET" ]
267     if tidy:
268         cmds.append("tidy -m -q $TARGET || true")
269     env.Command(html, scons_1, cmds)
270
271     tar_deps.append(html)
272     tar_list = tar_list + " " + html
273
274 #
275 # Now actually create the tar file of the documentation,
276 # for easy distribution to the web site.
277 #
278 if tar_deps:
279     env.Command(doc_tar_gz, tar_deps,
280                 "tar cf${TAR_HFLAG} - -C build/doc %s | gzip > $TARGET" % tar_list)