bcppcompiler.py (9469B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\stdlib\distutils\bcppcompiler.py 2 __revision__ = '$Id: bcppcompiler.py 82110 2010-06-20 13:38:51Z kristjan.jonsson $' 3 import os 4 from distutils.errors import DistutilsExecError, CompileError, LibError, LinkError, UnknownFileError 5 from distutils.ccompiler import CCompiler, gen_preprocess_options 6 from distutils.file_util import write_file 7 from distutils.dep_util import newer 8 from distutils import log 9 10 class BCPPCompiler(CCompiler): 11 compiler_type = 'bcpp' 12 executables = {} 13 _c_extensions = ['.c'] 14 _cpp_extensions = ['.cc', '.cpp', '.cxx'] 15 src_extensions = _c_extensions + _cpp_extensions 16 obj_extension = '.obj' 17 static_lib_extension = '.lib' 18 shared_lib_extension = '.dll' 19 static_lib_format = shared_lib_format = '%s%s' 20 exe_extension = '.exe' 21 22 def __init__(self, verbose = 0, dry_run = 0, force = 0): 23 CCompiler.__init__(self, verbose, dry_run, force) 24 self.cc = 'bcc32.exe' 25 self.linker = 'ilink32.exe' 26 self.lib = 'tlib.exe' 27 self.preprocess_options = None 28 self.compile_options = ['/tWM', 29 '/O2', 30 '/q', 31 '/g0'] 32 self.compile_options_debug = ['/tWM', 33 '/Od', 34 '/q', 35 '/g0'] 36 self.ldflags_shared = ['/Tpd', 37 '/Gn', 38 '/q', 39 '/x'] 40 self.ldflags_shared_debug = ['/Tpd', 41 '/Gn', 42 '/q', 43 '/x'] 44 self.ldflags_static = [] 45 self.ldflags_exe = ['/Gn', '/q', '/x'] 46 self.ldflags_exe_debug = ['/Gn', 47 '/q', 48 '/x', 49 '/r'] 50 51 def compile(self, sources, output_dir = None, macros = None, include_dirs = None, debug = 0, extra_preargs = None, extra_postargs = None, depends = None): 52 macros, objects, extra_postargs, pp_opts, build = self._setup_compile(output_dir, macros, include_dirs, sources, depends, extra_postargs) 53 compile_opts = extra_preargs or [] 54 compile_opts.append('-c') 55 if debug: 56 compile_opts.extend(self.compile_options_debug) 57 else: 58 compile_opts.extend(self.compile_options) 59 for obj in objects: 60 try: 61 src, ext = build[obj] 62 except KeyError: 63 continue 64 65 src = os.path.normpath(src) 66 obj = os.path.normpath(obj) 67 self.mkpath(os.path.dirname(obj)) 68 if ext == '.res': 69 continue 70 if ext == '.rc': 71 try: 72 self.spawn(['brcc32', 73 '-fo', 74 obj, 75 src]) 76 except DistutilsExecError as msg: 77 raise CompileError, msg 78 79 continue 80 if ext in self._c_extensions: 81 input_opt = '' 82 elif ext in self._cpp_extensions: 83 input_opt = '-P' 84 else: 85 input_opt = '' 86 output_opt = '-o' + obj 87 try: 88 self.spawn([self.cc] + compile_opts + pp_opts + [input_opt, output_opt] + extra_postargs + [src]) 89 except DistutilsExecError as msg: 90 raise CompileError, msg 91 92 return objects 93 94 def create_static_lib(self, objects, output_libname, output_dir = None, debug = 0, target_lang = None): 95 objects, output_dir = self._fix_object_args(objects, output_dir) 96 output_filename = self.library_filename(output_libname, output_dir=output_dir) 97 if self._need_link(objects, output_filename): 98 lib_args = [output_filename, '/u'] + objects 99 if debug: 100 pass 101 try: 102 self.spawn([self.lib] + lib_args) 103 except DistutilsExecError as msg: 104 raise LibError, msg 105 106 else: 107 log.debug('skipping %s (up-to-date)', output_filename) 108 109 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): 110 objects, output_dir = self._fix_object_args(objects, output_dir) 111 libraries, library_dirs, runtime_library_dirs = self._fix_lib_args(libraries, library_dirs, runtime_library_dirs) 112 if runtime_library_dirs: 113 log.warn("I don't know what to do with 'runtime_library_dirs': %s", str(runtime_library_dirs)) 114 if output_dir is not None: 115 output_filename = os.path.join(output_dir, output_filename) 116 if self._need_link(objects, output_filename): 117 if target_desc == CCompiler.EXECUTABLE: 118 startup_obj = 'c0w32' 119 if debug: 120 ld_args = self.ldflags_exe_debug[:] 121 else: 122 ld_args = self.ldflags_exe[:] 123 else: 124 startup_obj = 'c0d32' 125 if debug: 126 ld_args = self.ldflags_shared_debug[:] 127 else: 128 ld_args = self.ldflags_shared[:] 129 if export_symbols is None: 130 def_file = '' 131 else: 132 head, tail = os.path.split(output_filename) 133 modname, ext = os.path.splitext(tail) 134 temp_dir = os.path.dirname(objects[0]) 135 def_file = os.path.join(temp_dir, '%s.def' % modname) 136 contents = ['EXPORTS'] 137 for sym in export_symbols or []: 138 contents.append(' %s=_%s' % (sym, sym)) 139 140 self.execute(write_file, (def_file, contents), 'writing %s' % def_file) 141 objects2 = map(os.path.normpath, objects) 142 objects = [startup_obj] 143 resources = [] 144 for file in objects2: 145 base, ext = os.path.splitext(os.path.normcase(file)) 146 if ext == '.res': 147 resources.append(file) 148 else: 149 objects.append(file) 150 151 for l in library_dirs: 152 ld_args.append('/L%s' % os.path.normpath(l)) 153 154 ld_args.append('/L.') 155 ld_args.extend(objects) 156 ld_args.extend([',', output_filename]) 157 ld_args.append(',,') 158 for lib in libraries: 159 libfile = self.find_library_file(library_dirs, lib, debug) 160 if libfile is None: 161 ld_args.append(lib) 162 else: 163 ld_args.append(libfile) 164 165 ld_args.append('import32') 166 ld_args.append('cw32mt') 167 ld_args.extend([',', def_file]) 168 ld_args.append(',') 169 ld_args.extend(resources) 170 if extra_preargs: 171 ld_args[:0] = extra_preargs 172 if extra_postargs: 173 ld_args.extend(extra_postargs) 174 self.mkpath(os.path.dirname(output_filename)) 175 try: 176 self.spawn([self.linker] + ld_args) 177 except DistutilsExecError as msg: 178 raise LinkError, msg 179 180 else: 181 log.debug('skipping %s (up-to-date)', output_filename) 182 183 def find_library_file(self, dirs, lib, debug = 0): 184 if debug: 185 dlib = lib + '_d' 186 try_names = (dlib + '_bcpp', 187 lib + '_bcpp', 188 dlib, 189 lib) 190 else: 191 try_names = (lib + '_bcpp', lib) 192 for dir in dirs: 193 for name in try_names: 194 libfile = os.path.join(dir, self.library_filename(name)) 195 if os.path.exists(libfile): 196 return libfile 197 198 else: 199 return None 200 201 def object_filenames(self, source_filenames, strip_dir = 0, output_dir = ''): 202 if output_dir is None: 203 output_dir = '' 204 obj_names = [] 205 for src_name in source_filenames: 206 base, ext = os.path.splitext(os.path.normcase(src_name)) 207 if ext not in self.src_extensions + ['.rc', '.res']: 208 raise UnknownFileError, "unknown file type '%s' (from '%s')" % (ext, src_name) 209 if strip_dir: 210 base = os.path.basename(base) 211 if ext == '.res': 212 obj_names.append(os.path.join(output_dir, base + ext)) 213 elif ext == '.rc': 214 obj_names.append(os.path.join(output_dir, base + '.res')) 215 else: 216 obj_names.append(os.path.join(output_dir, base + self.obj_extension)) 217 218 return obj_names 219 220 def preprocess(self, source, output_file = None, macros = None, include_dirs = None, extra_preargs = None, extra_postargs = None): 221 _, macros, include_dirs = self._fix_compile_args(None, macros, include_dirs) 222 pp_opts = gen_preprocess_options(macros, include_dirs) 223 pp_args = ['cpp32.exe'] + pp_opts 224 if output_file is not None: 225 pp_args.append('-o' + output_file) 226 if extra_preargs: 227 pp_args[:0] = extra_preargs 228 if extra_postargs: 229 pp_args.extend(extra_postargs) 230 pp_args.append(source) 231 if self.force or output_file is None or newer(source, output_file): 232 if output_file: 233 self.mkpath(os.path.dirname(output_file)) 234 try: 235 self.spawn(pp_args) 236 except DistutilsExecError as msg: 237 print msg 238 raise CompileError, msg