Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 1 | # inspired by code by Hans-Martin von Gaudecker, 2012 |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 2 | |
Davide Pesavento | ac53789 | 2024-03-11 20:30:08 -0400 | [diff] [blame] | 3 | """Support for Sphinx documentation""" |
| 4 | |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 5 | import os |
Davide Pesavento | cbfc364 | 2024-03-11 20:40:52 -0400 | [diff] [blame] | 6 | from waflib import Task, TaskGen |
Davide Pesavento | b62f952 | 2024-03-11 17:50:53 -0400 | [diff] [blame] | 7 | |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 8 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 9 | class sphinx_build(Task.Task): |
| 10 | color = 'BLUE' |
Davide Pesavento | cbfc364 | 2024-03-11 20:40:52 -0400 | [diff] [blame] | 11 | run_str = '${SPHINX_BUILD} -q -b ${BUILDERNAME} -D ${VERSION} -D ${RELEASE} -d ${DOCTREEDIR} ${SRCDIR} ${OUTDIR}' |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 12 | |
Davide Pesavento | cbfc364 | 2024-03-11 20:40:52 -0400 | [diff] [blame] | 13 | def keyword(self): |
| 14 | return f'Processing ({self.env.BUILDERNAME})' |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 15 | |
Davide Pesavento | b62f952 | 2024-03-11 17:50:53 -0400 | [diff] [blame] | 16 | |
| 17 | # from https://docs.python.org/3.12/whatsnew/3.12.html#imp |
| 18 | def 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 Pesavento | ac53789 | 2024-03-11 20:30:08 -0400 | [diff] [blame] | 28 | @TaskGen.feature('sphinx') |
| 29 | @TaskGen.before_method('process_source') |
Davide Pesavento | cbfc364 | 2024-03-11 20:40:52 -0400 | [diff] [blame] | 30 | def process_sphinx(self): |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 31 | """Set up the task generator with a Sphinx instance and create a task.""" |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 33 | conf = self.path.find_node(self.config) |
Davide Pesavento | cbfc364 | 2024-03-11 20:40:52 -0400 | [diff] [blame] | 34 | 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 Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 39 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 40 | confdir = conf.parent.abspath() |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 41 | 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 Pesavento | cbfc364 | 2024-03-11 20:40:52 -0400 | [diff] [blame] | 45 | release = getattr(self, 'release', self.version) |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 47 | task.env['BUILDERNAME'] = buildername |
| 48 | task.env['SRCDIR'] = srcdir |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 49 | task.env['OUTDIR'] = outdir.abspath() |
Davide Pesavento | b62f952 | 2024-03-11 17:50:53 -0400 | [diff] [blame] | 50 | task.env['DOCTREEDIR'] = doctreedir |
Davide Pesavento | cbfc364 | 2024-03-11 20:40:52 -0400 | [diff] [blame] | 51 | task.env['VERSION'] = f'version={self.version}' |
| 52 | task.env['RELEASE'] = f'release={release}' |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 53 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 54 | if buildername == 'man': |
Davide Pesavento | b62f952 | 2024-03-11 17:50:53 -0400 | [diff] [blame] | 55 | confdata = load_source('sphinx_conf', conf.abspath()) |
| 56 | for i in confdata.man_pages: |
Davide Pesavento | cbfc364 | 2024-03-11 20:40:52 -0400 | [diff] [blame] | 57 | target = outdir.find_or_declare(f'{i[1]}.{i[4]}') |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 58 | task.outputs.append(target) |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 59 | if self.install_path: |
Davide Pesavento | cbfc364 | 2024-03-11 20:40:52 -0400 | [diff] [blame] | 60 | self.bld.install_files(f'{self.install_path}/man{i[4]}/', target) |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 61 | else: |
| 62 | task.outputs.append(outdir) |
| 63 | |
Davide Pesavento | cbfc364 | 2024-03-11 20:40:52 -0400 | [diff] [blame] | 64 | # prevent process_source from complaining that there is no extension mapping for .rst files |
| 65 | self.source = [] |
| 66 | |
Davide Pesavento | b62f952 | 2024-03-11 17:50:53 -0400 | [diff] [blame] | 67 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 68 | def configure(conf): |
Davide Pesavento | ac53789 | 2024-03-11 20:30:08 -0400 | [diff] [blame] | 69 | """Check if sphinx-build program is available.""" |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 70 | conf.find_program('sphinx-build', var='SPHINX_BUILD', mandatory=False) |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 71 | |
Davide Pesavento | b62f952 | 2024-03-11 17:50:53 -0400 | [diff] [blame] | 72 | |
Davide Pesavento | ac53789 | 2024-03-11 20:30:08 -0400 | [diff] [blame] | 73 | # sphinx command |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 74 | from waflib.Build import BuildContext |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 75 | class sphinx(BuildContext): |
Davide Pesavento | ac53789 | 2024-03-11 20:30:08 -0400 | [diff] [blame] | 76 | cmd = 'sphinx' |
| 77 | fun = 'sphinx' |