unixccompiler.py (8701B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\stdlib\distutils\unixccompiler.py 2 __revision__ = '$Id: unixccompiler.py 82385 2010-06-30 09:05:32Z kristjan.jonsson $' 3 import os, sys, re 4 from types import StringType, NoneType 5 from distutils import sysconfig 6 from distutils.dep_util import newer 7 from distutils.ccompiler import CCompiler, gen_preprocess_options, gen_lib_options 8 from distutils.errors import DistutilsExecError, CompileError, LibError, LinkError 9 from distutils import log 10 11 def _darwin_compiler_fixup(compiler_so, cc_args): 12 stripArch = stripSysroot = 0 13 compiler_so = list(compiler_so) 14 kernel_version = os.uname()[2] 15 major_version = int(kernel_version.split('.')[0]) 16 if major_version < 8: 17 stripArch = stripSysroot = True 18 else: 19 stripArch = '-arch' in cc_args 20 stripSysroot = '-isysroot' in cc_args 21 if stripArch or 'ARCHFLAGS' in os.environ: 22 while 1: 23 try: 24 index = compiler_so.index('-arch') 25 del compiler_so[index:index + 2] 26 except ValueError: 27 break 28 29 if 'ARCHFLAGS' in os.environ and not stripArch: 30 compiler_so = compiler_so + os.environ['ARCHFLAGS'].split() 31 if stripSysroot: 32 try: 33 index = compiler_so.index('-isysroot') 34 del compiler_so[index:index + 2] 35 except ValueError: 36 pass 37 38 sysroot = None 39 if '-isysroot' in cc_args: 40 idx = cc_args.index('-isysroot') 41 sysroot = cc_args[idx + 1] 42 elif '-isysroot' in compiler_so: 43 idx = compiler_so.index('-isysroot') 44 sysroot = compiler_so[idx + 1] 45 if sysroot and not os.path.isdir(sysroot): 46 log.warn("Compiling with an SDK that doesn't seem to exist: %s", sysroot) 47 log.warn('Please check your Xcode installation') 48 return compiler_so 49 50 51 class UnixCCompiler(CCompiler): 52 compiler_type = 'unix' 53 executables = {'preprocessor': None, 54 'compiler': ['cc'], 55 'compiler_so': ['cc'], 56 'compiler_cxx': ['cc'], 57 'linker_so': ['cc', '-shared'], 58 'linker_exe': ['cc'], 59 'archiver': ['ar', '-cr'], 60 'ranlib': None} 61 if sys.platform[:6] == 'darwin': 62 executables['ranlib'] = ['ranlib'] 63 src_extensions = ['.c', 64 '.C', 65 '.cc', 66 '.cxx', 67 '.cpp', 68 '.m'] 69 obj_extension = '.o' 70 static_lib_extension = '.a' 71 shared_lib_extension = '.so' 72 dylib_lib_extension = '.dylib' 73 static_lib_format = shared_lib_format = dylib_lib_format = 'lib%s%s' 74 if sys.platform == 'cygwin': 75 exe_extension = '.exe' 76 77 def preprocess(self, source, output_file = None, macros = None, include_dirs = None, extra_preargs = None, extra_postargs = None): 78 ignore, macros, include_dirs = self._fix_compile_args(None, macros, include_dirs) 79 pp_opts = gen_preprocess_options(macros, include_dirs) 80 pp_args = self.preprocessor + pp_opts 81 if output_file: 82 pp_args.extend(['-o', output_file]) 83 if extra_preargs: 84 pp_args[:0] = extra_preargs 85 if extra_postargs: 86 pp_args.extend(extra_postargs) 87 pp_args.append(source) 88 if self.force or output_file is None or newer(source, output_file): 89 if output_file: 90 self.mkpath(os.path.dirname(output_file)) 91 try: 92 self.spawn(pp_args) 93 except DistutilsExecError as msg: 94 raise CompileError, msg 95 96 def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts): 97 compiler_so = self.compiler_so 98 if sys.platform == 'darwin': 99 compiler_so = _darwin_compiler_fixup(compiler_so, cc_args + extra_postargs) 100 try: 101 self.spawn(compiler_so + cc_args + [src, '-o', obj] + extra_postargs) 102 except DistutilsExecError as msg: 103 raise CompileError, msg 104 105 def create_static_lib(self, objects, output_libname, output_dir = None, debug = 0, target_lang = None): 106 objects, output_dir = self._fix_object_args(objects, output_dir) 107 output_filename = self.library_filename(output_libname, output_dir=output_dir) 108 if self._need_link(objects, output_filename): 109 self.mkpath(os.path.dirname(output_filename)) 110 self.spawn(self.archiver + [output_filename] + objects + self.objects) 111 if self.ranlib: 112 try: 113 self.spawn(self.ranlib + [output_filename]) 114 except DistutilsExecError as msg: 115 raise LibError, msg 116 117 else: 118 log.debug('skipping %s (up-to-date)', output_filename) 119 120 def link(self, target_desc, objects, output_filename, 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): 121 objects, output_dir = self._fix_object_args(objects, output_dir) 122 libraries, library_dirs, runtime_library_dirs = self._fix_lib_args(libraries, library_dirs, runtime_library_dirs) 123 lib_opts = gen_lib_options(self, library_dirs, runtime_library_dirs, libraries) 124 if type(output_dir) not in (StringType, NoneType): 125 raise TypeError, "'output_dir' must be a string or None" 126 if output_dir is not None: 127 output_filename = os.path.join(output_dir, output_filename) 128 if self._need_link(objects, output_filename): 129 ld_args = objects + self.objects + lib_opts + ['-o', output_filename] 130 if debug: 131 ld_args[:0] = ['-g'] 132 if extra_preargs: 133 ld_args[:0] = extra_preargs 134 if extra_postargs: 135 ld_args.extend(extra_postargs) 136 self.mkpath(os.path.dirname(output_filename)) 137 try: 138 if target_desc == CCompiler.EXECUTABLE: 139 linker = self.linker_exe[:] 140 else: 141 linker = self.linker_so[:] 142 if target_lang == 'c++' and self.compiler_cxx: 143 i = 0 144 if os.path.basename(linker[0]) == 'env': 145 i = 1 146 while '=' in linker[i]: 147 i = i + 1 148 149 linker[i] = self.compiler_cxx[i] 150 if sys.platform == 'darwin': 151 linker = _darwin_compiler_fixup(linker, ld_args) 152 self.spawn(linker + ld_args) 153 except DistutilsExecError as msg: 154 raise LinkError, msg 155 156 else: 157 log.debug('skipping %s (up-to-date)', output_filename) 158 159 def library_dir_option(self, dir): 160 return '-L' + dir 161 162 def _is_gcc(self, compiler_name): 163 return 'gcc' in compiler_name or 'g++' in compiler_name 164 165 def runtime_library_dir_option(self, dir): 166 compiler = os.path.basename(sysconfig.get_config_var('CC')) 167 if sys.platform[:6] == 'darwin': 168 return '-L' + dir 169 elif sys.platform[:5] == 'hp-ux': 170 if self._is_gcc(compiler): 171 return ['-Wl,+s', '-L' + dir] 172 return ['+s', '-L' + dir] 173 elif sys.platform[:7] == 'irix646' or sys.platform[:6] == 'osf1V5': 174 return ['-rpath', dir] 175 elif self._is_gcc(compiler): 176 return '-Wl,-R' + dir 177 else: 178 return '-R' + dir 179 180 def library_option(self, lib): 181 return '-l' + lib 182 183 def find_library_file(self, dirs, lib, debug = 0): 184 shared_f = self.library_filename(lib, lib_type='shared') 185 dylib_f = self.library_filename(lib, lib_type='dylib') 186 static_f = self.library_filename(lib, lib_type='static') 187 if sys.platform == 'darwin': 188 cflags = sysconfig.get_config_var('CFLAGS') 189 m = re.search('-isysroot\\s+(\\S+)', cflags) 190 if m is None: 191 sysroot = '/' 192 else: 193 sysroot = m.group(1) 194 for dir in dirs: 195 shared = os.path.join(dir, shared_f) 196 dylib = os.path.join(dir, dylib_f) 197 static = os.path.join(dir, static_f) 198 if sys.platform == 'darwin' and (dir.startswith('/System/') or dir.startswith('/usr/') and not dir.startswith('/usr/local/')): 199 shared = os.path.join(sysroot, dir[1:], shared_f) 200 dylib = os.path.join(sysroot, dir[1:], dylib_f) 201 static = os.path.join(sysroot, dir[1:], static_f) 202 if os.path.exists(dylib): 203 return dylib 204 if os.path.exists(shared): 205 return shared 206 if os.path.exists(static): 207 return static