Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hhhrrrttt222111
GitHub Repository: hhhrrrttt222111/Dorkify
Path: blob/master/venv/Lib/site-packages/setuptools/command/build_ext.py
811 views
1
import os
2
import sys
3
import itertools
4
from distutils.command.build_ext import build_ext as _du_build_ext
5
from distutils.file_util import copy_file
6
from distutils.ccompiler import new_compiler
7
from distutils.sysconfig import customize_compiler, get_config_var
8
from distutils.errors import DistutilsError
9
from distutils import log
10
11
from setuptools.extension import Library
12
from setuptools.extern import six
13
14
if six.PY2:
15
import imp
16
17
EXTENSION_SUFFIXES = [
18
s for s, _, tp in imp.get_suffixes() if tp == imp.C_EXTENSION]
19
else:
20
from importlib.machinery import EXTENSION_SUFFIXES
21
22
try:
23
# Attempt to use Cython for building extensions, if available
24
from Cython.Distutils.build_ext import build_ext as _build_ext
25
# Additionally, assert that the compiler module will load
26
# also. Ref #1229.
27
__import__('Cython.Compiler.Main')
28
except ImportError:
29
_build_ext = _du_build_ext
30
31
# make sure _config_vars is initialized
32
get_config_var("LDSHARED")
33
from distutils.sysconfig import _config_vars as _CONFIG_VARS # noqa
34
35
36
def _customize_compiler_for_shlib(compiler):
37
if sys.platform == "darwin":
38
# building .dylib requires additional compiler flags on OSX; here we
39
# temporarily substitute the pyconfig.h variables so that distutils'
40
# 'customize_compiler' uses them before we build the shared libraries.
41
tmp = _CONFIG_VARS.copy()
42
try:
43
# XXX Help! I don't have any idea whether these are right...
44
_CONFIG_VARS['LDSHARED'] = (
45
"gcc -Wl,-x -dynamiclib -undefined dynamic_lookup")
46
_CONFIG_VARS['CCSHARED'] = " -dynamiclib"
47
_CONFIG_VARS['SO'] = ".dylib"
48
customize_compiler(compiler)
49
finally:
50
_CONFIG_VARS.clear()
51
_CONFIG_VARS.update(tmp)
52
else:
53
customize_compiler(compiler)
54
55
56
have_rtld = False
57
use_stubs = False
58
libtype = 'shared'
59
60
if sys.platform == "darwin":
61
use_stubs = True
62
elif os.name != 'nt':
63
try:
64
import dl
65
use_stubs = have_rtld = hasattr(dl, 'RTLD_NOW')
66
except ImportError:
67
pass
68
69
70
def if_dl(s):
71
return s if have_rtld else ''
72
73
74
def get_abi3_suffix():
75
"""Return the file extension for an abi3-compliant Extension()"""
76
for suffix in EXTENSION_SUFFIXES:
77
if '.abi3' in suffix: # Unix
78
return suffix
79
elif suffix == '.pyd': # Windows
80
return suffix
81
82
83
class build_ext(_build_ext):
84
def run(self):
85
"""Build extensions in build directory, then copy if --inplace"""
86
old_inplace, self.inplace = self.inplace, 0
87
_build_ext.run(self)
88
self.inplace = old_inplace
89
if old_inplace:
90
self.copy_extensions_to_source()
91
92
def copy_extensions_to_source(self):
93
build_py = self.get_finalized_command('build_py')
94
for ext in self.extensions:
95
fullname = self.get_ext_fullname(ext.name)
96
filename = self.get_ext_filename(fullname)
97
modpath = fullname.split('.')
98
package = '.'.join(modpath[:-1])
99
package_dir = build_py.get_package_dir(package)
100
dest_filename = os.path.join(package_dir,
101
os.path.basename(filename))
102
src_filename = os.path.join(self.build_lib, filename)
103
104
# Always copy, even if source is older than destination, to ensure
105
# that the right extensions for the current Python/platform are
106
# used.
107
copy_file(
108
src_filename, dest_filename, verbose=self.verbose,
109
dry_run=self.dry_run
110
)
111
if ext._needs_stub:
112
self.write_stub(package_dir or os.curdir, ext, True)
113
114
def get_ext_filename(self, fullname):
115
filename = _build_ext.get_ext_filename(self, fullname)
116
if fullname in self.ext_map:
117
ext = self.ext_map[fullname]
118
use_abi3 = (
119
not six.PY2
120
and getattr(ext, 'py_limited_api')
121
and get_abi3_suffix()
122
)
123
if use_abi3:
124
so_ext = get_config_var('EXT_SUFFIX')
125
filename = filename[:-len(so_ext)]
126
filename = filename + get_abi3_suffix()
127
if isinstance(ext, Library):
128
fn, ext = os.path.splitext(filename)
129
return self.shlib_compiler.library_filename(fn, libtype)
130
elif use_stubs and ext._links_to_dynamic:
131
d, fn = os.path.split(filename)
132
return os.path.join(d, 'dl-' + fn)
133
return filename
134
135
def initialize_options(self):
136
_build_ext.initialize_options(self)
137
self.shlib_compiler = None
138
self.shlibs = []
139
self.ext_map = {}
140
141
def finalize_options(self):
142
_build_ext.finalize_options(self)
143
self.extensions = self.extensions or []
144
self.check_extensions_list(self.extensions)
145
self.shlibs = [ext for ext in self.extensions
146
if isinstance(ext, Library)]
147
if self.shlibs:
148
self.setup_shlib_compiler()
149
for ext in self.extensions:
150
ext._full_name = self.get_ext_fullname(ext.name)
151
for ext in self.extensions:
152
fullname = ext._full_name
153
self.ext_map[fullname] = ext
154
155
# distutils 3.1 will also ask for module names
156
# XXX what to do with conflicts?
157
self.ext_map[fullname.split('.')[-1]] = ext
158
159
ltd = self.shlibs and self.links_to_dynamic(ext) or False
160
ns = ltd and use_stubs and not isinstance(ext, Library)
161
ext._links_to_dynamic = ltd
162
ext._needs_stub = ns
163
filename = ext._file_name = self.get_ext_filename(fullname)
164
libdir = os.path.dirname(os.path.join(self.build_lib, filename))
165
if ltd and libdir not in ext.library_dirs:
166
ext.library_dirs.append(libdir)
167
if ltd and use_stubs and os.curdir not in ext.runtime_library_dirs:
168
ext.runtime_library_dirs.append(os.curdir)
169
170
def setup_shlib_compiler(self):
171
compiler = self.shlib_compiler = new_compiler(
172
compiler=self.compiler, dry_run=self.dry_run, force=self.force
173
)
174
_customize_compiler_for_shlib(compiler)
175
176
if self.include_dirs is not None:
177
compiler.set_include_dirs(self.include_dirs)
178
if self.define is not None:
179
# 'define' option is a list of (name,value) tuples
180
for (name, value) in self.define:
181
compiler.define_macro(name, value)
182
if self.undef is not None:
183
for macro in self.undef:
184
compiler.undefine_macro(macro)
185
if self.libraries is not None:
186
compiler.set_libraries(self.libraries)
187
if self.library_dirs is not None:
188
compiler.set_library_dirs(self.library_dirs)
189
if self.rpath is not None:
190
compiler.set_runtime_library_dirs(self.rpath)
191
if self.link_objects is not None:
192
compiler.set_link_objects(self.link_objects)
193
194
# hack so distutils' build_extension() builds a library instead
195
compiler.link_shared_object = link_shared_object.__get__(compiler)
196
197
def get_export_symbols(self, ext):
198
if isinstance(ext, Library):
199
return ext.export_symbols
200
return _build_ext.get_export_symbols(self, ext)
201
202
def build_extension(self, ext):
203
ext._convert_pyx_sources_to_lang()
204
_compiler = self.compiler
205
try:
206
if isinstance(ext, Library):
207
self.compiler = self.shlib_compiler
208
_build_ext.build_extension(self, ext)
209
if ext._needs_stub:
210
cmd = self.get_finalized_command('build_py').build_lib
211
self.write_stub(cmd, ext)
212
finally:
213
self.compiler = _compiler
214
215
def links_to_dynamic(self, ext):
216
"""Return true if 'ext' links to a dynamic lib in the same package"""
217
# XXX this should check to ensure the lib is actually being built
218
# XXX as dynamic, and not just using a locally-found version or a
219
# XXX static-compiled version
220
libnames = dict.fromkeys([lib._full_name for lib in self.shlibs])
221
pkg = '.'.join(ext._full_name.split('.')[:-1] + [''])
222
return any(pkg + libname in libnames for libname in ext.libraries)
223
224
def get_outputs(self):
225
return _build_ext.get_outputs(self) + self.__get_stubs_outputs()
226
227
def __get_stubs_outputs(self):
228
# assemble the base name for each extension that needs a stub
229
ns_ext_bases = (
230
os.path.join(self.build_lib, *ext._full_name.split('.'))
231
for ext in self.extensions
232
if ext._needs_stub
233
)
234
# pair each base with the extension
235
pairs = itertools.product(ns_ext_bases, self.__get_output_extensions())
236
return list(base + fnext for base, fnext in pairs)
237
238
def __get_output_extensions(self):
239
yield '.py'
240
yield '.pyc'
241
if self.get_finalized_command('build_py').optimize:
242
yield '.pyo'
243
244
def write_stub(self, output_dir, ext, compile=False):
245
log.info("writing stub loader for %s to %s", ext._full_name,
246
output_dir)
247
stub_file = (os.path.join(output_dir, *ext._full_name.split('.')) +
248
'.py')
249
if compile and os.path.exists(stub_file):
250
raise DistutilsError(stub_file + " already exists! Please delete.")
251
if not self.dry_run:
252
f = open(stub_file, 'w')
253
f.write(
254
'\n'.join([
255
"def __bootstrap__():",
256
" global __bootstrap__, __file__, __loader__",
257
" import sys, os, pkg_resources" + if_dl(", dl"),
258
" from importlib.machinery import ExtensionFileLoader",
259
" __file__ = pkg_resources.resource_filename"
260
"(__name__,%r)"
261
% os.path.basename(ext._file_name),
262
" del __bootstrap__",
263
" if '__loader__' in globals():",
264
" del __loader__",
265
if_dl(" old_flags = sys.getdlopenflags()"),
266
" old_dir = os.getcwd()",
267
" try:",
268
" os.chdir(os.path.dirname(__file__))",
269
if_dl(" sys.setdlopenflags(dl.RTLD_NOW)"),
270
" ExtensionFileLoader(__name__,",
271
" __file__).exec_module()",
272
" finally:",
273
if_dl(" sys.setdlopenflags(old_flags)"),
274
" os.chdir(old_dir)",
275
"__bootstrap__()",
276
"" # terminal \n
277
])
278
)
279
f.close()
280
if compile:
281
from distutils.util import byte_compile
282
283
byte_compile([stub_file], optimize=0,
284
force=True, dry_run=self.dry_run)
285
optimize = self.get_finalized_command('install_lib').optimize
286
if optimize > 0:
287
byte_compile([stub_file], optimize=optimize,
288
force=True, dry_run=self.dry_run)
289
if os.path.exists(stub_file) and not self.dry_run:
290
os.unlink(stub_file)
291
292
293
if use_stubs or os.name == 'nt':
294
# Build shared libraries
295
#
296
def link_shared_object(
297
self, objects, output_libname, output_dir=None, libraries=None,
298
library_dirs=None, runtime_library_dirs=None, export_symbols=None,
299
debug=0, extra_preargs=None, extra_postargs=None, build_temp=None,
300
target_lang=None):
301
self.link(
302
self.SHARED_LIBRARY, objects, output_libname,
303
output_dir, libraries, library_dirs, runtime_library_dirs,
304
export_symbols, debug, extra_preargs, extra_postargs,
305
build_temp, target_lang
306
)
307
else:
308
# Build static libraries everywhere else
309
libtype = 'static'
310
311
def link_shared_object(
312
self, objects, output_libname, output_dir=None, libraries=None,
313
library_dirs=None, runtime_library_dirs=None, export_symbols=None,
314
debug=0, extra_preargs=None, extra_postargs=None, build_temp=None,
315
target_lang=None):
316
# XXX we need to either disallow these attrs on Library instances,
317
# or warn/abort here if set, or something...
318
# libraries=None, library_dirs=None, runtime_library_dirs=None,
319
# export_symbols=None, extra_preargs=None, extra_postargs=None,
320
# build_temp=None
321
322
assert output_dir is None # distutils build_ext doesn't pass this
323
output_dir, filename = os.path.split(output_libname)
324
basename, ext = os.path.splitext(filename)
325
if self.library_filename("x").startswith('lib'):
326
# strip 'lib' prefix; this is kludgy if some platform uses
327
# a different prefix
328
basename = basename[3:]
329
330
self.create_static_lib(
331
objects, basename, output_dir, debug, target_lang
332
)
333
334