-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsetup.py
More file actions
171 lines (153 loc) · 5.36 KB
/
setup.py
File metadata and controls
171 lines (153 loc) · 5.36 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
162
163
164
165
166
167
168
169
170
171
import glob
import itertools
import os
from Cython.Build import (
cythonize,
)
from setuptools import (
Extension,
setup,
)
from setuptools.command.build_ext import (
build_ext,
)
compile_time_env = {
"DEBUG": os.environ.get("DEBUG") in ("1", "yes", "true", "y"),
"PYPY": False,
}
try:
import __pypy__ # pylint: disable=unused-import
compile_time_env["PYPY"] = True
except ImportError:
...
def find_spsc_queue_hpp(boost_root):
for header in (
os.path.join(
boost_root,
"boost",
"lockfree",
"spsc_queue.hpp",
),
os.path.join(
boost_root,
"include",
"boost",
"lockfree",
"spsc_queue.hpp",
),
):
if os.path.exists(header):
yield header
def find_boost_system_lib_msvc(boost_root):
for lib_dir in itertools.chain.from_iterable(
map(
glob.glob,
(
os.path.join(boost_root, "lib"),
os.path.join(boost_root, "lib64"),
os.path.join(boost_root, "lib64-*"),
os.path.join(boost_root, "lib32"),
os.path.join(boost_root, "lib32-*"),
),
)
):
for lib in itertools.chain.from_iterable(
map(
glob.glob,
(
# Prefer multi-threaded
os.path.join(lib_dir, "boost_system-*mt*.dll"),
os.path.join(lib_dir, "libboost_system-*mt*.dll"),
os.path.join(lib_dir, "boost_system-*.dll"),
os.path.join(lib_dir, "libboost_system-*.dll"),
os.path.join(lib_dir, "boost_system.dll"),
os.path.join(lib_dir, "libboost_system.dll"),
),
)
):
yield lib
def find_boost_system_lib_unix(boost_root):
for lib_dir in itertools.chain.from_iterable(
map(
glob.glob,
(
os.path.join(boost_root, "lib"),
os.path.join(boost_root, "lib64"),
os.path.join(boost_root, "lib32"),
os.path.join(boost_root, "lib", "**"),
),
)
):
for ext in (
"so", # shared on Linux, BSD
"dylib", # shared on macOS
):
for lib in itertools.chain.from_iterable(
map(
glob.glob,
(
# Prefer multi-threaded
os.path.join(lib_dir, f"libboost_system-*mt*.{ext}"),
os.path.join(lib_dir, f"libboost_system-*.{ext}"),
os.path.join(lib_dir, f"libboost_system.{ext}"),
),
)
):
yield lib
class build_ext_compiler_check(build_ext):
def build_extensions(self):
compiler_type = self.compiler.compiler_type # usually "unix" or "msvc"
boost_root = os.environ.get("BOOST_ROOT")
if boost_root:
ringbufcy_ext = [e for e in self.extensions if e.name == "ringbuf.ringbufcy"][0]
# Try to find the include dir
for header in find_spsc_queue_hpp(boost_root):
include_dir = os.path.join(*header.split(os.path.sep)[:-3])
ringbufcy_ext.include_dirs.append(include_dir)
ringbufcy_ext.extra_compile_args.append(f"-I{include_dir}")
break
else:
raise ValueError(f"Could not find boost/lockfree/spsc_queue.hpp in BOOST_ROOT ({boost_root})")
# Try to find the library dir
if compiler_type == "msvc":
for lib in find_boost_system_lib_msvc(boost_root):
lib_dir = os.path.dirname(lib)
ringbufcy_ext.extra_link_args.append(f"/LIBPATH:{lib_dir}")
lib_name = os.path.splitext(os.path.basename(lib))[0]
ringbufcy_ext.libraries = [lib_name]
break
else:
raise ValueError(f"Could not find boost_system library in BOOST_ROOT ({boost_root})")
else:
for lib in find_boost_system_lib_unix(boost_root):
lib_dir = os.path.dirname(lib)
ringbufcy_ext.extra_link_args.append(f"-L{lib_dir}")
lib_name = os.path.splitext(os.path.basename(lib))[0]
# remove lib prefix
lib_name = lib_name[3:]
ringbufcy_ext.libraries = [lib_name]
break
else:
raise ValueError(f"Could not find boost_system library in BOOST_ROOT ({boost_root})")
build_ext.build_extensions(self)
setup(
ext_modules=cythonize(
[
Extension(
"ringbuf.libc_constants",
sources=["ringbuf/libc_constants.pyx"],
language="c",
),
Extension(
"ringbuf.ringbufcy",
sources=["ringbuf/ringbufcy.pyx"],
language="c++",
libraries=["boost_system"],
),
],
gdb_debug=compile_time_env["DEBUG"],
compile_time_env=compile_time_env,
compiler_directives={"language_level": 3},
),
cmdclass={"build_ext": build_ext_compiler_check},
)