-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup_tests.py
More file actions
93 lines (74 loc) · 2.59 KB
/
Copy pathsetup_tests.py
File metadata and controls
93 lines (74 loc) · 2.59 KB
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
88
89
90
91
92
93
import os
import sysconfig
import tempfile
from distutils.errors import CompileError
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
from typing import List, Optional, Tuple
from Cython.Build import cythonize
from Cython.Compiler import Options
import numpy as np
def has_flag(compiler, flagname):
with tempfile.NamedTemporaryFile('w', suffix='.cpp') as f:
f.write('int main (int argc, char **argv) { return 0; }')
try:
compiler.compile([f.name], extra_postargs=[flagname])
except CompileError:
return False
return True
def flag_filter(compiler, *flags):
result = []
for flag in flags:
if has_flag(compiler, flag):
result.append(flag)
return result
class BuildExt(build_ext):
compile_flags = {"msvc": ['/std:c++14'], "unix": ["-std=c++14"]}
def build_extensions(self):
ct = self.compiler.compiler_type
opts = self.compile_flags.get(ct, [])
if ct == 'unix':
# Only add flags which pass the `flag_filter`
opts += flag_filter(
self.compiler,
"-Wno-misleading-indentation",
"-fno-var-tracking-assignments"
)
for ext in self.extensions:
ext.extra_compile_args = list(set(opts) | set(ext.extra_compile_args))
build_ext.build_extensions(self)
Options.fast_fail = True
cython_macros: List[Tuple[str, Optional[str]]] = [
("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")
]
cflags_base = sysconfig.get_config_var('CFLAGS') or ""
cflags_env = os.getenv("CFLAGS", "")
cflags_set = set(cflags_base.split() + cflags_env.split())
cflags_set.discard('-Wstrict-prototypes') # not valid for C++
ldflags_base = sysconfig.get_config_var('LDFLAGS') or ""
ldflags_env = os.getenv("LDFLAGS", "")
ldflags_set = set(ldflags_base.split() + ldflags_env.split())
extensions = [
Extension(
"*",
["test/unit/*.pyx"],
define_macros=cython_macros,
language="c++",
include_dirs=[np.get_include(), "src", "src/commonnn"],
extra_compile_args=list(cflags_set),
extra_link_args=list(ldflags_set)
)
]
compiler_directives = {
"language_level": 3,
"binding": True,
"embedsignature": True,
"boundscheck": False,
"wraparound": False,
"cdivision": True,
"nonecheck": False,
"linetrace": True,
"annotation_typing": False,
}
extensions = cythonize(extensions, compiler_directives=compiler_directives)
setup(cmdclass={'build_ext': BuildExt}, ext_modules=extensions, package_dir={"": "test/unit"})