-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.py
287 lines (245 loc) · 8.89 KB
/
setup.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import os
import os.path
import subprocess
import sys
import warnings
from typing import Any, Dict, List, Optional, Tuple, Union, cast
from setuptools import Extension, setup
from distutils.command import build_ext
IS_DEBUG = os.getenv("CYKSUID_DEBUG", "") == "1"
ext_macros: List[Tuple[str, str]] = []
ext_include_dirs = []
if IS_DEBUG:
print("Build in debug mode")
# Adding cython line trace for coverage report
ext_macros += ("CYTHON_TRACE_NOGIL", "1"), ("CYTHON_TRACE", "1")
# Adding upper directory for supporting code coverage when running
# tests inside the cython package
ext_include_dirs += [".."]
# Some extra info for cython compiler
try:
from Cython.Build import cythonize
HAS_CYTHON = True
except ImportError:
HAS_CYTHON = False
def check_option(name: str) -> bool:
cli_arg = "--" + name
if cli_arg in sys.argv:
sys.argv.remove(cli_arg)
return True
env_var = name.replace("-", "_").upper()
if os.environ.get(env_var) in ("true", "1"):
return True
return False
USE_CYTHON = check_option("cython") or check_option("with-cython")
if check_option("no-cython") or check_option("without-cython"):
USE_CYTHON = False
if USE_CYTHON and not HAS_CYTHON:
print("WARNING: Cython not installed. Building without Cython.")
USE_CYTHON = False
if USE_CYTHON:
suffix = ".pyx"
else:
suffix = ".cpp"
ext_modules = [
Extension(
"cyksuid.fast_base62",
sources=["cyksuid/fast_base62" + suffix, "cyksuid/cbase62.cc"],
define_macros=ext_macros,
include_dirs=ext_include_dirs,
language="c++",
),
Extension(
"cyksuid._ksuid",
sources=["cyksuid/_ksuid" + suffix],
define_macros=ext_macros,
include_dirs=ext_include_dirs,
language="c++",
),
]
class BuildExt(build_ext.build_ext):
def initialize_options(self) -> None:
super().initialize_options()
self.cython_kwargs: Optional[Dict[str, Any]] = None
self.cython_compiler_directives: Optional[dict] = None
self.extra_compile_args: List[str] = []
self.extra_link_args: List[str] = []
def finalize_options(self) -> None:
super().finalize_options()
if self.cython_kwargs is None:
self.cython_kwargs = {
"force": True,
}
if self.cython_compiler_directives is None:
self.cython_compiler_directives = {
"language_level": "3",
"embedsignature": True,
"binding": True,
}
if IS_DEBUG:
self.cython_kwargs.update(
{
"gdb_debug": True,
"annotate": True,
}
)
self.cython_compiler_directives.update(
{
"linetrace": True,
"profile": True,
"binding": True,
}
)
def build_extensions(self) -> None:
is_msvc = self.compiler.compiler_type == "msvc"
is_windows = sys.platform[:3] == "win"
is_mingw = is_windows and (
self.compiler.compiler_type.lower()
in ["mingw32", "mingw64", "mingw", "msys", "msys2", "gcc", "g++"]
)
check_args: List[str] = []
if sys.platform == "darwin":
check_args += [
"-Wno-deprecated-register",
]
if is_msvc:
self.extra_compile_args += [
"/MT",
"/O2",
"/std:c++14",
]
else:
check_args += [
"-Wno-write-strings",
"-Wno-invalid-offsetof",
"-Wno-sign-compare",
"-Wno-unused-variable",
"-Wno-shorten-64-to-32",
]
self.add_extra_compile_args(check_args)
if not is_mingw:
self.add_highest_supported_cxx_standard()
if not IS_DEBUG:
self.add_O3()
self.add_no_math_errno()
self.add_no_trapping_math()
if not is_windows:
self.add_link_time_optimization()
for e in self.extensions:
e.extra_compile_args += self.extra_compile_args
if USE_CYTHON:
self.extensions = cythonize(
self.extensions,
compiler_directives=self.cython_compiler_directives,
**cast(dict, self.cython_kwargs),
)
super().build_extensions()
def add_extra_compile_args(self, args: List[str]) -> None:
for arg in args:
if self.test_supports_compile_arg(arg):
self.extra_compile_args.append(arg)
def add_highest_supported_cxx_standard(self):
cxx17 = "-std=c++17"
cxx14 = "-std=gnu++14"
cxx11 = "-std=c++11"
if self.test_supports_compile_arg(cxx17):
self.extra_compile_args.append(cxx17)
elif self.test_supports_compile_arg(cxx14):
self.extra_compile_args.append(cxx14)
elif self.test_supports_compile_arg(cxx11):
self.extra_compile_args.append(cxx11)
else:
msg = "\n\n\nWarning: compiler does not support C++11, compilation of cyksuid might fail without it.\n\n\n"
warnings.warn(msg)
def add_O3(self):
O3 = "-O3"
if self.test_supports_compile_arg(O3):
self.extra_compile_args.append(O3)
def add_no_math_errno(self):
arg_fnme = "-fno-math-errno"
if self.test_supports_compile_arg(arg_fnme):
self.extra_compile_args.append(arg_fnme)
self.extra_link_args.append(arg_fnme)
def add_no_trapping_math(self):
arg_fntm = "-fno-trapping-math"
if self.test_supports_compile_arg(arg_fntm):
self.extra_compile_args.append(arg_fntm)
self.extra_link_args.append(arg_fntm)
def add_link_time_optimization(self):
arg_lto = "-flto"
if self.test_supports_compile_arg(arg_lto):
self.extra_compile_args.append(arg_lto)
self.extra_link_args.append(arg_lto)
def test_supports_compile_arg(self, comm: Union[str, List[str]]) -> bool:
if not hasattr(self.compiler, "compiler_cxx"):
return False
if not isinstance(comm, list):
comm = [comm]
is_supported = False
try:
if not isinstance(self.compiler.compiler_cxx, list):
cmd = list(self.compiler.compiler_cxx)
else:
cmd = self.compiler.compiler_cxx
except Exception as e:
print("Error: could not get compiler_cxx: %s" % e)
cmd = self.compiler.compiler_cxx
print("--- Checking compiler support for option '%s'" % " ".join(comm), end="")
fname = "cyksuid_compiler_testing.cpp"
try:
with open(fname, "w") as ftest:
ftest.write("int main(int argc, char**argv) {return 0;}\n")
val_good = subprocess.call(cmd + [fname])
val = subprocess.call(cmd + comm + [fname])
is_supported = val == val_good
except Exception:
is_supported = False
finally:
try:
os.remove(fname)
except Exception:
pass
print(" ... %s" % ("yes" if is_supported else "no"))
return is_supported
setup(
name="cyksuid",
version="2.1.0",
description="Cython implementation of ksuid",
ext_modules=ext_modules,
cmdclass={"build_ext": BuildExt},
long_description=open("README.md").read() if os.path.exists("README.md") else "",
long_description_content_type="text/markdown",
url="https://github.com/timonwong/cyksuid",
author="Timon Wong",
author_email="[email protected]",
license="BSD",
packages=["cyksuid"],
package_data={
"cyksuid": ["*.pyx", "*.pxd", "*.pyi", "py.typed", "*.h", "*.cc", "*.cpp"],
},
keywords=["ksuid"],
python_requires=">=3.7",
classifiers=[
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
"Programming Language :: Cython",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Scientific/Engineering",
"Topic :: Software Development",
"Topic :: Software Development :: Libraries",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Utilities",
],
install_requires=[],
zip_safe=False,
)