Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 1 | """CCompiler implementation for the Borland C++ compiler.""" |
| 2 | |
| 3 | # This implementation by Lyle Johnson, based on the original msvccompiler.py |
| 4 | # module and using the directions originally published by Gordon Williams. |
| 5 | |
| 6 | # XXX looks like there's a LOT of overlap between these two classes: |
| 7 | # someone should sit down and factor out the common code as |
| 8 | # WindowsCCompiler! --GPW |
| 9 | |
| 10 | import os |
| 11 | |
| 12 | from packaging.errors import (PackagingExecError, CompileError, LibError, |
| 13 | LinkError, UnknownFileError) |
| 14 | from packaging.compiler.ccompiler import CCompiler |
| 15 | from packaging.compiler import gen_preprocess_options |
| 16 | from packaging.file_util import write_file |
| 17 | from packaging.dep_util import newer |
| 18 | from packaging import logger |
| 19 | |
| 20 | |
| 21 | class BCPPCompiler(CCompiler) : |
| 22 | """Concrete class that implements an interface to the Borland C/C++ |
| 23 | compiler, as defined by the CCompiler abstract class. |
| 24 | """ |
| 25 | |
| 26 | name = 'bcpp' |
| 27 | description = 'Borland C++ Compiler' |
| 28 | |
| 29 | # Just set this so CCompiler's constructor doesn't barf. We currently |
| 30 | # don't use the 'set_executables()' bureaucracy provided by CCompiler, |
| 31 | # as it really isn't necessary for this sort of single-compiler class. |
| 32 | # Would be nice to have a consistent interface with UnixCCompiler, |
| 33 | # though, so it's worth thinking about. |
| 34 | executables = {} |
| 35 | |
| 36 | # Private class data (need to distinguish C from C++ source for compiler) |
| 37 | _c_extensions = ['.c'] |
| 38 | _cpp_extensions = ['.cc', '.cpp', '.cxx'] |
| 39 | |
| 40 | # Needed for the filename generation methods provided by the |
| 41 | # base class, CCompiler. |
| 42 | src_extensions = _c_extensions + _cpp_extensions |
| 43 | obj_extension = '.obj' |
| 44 | static_lib_extension = '.lib' |
| 45 | shared_lib_extension = '.dll' |
| 46 | static_lib_format = shared_lib_format = '%s%s' |
| 47 | exe_extension = '.exe' |
| 48 | |
| 49 | |
| 50 | def __init__(self, verbose=0, dry_run=False, force=False): |
| 51 | CCompiler.__init__(self, verbose, dry_run, force) |
| 52 | |
| 53 | # These executables are assumed to all be in the path. |
| 54 | # Borland doesn't seem to use any special registry settings to |
| 55 | # indicate their installation locations. |
| 56 | |
| 57 | self.cc = "bcc32.exe" |
| 58 | self.linker = "ilink32.exe" |
| 59 | self.lib = "tlib.exe" |
| 60 | |
| 61 | self.preprocess_options = None |
| 62 | self.compile_options = ['/tWM', '/O2', '/q', '/g0'] |
| 63 | self.compile_options_debug = ['/tWM', '/Od', '/q', '/g0'] |
| 64 | |
| 65 | self.ldflags_shared = ['/Tpd', '/Gn', '/q', '/x'] |
| 66 | self.ldflags_shared_debug = ['/Tpd', '/Gn', '/q', '/x'] |
| 67 | self.ldflags_static = [] |
| 68 | self.ldflags_exe = ['/Gn', '/q', '/x'] |
| 69 | self.ldflags_exe_debug = ['/Gn', '/q', '/x','/r'] |
| 70 | |
| 71 | |
| 72 | # -- Worker methods ------------------------------------------------ |
| 73 | |
| 74 | def compile(self, sources, |
| 75 | output_dir=None, macros=None, include_dirs=None, debug=False, |
| 76 | extra_preargs=None, extra_postargs=None, depends=None): |
| 77 | |
| 78 | macros, objects, extra_postargs, pp_opts, build = \ |
| 79 | self._setup_compile(output_dir, macros, include_dirs, sources, |
| 80 | depends, extra_postargs) |
| 81 | compile_opts = extra_preargs or [] |
| 82 | compile_opts.append('-c') |
| 83 | if debug: |
| 84 | compile_opts.extend(self.compile_options_debug) |
| 85 | else: |
| 86 | compile_opts.extend(self.compile_options) |
| 87 | |
| 88 | for obj in objects: |
| 89 | try: |
| 90 | src, ext = build[obj] |
| 91 | except KeyError: |
| 92 | continue |
| 93 | # XXX why do the normpath here? |
| 94 | src = os.path.normpath(src) |
| 95 | obj = os.path.normpath(obj) |
| 96 | # XXX _setup_compile() did a mkpath() too but before the normpath. |
| 97 | # Is it possible to skip the normpath? |
| 98 | self.mkpath(os.path.dirname(obj)) |
| 99 | |
| 100 | if ext == '.res': |
| 101 | # This is already a binary file -- skip it. |
| 102 | continue # the 'for' loop |
| 103 | if ext == '.rc': |
| 104 | # This needs to be compiled to a .res file -- do it now. |
| 105 | try: |
| 106 | self.spawn(["brcc32", "-fo", obj, src]) |
| 107 | except PackagingExecError as msg: |
| 108 | raise CompileError(msg) |
| 109 | continue # the 'for' loop |
| 110 | |
| 111 | # The next two are both for the real compiler. |
| 112 | if ext in self._c_extensions: |
| 113 | input_opt = "" |
| 114 | elif ext in self._cpp_extensions: |
| 115 | input_opt = "-P" |
| 116 | else: |
| 117 | # Unknown file type -- no extra options. The compiler |
| 118 | # will probably fail, but let it just in case this is a |
| 119 | # file the compiler recognizes even if we don't. |
| 120 | input_opt = "" |
| 121 | |
| 122 | output_opt = "-o" + obj |
| 123 | |
| 124 | # Compiler command line syntax is: "bcc32 [options] file(s)". |
| 125 | # Note that the source file names must appear at the end of |
| 126 | # the command line. |
| 127 | try: |
| 128 | self.spawn([self.cc] + compile_opts + pp_opts + |
| 129 | [input_opt, output_opt] + |
| 130 | extra_postargs + [src]) |
| 131 | except PackagingExecError as msg: |
| 132 | raise CompileError(msg) |
| 133 | |
| 134 | return objects |
| 135 | |
| 136 | |
| 137 | def create_static_lib(self, objects, output_libname, output_dir=None, |
| 138 | debug=False, target_lang=None): |
| 139 | objects, output_dir = self._fix_object_args(objects, output_dir) |
| 140 | output_filename = \ |
| 141 | self.library_filename(output_libname, output_dir=output_dir) |
| 142 | |
| 143 | if self._need_link(objects, output_filename): |
| 144 | lib_args = [output_filename, '/u'] + objects |
| 145 | if debug: |
| 146 | pass # XXX what goes here? |
| 147 | try: |
| 148 | self.spawn([self.lib] + lib_args) |
| 149 | except PackagingExecError as msg: |
| 150 | raise LibError(msg) |
| 151 | else: |
| 152 | logger.debug("skipping %s (up-to-date)", output_filename) |
| 153 | |
| 154 | |
| 155 | def link(self, target_desc, objects, output_filename, output_dir=None, |
| 156 | libraries=None, library_dirs=None, runtime_library_dirs=None, |
| 157 | export_symbols=None, debug=False, extra_preargs=None, |
| 158 | extra_postargs=None, build_temp=None, target_lang=None): |
| 159 | |
| 160 | # XXX this ignores 'build_temp'! should follow the lead of |
| 161 | # msvccompiler.py |
| 162 | |
| 163 | objects, output_dir = self._fix_object_args(objects, output_dir) |
| 164 | libraries, library_dirs, runtime_library_dirs = \ |
| 165 | self._fix_lib_args(libraries, library_dirs, runtime_library_dirs) |
| 166 | |
| 167 | if runtime_library_dirs: |
| 168 | logger.warning("don't know what to do with " |
| 169 | "'runtime_library_dirs': %r", runtime_library_dirs) |
| 170 | |
| 171 | if output_dir is not None: |
| 172 | output_filename = os.path.join(output_dir, output_filename) |
| 173 | |
| 174 | if self._need_link(objects, output_filename): |
| 175 | |
| 176 | # Figure out linker args based on type of target. |
| 177 | if target_desc == CCompiler.EXECUTABLE: |
| 178 | startup_obj = 'c0w32' |
| 179 | if debug: |
| 180 | ld_args = self.ldflags_exe_debug[:] |
| 181 | else: |
| 182 | ld_args = self.ldflags_exe[:] |
| 183 | else: |
| 184 | startup_obj = 'c0d32' |
| 185 | if debug: |
| 186 | ld_args = self.ldflags_shared_debug[:] |
| 187 | else: |
| 188 | ld_args = self.ldflags_shared[:] |
| 189 | |
| 190 | |
| 191 | # Create a temporary exports file for use by the linker |
| 192 | if export_symbols is None: |
| 193 | def_file = '' |
| 194 | else: |
| 195 | head, tail = os.path.split(output_filename) |
| 196 | modname, ext = os.path.splitext(tail) |
| 197 | temp_dir = os.path.dirname(objects[0]) # preserve tree structure |
| 198 | def_file = os.path.join(temp_dir, '%s.def' % modname) |
| 199 | contents = ['EXPORTS'] |
| 200 | for sym in (export_symbols or []): |
| 201 | contents.append(' %s=_%s' % (sym, sym)) |
| 202 | self.execute(write_file, (def_file, contents), |
| 203 | "writing %s" % def_file) |
| 204 | |
| 205 | # Borland C++ has problems with '/' in paths |
| 206 | objects2 = [os.path.normpath(o) for o in objects] |
| 207 | # split objects in .obj and .res files |
| 208 | # Borland C++ needs them at different positions in the command line |
| 209 | objects = [startup_obj] |
| 210 | resources = [] |
| 211 | for file in objects2: |
| 212 | base, ext = os.path.splitext(os.path.normcase(file)) |
| 213 | if ext == '.res': |
| 214 | resources.append(file) |
| 215 | else: |
| 216 | objects.append(file) |
| 217 | |
| 218 | |
| 219 | for l in library_dirs: |
| 220 | ld_args.append("/L%s" % os.path.normpath(l)) |
| 221 | ld_args.append("/L.") # we sometimes use relative paths |
| 222 | |
| 223 | # list of object files |
| 224 | ld_args.extend(objects) |
| 225 | |
| 226 | # XXX the command line syntax for Borland C++ is a bit wonky; |
| 227 | # certain filenames are jammed together in one big string, but |
| 228 | # comma-delimited. This doesn't mesh too well with the |
| 229 | # Unix-centric attitude (with a DOS/Windows quoting hack) of |
| 230 | # 'spawn()', so constructing the argument list is a bit |
| 231 | # awkward. Note that doing the obvious thing and jamming all |
| 232 | # the filenames and commas into one argument would be wrong, |
| 233 | # because 'spawn()' would quote any filenames with spaces in |
| 234 | # them. Arghghh!. Apparently it works fine as coded... |
| 235 | |
| 236 | # name of dll/exe file |
| 237 | ld_args.extend((',',output_filename)) |
| 238 | # no map file and start libraries |
| 239 | ld_args.append(',,') |
| 240 | |
| 241 | for lib in libraries: |
| 242 | # see if we find it and if there is a bcpp specific lib |
| 243 | # (xxx_bcpp.lib) |
| 244 | libfile = self.find_library_file(library_dirs, lib, debug) |
| 245 | if libfile is None: |
| 246 | ld_args.append(lib) |
| 247 | # probably a BCPP internal library -- don't warn |
| 248 | else: |
| 249 | # full name which prefers bcpp_xxx.lib over xxx.lib |
| 250 | ld_args.append(libfile) |
| 251 | |
| 252 | # some default libraries |
| 253 | ld_args.append('import32') |
| 254 | ld_args.append('cw32mt') |
| 255 | |
| 256 | # def file for export symbols |
| 257 | ld_args.extend((',',def_file)) |
| 258 | # add resource files |
| 259 | ld_args.append(',') |
| 260 | ld_args.extend(resources) |
| 261 | |
| 262 | |
| 263 | if extra_preargs: |
| 264 | ld_args[:0] = extra_preargs |
| 265 | if extra_postargs: |
| 266 | ld_args.extend(extra_postargs) |
| 267 | |
| 268 | self.mkpath(os.path.dirname(output_filename)) |
| 269 | try: |
| 270 | self.spawn([self.linker] + ld_args) |
| 271 | except PackagingExecError as msg: |
| 272 | raise LinkError(msg) |
| 273 | |
| 274 | else: |
| 275 | logger.debug("skipping %s (up-to-date)", output_filename) |
| 276 | |
| 277 | # -- Miscellaneous methods ----------------------------------------- |
| 278 | |
| 279 | |
| 280 | def find_library_file(self, dirs, lib, debug=False): |
| 281 | # List of effective library names to try, in order of preference: |
| 282 | # xxx_bcpp.lib is better than xxx.lib |
| 283 | # and xxx_d.lib is better than xxx.lib if debug is set |
| 284 | # |
| 285 | # The "_bcpp" suffix is to handle a Python installation for people |
| 286 | # with multiple compilers (primarily Packaging hackers, I suspect |
| 287 | # ;-). The idea is they'd have one static library for each |
| 288 | # compiler they care about, since (almost?) every Windows compiler |
| 289 | # seems to have a different format for static libraries. |
| 290 | if debug: |
| 291 | dlib = (lib + "_d") |
| 292 | try_names = (dlib + "_bcpp", lib + "_bcpp", dlib, lib) |
| 293 | else: |
| 294 | try_names = (lib + "_bcpp", lib) |
| 295 | |
| 296 | for dir in dirs: |
| 297 | for name in try_names: |
| 298 | libfile = os.path.join(dir, self.library_filename(name)) |
| 299 | if os.path.exists(libfile): |
| 300 | return libfile |
| 301 | else: |
| 302 | # Oops, didn't find it in *any* of 'dirs' |
| 303 | return None |
| 304 | |
| 305 | # overwrite the one from CCompiler to support rc and res-files |
| 306 | def object_filenames(self, source_filenames, strip_dir=False, |
| 307 | output_dir=''): |
| 308 | if output_dir is None: |
| 309 | output_dir = '' |
| 310 | obj_names = [] |
| 311 | for src_name in source_filenames: |
| 312 | # use normcase to make sure '.rc' is really '.rc' and not '.RC' |
| 313 | base, ext = os.path.splitext(os.path.normcase(src_name)) |
| 314 | if ext not in (self.src_extensions + ['.rc','.res']): |
| 315 | raise UnknownFileError("unknown file type '%s' (from '%s')" % \ |
| 316 | (ext, src_name)) |
| 317 | if strip_dir: |
| 318 | base = os.path.basename(base) |
| 319 | if ext == '.res': |
| 320 | # these can go unchanged |
| 321 | obj_names.append(os.path.join(output_dir, base + ext)) |
| 322 | elif ext == '.rc': |
| 323 | # these need to be compiled to .res-files |
| 324 | obj_names.append(os.path.join(output_dir, base + '.res')) |
| 325 | else: |
| 326 | obj_names.append(os.path.join(output_dir, |
| 327 | base + self.obj_extension)) |
| 328 | return obj_names |
| 329 | |
| 330 | |
| 331 | def preprocess(self, source, output_file=None, macros=None, |
| 332 | include_dirs=None, extra_preargs=None, |
| 333 | extra_postargs=None): |
| 334 | _, macros, include_dirs = \ |
| 335 | self._fix_compile_args(None, macros, include_dirs) |
| 336 | pp_opts = gen_preprocess_options(macros, include_dirs) |
| 337 | pp_args = ['cpp32.exe'] + pp_opts |
| 338 | if output_file is not None: |
| 339 | pp_args.append('-o' + output_file) |
| 340 | if extra_preargs: |
| 341 | pp_args[:0] = extra_preargs |
| 342 | if extra_postargs: |
| 343 | pp_args.extend(extra_postargs) |
| 344 | pp_args.append(source) |
| 345 | |
| 346 | # We need to preprocess: either we're being forced to, or the |
| 347 | # source file is newer than the target (or the target doesn't |
| 348 | # exist). |
| 349 | if self.force or output_file is None or newer(source, output_file): |
| 350 | if output_file: |
| 351 | self.mkpath(os.path.dirname(output_file)) |
| 352 | try: |
| 353 | self.spawn(pp_args) |
| 354 | except PackagingExecError as msg: |
| 355 | print(msg) |
| 356 | raise CompileError(msg) |