blob: 7fccd64f30571817b8646c26550e0250a250e177 [file] [log] [blame]
Beichuan Zhang55b8ed42014-04-26 22:25:44 -07001# inspired by code by Hans-Martin von Gaudecker, 2012
Alexander Afanasyev49272f72014-04-06 21:49:46 -07002
Davide Pesavento0d89d412024-03-13 14:20:02 -04003"""Support for Sphinx documentation"""
4
Alexander Afanasyev49272f72014-04-06 21:49:46 -07005import os
Davide Pesavento150dbcb2024-03-13 14:28:24 -04006from waflib import Task, TaskGen
Davide Pesavento0e7f7342024-03-13 14:00:36 -04007
Alexander Afanasyev49272f72014-04-06 21:49:46 -07008
9class sphinx_build(Task.Task):
10 color = 'BLUE'
Davide Pesavento150dbcb2024-03-13 14:28:24 -040011 run_str = '${SPHINX_BUILD} -q -b ${BUILDERNAME} -D ${VERSION} -D ${RELEASE} -d ${DOCTREEDIR} ${SRCDIR} ${OUTDIR}'
Alexander Afanasyev49272f72014-04-06 21:49:46 -070012
Davide Pesavento150dbcb2024-03-13 14:28:24 -040013 def keyword(self):
14 return f'Processing ({self.env.BUILDERNAME})'
Alexander Afanasyev49272f72014-04-06 21:49:46 -070015
Davide Pesavento0e7f7342024-03-13 14:00:36 -040016
17# from https://docs.python.org/3.12/whatsnew/3.12.html#imp
18def load_source(modname, filename):
19 import importlib.util
20 from importlib.machinery import SourceFileLoader
21 loader = SourceFileLoader(modname, filename)
22 spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
23 module = importlib.util.module_from_spec(spec)
24 loader.exec_module(module)
25 return module
26
27
Davide Pesavento0d89d412024-03-13 14:20:02 -040028@TaskGen.feature('sphinx')
29@TaskGen.before_method('process_source')
Davide Pesavento150dbcb2024-03-13 14:28:24 -040030def process_sphinx(self):
Alexander Afanasyev49272f72014-04-06 21:49:46 -070031 """Set up the task generator with a Sphinx instance and create a task."""
32
Alexander Afanasyev49272f72014-04-06 21:49:46 -070033 conf = self.path.find_node(self.config)
Davide Pesavento150dbcb2024-03-13 14:28:24 -040034 if not conf:
35 self.bld.fatal(f'Sphinx configuration file {repr(self.config)} not found')
36
37 inputs = [conf] + self.to_nodes(self.source)
38 task = self.create_task('sphinx_build', inputs, always_run=getattr(self, 'always', False))
Alexander Afanasyev49272f72014-04-06 21:49:46 -070039
40 confdir = conf.parent.abspath()
Davide Pesavento08b91c82019-04-13 19:42:10 -040041 buildername = getattr(self, 'builder', 'html')
42 srcdir = getattr(self, 'srcdir', confdir)
43 outdir = self.path.find_or_declare(getattr(self, 'outdir', buildername)).get_bld()
44 doctreedir = getattr(self, 'doctreedir', os.path.join(outdir.abspath(), '.doctrees'))
Davide Pesavento150dbcb2024-03-13 14:28:24 -040045 release = getattr(self, 'release', self.version)
Alexander Afanasyev49272f72014-04-06 21:49:46 -070046
47 task.env['BUILDERNAME'] = buildername
48 task.env['SRCDIR'] = srcdir
Alexander Afanasyev49272f72014-04-06 21:49:46 -070049 task.env['OUTDIR'] = outdir.abspath()
Davide Pesavento0e7f7342024-03-13 14:00:36 -040050 task.env['DOCTREEDIR'] = doctreedir
Davide Pesavento150dbcb2024-03-13 14:28:24 -040051 task.env['VERSION'] = f'version={self.version}'
52 task.env['RELEASE'] = f'release={release}'
Alexander Afanasyev49272f72014-04-06 21:49:46 -070053
Davide Pesavento08b91c82019-04-13 19:42:10 -040054 if buildername == 'man':
Davide Pesavento0e7f7342024-03-13 14:00:36 -040055 confdata = load_source('sphinx_conf', conf.abspath())
56 for i in confdata.man_pages:
Davide Pesavento150dbcb2024-03-13 14:28:24 -040057 target = outdir.find_or_declare(f'{i[1]}.{i[4]}')
Alexander Afanasyev49272f72014-04-06 21:49:46 -070058 task.outputs.append(target)
Alexander Afanasyev49272f72014-04-06 21:49:46 -070059 if self.install_path:
Davide Pesavento150dbcb2024-03-13 14:28:24 -040060 self.bld.install_files(f'{self.install_path}/man{i[4]}/', target)
Alexander Afanasyev49272f72014-04-06 21:49:46 -070061 else:
62 task.outputs.append(outdir)
63
Davide Pesavento150dbcb2024-03-13 14:28:24 -040064 # prevent process_source from complaining that there is no extension mapping for .rst files
65 self.source = []
66
Davide Pesavento0e7f7342024-03-13 14:00:36 -040067
Alexander Afanasyev49272f72014-04-06 21:49:46 -070068def configure(conf):
Davide Pesavento0d89d412024-03-13 14:20:02 -040069 """Check if sphinx-build program is available."""
Alexander Afanasyev49272f72014-04-06 21:49:46 -070070 conf.find_program('sphinx-build', var='SPHINX_BUILD', mandatory=False)
71
Davide Pesavento0e7f7342024-03-13 14:00:36 -040072
Davide Pesavento0d89d412024-03-13 14:20:02 -040073# sphinx command
Alexander Afanasyev49272f72014-04-06 21:49:46 -070074from waflib.Build import BuildContext
75class sphinx(BuildContext):
Davide Pesavento0d89d412024-03-13 14:20:02 -040076 cmd = 'sphinx'
77 fun = 'sphinx'