blob: 3666d5a68a25ff6f5afc05f5ad69cfda67ad16b5 [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
3import os
Davide Pesaventob62f9522024-03-11 17:50:53 -04004from waflib import Node, Task, TaskGen, Utils
5
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -08006
Alexander Afanasyev1160baa2014-04-10 18:50:29 -07007class sphinx_build(Task.Task):
8 color = 'BLUE'
Alexander Afanasyeva06fdda2014-04-29 19:15:00 -07009 run_str = '${SPHINX_BUILD} -D ${VERSION} -D ${RELEASE} -q -b ${BUILDERNAME} -d ${DOCTREEDIR} ${SRCDIR} ${OUTDIR}'
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080010
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070011 def __str__(self):
12 env = self.env
Alexander Afanasyev8b1674a2014-05-15 00:58:43 -070013 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 Afanasyev1160baa2014-04-10 18:50:29 -070015 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)
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080019
Davide Pesaventob62f9522024-03-11 17:50:53 -040020
21# from https://docs.python.org/3.12/whatsnew/3.12.html#imp
22def 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 Afanasyev1160baa2014-04-10 18:50:29 -070032@TaskGen.extension('.py', '.rst')
33def sig_hook(self, node):
34 node.sig=Utils.h_file(node.abspath())
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080035
Davide Pesaventob62f9522024-03-11 17:50:53 -040036
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080037@TaskGen.feature("sphinx")
38@TaskGen.before_method("process_source")
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070039def apply_sphinx(self):
40 """Set up the task generator with a Sphinx instance and create a task."""
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080041
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070042 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)
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080051
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070052 task = self.create_task('sphinx_build', inputs)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070053
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070054 conf = self.path.find_node(self.config)
55 task.inputs.append(conf)
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080056
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070057 confdir = conf.parent.abspath()
Davide Pesaventob310efb2019-04-11 22:10:24 -040058 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 Afanasyeva1ae0a12014-01-28 15:21:02 -080062
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070063 task.env['BUILDERNAME'] = buildername
64 task.env['SRCDIR'] = srcdir
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070065 task.env['OUTDIR'] = outdir.abspath()
Davide Pesaventob62f9522024-03-11 17:50:53 -040066 task.env['DOCTREEDIR'] = doctreedir
Davide Pesaventob310efb2019-04-11 22:10:24 -040067 task.env['VERSION'] = 'version=%s' % self.version
68 task.env['RELEASE'] = 'release=%s' % getattr(self, 'release', self.version)
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080069
Davide Pesaventob310efb2019-04-11 22:10:24 -040070 if buildername == 'man':
Davide Pesaventob62f9522024-03-11 17:50:53 -040071 confdata = load_source('sphinx_conf', conf.abspath())
72 for i in confdata.man_pages:
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070073 target = outdir.find_or_declare('%s.%d' % (i[1], i[4]))
74 task.outputs.append(target)
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080075
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070076 if self.install_path:
Davide Pesaventob310efb2019-04-11 22:10:24 -040077 self.bld.install_files('%s/man%d/' % (self.install_path, i[4]), target)
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070078 else:
79 task.outputs.append(outdir)
80
Davide Pesaventob62f9522024-03-11 17:50:53 -040081
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070082def configure(conf):
83 conf.find_program('sphinx-build', var='SPHINX_BUILD', mandatory=False)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070084
Davide Pesaventob62f9522024-03-11 17:50:53 -040085
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070086# sphinx docs
87from waflib.Build import BuildContext
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070088class sphinx(BuildContext):
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070089 cmd = "sphinx"
90 fun = "sphinx"