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

4.115
Last change on this file since dd43d0b was dd43d0b, checked in by Chris Johns <chrisj@…>, on 10/30/16 at 21:48:21

waf: Add an install command.

  • Property mode set to 100644
File size: 7.5 KB
Line 
1import sys, os, re
2from waflib.Build import BuildContext
3
4sphinx_min_version = (1,3)
5
6
7def cmd_spell(ctx):
8        from waflib import Options
9        from sys import argv
10        from subprocess import call
11
12        Options.commands = None # stop warnings about knowing commands.
13
14        if not ctx.env.BIN_ASPELL:
15                ctx.fatal("'aspell' is required please add binary to your path and re-run configure.")
16
17        if len(argv) < 3:
18                ctx.fatal("Please supply at least one file name")
19
20        files = argv[2:]
21
22        path = ctx.path.parent.abspath()
23
24        # XXX: add error checking eg check if file exists.
25        for file in files:
26                cmd = ctx.env.BIN_ASPELL + ["-c", "--personal=%s/common/spell/dict/rtems" % path, "--extra-dicts=%s/common/spell/en_GB-ise-w_accents.multi" % path, file]
27
28                print("running:", cmd)
29                call(cmd)
30
31
32def cmd_linkcheck(ctx, conf_dir=".", source_dir="."):
33        ctx(
34                rule    = "${BIN_SPHINX_BUILD} -b linkcheck -c %s -j %d -d build/doctrees %s build/linkcheck" % (conf_dir, ctx.options.jobs, source_dir),
35                cwd             = ctx.path.abspath(),
36                source  = ctx.path.ant_glob('**/*.rst'),
37                target  = "linkcheck/output.txt"
38        )
39
40
41class spell(BuildContext):
42        __doc__ = "Check spelling.  Supply a list of files or a glob (*.rst)"
43        cmd = 'spell'
44        fun = 'cmd_spell'
45
46
47class linkcheck(BuildContext):
48        __doc__ = "Check all external URL references."
49        cmd = 'linkcheck'
50        fun = 'cmd_linkcheck'
51
52
53def check_sphinx_version(ctx, minver):
54        version = ctx.cmd_and_log(ctx.env.BIN_SPHINX_BUILD + ['--version']).split(" ")[-1:][0].strip()
55        try:
56                ver = tuple(map(int, re.split('[\D]', version)))
57        except:
58                ctx.fatal("Sphinx version cannot be checked: %s" % version)
59        if ver < minver:
60                ctx.fatal("Sphinx version is too old: %s" % ".".join(map(str, ver)))
61
62        return ver
63
64def sphinx_verbose(ctx):
65        return ctx.options.sphinx_verbose
66
67def cmd_configure(ctx):
68        ctx.load('tex')
69
70        ctx.env.append_value('PDFLATEXFLAGS', '-shell-escape')
71
72        ctx.find_program("sphinx-build", var="BIN_SPHINX_BUILD", mandatory=True)
73        ctx.find_program("aspell", var="BIN_ASPELL", mandatory=False)
74        ctx.find_program("inliner", var="BIN_INLINER", mandatory=False)
75
76        ctx.start_msg("Checking if Sphinx is at least %s.%s" % sphinx_min_version)
77        ver = check_sphinx_version(ctx, sphinx_min_version)
78
79        ctx.end_msg("yes (%s)" % ".".join(map(str, ver)))
80
81
82def html_resources(ctx):
83        for dir_name in ["_static", "_templates"]:
84                files = ctx.path.parent.find_node("common").ant_glob("%s/*" % dir_name)
85                fnode = ctx.path.get_bld().make_node(os.path.join('html', dir_name))
86                fnode.mkdir() # dirs
87                ctx(
88                        features = "subst",
89                        is_copy  = True,
90                        source   = files,
91                        target   = [fnode.make_node(x.name) for x in files]
92                )
93
94        # copy images
95#       ctx.path.get_bld().make_node("images").mkdir()
96#       files = ctx.path.parent.ant_glob("images/**")
97#       ctx(
98#               features    = "subst",
99#               is_copy     = True,
100#               source      = files,
101#               target      = [x.srcpath().replace("../", "") for x in files]
102#       )
103
104def doc_pdf(ctx, source_dir, conf_dir):
105        if not ctx.env.PDFLATEX or not ctx.env.MAKEINDEX:
106                ctx.fatal('The programs pdflatex and makeindex are required')
107
108        build_dir = ctx.path.get_bld().relpath()
109        output_node = ctx.path.get_bld().make_node('latex')
110        output_dir = output_node.abspath()
111
112        ctx(
113                rule         = "${BIN_SPHINX_BUILD} %s -b latex -c %s -d build/%s/doctrees %s %s" % (sphinx_verbose(ctx), conf_dir, build_dir, source_dir, output_dir),
114                cwd          = ctx.path,
115                source       = ctx.path.ant_glob('**/*.rst'),
116                target       = ctx.path.find_or_declare("latex/%s.tex" % (ctx.path.name))
117        )
118
119        ctx.add_group()
120
121        ctx(
122                features     = 'tex',
123                cwd          = output_dir,
124                type         = 'pdflatex',
125                source       = "latex/%s.tex" % ctx.path.name,
126                prompt       = 0
127        )
128
129        ctx.install_files('${PREFIX}/%s' % (ctx.path.name),
130                          'latex/%s.pdf' % (ctx.path.name),
131                          cwd = output_node,
132                          quiet = True)
133
134def doc_singlehtml(ctx, source_dir, conf_dir):
135        if not ctx.env.BIN_INLINER:
136                ctx.fatal("Node inliner is required install with 'npm install -g inliner' (https://github.com/remy/inliner)")
137
138        html_resources(ctx)
139
140        ctx(
141                rule    = "${BIN_SPHINX_BUILD} -b singlehtml -c %s -j %d -d build/doctrees %s build/singlehtml" % (conf_dir, ctx.options.jobs, source_dir),
142                cwd             = ctx.path.abspath(),
143                source  = ctx.path.ant_glob('**/*.rst'),
144                target  = "singlehtml/index.html",
145                install_path = None
146        )
147
148        ctx.add_group()
149
150        ctx(
151                rule    = "${BIN_INLINER} ${SRC} > ${TGT}",
152                source  = "singlehtml/index.html",
153                target  = "singlehtml/%s.html" % ctx.path.name,
154                install_path = None
155        )
156
157def doc_html(ctx, conf_dir, source_dir):
158
159        html_resources(ctx)
160
161        build_dir = ctx.path.get_bld().relpath()
162        output_node = ctx.path.get_bld().make_node('html')
163        output_dir = output_node.abspath()
164
165        ctx(
166                rule         = "${BIN_SPHINX_BUILD} %s -b html -c %s -d build/%s/doctrees %s %s" % (sphinx_verbose(ctx), conf_dir, build_dir, source_dir, output_dir),
167                cwd          = ctx.path,
168                source       =  ctx.path.ant_glob('**/*.rst'),# + ctx.path.ant_glob('conf.py'),
169                target       = ctx.path.find_or_declare('html/index.html'),
170                install_path = None
171        )
172
173        ctx.install_files('${PREFIX}/%s' % (ctx.path.name),
174                          output_node.ant_glob('**/*'),
175                          cwd = output_node,
176                          relative_trick = True,
177                          quiet = True)
178
179def is_top_build(ctx):
180        from_top = False
181        if ctx.env['BUILD_FROM_TOP'] and ctx.env['BUILD_FROM_TOP'] == 'yes':
182                from_top = True
183        return from_top
184
185def build_type(ctx):
186        build_type = 'html'
187        if ctx.options.pdf:
188                build_type = 'pdf'
189        return build_type
190
191def build_dir_setup(ctx):
192        btype = build_type(ctx)
193        where = btype
194        if is_top_build(ctx):
195                where = os.path.join(ctx.path.name, where)
196        bnode = ctx.bldnode.find_node(where)
197        if bnode is None:
198                ctx.bldnode.make_node(where).mkdir()
199        return where
200
201def cmd_build(ctx, conf_dir = ".", source_dir = "."):
202        build_dir_setup(ctx)
203
204        srcnode = ctx.srcnode.abspath()
205
206        if ctx.options.pdf:
207                doc_pdf(ctx, source_dir, conf_dir)
208        elif ctx.options.singlehtml:
209                html_resources(ctx)
210                doc_singlehtml(ctx, source_dir, conf_dir)
211        else:
212                doc_html(ctx, source_dir, conf_dir)
213
214def cmd_options(ctx):
215        ctx.add_option('--sphinx-verbose', action='store', default="-Q", help="Sphinx verbose.")
216        ctx.add_option('--pdf', action='store_true', default=False, help="Build PDF.")
217        ctx.add_option('--singlehtml', action='store_true', default=False, help="Build Single HTML file, requires Node Inliner")
218
219
220def cmd_options_path(ctx):
221        cmd_options(ctx)
222        ctx.add_option('--rtems-path-py', type='string', help="Full path to py/ in RTEMS source repository.")
223
224
225def cmd_configure_path(ctx):
226        if not ctx.options.rtems_path_py:
227                ctx.fatal("--rtems-path-py is required")
228
229        ctx.env.RTEMS_PATH = ctx.options.rtems_path_py
230
231        cmd_configure(ctx)
232
233
234CONF_FRAG = """
235sys.path.append(os.path.abspath('../../common/'))
236sys.path.append('%s')
237templates_path = ['_templates']
238html_static_path = ['_static']
239"""
240
241
242# XXX: fix this ugly hack.  No time to waste on it.
243def cmd_build_path(ctx):
244        def run(task):
245
246                with open("conf.py") as fp:
247                        conf = "import sys, os\nsys.path.append(os.path.abspath('../../common/'))\n"
248                        conf += fp.read()
249
250                task.inputs[0].abspath()
251                task.outputs[0].write(conf + (CONF_FRAG % ctx.env.RTEMS_PATH))
252
253        ctx(
254                rule   = run,
255                source = [ctx.path.parent.find_node("common/conf.py"), ctx.path.find_node("./conf.py")],
256                target = ctx.path.get_bld().make_node('conf.py')
257    )
258
259        cmd_build(ctx, conf_dir="build", source_dir="build")
Note: See TracBrowser for help on using the repository browser.