source: rtems-docs/common/waf.py @ 8127604

4.11
Last change on this file since 8127604 was 8127604, checked in by Chris Johns <chrisj@…>, on 03/20/17 at 00:42:56

Use a single top level version number.

Fix the path in the catalogue links to allow prefix testing on a local
disk.

Close #2940.

  • Property mode set to 100644
File size: 14.7 KB
Line 
1import sys, os, re
2from waflib.Build import BuildContext
3
4import latex
5
6sphinx_min_version = (1, 3)
7
8def build_date():
9    import datetime
10    now = datetime.date.today()
11    m = now.strftime('%B')
12    y = now.strftime('%Y')
13    if now.day % 10 == 1:
14        s = 'st'
15    elif now.day % 10 == 2:
16        s = 'nd'
17    elif now.day == 3:
18        s = 'rd'
19    else:
20        s = 'th'
21    d = '%2d%s' % (now.day, s)
22    return '%s %s %s' % (d, m, y)
23
24def cmd_spell(ctx):
25    from waflib import Options
26    from sys import argv
27    from subprocess import call
28
29    Options.commands = None # stop warnings about knowing commands.
30
31    if not ctx.env.BIN_ASPELL:
32        ctx.fatal("'aspell' is required please install and re-run configure.")
33
34    if len(argv) < 3:
35        ctx.fatal("Please supply at least one file name")
36
37    files = argv[2:]
38
39    path = ctx.path.parent.abspath()
40
41    # XXX: add error checking eg check if file exists.
42    for file in files:
43        cmd = ctx.env.BIN_ASPELL + \
44              ["-c",
45               "--personal=%s/common/spell/dict/rtems" % path,
46               "--extra-dicts=%s/common/spell/en_GB-ise-w_accents.multi" % path,
47               file]
48        print("running:", cmd)
49        call(cmd)
50
51
52def cmd_linkcheck(ctx, conf_dir=".", source_dir="."):
53    ctx_rule = "${BIN_SPHINX_BUILD} -b linkcheck -c %s -j %d " + \
54               "-d build/doctrees %s build/linkcheck" % (conf_dir,
55                                                         ctx.options.jobs,
56                                                             source_dir)
57    ctx(
58        rule   = ctx_rule,
59        cwd    = ctx.path.abspath(),
60        source = ctx.path.ant_glob('**/*.rst'),
61        target = "linkcheck/output.txt"
62    )
63
64class spell(BuildContext):
65    __doc__ = "Check spelling.  Supply a list of files or a glob (*.rst)"
66    cmd = 'spell'
67    fun = 'cmd_spell'
68
69class linkcheck(BuildContext):
70    __doc__ = "Check all external URL references."
71    cmd = 'linkcheck'
72    fun = 'cmd_linkcheck'
73
74def check_sphinx_version(ctx, minver):
75    version = ctx.cmd_and_log(ctx.env.BIN_SPHINX_BUILD +
76                              ['--version']).split(" ")[-1:][0].strip()
77    try:
78        ver = tuple(map(int, re.split('[\D]', version)))
79    except:
80        ctx.fatal("Sphinx version cannot be checked: %s" % version)
81    if ver < minver:
82        ctx.fatal("Sphinx version is too old: %s" % ".".join(map(str, ver)))
83    return ver
84
85def sphinx_verbose(ctx):
86    return ' '.join(ctx.env.SPHINX_VERBOSE)
87
88def is_top_build(ctx):
89    from_top = False
90    if ctx.env['BUILD_FROM_TOP'] and ctx.env['BUILD_FROM_TOP'] == 'yes':
91        from_top = True
92    return from_top
93
94def build_dir_setup(ctx, buildtype):
95    where = buildtype
96    if is_top_build(ctx):
97        where = os.path.join(ctx.path.name, where)
98    bnode = ctx.bldnode.find_node(where)
99    if bnode is None:
100        ctx.bldnode.make_node(where).mkdir()
101    build_dir = ctx.path.get_bld().relpath()
102    output_node = ctx.path.get_bld().make_node(buildtype)
103    output_dir = output_node.abspath()
104    doctrees = os.path.join(os.path.dirname(output_dir), 'doctrees', buildtype)
105    return build_dir, output_node, output_dir, doctrees
106
107def version_cmdline(ctx):
108    return "-Drelease='%s' -Dversion='%s'" % (ctx.env.VERSION, ctx.env.VERSION)
109
110def pdf_resources(ctx, buildtype):
111    packages_base = ctx.path.parent.find_dir('common/latex')
112    if packages_base is None:
113        ctx.fatal('Latex package directory not found')
114    base = packages_base.path_from(ctx.path)
115    fnode = ctx.path.get_bld().make_node(buildtype)
116    fnode.mkdir()
117    local_packages = latex.local_packages()
118    if local_packages is not None:
119        srcs = [os.path.join(base, p) for p in local_packages]
120        ctx(
121            features = "subst",
122            is_copy  = True,
123            source   = srcs,
124            target   = [fnode.make_node(p) for p in local_packages]
125        )
126    ctx(
127        features = "subst",
128        is_copy  = True,
129        source   = os.path.join(base, ctx.env.RTEMSEXTRAFONTS),
130        target   = fnode.make_node('rtemsextrafonts.sty')
131    )
132
133def html_resources(ctx, buildtype):
134    for dir_name in ["_static", "_templates"]:
135        files = ctx.path.parent.find_node("common").ant_glob("%s/*" % dir_name)
136        fnode = ctx.path.get_bld().make_node(os.path.join(buildtype, dir_name))
137        fnode.mkdir() # dirs
138        ctx(
139            features = "subst",
140            is_copy  = True,
141            source   = files,
142            target   = [fnode.make_node(x.name) for x in files]
143        )
144
145    # copy images
146#    ctx.path.get_bld().make_node("images").mkdir()
147#    files = ctx.path.parent.ant_glob("images/**")
148#    ctx(
149#        features    = "subst",
150#        is_copy     = True,
151#        source      = files,
152#        target      = [x.srcpath().replace("../", "") for x in files]
153#    )
154
155
156def cmd_configure(ctx):
157    ctx.msg('Checking version', ctx.env.VERSION)
158
159    ctx.find_program("sphinx-build", var="BIN_SPHINX_BUILD", mandatory = True)
160    ctx.find_program("aspell", var = "BIN_ASPELL", mandatory = False)
161
162    ctx.start_msg("Checking if Sphinx is at least %s.%s" % sphinx_min_version)
163    ver = check_sphinx_version(ctx, sphinx_min_version)
164    ctx.end_msg("yes (%s)" % ".".join(map(str, ver)))
165
166    ctx.start_msg("Sphinx Verbose: ")
167    if 'SPHINX_VERBOSE' not in ctx.env:
168        ctx.env.append_value('SPHINX_VERBOSE', ctx.options.sphinx_verbose)
169    level = sphinx_verbose(ctx)
170    if level == '-Q':
171        level = 'quiet'
172    ctx.end_msg(level)
173
174    #
175    # Optional builds.
176    #
177    ctx.env.BUILD_PDF = 'no'
178    if ctx.options.pdf:
179        check_tex = not ctx.env.PDFLATEX
180        if check_tex:
181            ctx.load('tex')
182            if not ctx.env.PDFLATEX or not ctx.env.MAKEINDEX:
183                ctx.fatal('The programs pdflatex and makeindex are required for PDF output')
184            if 'PDFLATEXFLAGS' not in ctx.env or \
185               '-shell-escape' not in ctx.env['PDFLATEXFLAGS']:
186                ctx.env.append_value('PDFLATEXFLAGS', '-shell-escape')
187            latex.configure_tests(ctx)
188        else:
189            ctx.msg('Check for Tex packages', 'skipping, already checked')
190        ctx.env.BUILD_PDF = 'yes'
191
192    ctx.envBUILD_SINGLEHTML = 'no'
193    if ctx.options.singlehtml:
194        ctx.env.BUILD_SINGLEHTML = 'yes'
195        ctx.find_program("inliner", var = "BIN_INLINER", mandatory = False)
196        if not ctx.env.BIN_INLINER:
197            ctx.fatal("Node inliner is required install with 'npm install -g inliner' " +
198                      "(https://github.com/remy/inliner)")
199
200def doc_pdf(ctx, source_dir, conf_dir):
201    buildtype = 'latex'
202    build_dir, output_node, output_dir, doctrees = build_dir_setup(ctx, buildtype)
203    pdf_resources(ctx, buildtype)
204    rule = "${BIN_SPHINX_BUILD} %s -b %s -c %s %s -d %s %s %s" % \
205           (sphinx_verbose(ctx), buildtype, conf_dir, version_cmdline(ctx),
206            doctrees, source_dir, output_dir)
207    ctx(
208        rule         = rule,
209        cwd          = ctx.path,
210        source       = ctx.path.ant_glob('**/*.rst'),
211        target       = ctx.path.find_or_declare("%s/%s.tex" % (buildtype,
212                                                               ctx.path.name))
213    )
214    ctx(
215        features     = 'tex',
216        cwd          = output_dir,
217        type         = 'pdflatex',
218        source       = "%s/%s.tex" % (buildtype, ctx.path.name),
219        prompt       = 0
220    )
221    ctx.install_files('${PREFIX}',
222                      '%s/%s.pdf' % (buildtype, ctx.path.name),
223                      cwd = output_node,
224                      quiet = True)
225
226def doc_singlehtml(ctx, source_dir, conf_dir):
227    #
228    # Use a run command to handle stdout and stderr output from inliner. Using
229    # a standard rule in the build context locks up.
230    #
231    def run(task):
232        src = task.inputs[0].abspath()
233        tgt = task.outputs[0].abspath()
234        cmd = '%s %s' % (task.env.BIN_INLINER[0], src)
235        so = open(tgt, 'w')
236        se = open(tgt + '.err', 'w')
237        r = task.exec_command(cmd, stdout = so, stderr = se)
238        so.close()
239        se.close()
240        #
241        # The inliner does not handle internal href's correctly and places the
242        # input's file name in the href. Strip these.
243        #
244        with open(tgt, 'r') as i:
245            before = i.read()
246            after = before.replace('index.html', '')
247        i.close()
248        with open(tgt, 'w') as o:
249            o.write(after)
250        o.close()
251        return r
252
253    buildtype = 'singlehtml'
254    build_dir, output_node, output_dir, doctrees = build_dir_setup(ctx, buildtype)
255    html_resources(ctx, buildtype)
256    rule = "${BIN_SPHINX_BUILD} %s -b %s -c %s %s -d %s %s %s" % \
257           (sphinx_verbose(ctx), buildtype, conf_dir, version_cmdline(ctx),
258            doctrees, source_dir, output_dir)
259    ctx(
260        rule         = rule,
261        cwd          = ctx.path,
262        source       = ctx.path.ant_glob('**/*.rst'),
263        target       = ctx.path.find_or_declare("%s/index.html" % (buildtype)),
264        install_path = None
265    )
266    ctx(
267        rule         = run,
268        inliner      = ctx.env.BIN_INLINER,
269        source       = "%s/index.html" % buildtype,
270        target       = "%s/%s.html" % (buildtype, ctx.path.name),
271        install_path = '${PREFIX}'
272    )
273
274def doc_html(ctx, conf_dir, source_dir):
275    buildtype = 'html'
276    build_dir, output_node, output_dir, doctrees = build_dir_setup(ctx, buildtype)
277    html_resources(ctx, buildtype)
278    rule = "${BIN_SPHINX_BUILD} %s -b %s -c %s %s -d %s %s %s" % \
279           (sphinx_verbose(ctx), buildtype, conf_dir, version_cmdline(ctx),
280            doctrees, source_dir, output_dir)
281    ctx(
282        rule         = rule,
283        cwd          = ctx.path,
284        source       = ctx.path.ant_glob('**/*.rst'),
285        target       = ctx.path.find_or_declare('%s/index.html' % buildtype),
286        install_path = None
287    )
288    ctx.install_files('${PREFIX}/%s' % (ctx.path.name),
289                      output_node.ant_glob('**/*', quiet = True),
290                      cwd = output_node,
291                      relative_trick = True,
292                      quiet = True)
293
294def cmd_build(ctx, conf_dir = ".", source_dir = "."):
295    srcnode = ctx.srcnode.abspath()
296
297    if ctx.env.BUILD_PDF == 'yes':
298        doc_pdf(ctx, source_dir, conf_dir)
299
300    if ctx.env.BUILD_SINGLEHTML == 'yes':
301        doc_singlehtml(ctx, source_dir, conf_dir)
302
303    doc_html(ctx, source_dir, conf_dir)
304
305def cmd_options(ctx):
306    ctx.add_option('--disable-extra-fonts',
307                   action = 'store_true',
308                   default = False,
309                   help = "Disable building with extra fonts for better quality (lower quality).")
310    ctx.add_option('--sphinx-verbose',
311                   action = 'store',
312                   default = "-Q",
313                   help = "Sphinx verbose.")
314    ctx.add_option('--pdf',
315                   action='store_true',
316                   default = False,
317                   help = "Build PDF.")
318    ctx.add_option('--singlehtml',
319                   action='store_true',
320                   default = False,
321                   help = "Build Single HTML file, requires Node Inliner")
322
323def cmd_options_path(ctx):
324    cmd_options(ctx)
325    ctx.add_option('--rtems-path-py',
326                   type = 'string',
327                   help = "Full path to py/ in RTEMS source repository.")
328
329def cmd_configure_path(ctx):
330    if not ctx.options.rtems_path_py:
331        ctx.fatal("--rtems-path-py is required")
332
333    ctx.env.RTEMS_PATH = ctx.options.rtems_path_py
334
335    cmd_configure(ctx)
336
337def xml_catalogue(ctx, building):
338    #
339    # The following is a hack to find the top_dir because the task does
340    # provided a reference to top_dir like a build context.
341    #
342    top_dir = ctx.get_cwd().find_node('..')
343    #
344    # Read the conf.py files in each directory to gather the doc details.
345    #
346    catalogue = {}
347    sp = sys.path[:]
348    for doc in building:
349        sys.path.insert(0, top_dir.find_node(doc).abspath())
350        #
351        # Import using the imp API so the module is reloaded for us.
352        #
353        import imp
354        mf = imp.find_module('conf')
355        try:
356            bconf = imp.load_module('bconf', mf[0], mf[1], mf[2])
357        finally:
358            mf[0].close()
359        sys.path = sp[:]
360        catalogue[doc] = {
361            'title': bconf.project,
362            'version': str(ctx.env.VERSION),
363            'release': str(ctx.env.VERSION),
364            'pdf': bconf.latex_documents[0][1].replace('.tex', '.pdf'),
365            'html': '%s/index.html' % (doc),
366            'singlehtml': '%s.html' % (doc)
367        }
368        bconf = None
369
370    import xml.dom.minidom as xml
371    cat = xml.Document()
372
373    root = cat.createElement('rtems-docs')
374    root.setAttribute('date', build_date())
375    cat.appendChild(root)
376
377    heading = cat.createElement('catalogue')
378    text = cat.createTextNode(str(ctx.env.VERSION))
379    heading.appendChild(text)
380    root.appendChild(heading)
381
382    builds = ['html']
383    if ctx.env.BUILD_PDF == 'yes':
384        builds += ['pdf']
385    if ctx.env.BUILD_SINGLEHTML == 'yes':
386        builds += ['singlehtml']
387
388    for d in building:
389        doc = cat.createElement('doc')
390        name = cat.createElement('name')
391        text = cat.createTextNode(d)
392        name.appendChild(text)
393        title = cat.createElement('title')
394        text = cat.createTextNode(catalogue[d]['title'])
395        title.appendChild(text)
396        release = cat.createElement('release')
397        text = cat.createTextNode(catalogue[d]['release'])
398        release.appendChild(text)
399        version = cat.createElement('version')
400        text = cat.createTextNode(catalogue[d]['version'])
401        version.appendChild(text)
402        doc.appendChild(name)
403        doc.appendChild(title)
404        doc.appendChild(release)
405        doc.appendChild(version)
406        for b in builds:
407            output = cat.createElement(b)
408            text = cat.createTextNode(catalogue[d][b])
409            output.appendChild(text)
410            doc.appendChild(output)
411        root.appendChild(doc)
412
413    catnode = ctx.get_cwd().make_node('catalogue.xml')
414    catnode.write(cat.toprettyxml(indent = ' ' * 2, newl = os.linesep))
415
416    cat.unlink()
417
418CONF_FRAG = """
419sys.path.append(os.path.abspath('../../common/'))
420sys.path.append('%s')
421templates_path = ['_templates']
422html_static_path = ['_static']
423"""
424
425# XXX: fix this ugly hack.  No time to waste on it.
426def cmd_build_path(ctx):
427    def run(task):
428
429        with open("conf.py") as fp:
430            conf = "import sys, os\nsys.path.append(os.path.abspath('../../common/'))\n"
431            conf += fp.read()
432
433        task.inputs[0].abspath()
434        task.outputs[0].write(conf + (CONF_FRAG % ctx.env.RTEMS_PATH))
435
436    ctx(
437        rule   = run,
438        source = [ctx.path.parent.find_node("common/conf.py"),
439                  ctx.path.find_node("./conf.py")],
440        target = ctx.path.get_bld().make_node('conf.py')
441    )
442
443    cmd_build(ctx, conf_dir = "build", source_dir = "build")
Note: See TracBrowser for help on using the repository browser.