3 # Thomas Nagy, 2010-2015
6 from waflib import Task, Logs
7 from waflib.TaskGen import extension
9 cy_api_pat = re.compile(r'\s*?cdef\s*?(public|api)\w*')
10 re_cyt = re.compile(r"""
11 (?:from\s+(\w+)\s+)? # optionally match "from foo" and capture foo
12 c?import\s(\w+|[*]) # require "import bar" and capture bar
13 """, re.M | re.VERBOSE)
16 def add_cython_file(self, node):
18 Process a *.pyx* file given in the list of source files. No additional
22 bld(features='c cshlib pyext', source='main.c foo.pyx', target='app')
25 if 'cxx' in self.features:
26 self.env.append_unique('CYTHONFLAGS', '--cplus')
29 for x in getattr(self, 'cython_includes', []):
30 # TODO re-use these nodes in "scan" below
31 d = self.path.find_dir(x)
33 self.env.append_unique('CYTHONFLAGS', '-I%s' % d.abspath())
35 tsk = self.create_task('cython', node, node.change_ext(ext))
36 self.source += tsk.outputs
38 class cython(Task.Task):
39 run_str = '${CYTHON} ${CYTHONFLAGS} -o ${TGT[0].abspath()} ${SRC}'
44 Rebuild whenever the INCLUDES change. The variables such as CYTHONFLAGS will be appended
50 The creation of a .h file is known only after the build has begun, so it is not
51 possible to compute a build order just by looking at the task inputs/outputs.
54 def runnable_status(self):
56 Perform a double-check to add the headers created by cython
57 to the output nodes. The scanner is executed only when the cython task
58 must be executed (optimization).
60 ret = super(cython, self).runnable_status()
61 if ret == Task.ASK_LATER:
63 for x in self.generator.bld.raw_deps[self.uid()]:
64 if x.startswith('header:'):
65 self.outputs.append(self.inputs[0].parent.find_or_declare(x.replace('header:', '')))
66 return super(cython, self).runnable_status()
69 for x in self.outputs:
70 if x.name.endswith('.h'):
73 Logs.warn('Expected %r', x.abspath())
75 return Task.Task.post_run(self)
79 Return the dependent files (.pxd) by looking in the include folders.
80 Put the headers to generate in the custom list "bld.raw_deps".
81 To inspect the scanne results use::
83 $ waf clean build --zones=deps
89 for m in re_cyt.finditer(txt):
90 if m.group(1): # matches "from foo import bar"
91 mods.append(m.group(1))
93 mods.append(m.group(2))
95 Logs.debug('cython: mods %r', mods)
96 incs = getattr(self.generator, 'cython_includes', [])
97 incs = [self.generator.path.find_dir(x) for x in incs]
98 incs.append(node.parent)
104 k = y.find_resource(x + '.pxd')
111 # the cython file implicitly depends on a pxd file that might be present
112 implicit = node.parent.find_resource(node.name[:-3] + 'pxd')
114 found.append(implicit)
116 Logs.debug('cython: found %r', found)
118 # Now the .h created - store them in bld.raw_deps for later use
121 for l in txt.splitlines():
122 if cy_api_pat.match(l):
127 name = node.name.replace('.pyx', '')
129 missing.append('header:%s_api.h' % name)
131 missing.append('header:%s.h' % name)
133 return (found, missing)
136 ctx.add_option('--cython-flags', action='store', default='', help='space separated list of flags to pass to cython')
139 if not ctx.env.CC and not ctx.env.CXX:
140 ctx.fatal('Load a C/C++ compiler first')
141 if not ctx.env.PYTHON:
142 ctx.fatal('Load the python tool first!')
143 ctx.find_program('cython', var='CYTHON')
144 if ctx.options.cython_flags:
145 ctx.env.CYTHONFLAGS = ctx.options.cython_flags