blob: abe302a80441a7342e19fcf21827dcf3664f0da9 [file] [log] [blame]
Greg Wardfe9b8182000-06-28 01:20:35 +00001"""distutils.bcppcompiler
2
3Contains BorlandCCompiler, an implementation of the abstract CCompiler class
4for 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
Greg Wardfe9b8182000-06-28 01:20:35 +000014__revision__ = "$Id$"
15
16
Eric S. Raymond8b3cf582001-02-09 11:14:08 +000017import sys, os
Greg Wardfe9b8182000-06-28 01:20:35 +000018from distutils.errors import \
19 DistutilsExecError, DistutilsPlatformError, \
Andrew M. Kuchling6386a4c2001-08-09 21:02:34 +000020 CompileError, LibError, LinkError, UnknownFileError
Greg Wardfe9b8182000-06-28 01:20:35 +000021from distutils.ccompiler import \
22 CCompiler, gen_preprocess_options, gen_lib_options
Greg Wardc58c5172000-08-02 01:03:23 +000023from distutils.file_util import write_file
Andrew M. Kuchlingdb7aed52001-08-16 20:17:41 +000024from distutils.dep_util import newer
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000025from distutils import log
Greg Wardfe9b8182000-06-28 01:20:35 +000026
27class BCPPCompiler(CCompiler) :
28 """Concrete class that implements an interface to the Borland C/C++
29 compiler, as defined by the CCompiler abstract class.
30 """
31
32 compiler_type = 'bcpp'
33
34 # Just set this so CCompiler's constructor doesn't barf. We currently
35 # don't use the 'set_executables()' bureaucracy provided by CCompiler,
36 # as it really isn't necessary for this sort of single-compiler class.
37 # Would be nice to have a consistent interface with UnixCCompiler,
38 # though, so it's worth thinking about.
39 executables = {}
40
41 # Private class data (need to distinguish C from C++ source for compiler)
42 _c_extensions = ['.c']
43 _cpp_extensions = ['.cc', '.cpp', '.cxx']
44
45 # Needed for the filename generation methods provided by the
46 # base class, CCompiler.
47 src_extensions = _c_extensions + _cpp_extensions
48 obj_extension = '.obj'
49 static_lib_extension = '.lib'
50 shared_lib_extension = '.dll'
51 static_lib_format = shared_lib_format = '%s%s'
52 exe_extension = '.exe'
53
54
55 def __init__ (self,
56 verbose=0,
57 dry_run=0,
58 force=0):
59
60 CCompiler.__init__ (self, verbose, dry_run, force)
61
62 # These executables are assumed to all be in the path.
63 # Borland doesn't seem to use any special registry settings to
64 # indicate their installation locations.
65
66 self.cc = "bcc32.exe"
Greg Ward42406482000-09-27 02:08:14 +000067 self.linker = "ilink32.exe"
Greg Wardfe9b8182000-06-28 01:20:35 +000068 self.lib = "tlib.exe"
69
70 self.preprocess_options = None
Greg Warda4662bc2000-08-13 00:43:16 +000071 self.compile_options = ['/tWM', '/O2', '/q', '/g0']
72 self.compile_options_debug = ['/tWM', '/Od', '/q', '/g0']
Greg Wardfe9b8182000-06-28 01:20:35 +000073
74 self.ldflags_shared = ['/Tpd', '/Gn', '/q', '/x']
75 self.ldflags_shared_debug = ['/Tpd', '/Gn', '/q', '/x']
76 self.ldflags_static = []
Greg Ward42406482000-09-27 02:08:14 +000077 self.ldflags_exe = ['/Gn', '/q', '/x']
78 self.ldflags_exe_debug = ['/Gn', '/q', '/x','/r']
Greg Wardfe9b8182000-06-28 01:20:35 +000079
80
81 # -- Worker methods ------------------------------------------------
82
Jeremy Hylton1bba31d2002-06-13 17:28:18 +000083 def compile(self, sources,
84 output_dir=None, macros=None, include_dirs=None, debug=0,
85 extra_preargs=None, extra_postargs=None, depends=None):
86
87 macros, objects, extra_postargs, pp_opts, build = \
88 self._setup_compile(output_dir, macros, include_dirs, sources,
89 depends, extra_postargs)
Greg Wardfe9b8182000-06-28 01:20:35 +000090 compile_opts = extra_preargs or []
91 compile_opts.append ('-c')
92 if debug:
93 compile_opts.extend (self.compile_options_debug)
94 else:
95 compile_opts.extend (self.compile_options)
Fred Drakeb94b8492001-12-06 20:51:35 +000096
Jeremy Hylton1bba31d2002-06-13 17:28:18 +000097 for obj, (src, ext) in build.items():
98 # XXX why do the normpath here?
99 src = os.path.normpath(src)
100 obj = os.path.normpath(obj)
101 # XXX _setup_compile() did a mkpath() too but before the normpath.
102 # Is it possible to skip the normpath?
103 self.mkpath(os.path.dirname(obj))
Greg Wardfe9b8182000-06-28 01:20:35 +0000104
Jeremy Hylton1bba31d2002-06-13 17:28:18 +0000105 if ext == '.res':
106 # This is already a binary file -- skip it.
107 continue # the 'for' loop
108 if ext == '.rc':
109 # This needs to be compiled to a .res file -- do it now.
Greg Wardfe9b8182000-06-28 01:20:35 +0000110 try:
Jeremy Hylton1bba31d2002-06-13 17:28:18 +0000111 self.spawn (["brcc32", "-fo", obj, src])
Greg Wardfe9b8182000-06-28 01:20:35 +0000112 except DistutilsExecError, msg:
113 raise CompileError, msg
Jeremy Hylton1bba31d2002-06-13 17:28:18 +0000114 continue # the 'for' loop
115
116 # The next two are both for the real compiler.
117 if ext in self._c_extensions:
118 input_opt = ""
119 elif ext in self._cpp_extensions:
120 input_opt = "-P"
121 else:
122 # Unknown file type -- no extra options. The compiler
123 # will probably fail, but let it just in case this is a
124 # file the compiler recognizes even if we don't.
125 input_opt = ""
126
127 output_opt = "-o" + obj
128
129 # Compiler command line syntax is: "bcc32 [options] file(s)".
130 # Note that the source file names must appear at the end of
131 # the command line.
132 try:
133 self.spawn ([self.cc] + compile_opts + pp_opts +
134 [input_opt, output_opt] +
135 extra_postargs + [src])
136 except DistutilsExecError, msg:
137 raise CompileError, msg
Greg Wardfe9b8182000-06-28 01:20:35 +0000138
139 return objects
140
141 # compile ()
142
143
144 def create_static_lib (self,
145 objects,
146 output_libname,
147 output_dir=None,
148 debug=0,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000149 target_lang=None):
Greg Wardfe9b8182000-06-28 01:20:35 +0000150
151 (objects, output_dir) = self._fix_object_args (objects, output_dir)
152 output_filename = \
153 self.library_filename (output_libname, output_dir=output_dir)
154
155 if self._need_link (objects, output_filename):
156 lib_args = [output_filename, '/u'] + objects
157 if debug:
158 pass # XXX what goes here?
Greg Wardfe9b8182000-06-28 01:20:35 +0000159 try:
Fred Drakeb94b8492001-12-06 20:51:35 +0000160 self.spawn ([self.lib] + lib_args)
Greg Wardfe9b8182000-06-28 01:20:35 +0000161 except DistutilsExecError, msg:
Fred Drakeb94b8492001-12-06 20:51:35 +0000162 raise LibError, msg
Greg Wardfe9b8182000-06-28 01:20:35 +0000163 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000164 log.debug("skipping %s (up-to-date)", output_filename)
Greg Wardfe9b8182000-06-28 01:20:35 +0000165
166 # create_static_lib ()
Fred Drakeb94b8492001-12-06 20:51:35 +0000167
168
Greg Ward42406482000-09-27 02:08:14 +0000169 def link (self,
Fred Drakeb94b8492001-12-06 20:51:35 +0000170 target_desc,
Greg Ward42406482000-09-27 02:08:14 +0000171 objects,
172 output_filename,
173 output_dir=None,
174 libraries=None,
175 library_dirs=None,
176 runtime_library_dirs=None,
177 export_symbols=None,
178 debug=0,
179 extra_preargs=None,
180 extra_postargs=None,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000181 build_temp=None,
182 target_lang=None):
Greg Wardfe9b8182000-06-28 01:20:35 +0000183
Greg Wardc58c5172000-08-02 01:03:23 +0000184 # XXX this ignores 'build_temp'! should follow the lead of
185 # msvccompiler.py
186
Greg Wardfe9b8182000-06-28 01:20:35 +0000187 (objects, output_dir) = self._fix_object_args (objects, output_dir)
188 (libraries, library_dirs, runtime_library_dirs) = \
189 self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
190
191 if runtime_library_dirs:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000192 log.warn("I don't know what to do with 'runtime_library_dirs': %s",
193 str(runtime_library_dirs))
Greg Ward42406482000-09-27 02:08:14 +0000194
Greg Wardfe9b8182000-06-28 01:20:35 +0000195 if output_dir is not None:
196 output_filename = os.path.join (output_dir, output_filename)
197
198 if self._need_link (objects, output_filename):
199
Greg Ward42406482000-09-27 02:08:14 +0000200 # Figure out linker args based on type of target.
201 if target_desc == CCompiler.EXECUTABLE:
202 startup_obj = 'c0w32'
203 if debug:
204 ld_args = self.ldflags_exe_debug[:]
205 else:
206 ld_args = self.ldflags_exe[:]
Greg Wardfe9b8182000-06-28 01:20:35 +0000207 else:
Greg Ward42406482000-09-27 02:08:14 +0000208 startup_obj = 'c0d32'
209 if debug:
210 ld_args = self.ldflags_shared_debug[:]
211 else:
212 ld_args = self.ldflags_shared[:]
213
Greg Wardfe9b8182000-06-28 01:20:35 +0000214
Greg Wardfe9b8182000-06-28 01:20:35 +0000215 # Create a temporary exports file for use by the linker
Greg Ward42406482000-09-27 02:08:14 +0000216 if export_symbols is None:
217 def_file = ''
218 else:
219 head, tail = os.path.split (output_filename)
220 modname, ext = os.path.splitext (tail)
221 temp_dir = os.path.dirname(objects[0]) # preserve tree structure
222 def_file = os.path.join (temp_dir, '%s.def' % modname)
223 contents = ['EXPORTS']
224 for sym in (export_symbols or []):
225 contents.append(' %s=_%s' % (sym, sym))
226 self.execute(write_file, (def_file, contents),
227 "writing %s" % def_file)
Greg Wardfe9b8182000-06-28 01:20:35 +0000228
Greg Wardcec15682000-09-01 01:28:33 +0000229 # Borland C++ has problems with '/' in paths
Greg Ward42406482000-09-27 02:08:14 +0000230 objects2 = map(os.path.normpath, objects)
231 # split objects in .obj and .res files
232 # Borland C++ needs them at different positions in the command line
233 objects = [startup_obj]
234 resources = []
235 for file in objects2:
236 (base, ext) = os.path.splitext(os.path.normcase(file))
237 if ext == '.res':
238 resources.append(file)
239 else:
240 objects.append(file)
Fred Drakeb94b8492001-12-06 20:51:35 +0000241
242
Greg Wardc58c5172000-08-02 01:03:23 +0000243 for l in library_dirs:
Fred Drakeb94b8492001-12-06 20:51:35 +0000244 ld_args.append("/L%s" % os.path.normpath(l))
Greg Ward42406482000-09-27 02:08:14 +0000245 ld_args.append("/L.") # we sometimes use relative paths
246
Fred Drakeb94b8492001-12-06 20:51:35 +0000247 # list of object files
248 ld_args.extend(objects)
Greg Wardc58c5172000-08-02 01:03:23 +0000249
Greg Ward13980452000-08-13 00:54:39 +0000250 # XXX the command-line syntax for Borland C++ is a bit wonky;
251 # certain filenames are jammed together in one big string, but
252 # comma-delimited. This doesn't mesh too well with the
253 # Unix-centric attitude (with a DOS/Windows quoting hack) of
254 # 'spawn()', so constructing the argument list is a bit
255 # awkward. Note that doing the obvious thing and jamming all
256 # the filenames and commas into one argument would be wrong,
257 # because 'spawn()' would quote any filenames with spaces in
258 # them. Arghghh!. Apparently it works fine as coded...
259
Greg Ward42406482000-09-27 02:08:14 +0000260 # name of dll/exe file
Greg Wardc58c5172000-08-02 01:03:23 +0000261 ld_args.extend([',',output_filename])
Fred Drakeb94b8492001-12-06 20:51:35 +0000262 # no map file and start libraries
Greg Warda4662bc2000-08-13 00:43:16 +0000263 ld_args.append(',,')
Greg Wardc58c5172000-08-02 01:03:23 +0000264
265 for lib in libraries:
Fred Drakeb94b8492001-12-06 20:51:35 +0000266 # see if we find it and if there is a bcpp specific lib
Greg Ward42406482000-09-27 02:08:14 +0000267 # (xxx_bcpp.lib)
Greg Wardc58c5172000-08-02 01:03:23 +0000268 libfile = self.find_library_file(library_dirs, lib, debug)
269 if libfile is None:
270 ld_args.append(lib)
271 # probably a BCPP internal library -- don't warn
Greg Wardc58c5172000-08-02 01:03:23 +0000272 else:
273 # full name which prefers bcpp_xxx.lib over xxx.lib
274 ld_args.append(libfile)
Greg Ward42406482000-09-27 02:08:14 +0000275
276 # some default libraries
277 ld_args.append ('import32')
278 ld_args.append ('cw32mt')
279
Greg Wardc58c5172000-08-02 01:03:23 +0000280 # def file for export symbols
281 ld_args.extend([',',def_file])
Greg Ward42406482000-09-27 02:08:14 +0000282 # add resource files
283 ld_args.append(',')
284 ld_args.extend(resources)
285
Fred Drakeb94b8492001-12-06 20:51:35 +0000286
Greg Wardfe9b8182000-06-28 01:20:35 +0000287 if extra_preargs:
288 ld_args[:0] = extra_preargs
289 if extra_postargs:
Greg Wardc58c5172000-08-02 01:03:23 +0000290 ld_args.extend(extra_postargs)
Greg Wardfe9b8182000-06-28 01:20:35 +0000291
292 self.mkpath (os.path.dirname (output_filename))
293 try:
Greg Ward42406482000-09-27 02:08:14 +0000294 self.spawn ([self.linker] + ld_args)
Greg Wardfe9b8182000-06-28 01:20:35 +0000295 except DistutilsExecError, msg:
296 raise LinkError, msg
297
298 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000299 log.debug("skipping %s (up-to-date)", output_filename)
Greg Wardfe9b8182000-06-28 01:20:35 +0000300
Greg Ward42406482000-09-27 02:08:14 +0000301 # link ()
Greg Wardfe9b8182000-06-28 01:20:35 +0000302
303 # -- Miscellaneous methods -----------------------------------------
Greg Wardfe9b8182000-06-28 01:20:35 +0000304
305
Greg Wardc58c5172000-08-02 01:03:23 +0000306 def find_library_file (self, dirs, lib, debug=0):
Greg Ward5db2c3a2000-08-04 01:30:03 +0000307 # List of effective library names to try, in order of preference:
Greg Ward42406482000-09-27 02:08:14 +0000308 # xxx_bcpp.lib is better than xxx.lib
Greg Wardc58c5172000-08-02 01:03:23 +0000309 # and xxx_d.lib is better than xxx.lib if debug is set
Greg Ward5db2c3a2000-08-04 01:30:03 +0000310 #
Greg Ward42406482000-09-27 02:08:14 +0000311 # The "_bcpp" suffix is to handle a Python installation for people
Greg Ward5db2c3a2000-08-04 01:30:03 +0000312 # with multiple compilers (primarily Distutils hackers, I suspect
313 # ;-). The idea is they'd have one static library for each
314 # compiler they care about, since (almost?) every Windows compiler
315 # seems to have a different format for static libraries.
316 if debug:
317 dlib = (lib + "_d")
Greg Wardcec15682000-09-01 01:28:33 +0000318 try_names = (dlib + "_bcpp", lib + "_bcpp", dlib, lib)
Greg Ward5db2c3a2000-08-04 01:30:03 +0000319 else:
Greg Wardcec15682000-09-01 01:28:33 +0000320 try_names = (lib + "_bcpp", lib)
Greg Wardfe9b8182000-06-28 01:20:35 +0000321
Greg Ward5db2c3a2000-08-04 01:30:03 +0000322 for dir in dirs:
323 for name in try_names:
324 libfile = os.path.join(dir, self.library_filename(name))
325 if os.path.exists(libfile):
326 return libfile
Greg Wardfe9b8182000-06-28 01:20:35 +0000327 else:
328 # Oops, didn't find it in *any* of 'dirs'
329 return None
330
Greg Ward42406482000-09-27 02:08:14 +0000331 # overwrite the one from CCompiler to support rc and res-files
332 def object_filenames (self,
333 source_filenames,
334 strip_dir=0,
335 output_dir=''):
336 if output_dir is None: output_dir = ''
337 obj_names = []
338 for src_name in source_filenames:
339 # use normcase to make sure '.rc' is really '.rc' and not '.RC'
340 (base, ext) = os.path.splitext (os.path.normcase(src_name))
341 if ext not in (self.src_extensions + ['.rc','.res']):
342 raise UnknownFileError, \
343 "unknown file type '%s' (from '%s')" % \
344 (ext, src_name)
345 if strip_dir:
346 base = os.path.basename (base)
347 if ext == '.res':
348 # these can go unchanged
349 obj_names.append (os.path.join (output_dir, base + ext))
350 elif ext == '.rc':
351 # these need to be compiled to .res-files
352 obj_names.append (os.path.join (output_dir, base + '.res'))
353 else:
354 obj_names.append (os.path.join (output_dir,
355 base + self.obj_extension))
356 return obj_names
357
358 # object_filenames ()
Andrew M. Kuchlingdb7aed52001-08-16 20:17:41 +0000359
360 def preprocess (self,
361 source,
362 output_file=None,
363 macros=None,
364 include_dirs=None,
365 extra_preargs=None,
366 extra_postargs=None):
367
368 (_, macros, include_dirs) = \
369 self._fix_compile_args(None, macros, include_dirs)
370 pp_opts = gen_preprocess_options(macros, include_dirs)
371 pp_args = ['cpp32.exe'] + pp_opts
372 if output_file is not None:
373 pp_args.append('-o' + output_file)
374 if extra_preargs:
375 pp_args[:0] = extra_preargs
376 if extra_postargs:
377 pp_args.extend(extra_postargs)
378 pp_args.append(source)
379
380 # We need to preprocess: either we're being forced to, or the
381 # source file is newer than the target (or the target doesn't
382 # exist).
383 if self.force or output_file is None or newer(source, output_file):
384 if output_file:
385 self.mkpath(os.path.dirname(output_file))
386 try:
387 self.spawn(pp_args)
388 except DistutilsExecError, msg:
389 print msg
390 raise CompileError, msg
391
392 # preprocess()