comparison env/lib/python3.7/site-packages/setuptools/command/build_ext.py @ 0:26e78fe6e8c4 draft

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