blob: 019244cd16c2255ea0f220cccb7ffb38086bb9d2 [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
83 def compile (self,
84 sources,
85 output_dir=None,
86 macros=None,
87 include_dirs=None,
88 debug=0,
89 extra_preargs=None,
90 extra_postargs=None):
91
92 (output_dir, macros, include_dirs) = \
93 self._fix_compile_args (output_dir, macros, include_dirs)
94 (objects, skip_sources) = self._prep_compile (sources, output_dir)
95
96 if extra_postargs is None:
97 extra_postargs = []
98
99 pp_opts = gen_preprocess_options (macros, include_dirs)
100 compile_opts = extra_preargs or []
101 compile_opts.append ('-c')
102 if debug:
103 compile_opts.extend (self.compile_options_debug)
104 else:
105 compile_opts.extend (self.compile_options)
Fred Drakeb94b8492001-12-06 20:51:35 +0000106
Greg Wardfe9b8182000-06-28 01:20:35 +0000107 for i in range (len (sources)):
108 src = sources[i] ; obj = objects[i]
109 ext = (os.path.splitext (src))[1]
110
111 if skip_sources[src]:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000112 log.debug("skipping %s (%s up-to-date)", src, obj)
Greg Wardfe9b8182000-06-28 01:20:35 +0000113 else:
Greg Ward42406482000-09-27 02:08:14 +0000114 src = os.path.normpath(src)
115 obj = os.path.normpath(obj)
116 self.mkpath(os.path.dirname(obj))
117
118 if ext == '.res':
119 # This is already a binary file -- skip it.
120 continue # the 'for' loop
121 if ext == '.rc':
122 # This needs to be compiled to a .res file -- do it now.
123 try:
124 self.spawn (["brcc32", "-fo", obj, src])
125 except DistutilsExecError, msg:
126 raise CompileError, msg
127 continue # the 'for' loop
128
129 # The next two are both for the real compiler.
Greg Wardfe9b8182000-06-28 01:20:35 +0000130 if ext in self._c_extensions:
131 input_opt = ""
132 elif ext in self._cpp_extensions:
133 input_opt = "-P"
Fred Drakeb94b8492001-12-06 20:51:35 +0000134 else:
Greg Ward42406482000-09-27 02:08:14 +0000135 # Unknown file type -- no extra options. The compiler
136 # will probably fail, but let it just in case this is a
137 # file the compiler recognizes even if we don't.
Fred Drakeb94b8492001-12-06 20:51:35 +0000138 input_opt = ""
Greg Wardfe9b8182000-06-28 01:20:35 +0000139
140 output_opt = "-o" + obj
Greg Wardfe9b8182000-06-28 01:20:35 +0000141
142 # Compiler command line syntax is: "bcc32 [options] file(s)".
143 # Note that the source file names must appear at the end of
144 # the command line.
Greg Wardfe9b8182000-06-28 01:20:35 +0000145 try:
146 self.spawn ([self.cc] + compile_opts + pp_opts +
147 [input_opt, output_opt] +
148 extra_postargs + [src])
149 except DistutilsExecError, msg:
150 raise CompileError, msg
151
152 return objects
153
154 # compile ()
155
156
157 def create_static_lib (self,
158 objects,
159 output_libname,
160 output_dir=None,
161 debug=0,
162 extra_preargs=None,
163 extra_postargs=None):
164
165 (objects, output_dir) = self._fix_object_args (objects, output_dir)
166 output_filename = \
167 self.library_filename (output_libname, output_dir=output_dir)
168
169 if self._need_link (objects, output_filename):
170 lib_args = [output_filename, '/u'] + objects
171 if debug:
172 pass # XXX what goes here?
173 if extra_preargs:
174 lib_args[:0] = extra_preargs
175 if extra_postargs:
176 lib_args.extend (extra_postargs)
177 try:
Fred Drakeb94b8492001-12-06 20:51:35 +0000178 self.spawn ([self.lib] + lib_args)
Greg Wardfe9b8182000-06-28 01:20:35 +0000179 except DistutilsExecError, msg:
Fred Drakeb94b8492001-12-06 20:51:35 +0000180 raise LibError, msg
Greg Wardfe9b8182000-06-28 01:20:35 +0000181 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000182 log.debug("skipping %s (up-to-date)", output_filename)
Greg Wardfe9b8182000-06-28 01:20:35 +0000183
184 # create_static_lib ()
Fred Drakeb94b8492001-12-06 20:51:35 +0000185
186
Greg Ward42406482000-09-27 02:08:14 +0000187 def link (self,
Fred Drakeb94b8492001-12-06 20:51:35 +0000188 target_desc,
Greg Ward42406482000-09-27 02:08:14 +0000189 objects,
190 output_filename,
191 output_dir=None,
192 libraries=None,
193 library_dirs=None,
194 runtime_library_dirs=None,
195 export_symbols=None,
196 debug=0,
197 extra_preargs=None,
198 extra_postargs=None,
199 build_temp=None):
Greg Wardfe9b8182000-06-28 01:20:35 +0000200
Greg Wardc58c5172000-08-02 01:03:23 +0000201 # XXX this ignores 'build_temp'! should follow the lead of
202 # msvccompiler.py
203
Greg Wardfe9b8182000-06-28 01:20:35 +0000204 (objects, output_dir) = self._fix_object_args (objects, output_dir)
205 (libraries, library_dirs, runtime_library_dirs) = \
206 self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
207
208 if runtime_library_dirs:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000209 log.warn("I don't know what to do with 'runtime_library_dirs': %s",
210 str(runtime_library_dirs))
Greg Ward42406482000-09-27 02:08:14 +0000211
Greg Wardfe9b8182000-06-28 01:20:35 +0000212 if output_dir is not None:
213 output_filename = os.path.join (output_dir, output_filename)
214
215 if self._need_link (objects, output_filename):
216
Greg Ward42406482000-09-27 02:08:14 +0000217 # Figure out linker args based on type of target.
218 if target_desc == CCompiler.EXECUTABLE:
219 startup_obj = 'c0w32'
220 if debug:
221 ld_args = self.ldflags_exe_debug[:]
222 else:
223 ld_args = self.ldflags_exe[:]
Greg Wardfe9b8182000-06-28 01:20:35 +0000224 else:
Greg Ward42406482000-09-27 02:08:14 +0000225 startup_obj = 'c0d32'
226 if debug:
227 ld_args = self.ldflags_shared_debug[:]
228 else:
229 ld_args = self.ldflags_shared[:]
230
Greg Wardfe9b8182000-06-28 01:20:35 +0000231
Greg Wardfe9b8182000-06-28 01:20:35 +0000232 # Create a temporary exports file for use by the linker
Greg Ward42406482000-09-27 02:08:14 +0000233 if export_symbols is None:
234 def_file = ''
235 else:
236 head, tail = os.path.split (output_filename)
237 modname, ext = os.path.splitext (tail)
238 temp_dir = os.path.dirname(objects[0]) # preserve tree structure
239 def_file = os.path.join (temp_dir, '%s.def' % modname)
240 contents = ['EXPORTS']
241 for sym in (export_symbols or []):
242 contents.append(' %s=_%s' % (sym, sym))
243 self.execute(write_file, (def_file, contents),
244 "writing %s" % def_file)
Greg Wardfe9b8182000-06-28 01:20:35 +0000245
Greg Wardcec15682000-09-01 01:28:33 +0000246 # Borland C++ has problems with '/' in paths
Greg Ward42406482000-09-27 02:08:14 +0000247 objects2 = map(os.path.normpath, objects)
248 # split objects in .obj and .res files
249 # Borland C++ needs them at different positions in the command line
250 objects = [startup_obj]
251 resources = []
252 for file in objects2:
253 (base, ext) = os.path.splitext(os.path.normcase(file))
254 if ext == '.res':
255 resources.append(file)
256 else:
257 objects.append(file)
Fred Drakeb94b8492001-12-06 20:51:35 +0000258
259
Greg Wardc58c5172000-08-02 01:03:23 +0000260 for l in library_dirs:
Fred Drakeb94b8492001-12-06 20:51:35 +0000261 ld_args.append("/L%s" % os.path.normpath(l))
Greg Ward42406482000-09-27 02:08:14 +0000262 ld_args.append("/L.") # we sometimes use relative paths
263
Fred Drakeb94b8492001-12-06 20:51:35 +0000264 # list of object files
265 ld_args.extend(objects)
Greg Wardc58c5172000-08-02 01:03:23 +0000266
Greg Ward13980452000-08-13 00:54:39 +0000267 # XXX the command-line syntax for Borland C++ is a bit wonky;
268 # certain filenames are jammed together in one big string, but
269 # comma-delimited. This doesn't mesh too well with the
270 # Unix-centric attitude (with a DOS/Windows quoting hack) of
271 # 'spawn()', so constructing the argument list is a bit
272 # awkward. Note that doing the obvious thing and jamming all
273 # the filenames and commas into one argument would be wrong,
274 # because 'spawn()' would quote any filenames with spaces in
275 # them. Arghghh!. Apparently it works fine as coded...
276
Greg Ward42406482000-09-27 02:08:14 +0000277 # name of dll/exe file
Greg Wardc58c5172000-08-02 01:03:23 +0000278 ld_args.extend([',',output_filename])
Fred Drakeb94b8492001-12-06 20:51:35 +0000279 # no map file and start libraries
Greg Warda4662bc2000-08-13 00:43:16 +0000280 ld_args.append(',,')
Greg Wardc58c5172000-08-02 01:03:23 +0000281
282 for lib in libraries:
Fred Drakeb94b8492001-12-06 20:51:35 +0000283 # see if we find it and if there is a bcpp specific lib
Greg Ward42406482000-09-27 02:08:14 +0000284 # (xxx_bcpp.lib)
Greg Wardc58c5172000-08-02 01:03:23 +0000285 libfile = self.find_library_file(library_dirs, lib, debug)
286 if libfile is None:
287 ld_args.append(lib)
288 # probably a BCPP internal library -- don't warn
Greg Wardc58c5172000-08-02 01:03:23 +0000289 else:
290 # full name which prefers bcpp_xxx.lib over xxx.lib
291 ld_args.append(libfile)
Greg Ward42406482000-09-27 02:08:14 +0000292
293 # some default libraries
294 ld_args.append ('import32')
295 ld_args.append ('cw32mt')
296
Greg Wardc58c5172000-08-02 01:03:23 +0000297 # def file for export symbols
298 ld_args.extend([',',def_file])
Greg Ward42406482000-09-27 02:08:14 +0000299 # add resource files
300 ld_args.append(',')
301 ld_args.extend(resources)
302
Fred Drakeb94b8492001-12-06 20:51:35 +0000303
Greg Wardfe9b8182000-06-28 01:20:35 +0000304 if extra_preargs:
305 ld_args[:0] = extra_preargs
306 if extra_postargs:
Greg Wardc58c5172000-08-02 01:03:23 +0000307 ld_args.extend(extra_postargs)
Greg Wardfe9b8182000-06-28 01:20:35 +0000308
309 self.mkpath (os.path.dirname (output_filename))
310 try:
Greg Ward42406482000-09-27 02:08:14 +0000311 self.spawn ([self.linker] + ld_args)
Greg Wardfe9b8182000-06-28 01:20:35 +0000312 except DistutilsExecError, msg:
313 raise LinkError, msg
314
315 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000316 log.debug("skipping %s (up-to-date)", output_filename)
Greg Wardfe9b8182000-06-28 01:20:35 +0000317
Greg Ward42406482000-09-27 02:08:14 +0000318 # link ()
Greg Wardfe9b8182000-06-28 01:20:35 +0000319
320 # -- Miscellaneous methods -----------------------------------------
Greg Wardfe9b8182000-06-28 01:20:35 +0000321
322
Greg Wardc58c5172000-08-02 01:03:23 +0000323 def find_library_file (self, dirs, lib, debug=0):
Greg Ward5db2c3a2000-08-04 01:30:03 +0000324 # List of effective library names to try, in order of preference:
Greg Ward42406482000-09-27 02:08:14 +0000325 # xxx_bcpp.lib is better than xxx.lib
Greg Wardc58c5172000-08-02 01:03:23 +0000326 # and xxx_d.lib is better than xxx.lib if debug is set
Greg Ward5db2c3a2000-08-04 01:30:03 +0000327 #
Greg Ward42406482000-09-27 02:08:14 +0000328 # The "_bcpp" suffix is to handle a Python installation for people
Greg Ward5db2c3a2000-08-04 01:30:03 +0000329 # with multiple compilers (primarily Distutils hackers, I suspect
330 # ;-). The idea is they'd have one static library for each
331 # compiler they care about, since (almost?) every Windows compiler
332 # seems to have a different format for static libraries.
333 if debug:
334 dlib = (lib + "_d")
Greg Wardcec15682000-09-01 01:28:33 +0000335 try_names = (dlib + "_bcpp", lib + "_bcpp", dlib, lib)
Greg Ward5db2c3a2000-08-04 01:30:03 +0000336 else:
Greg Wardcec15682000-09-01 01:28:33 +0000337 try_names = (lib + "_bcpp", lib)
Greg Wardfe9b8182000-06-28 01:20:35 +0000338
Greg Ward5db2c3a2000-08-04 01:30:03 +0000339 for dir in dirs:
340 for name in try_names:
341 libfile = os.path.join(dir, self.library_filename(name))
342 if os.path.exists(libfile):
343 return libfile
Greg Wardfe9b8182000-06-28 01:20:35 +0000344 else:
345 # Oops, didn't find it in *any* of 'dirs'
346 return None
347
Greg Ward42406482000-09-27 02:08:14 +0000348 # overwrite the one from CCompiler to support rc and res-files
349 def object_filenames (self,
350 source_filenames,
351 strip_dir=0,
352 output_dir=''):
353 if output_dir is None: output_dir = ''
354 obj_names = []
355 for src_name in source_filenames:
356 # use normcase to make sure '.rc' is really '.rc' and not '.RC'
357 (base, ext) = os.path.splitext (os.path.normcase(src_name))
358 if ext not in (self.src_extensions + ['.rc','.res']):
359 raise UnknownFileError, \
360 "unknown file type '%s' (from '%s')" % \
361 (ext, src_name)
362 if strip_dir:
363 base = os.path.basename (base)
364 if ext == '.res':
365 # these can go unchanged
366 obj_names.append (os.path.join (output_dir, base + ext))
367 elif ext == '.rc':
368 # these need to be compiled to .res-files
369 obj_names.append (os.path.join (output_dir, base + '.res'))
370 else:
371 obj_names.append (os.path.join (output_dir,
372 base + self.obj_extension))
373 return obj_names
374
375 # object_filenames ()
Andrew M. Kuchlingdb7aed52001-08-16 20:17:41 +0000376
377 def preprocess (self,
378 source,
379 output_file=None,
380 macros=None,
381 include_dirs=None,
382 extra_preargs=None,
383 extra_postargs=None):
384
385 (_, macros, include_dirs) = \
386 self._fix_compile_args(None, macros, include_dirs)
387 pp_opts = gen_preprocess_options(macros, include_dirs)
388 pp_args = ['cpp32.exe'] + pp_opts
389 if output_file is not None:
390 pp_args.append('-o' + output_file)
391 if extra_preargs:
392 pp_args[:0] = extra_preargs
393 if extra_postargs:
394 pp_args.extend(extra_postargs)
395 pp_args.append(source)
396
397 # We need to preprocess: either we're being forced to, or the
398 # source file is newer than the target (or the target doesn't
399 # exist).
400 if self.force or output_file is None or newer(source, output_file):
401 if output_file:
402 self.mkpath(os.path.dirname(output_file))
403 try:
404 self.spawn(pp_args)
405 except DistutilsExecError, msg:
406 print msg
407 raise CompileError, msg
408
409 # preprocess()