setuptools_build_ext.py (9697B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\stdlib\distutils\tests\setuptools_build_ext.py 2 from distutils.command.build_ext import build_ext as _du_build_ext 3 try: 4 from Pyrex.Distutils.build_ext import build_ext as _build_ext 5 except ImportError: 6 _build_ext = _du_build_ext 7 8 import os, sys 9 from distutils.file_util import copy_file 10 from distutils.tests.setuptools_extension import Library 11 from distutils.ccompiler import new_compiler 12 from distutils.sysconfig import customize_compiler, get_config_var 13 get_config_var('LDSHARED') 14 from distutils.sysconfig import _config_vars 15 from distutils import log 16 from distutils.errors import * 17 have_rtld = False 18 use_stubs = False 19 libtype = 'shared' 20 if sys.platform == 'darwin': 21 use_stubs = True 22 elif os.name != 'nt': 23 try: 24 from dl import RTLD_NOW 25 have_rtld = True 26 use_stubs = True 27 except ImportError: 28 pass 29 30 def if_dl(s): 31 if have_rtld: 32 return s 33 return '' 34 35 36 class build_ext(_build_ext): 37 38 def run(self): 39 old_inplace, self.inplace = self.inplace, 0 40 _build_ext.run(self) 41 self.inplace = old_inplace 42 if old_inplace: 43 self.copy_extensions_to_source() 44 45 def copy_extensions_to_source(self): 46 build_py = self.get_finalized_command('build_py') 47 for ext in self.extensions: 48 fullname = self.get_ext_fullname(ext.name) 49 filename = self.get_ext_filename(fullname) 50 modpath = fullname.split('.') 51 package = '.'.join(modpath[:-1]) 52 package_dir = build_py.get_package_dir(package) 53 dest_filename = os.path.join(package_dir, os.path.basename(filename)) 54 src_filename = os.path.join(self.build_lib, filename) 55 copy_file(src_filename, dest_filename, verbose=self.verbose, dry_run=self.dry_run) 56 if ext._needs_stub: 57 self.write_stub(package_dir or os.curdir, ext, True) 58 59 if _build_ext is not _du_build_ext and not hasattr(_build_ext, 'pyrex_sources'): 60 61 def swig_sources(self, sources, *otherargs): 62 sources = _build_ext.swig_sources(self, sources) or sources 63 return _du_build_ext.swig_sources(self, sources, *otherargs) 64 65 def get_ext_filename(self, fullname): 66 filename = _build_ext.get_ext_filename(self, fullname) 67 ext = self.ext_map[fullname] 68 if isinstance(ext, Library): 69 fn, ext = os.path.splitext(filename) 70 return self.shlib_compiler.library_filename(fn, libtype) 71 elif use_stubs and ext._links_to_dynamic: 72 d, fn = os.path.split(filename) 73 return os.path.join(d, 'dl-' + fn) 74 else: 75 return filename 76 77 def initialize_options(self): 78 _build_ext.initialize_options(self) 79 self.shlib_compiler = None 80 self.shlibs = [] 81 self.ext_map = {} 82 83 def finalize_options(self): 84 _build_ext.finalize_options(self) 85 self.extensions = self.extensions or [] 86 self.check_extensions_list(self.extensions) 87 self.shlibs = [ ext for ext in self.extensions if isinstance(ext, Library) ] 88 if self.shlibs: 89 self.setup_shlib_compiler() 90 for ext in self.extensions: 91 ext._full_name = self.get_ext_fullname(ext.name) 92 93 for ext in self.extensions: 94 fullname = ext._full_name 95 self.ext_map[fullname] = ext 96 ltd = ext._links_to_dynamic = self.shlibs and self.links_to_dynamic(ext) or False 97 ext._needs_stub = ltd and use_stubs and not isinstance(ext, Library) 98 filename = ext._file_name = self.get_ext_filename(fullname) 99 libdir = os.path.dirname(os.path.join(self.build_lib, filename)) 100 if ltd and libdir not in ext.library_dirs: 101 ext.library_dirs.append(libdir) 102 if ltd and use_stubs and os.curdir not in ext.runtime_library_dirs: 103 ext.runtime_library_dirs.append(os.curdir) 104 105 def setup_shlib_compiler(self): 106 compiler = self.shlib_compiler = new_compiler(compiler=self.compiler, dry_run=self.dry_run, force=self.force) 107 if sys.platform == 'darwin': 108 tmp = _config_vars.copy() 109 try: 110 _config_vars['LDSHARED'] = 'gcc -Wl,-x -dynamiclib -undefined dynamic_lookup' 111 _config_vars['CCSHARED'] = ' -dynamiclib' 112 _config_vars['SO'] = '.dylib' 113 customize_compiler(compiler) 114 finally: 115 _config_vars.clear() 116 _config_vars.update(tmp) 117 118 else: 119 customize_compiler(compiler) 120 if self.include_dirs is not None: 121 compiler.set_include_dirs(self.include_dirs) 122 if self.define is not None: 123 for name, value in self.define: 124 compiler.define_macro(name, value) 125 126 if self.undef is not None: 127 for macro in self.undef: 128 compiler.undefine_macro(macro) 129 130 if self.libraries is not None: 131 compiler.set_libraries(self.libraries) 132 if self.library_dirs is not None: 133 compiler.set_library_dirs(self.library_dirs) 134 if self.rpath is not None: 135 compiler.set_runtime_library_dirs(self.rpath) 136 if self.link_objects is not None: 137 compiler.set_link_objects(self.link_objects) 138 compiler.link_shared_object = link_shared_object.__get__(compiler) 139 140 def get_export_symbols(self, ext): 141 if isinstance(ext, Library): 142 return ext.export_symbols 143 return _build_ext.get_export_symbols(self, ext) 144 145 def build_extension(self, ext): 146 _compiler = self.compiler 147 try: 148 if isinstance(ext, Library): 149 self.compiler = self.shlib_compiler 150 _build_ext.build_extension(self, ext) 151 if ext._needs_stub: 152 self.write_stub(self.get_finalized_command('build_py').build_lib, ext) 153 finally: 154 self.compiler = _compiler 155 156 def links_to_dynamic(self, ext): 157 libnames = dict.fromkeys([ lib._full_name for lib in self.shlibs ]) 158 pkg = '.'.join(ext._full_name.split('.')[:-1] + ['']) 159 for libname in ext.libraries: 160 if pkg + libname in libnames: 161 return True 162 163 return False 164 165 def get_outputs(self): 166 outputs = _build_ext.get_outputs(self) 167 optimize = self.get_finalized_command('build_py').optimize 168 for ext in self.extensions: 169 if ext._needs_stub: 170 base = os.path.join(self.build_lib, *ext._full_name.split('.')) 171 outputs.append(base + '.py') 172 outputs.append(base + '.pyc') 173 if optimize: 174 outputs.append(base + '.pyo') 175 176 return outputs 177 178 def write_stub(self, output_dir, ext, compile = False): 179 log.info('writing stub loader for %s to %s', ext._full_name, output_dir) 180 stub_file = os.path.join(output_dir, *ext._full_name.split('.')) + '.py' 181 if compile and os.path.exists(stub_file): 182 raise DistutilsError(stub_file + ' already exists! Please delete.') 183 if not self.dry_run: 184 f = open(stub_file, 'w') 185 f.write('\n'.join(['def __bootstrap__():', 186 ' global __bootstrap__, __file__, __loader__', 187 ' import sys, os, pkg_resources, imp' + if_dl(', dl'), 188 ' __file__ = pkg_resources.resource_filename(__name__,%r)' % os.path.basename(ext._file_name), 189 ' del __bootstrap__', 190 " if '__loader__' in globals():", 191 ' del __loader__', 192 if_dl(' old_flags = sys.getdlopenflags()'), 193 ' old_dir = os.getcwd()', 194 ' try:', 195 ' os.chdir(os.path.dirname(__file__))', 196 if_dl(' sys.setdlopenflags(dl.RTLD_NOW)'), 197 ' imp.load_dynamic(__name__,__file__)', 198 ' finally:', 199 if_dl(' sys.setdlopenflags(old_flags)'), 200 ' os.chdir(old_dir)', 201 '__bootstrap__()', 202 ''])) 203 f.close() 204 if compile: 205 from distutils.util import byte_compile 206 byte_compile([stub_file], optimize=0, force=True, dry_run=self.dry_run) 207 optimize = self.get_finalized_command('install_lib').optimize 208 if optimize > 0: 209 byte_compile([stub_file], optimize=optimize, force=True, dry_run=self.dry_run) 210 if os.path.exists(stub_file) and not self.dry_run: 211 os.unlink(stub_file) 212 213 214 if use_stubs or os.name == 'nt': 215 216 def link_shared_object(self, objects, output_libname, output_dir = None, libraries = None, library_dirs = None, runtime_library_dirs = None, export_symbols = None, debug = 0, extra_preargs = None, extra_postargs = None, build_temp = None, target_lang = None): 217 self.link(self.SHARED_LIBRARY, objects, output_libname, output_dir, libraries, library_dirs, runtime_library_dirs, export_symbols, debug, extra_preargs, extra_postargs, build_temp, target_lang) 218 219 220 else: 221 libtype = 'static' 222 223 def link_shared_object(self, objects, output_libname, output_dir = None, libraries = None, library_dirs = None, runtime_library_dirs = None, export_symbols = None, debug = 0, extra_preargs = None, extra_postargs = None, build_temp = None, target_lang = None): 224 output_dir, filename = os.path.split(output_libname) 225 basename, ext = os.path.splitext(filename) 226 if self.library_filename('x').startswith('lib'): 227 basename = basename[3:] 228 self.create_static_lib(objects, basename, output_dir, debug, target_lang)