|
| 1 | +import re |
1 | 2 | import SCons
|
2 | 3 | from SCons.Action import Action
|
| 4 | +from SCons.Scanner import Scanner |
3 | 5 |
|
| 6 | +pyx_from_import_re = re.compile(r'^from\s+(\S+)\s+cimport', re.M) |
| 7 | +pyx_import_re = re.compile(r'^cimport\s+(\S+)', re.M) |
| 8 | +cdef_import_re = re.compile(r'^cdef extern from\s+.(\S+).:', re.M) |
| 9 | + |
| 10 | + |
| 11 | +def pyx_scan(node, env, path, arg=None): |
| 12 | + contents = node.get_text_contents() |
| 13 | + |
| 14 | + # from <module> cimport ... |
| 15 | + matches = pyx_from_import_re.findall(contents) |
| 16 | + # cimport <module> |
| 17 | + matches += pyx_import_re.findall(contents) |
| 18 | + |
| 19 | + # Modules can be either .pxd or .pyx files |
| 20 | + files = [m.replace('.', '/') + '.pxd' for m in matches] |
| 21 | + files += [m.replace('.', '/') + '.pyx' for m in matches] |
| 22 | + |
| 23 | + # cdef extern from <file> |
| 24 | + files += cdef_import_re.findall(contents) |
| 25 | + |
| 26 | + # Handle relative imports |
| 27 | + cur_dir = str(node.get_dir()) |
| 28 | + files = [cur_dir + f if f.startswith('/') else f for f in files] |
| 29 | + |
| 30 | + # Filter out non-existing files (probably system imports) |
| 31 | + files = [f for f in files if env.File(f).exists()] |
| 32 | + return env.File(files) |
| 33 | + |
| 34 | + |
| 35 | +pyxscanner = Scanner(function=pyx_scan, skeys=['.pyx', '.pxd'], recursive=True) |
4 | 36 | cythonAction = Action("$CYTHONCOM")
|
5 | 37 |
|
| 38 | + |
6 | 39 | def create_builder(env):
|
7 | 40 | try:
|
8 | 41 | cython = env['BUILDERS']['Cython']
|
9 | 42 | except KeyError:
|
10 | 43 | cython = SCons.Builder.Builder(
|
11 |
| - action = cythonAction, |
12 |
| - emitter = {}, |
13 |
| - suffix = cython_suffix_emitter, |
14 |
| - single_source = 1 |
15 |
| - ) |
| 44 | + action=cythonAction, |
| 45 | + emitter={}, |
| 46 | + suffix=cython_suffix_emitter, |
| 47 | + single_source=1 |
| 48 | + ) |
| 49 | + env.Append(SCANNERS=pyxscanner) |
16 | 50 | env['BUILDERS']['Cython'] = cython
|
17 | 51 | return cython
|
18 | 52 |
|
|
0 commit comments