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