blob: 7fccd64f30571817b8646c26550e0250a250e177 [file] [log] [blame]
Alexander Afanasyev1160baa2014-04-10 18:50:29 -07001# inspired by code by Hans-Martin von Gaudecker, 2012
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -08002
Davide Pesaventoac537892024-03-11 20:30:08 -04003"""Support for Sphinx documentation"""
4
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -08005import os
Davide Pesaventocbfc3642024-03-11 20:40:52 -04006from waflib import Task, TaskGen
Davide Pesaventob62f9522024-03-11 17:50:53 -04007
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -08008
Alexander Afanasyev1160baa2014-04-10 18:50:29 -07009class sphinx_build(Task.Task):
10 color = 'BLUE'
Davide Pesaventocbfc3642024-03-11 20:40:52 -040011 run_str = '${SPHINX_BUILD} -q -b ${BUILDERNAME} -D ${VERSION} -D ${RELEASE} -d ${DOCTREEDIR} ${SRCDIR} ${OUTDIR}'
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080012
Davide Pesaventocbfc3642024-03-11 20:40:52 -040013 def keyword(self):
14 return f'Processing ({self.env.BUILDERNAME})'
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080015
Davide Pesaventob62f9522024-03-11 17:50:53 -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 Pesaventoac537892024-03-11 20:30:08 -040028@TaskGen.feature('sphinx')
29@TaskGen.before_method('process_source')
Davide Pesaventocbfc3642024-03-11 20:40:52 -040030def process_sphinx(self):
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070031 """Set up the task generator with a Sphinx instance and create a task."""
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080032
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070033 conf = self.path.find_node(self.config)
Davide Pesaventocbfc3642024-03-11 20:40:52 -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 Afanasyeva1ae0a12014-01-28 15:21:02 -080039
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070040 confdir = conf.parent.abspath()
Davide Pesaventob310efb2019-04-11 22:10:24 -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 Pesaventocbfc3642024-03-11 20:40:52 -040045 release = getattr(self, 'release', self.version)
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080046
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070047 task.env['BUILDERNAME'] = buildername
48 task.env['SRCDIR'] = srcdir
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070049 task.env['OUTDIR'] = outdir.abspath()
Davide Pesaventob62f9522024-03-11 17:50:53 -040050 task.env['DOCTREEDIR'] = doctreedir
Davide Pesaventocbfc3642024-03-11 20:40:52 -040051 task.env['VERSION'] = f'version={self.version}'
52 task.env['RELEASE'] = f'release={release}'
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080053
Davide Pesaventob310efb2019-04-11 22:10:24 -040054 if buildername == 'man':
Davide Pesaventob62f9522024-03-11 17:50:53 -040055 confdata = load_source('sphinx_conf', conf.abspath())
56 for i in confdata.man_pages:
Davide Pesaventocbfc3642024-03-11 20:40:52 -040057 target = outdir.find_or_declare(f'{i[1]}.{i[4]}')
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070058 task.outputs.append(target)
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070059 if self.install_path:
Davide Pesaventocbfc3642024-03-11 20:40:52 -040060 self.bld.install_files(f'{self.install_path}/man{i[4]}/', target)
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070061 else:
62 task.outputs.append(outdir)
63
Davide Pesaventocbfc3642024-03-11 20:40:52 -040064 # prevent process_source from complaining that there is no extension mapping for .rst files
65 self.source = []
66
Davide Pesaventob62f9522024-03-11 17:50:53 -040067
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070068def configure(conf):
Davide Pesaventoac537892024-03-11 20:30:08 -040069 """Check if sphinx-build program is available."""
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070070 conf.find_program('sphinx-build', var='SPHINX_BUILD', mandatory=False)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070071
Davide Pesaventob62f9522024-03-11 17:50:53 -040072
Davide Pesaventoac537892024-03-11 20:30:08 -040073# sphinx command
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070074from waflib.Build import BuildContext
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070075class sphinx(BuildContext):
Davide Pesaventoac537892024-03-11 20:30:08 -040076 cmd = 'sphinx'
77 fun = 'sphinx'