Greg Ward | fe9b818 | 2000-06-28 01:20:35 +0000 | [diff] [blame] | 1 | """distutils.bcppcompiler |
| 2 | |
| 3 | Contains BorlandCCompiler, an implementation of the abstract CCompiler class |
| 4 | for the Borland C++ compiler. |
| 5 | """ |
| 6 | |
| 7 | # This implementation by Lyle Johnson, based on the original msvccompiler.py |
| 8 | # module and using the directions originally published by Gordon Williams. |
| 9 | |
| 10 | # XXX looks like there's a LOT of overlap between these two classes: |
| 11 | # someone should sit down and factor out the common code as |
| 12 | # WindowsCCompiler! --GPW |
| 13 | |
| 14 | # XXX Lyle reports that this doesn't quite work yet: |
| 15 | # """...but this is what I've got so far. The compile step works fine but |
| 16 | # when it runs the link step I get an "out of memory" failure. Since |
| 17 | # spawn() echoes the command it's trying to spawn, I can type the link line |
| 18 | # verbatim at the DOS prompt and it links the Windows DLL correctly -- so |
| 19 | # the syntax is correct. There's just some weird interaction going on when |
| 20 | # it tries to "spawn" the link process from within the setup.py script. I'm |
| 21 | # not really sure how to debug this one right off-hand; obviously there's |
| 22 | # nothing wrong with the "spawn()" function since it's working properly for |
| 23 | # the compile stage.""" |
| 24 | |
| 25 | __revision__ = "$Id$" |
| 26 | |
| 27 | |
| 28 | import sys, os, string |
| 29 | from distutils.errors import \ |
| 30 | DistutilsExecError, DistutilsPlatformError, \ |
| 31 | CompileError, LibError, LinkError |
| 32 | from distutils.ccompiler import \ |
| 33 | CCompiler, gen_preprocess_options, gen_lib_options |
| 34 | |
| 35 | |
| 36 | class BCPPCompiler(CCompiler) : |
| 37 | """Concrete class that implements an interface to the Borland C/C++ |
| 38 | compiler, as defined by the CCompiler abstract class. |
| 39 | """ |
| 40 | |
| 41 | compiler_type = 'bcpp' |
| 42 | |
| 43 | # Just set this so CCompiler's constructor doesn't barf. We currently |
| 44 | # don't use the 'set_executables()' bureaucracy provided by CCompiler, |
| 45 | # as it really isn't necessary for this sort of single-compiler class. |
| 46 | # Would be nice to have a consistent interface with UnixCCompiler, |
| 47 | # though, so it's worth thinking about. |
| 48 | executables = {} |
| 49 | |
| 50 | # Private class data (need to distinguish C from C++ source for compiler) |
| 51 | _c_extensions = ['.c'] |
| 52 | _cpp_extensions = ['.cc', '.cpp', '.cxx'] |
| 53 | |
| 54 | # Needed for the filename generation methods provided by the |
| 55 | # base class, CCompiler. |
| 56 | src_extensions = _c_extensions + _cpp_extensions |
| 57 | obj_extension = '.obj' |
| 58 | static_lib_extension = '.lib' |
| 59 | shared_lib_extension = '.dll' |
| 60 | static_lib_format = shared_lib_format = '%s%s' |
| 61 | exe_extension = '.exe' |
| 62 | |
| 63 | |
| 64 | def __init__ (self, |
| 65 | verbose=0, |
| 66 | dry_run=0, |
| 67 | force=0): |
| 68 | |
| 69 | CCompiler.__init__ (self, verbose, dry_run, force) |
| 70 | |
| 71 | # These executables are assumed to all be in the path. |
| 72 | # Borland doesn't seem to use any special registry settings to |
| 73 | # indicate their installation locations. |
| 74 | |
| 75 | self.cc = "bcc32.exe" |
| 76 | self.link = "ilink32.exe" |
| 77 | self.lib = "tlib.exe" |
| 78 | |
| 79 | self.preprocess_options = None |
| 80 | self.compile_options = ['/tWM', '/O2', '/q'] |
| 81 | self.compile_options_debug = ['/tWM', '/Od', '/q'] |
| 82 | |
| 83 | self.ldflags_shared = ['/Tpd', '/Gn', '/q', '/x'] |
| 84 | self.ldflags_shared_debug = ['/Tpd', '/Gn', '/q', '/x'] |
| 85 | self.ldflags_static = [] |
| 86 | |
| 87 | |
| 88 | # -- Worker methods ------------------------------------------------ |
| 89 | |
| 90 | def compile (self, |
| 91 | sources, |
| 92 | output_dir=None, |
| 93 | macros=None, |
| 94 | include_dirs=None, |
| 95 | debug=0, |
| 96 | extra_preargs=None, |
| 97 | extra_postargs=None): |
| 98 | |
| 99 | (output_dir, macros, include_dirs) = \ |
| 100 | self._fix_compile_args (output_dir, macros, include_dirs) |
| 101 | (objects, skip_sources) = self._prep_compile (sources, output_dir) |
| 102 | |
| 103 | if extra_postargs is None: |
| 104 | extra_postargs = [] |
| 105 | |
| 106 | pp_opts = gen_preprocess_options (macros, include_dirs) |
| 107 | compile_opts = extra_preargs or [] |
| 108 | compile_opts.append ('-c') |
| 109 | if debug: |
| 110 | compile_opts.extend (self.compile_options_debug) |
| 111 | else: |
| 112 | compile_opts.extend (self.compile_options) |
| 113 | |
| 114 | for i in range (len (sources)): |
| 115 | src = sources[i] ; obj = objects[i] |
| 116 | ext = (os.path.splitext (src))[1] |
| 117 | |
| 118 | if skip_sources[src]: |
| 119 | self.announce ("skipping %s (%s up-to-date)" % (src, obj)) |
| 120 | else: |
| 121 | if ext in self._c_extensions: |
| 122 | input_opt = "" |
| 123 | elif ext in self._cpp_extensions: |
| 124 | input_opt = "-P" |
| 125 | |
| 126 | output_opt = "-o" + obj |
| 127 | |
| 128 | self.mkpath (os.path.dirname (obj)) |
| 129 | |
| 130 | # Compiler command line syntax is: "bcc32 [options] file(s)". |
| 131 | # Note that the source file names must appear at the end of |
| 132 | # the command line. |
| 133 | |
| 134 | try: |
| 135 | self.spawn ([self.cc] + compile_opts + pp_opts + |
| 136 | [input_opt, output_opt] + |
| 137 | extra_postargs + [src]) |
| 138 | except DistutilsExecError, msg: |
| 139 | raise CompileError, msg |
| 140 | |
| 141 | return objects |
| 142 | |
| 143 | # compile () |
| 144 | |
| 145 | |
| 146 | def create_static_lib (self, |
| 147 | objects, |
| 148 | output_libname, |
| 149 | output_dir=None, |
| 150 | debug=0, |
| 151 | extra_preargs=None, |
| 152 | extra_postargs=None): |
| 153 | |
| 154 | (objects, output_dir) = self._fix_object_args (objects, output_dir) |
| 155 | output_filename = \ |
| 156 | self.library_filename (output_libname, output_dir=output_dir) |
| 157 | |
| 158 | if self._need_link (objects, output_filename): |
| 159 | lib_args = [output_filename, '/u'] + objects |
| 160 | if debug: |
| 161 | pass # XXX what goes here? |
| 162 | if extra_preargs: |
| 163 | lib_args[:0] = extra_preargs |
| 164 | if extra_postargs: |
| 165 | lib_args.extend (extra_postargs) |
| 166 | try: |
| 167 | self.spawn ([self.lib] + lib_args) |
| 168 | except DistutilsExecError, msg: |
| 169 | raise LibError, msg |
| 170 | else: |
| 171 | self.announce ("skipping %s (up-to-date)" % output_filename) |
| 172 | |
| 173 | # create_static_lib () |
| 174 | |
| 175 | |
| 176 | def link_shared_lib (self, |
| 177 | objects, |
| 178 | output_libname, |
| 179 | output_dir=None, |
| 180 | libraries=None, |
| 181 | library_dirs=None, |
| 182 | runtime_library_dirs=None, |
| 183 | export_symbols=None, |
| 184 | debug=0, |
| 185 | extra_preargs=None, |
| 186 | extra_postargs=None, |
| 187 | build_temp=None): |
| 188 | |
| 189 | self.link_shared_object (objects, |
| 190 | self.shared_library_name(output_libname), |
| 191 | output_dir=output_dir, |
| 192 | libraries=libraries, |
| 193 | library_dirs=library_dirs, |
| 194 | runtime_library_dirs=runtime_library_dirs, |
| 195 | export_symbols=export_symbols, |
| 196 | debug=debug, |
| 197 | extra_preargs=extra_preargs, |
| 198 | extra_postargs=extra_postargs, |
| 199 | build_temp=build_temp) |
| 200 | |
| 201 | |
| 202 | def link_shared_object (self, |
| 203 | objects, |
| 204 | output_filename, |
| 205 | output_dir=None, |
| 206 | libraries=None, |
| 207 | library_dirs=None, |
| 208 | runtime_library_dirs=None, |
| 209 | export_symbols=None, |
| 210 | debug=0, |
| 211 | extra_preargs=None, |
| 212 | extra_postargs=None, |
| 213 | build_temp=None): |
| 214 | |
| 215 | (objects, output_dir) = self._fix_object_args (objects, output_dir) |
| 216 | (libraries, library_dirs, runtime_library_dirs) = \ |
| 217 | self._fix_lib_args (libraries, library_dirs, runtime_library_dirs) |
| 218 | |
| 219 | if runtime_library_dirs: |
| 220 | self.warn ("I don't know what to do with 'runtime_library_dirs': " |
| 221 | + str (runtime_library_dirs)) |
| 222 | |
| 223 | if output_dir is not None: |
| 224 | output_filename = os.path.join (output_dir, output_filename) |
| 225 | |
| 226 | if self._need_link (objects, output_filename): |
| 227 | |
| 228 | if debug: |
| 229 | ldflags = self.ldflags_shared_debug |
| 230 | else: |
| 231 | ldflags = self.ldflags_shared |
| 232 | |
| 233 | startup_obj = 'c0d32' |
| 234 | |
| 235 | libraries.append ('mypylib') |
| 236 | libraries.append ('import32') |
| 237 | libraries.append ('cw32mt') |
| 238 | |
| 239 | # Create a temporary exports file for use by the linker |
| 240 | head, tail = os.path.split (output_filename) |
| 241 | modname, ext = os.path.splitext (tail) |
| 242 | def_file = os.path.join (build_temp, '%s.def' % modname) |
| 243 | f = open (def_file, 'w') |
| 244 | f.write ('EXPORTS\n') |
| 245 | for sym in (export_symbols or []): |
| 246 | f.write (' %s=_%s\n' % (sym, sym)) |
| 247 | |
| 248 | ld_args = ldflags + [startup_obj] + objects + \ |
| 249 | [',%s,,' % output_filename] + \ |
| 250 | libraries + [',' + def_file] |
| 251 | |
| 252 | if extra_preargs: |
| 253 | ld_args[:0] = extra_preargs |
| 254 | if extra_postargs: |
| 255 | ld_args.extend (extra_postargs) |
| 256 | |
| 257 | self.mkpath (os.path.dirname (output_filename)) |
| 258 | try: |
| 259 | self.spawn ([self.link] + ld_args) |
| 260 | except DistutilsExecError, msg: |
| 261 | raise LinkError, msg |
| 262 | |
| 263 | else: |
| 264 | self.announce ("skipping %s (up-to-date)" % output_filename) |
| 265 | |
| 266 | # link_shared_object () |
| 267 | |
| 268 | |
| 269 | def link_executable (self, |
| 270 | objects, |
| 271 | output_progname, |
| 272 | output_dir=None, |
| 273 | libraries=None, |
| 274 | library_dirs=None, |
| 275 | runtime_library_dirs=None, |
| 276 | debug=0, |
| 277 | extra_preargs=None, |
| 278 | extra_postargs=None): |
| 279 | |
| 280 | (objects, output_dir) = self._fix_object_args (objects, output_dir) |
| 281 | (libraries, library_dirs, runtime_library_dirs) = \ |
| 282 | self._fix_lib_args (libraries, library_dirs, runtime_library_dirs) |
| 283 | |
| 284 | if runtime_library_dirs: |
| 285 | self.warn ("I don't know what to do with 'runtime_library_dirs': " |
| 286 | + str (runtime_library_dirs)) |
| 287 | |
| 288 | lib_opts = gen_lib_options (self, |
| 289 | library_dirs, runtime_library_dirs, |
| 290 | libraries) |
| 291 | output_filename = output_progname + self.exe_extension |
| 292 | if output_dir is not None: |
| 293 | output_filename = os.path.join (output_dir, output_filename) |
| 294 | |
| 295 | if self._need_link (objects, output_filename): |
| 296 | |
| 297 | if debug: |
| 298 | ldflags = self.ldflags_shared_debug[1:] |
| 299 | else: |
| 300 | ldflags = self.ldflags_shared[1:] |
| 301 | |
| 302 | ld_args = ldflags + lib_opts + \ |
| 303 | objects + ['/OUT:' + output_filename] |
| 304 | |
| 305 | if extra_preargs: |
| 306 | ld_args[:0] = extra_preargs |
| 307 | if extra_postargs: |
| 308 | ld_args.extend (extra_postargs) |
| 309 | |
| 310 | self.mkpath (os.path.dirname (output_filename)) |
| 311 | try: |
| 312 | self.spawn ([self.link] + ld_args) |
| 313 | except DistutilsExecError, msg: |
| 314 | raise LinkError, msg |
| 315 | else: |
| 316 | self.announce ("skipping %s (up-to-date)" % output_filename) |
| 317 | |
| 318 | |
| 319 | # -- Miscellaneous methods ----------------------------------------- |
| 320 | # These are all used by the 'gen_lib_options() function, in |
| 321 | # ccompiler.py. |
| 322 | |
| 323 | def library_dir_option (self, dir): |
| 324 | return "-L" + dir |
| 325 | |
| 326 | def runtime_library_dir_option (self, dir): |
| 327 | raise DistutilsPlatformError, \ |
| 328 | "don't know how to set runtime library search path for MSVC++" |
| 329 | |
| 330 | def library_option (self, lib): |
| 331 | return self.library_filename (lib) |
| 332 | |
| 333 | |
| 334 | def find_library_file (self, dirs, lib): |
| 335 | |
| 336 | for dir in dirs: |
| 337 | libfile = os.path.join (dir, self.library_filename (lib)) |
| 338 | if os.path.exists (libfile): |
| 339 | return libfile |
| 340 | |
| 341 | else: |
| 342 | # Oops, didn't find it in *any* of 'dirs' |
| 343 | return None |
| 344 | |