Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
Latest commit
186 lines (148 loc) · 5.88 KB
/
Copy pathsetup.py
File metadata and controls
186 lines (148 loc) · 5.88 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
importglob
importos
importsubprocess
frommultiprocessingimportcpu_count
fromCython.Buildimportcythonize
fromCython.CompilerimportOptions
fromsetuptoolsimportsetup
fromsetuptools.commandimportbuild_extasdst_build_ext
fromsetuptools.extensionimportExtension
MODULEDIR="src/pkgcraft"
PACKAGEDIR=os.path.dirname(MODULEDIR)
# version requirements for pkgcraft C library
MIN_VERSION="0.0.17"
MAX_VERSION="0.0.17"
defpkg_config(*packages, **kw):
"""Translate pkg-config data to compatible Extension parameters.
Example usage:
>>> from setuptools.extension import Extension
>>>
>>> ext_kwargs = dict(
... include_dirs=['include'],
... extra_compile_args=['-std=c++11'],
... )
>>> extensions = [
... Extension('foo', ['foo.c']),
... Extension('bar', ['bar.c'], **pkg_config('lcms2')),
... Extension('ext', ['ext.cpp'], **pkg_config(('nss', 'libusb-1.0'), **ext_kwargs)),
... ]
"""
flag_map= {
"-I": "include_dirs",
"-L": "library_dirs",
"-l": "libraries",
}
try:
tokens=subprocess.check_output(
["pkg-config", "--libs", "--cflags"] +list(packages)
).split()
exceptsubprocess.CalledProcessError:
raiseSystemExit(f"running pkg-config failed")
fortokenintokens:
token=token.decode()
iftoken[:2] inflag_map:
kw.setdefault(flag_map.get(token[:2]), []).append(token[2:])
else:
kw.setdefault("extra_compile_args", []).append(token)
returnkw
defextensions(**build_opts):
"""Register cython extensions to be built."""
ifnotbuild_opts:
build_opts= {"depends": [], "include_dirs": []}
exts= []
forextinglob.glob(f"{MODULEDIR}/**/*.pyx", recursive=True):
# strip package dir
module=ext.rpartition(PACKAGEDIR)[-1].lstrip(os.path.sep)
# strip file extension and translate to module namespace
module=os.path.splitext(module)[0].replace(os.path.sep, ".")
exts.append(Extension(name=module, sources=[ext], **build_opts))
returnexts
classbuild_ext(dst_build_ext.build_ext):
"""Enable building cython extensions with coverage support."""
user_options=dst_build_ext.build_ext.user_options+ [
("cython-coverage", None, "enable cython coverage support"),
("develop", None, "convert cythonize and compile-time warnings into errors"),
("doc", None, "support API doc generation"),
]
definitialize_options(self):
self.cython_coverage=False
self.develop=False
self.doc=False
super().initialize_options()
deffinalize_options(self):
self.cython_coverage=bool(self.cython_coverage)
self.develop=bool(self.develop)
self.doc=bool(self.doc)
# default to parallelizing build across all cores
ifself.parallelisNone:
self.parallel=cpu_count()
super().finalize_options()
defrun(self):
# default cython compiler directives
compiler_directives= {"language_level": 3}
ifself.cython_coverage:
compiler_directives["linetrace"] =True
trace_macros= [("CYTHON_TRACE", "1"), ("CYTHON_TRACE_NOGIL", "1")]
forextinself.extensions:
ext.define_macros.extend(trace_macros)
ifself.develop:
Options.warning_errors=True
os.environ["CFLAGS"] =os.environ.get("CFLAGS", "") +" -Werror"
ifself.doc:
compiler_directives["binding"] =True
compiler_directives["embedsignature"] =True
# generate C modules with cython
self.extensions[:] =cythonize(
self.extensions,
nthreads=self.parallel,
force=self.force,
compiler_directives=compiler_directives,
annotate=False,
)
# HACK: Force setuptools to reinject its private attributes to
# extension objects that were overridden by `cythonize`, otherwise the
# build fails when it tries to access them.
build_ext=self.reinitialize_command("build_ext")
build_ext.ensure_finalized()
# delay pkg-config to avoid requiring library during sdist
pkgcraft_opts=pkg_config("pkgcraft")
try:
p=subprocess.run(
["pkg-config", "--modversion", "pkgcraft"],
capture_output=True,
check=True,
text=True,
)
version=p.stdout.strip()
exceptsubprocess.CalledProcessError:
raiseSystemExit("failed retrieving pkgcraft C library version")
try:
subprocess.check_output(["pkg-config", "--atleast-version", MIN_VERSION, "pkgcraft"])
exceptsubprocess.CalledProcessError:
raiseSystemExit(f"pkgcraft C library {version} fails requirements >={MIN_VERSION}")
try:
subprocess.check_output(["pkg-config", "--max-version", MAX_VERSION, "pkgcraft"])
exceptsubprocess.CalledProcessError:
raiseSystemExit(f"pkgcraft C library {version} fails requirements <={MAX_VERSION}")
forextinself.extensions:
forattr, datainpkgcraft_opts.items():
getattr(ext, attr).extend(data)
print(f"Building against pkgcraft-c-{version}")
super().run()
defexclude_cython_files():
"""Generate package data exclusion mapping to avoid installing cython files."""
excluded= ["*.pxd", "*.pyx", "*.c"]
excludes= {}
forroot, dirs, _filesinos.walk("src"):
fordindirs:
path=os.path.join(root, d).lstrip("src/")
module=path.replace(os.path.sep, ".")
excludes[module] =excluded
returnexcludes
setup(
ext_modules=extensions(),
cmdclass={
"build_ext": build_ext,
},
exclude_package_data=exclude_cython_files(),
)