forked from fos/fos-legacy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build_helpers.py
82 lines (70 loc) · 2.37 KB
/
build_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
''' Module to automate cython building '''
import os
from os.path import join as pjoin
from distutils.extension import Extension
def make_cython_ext(modulename,
has_cython,
include_dirs=None,
extra_c_sources=None):
''' Create Cython extension builder from module names
Returns extension for building and command class depending on
whether you want to use Cython and ``.pyx`` files for building
(`has_cython` == True) or the Cython-generated C files (`has_cython`
== False).
Assumes ``pyx`` or C file has the same path as that implied by
modulename.
Parameters
----------
modulename : string
module name, relative to setup.py path, with python dot
separators, e.g mypkg.mysubpkg.mymodule
has_cython : bool
True if we have cython, False otherwise
include_dirs : None or sequence
include directories
extra_c_sources : None or sequence
sequence of strings giving extra C source files
Returns
-------
ext : extension object
cmdclass : dict
command class dictionary for setup.py
Examples
--------
You will need Cython on your python path to run these tests.
>>> modulename = 'pkg.subpkg.mymodule'
>>> ext, cmdclass = make_cython_ext(modulename, True, None,['test.c'])
>>> ext.name == modulename
True
>>> pyx_src = os.path.join('pkg', 'subpkg', 'mymodule.pyx')
>>> ext.sources == [pyx_src, 'test.c']
True
>>> import Cython.Distutils
>>> cmdclass['build_ext'] == Cython.Distutils.build_ext
True
>>> ext, cmdclass = make_cython_ext(modulename, False, None, ['test.c'])
>>> ext.name == modulename
True
>>> pyx_src = os.path.join('pkg', 'subpkg', 'mymodule.c')
>>> ext.sources == [pyx_src, 'test.c']
True
>>> cmdclass
{}
'''
if include_dirs is None:
include_dirs = []
if extra_c_sources is None:
extra_c_sources = []
if has_cython:
src_ext = '.pyx'
else:
src_ext = '.c'
pyx_src = pjoin(*modulename.split('.')) + src_ext
sources = [pyx_src] + extra_c_sources
ext = Extension(modulename, sources, include_dirs = include_dirs)
if has_cython:
from Cython.Distutils import build_ext
cmdclass = {'build_ext': build_ext}
else:
cmdclass = {}
return ext, cmdclass