Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsetup.py
More file actions
Latest commit
161 lines (133 loc) · 5.13 KB
/
Copy pathsetup.py
File metadata and controls
161 lines (133 loc) · 5.13 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
importplatform
importsys
fromglobimportglob
frompathlibimportPath
importpybind11
fromsetuptoolsimportExtension, find_packages, setup
fromsetuptools.command.build_clibimportbuild_clib
fromsetuptools.command.build_extimportbuild_ext
PACKAGE_NAME="ratapi"
withopen("README.md") asf:
LONG_DESCRIPTION=f.read()
libevent= ("eventManager", {"sources": ["cpp/RAT/events/eventManager.cpp"], "include_dirs": ["cpp/RAT/events/"]})
ext_modules= [
Extension(
"ratapi.rat_core",
sources=["cpp/rat.cpp", *glob("cpp/RAT/*.c*")],
include_dirs=[
# Path to pybind11 headers
pybind11.get_include(),
pybind11.get_include(True),
"cpp/RAT/",
"cpp/includes/",
],
language="c++",
),
]
# check whether compiler supports a flag
defhas_flag(compiler, flagname):
importtempfile
fromsetuptools.errorsimportCompileError
withtempfile.NamedTemporaryFile("w", suffix=".cpp") asf:
f.write("int main (int argc, char **argv) { return 0; }")
try:
compiler.compile([f.name], extra_postargs=[flagname])
exceptCompileError:
returnFalse
returnTrue
defget_shared_object_name(lib_name):
ifplatform.system() =="Windows":
returnf"{lib_name}.dll"
elifplatform.system() =="Darwin":
returnf"{lib_name}.dylib"
else:
returnf"{lib_name}.so"
classBuildExt(build_ext):
"""A custom build extension for adding compiler-specific options."""
c_opts= {
"msvc": ["/O2", "/EHsc", "/openmp"],
"unix": ["-O2", "-fopenmp", "-std=c++11"],
}
l_opts= {
"msvc": [],
"unix": ["-fopenmp"],
}
ifsys.platform=="darwin":
darwin_opts= ["-stdlib=libc++"]
c_opts["unix"] = [*darwin_opts, "-fopenmp", "-O2"]
l_opts["unix"] = [*darwin_opts, "-lomp"]
defbuild_extensions(self):
ct=self.compiler.compiler_type
opts=self.c_opts.get(ct, [])
link_opts=self.l_opts.get(ct, [])
ifct=="unix":
if"-Wstrict-prototypes"inself.compiler.compiler_so:
self.compiler.compiler_so.remove("-Wstrict-prototypes")
opts.append(f'-DVERSION_INFO="{self.distribution.get_version()}"')
ifhas_flag(self.compiler, "-fvisibility=hidden"):
opts.append("-fvisibility=hidden")
elifct=="msvc":
opts.append(f'/DVERSION_INFO=\\"{self.distribution.get_version()}\\"')
forextinself.extensions:
ext.extra_compile_args=opts
ext.extra_link_args=link_opts
build_ext.build_extensions(self)
defrun(self):
super().run()
build_py=self.get_finalized_command("build_py")
package_dir=f"{build_py.build_lib}/{PACKAGE_NAME}/"
forpinPath(package_dir).glob("**/*"):
ifp.suffixin {".exp", ".a", ".lib"}:
p.unlink()
ifself.inplace:
obj_name=get_shared_object_name(libevent[0])
src=f"{build_py.build_lib}/{PACKAGE_NAME}/{obj_name}"
dest=f"{build_py.get_package_dir(PACKAGE_NAME)}/{obj_name}"
build_py.copy_file(src, dest)
classBuildClib(build_clib):
definitialize_options(self):
super().initialize_options()
build_py=self.get_finalized_command("build_py")
self.build_clib=f"{build_py.build_lib}/{PACKAGE_NAME}"
defbuild_libraries(self, libraries):
# bug in distutils: flag not valid for c++
flag="-Wstrict-prototypes"
ifhasattr(self.compiler, "compiler_so") andflaginself.compiler.compiler_so:
self.compiler.compiler_so.remove(flag)
compiler_type=self.compiler.compiler_type
ifcompiler_type=="msvc":
compile_args= ["/EHsc", "/LD", "-D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR"]
else:
compile_args= ["-std=c++11", "-fPIC"]
forlib_name, build_infoinlibraries:
build_info["cflags"] =compile_args
macros=build_info.get("macros")
include_dirs=build_info.get("include_dirs")
cflags=build_info.get("cflags")
sources=list(build_info.get("sources"))
objects=self.compiler.compile(
sources,
output_dir=self.build_temp,
macros=macros,
include_dirs=include_dirs,
extra_postargs=cflags,
debug=self.debug,
)
language=self.compiler.detect_language(sources)
self.compiler.link_shared_object(
objects,
get_shared_object_name(lib_name),
output_dir=self.build_clib,
target_lang=language,
)
super().build_libraries(libraries)
setup(
name=PACKAGE_NAME,
packages=find_packages(exclude=("tests",)),
include_package_data=True,
package_data={"": [get_shared_object_name(libevent[0])], "ratapi.examples": ["data/*.dat"]},
cmdclass={"build_clib": BuildClib, "build_ext": BuildExt},
libraries=[libevent],
ext_modules=ext_modules,
zip_safe=False,
)