-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
87 lines (67 loc) · 2.94 KB
/
build.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
82
83
84
85
86
87
import os
import shutil
#from distutils.command.build_ext import build_ext
#from distutils.core import Distribution, Extension
#from setuptools.command.build_ext import build_ext
from setuptools.extension import Extension
from setuptools.dist import Distribution
import numpy as np
import ailist
from Cython.Build import build_ext, cythonize
include_dirs = [".", np.get_include(), ailist.get_include()]
def declare_cython_extension(extName, use_openmp=False, include_dirs=None):
"""
Declare a Cython extension module for setuptools.
Arguments:
extName : str
Absolute module name, e.g. use `mylibrary.mypackage.mymodule`
for the Cython source file `mylibrary/mypackage/mymodule.pyx`.
use_math : bool
If True, set math flags and link with ``libm``.
use_openmp : bool
If True, compile and link with OpenMP.
Returns:
Extension object
that can be passed to ``setuptools.setup``.
"""
extPath = extName.replace(".", os.path.sep)+".pyx"
compile_args = ["-march=native", "-O3", "-msse", "-msse2", "-mfma", "-mfpmath=sse"]
compile_args = ["-O3"]
link_args = []
libraries = None # value if no libraries, see setuptools.extension._Extension
# OpenMP
if use_openmp:
compile_args.insert( 0, ['-fopenmp'] )
link_args.insert( 0, ['-fopenmp'] )
return Extension( extName,
[extPath],
extra_compile_args=compile_args,
extra_link_args=link_args,
include_dirs=include_dirs,
libraries=libraries
)
def build():
# declare Cython extension modules here
ext_module_hmmCNV = declare_cython_extension("hmmCNV.hmm.hmm", use_openmp=False, include_dirs=include_dirs)
# this is mainly to allow a manual logical ordering of the declared modules
cython_ext_modules = [ext_module_hmmCNV]
# Call cythonize() explicitly, as recommended in the Cython documentation. See
# This will favor Cython's own handling of '.pyx' sources over that provided by setuptools.
# cythonize() just performs the Cython-level processing, and returns a list of Extension objects.
ext_modules = cythonize(cython_ext_modules, include_path=include_dirs, gdb_debug=False, language_level=3)
distribution = Distribution({"name": "extended", "ext_modules": ext_modules})
distribution.package_dir = "extended"
cmd = build_ext(distribution)
cmd.ensure_finalized()
cmd.run()
# Copy built extensions back to the project
#files = os.listdir(cmd.build_lib)
#print(files)
for output in cmd.get_outputs():
relative_extension = os.path.relpath(output, cmd.build_lib)
shutil.copyfile(output, relative_extension)
# mode = os.stat(relative_extension).st_mode
# mode |= (mode & 0o444) >> 2
# os.chmod(relative_extension, mode)
if __name__ == "__main__":
build()