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 | b62f952 | 2024-03-11 17:50:53 -0400 | [diff] [blame] | 6 | from waflib import Node, Task, TaskGen, Utils |
| 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' |
Alexander Afanasyev | a06fdda | 2014-04-29 19:15:00 -0700 | [diff] [blame] | 11 | run_str = '${SPHINX_BUILD} -D ${VERSION} -D ${RELEASE} -q -b ${BUILDERNAME} -d ${DOCTREEDIR} ${SRCDIR} ${OUTDIR}' |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 12 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 13 | def __str__(self): |
| 14 | env = self.env |
Alexander Afanasyev | 8b1674a | 2014-05-15 00:58:43 -0700 | [diff] [blame] | 15 | src_str = ' '.join([a.path_from(a.ctx.launch_node()) for a in self.inputs]) |
| 16 | tgt_str = ' '.join([a.path_from(a.ctx.launch_node()) for a in self.outputs]) |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 17 | if self.outputs: sep = ' -> ' |
| 18 | else: sep = '' |
| 19 | return'%s [%s]: %s%s%s\n'%(self.__class__.__name__.replace('_task',''), |
| 20 | self.env['BUILDERNAME'], src_str, sep, tgt_str) |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 21 | |
Davide Pesavento | b62f952 | 2024-03-11 17:50:53 -0400 | [diff] [blame] | 22 | |
| 23 | # from https://docs.python.org/3.12/whatsnew/3.12.html#imp |
| 24 | def load_source(modname, filename): |
| 25 | import importlib.util |
| 26 | from importlib.machinery import SourceFileLoader |
| 27 | loader = SourceFileLoader(modname, filename) |
| 28 | spec = importlib.util.spec_from_file_location(modname, filename, loader=loader) |
| 29 | module = importlib.util.module_from_spec(spec) |
| 30 | loader.exec_module(module) |
| 31 | return module |
| 32 | |
| 33 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 34 | @TaskGen.extension('.py', '.rst') |
| 35 | def sig_hook(self, node): |
| 36 | node.sig=Utils.h_file(node.abspath()) |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 37 | |
Davide Pesavento | b62f952 | 2024-03-11 17:50:53 -0400 | [diff] [blame] | 38 | |
Davide Pesavento | ac53789 | 2024-03-11 20:30:08 -0400 | [diff] [blame^] | 39 | @TaskGen.feature('sphinx') |
| 40 | @TaskGen.before_method('process_source') |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 41 | def apply_sphinx(self): |
| 42 | """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] | 43 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 44 | inputs = [] |
| 45 | for i in Utils.to_list(self.source): |
| 46 | if not isinstance(i, Node.Node): |
| 47 | node = self.path.find_node(node) |
| 48 | else: |
| 49 | node = i |
| 50 | if not node: |
| 51 | raise ValueError('[%s] file not found' % i) |
| 52 | inputs.append(node) |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 53 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 54 | task = self.create_task('sphinx_build', inputs) |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 55 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 56 | conf = self.path.find_node(self.config) |
| 57 | task.inputs.append(conf) |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 59 | confdir = conf.parent.abspath() |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 60 | buildername = getattr(self, 'builder', 'html') |
| 61 | srcdir = getattr(self, 'srcdir', confdir) |
| 62 | outdir = self.path.find_or_declare(getattr(self, 'outdir', buildername)).get_bld() |
| 63 | doctreedir = getattr(self, 'doctreedir', os.path.join(outdir.abspath(), '.doctrees')) |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 64 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 65 | task.env['BUILDERNAME'] = buildername |
| 66 | task.env['SRCDIR'] = srcdir |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 67 | task.env['OUTDIR'] = outdir.abspath() |
Davide Pesavento | b62f952 | 2024-03-11 17:50:53 -0400 | [diff] [blame] | 68 | task.env['DOCTREEDIR'] = doctreedir |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 69 | task.env['VERSION'] = 'version=%s' % self.version |
| 70 | task.env['RELEASE'] = 'release=%s' % getattr(self, 'release', self.version) |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 71 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 72 | if buildername == 'man': |
Davide Pesavento | b62f952 | 2024-03-11 17:50:53 -0400 | [diff] [blame] | 73 | confdata = load_source('sphinx_conf', conf.abspath()) |
| 74 | for i in confdata.man_pages: |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 75 | target = outdir.find_or_declare('%s.%d' % (i[1], i[4])) |
| 76 | task.outputs.append(target) |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 78 | if self.install_path: |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 79 | self.bld.install_files('%s/man%d/' % (self.install_path, i[4]), target) |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 80 | else: |
| 81 | task.outputs.append(outdir) |
| 82 | |
Davide Pesavento | b62f952 | 2024-03-11 17:50:53 -0400 | [diff] [blame] | 83 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 84 | def configure(conf): |
Davide Pesavento | ac53789 | 2024-03-11 20:30:08 -0400 | [diff] [blame^] | 85 | """Check if sphinx-build program is available.""" |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 86 | conf.find_program('sphinx-build', var='SPHINX_BUILD', mandatory=False) |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 87 | |
Davide Pesavento | b62f952 | 2024-03-11 17:50:53 -0400 | [diff] [blame] | 88 | |
Davide Pesavento | ac53789 | 2024-03-11 20:30:08 -0400 | [diff] [blame^] | 89 | # sphinx command |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 90 | from waflib.Build import BuildContext |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 91 | class sphinx(BuildContext): |
Davide Pesavento | ac53789 | 2024-03-11 20:30:08 -0400 | [diff] [blame^] | 92 | cmd = 'sphinx' |
| 93 | fun = 'sphinx' |