blob: 7daa597b75fbd711c0a1447cc02a4dfede7eefee [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
17import sys, os, string
18from distutils.errors import \
19 DistutilsExecError, DistutilsPlatformError, \
20 CompileError, LibError, LinkError
21from 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
Greg Wardfe9b8182000-06-28 01:20:35 +000024
25
26class BCPPCompiler(CCompiler) :
27 """Concrete class that implements an interface to the Borland C/C++
28 compiler, as defined by the CCompiler abstract class.
29 """
30
31 compiler_type = 'bcpp'
32
33 # Just set this so CCompiler's constructor doesn't barf. We currently
34 # don't use the 'set_executables()' bureaucracy provided by CCompiler,
35 # as it really isn't necessary for this sort of single-compiler class.
36 # Would be nice to have a consistent interface with UnixCCompiler,
37 # though, so it's worth thinking about.
38 executables = {}
39
40 # Private class data (need to distinguish C from C++ source for compiler)
41 _c_extensions = ['.c']
42 _cpp_extensions = ['.cc', '.cpp', '.cxx']
43
44 # Needed for the filename generation methods provided by the
45 # base class, CCompiler.
46 src_extensions = _c_extensions + _cpp_extensions
47 obj_extension = '.obj'
48 static_lib_extension = '.lib'
49 shared_lib_extension = '.dll'
50 static_lib_format = shared_lib_format = '%s%s'
51 exe_extension = '.exe'
52
53
54 def __init__ (self,
55 verbose=0,
56 dry_run=0,
57 force=0):
58
59 CCompiler.__init__ (self, verbose, dry_run, force)
60
61 # These executables are assumed to all be in the path.
62 # Borland doesn't seem to use any special registry settings to
63 # indicate their installation locations.
64
65 self.cc = "bcc32.exe"
66 self.link = "ilink32.exe"
67 self.lib = "tlib.exe"
68
69 self.preprocess_options = None
70 self.compile_options = ['/tWM', '/O2', '/q']
71 self.compile_options_debug = ['/tWM', '/Od', '/q']
72
73 self.ldflags_shared = ['/Tpd', '/Gn', '/q', '/x']
74 self.ldflags_shared_debug = ['/Tpd', '/Gn', '/q', '/x']
75 self.ldflags_static = []
76
77
78 # -- Worker methods ------------------------------------------------
79
80 def compile (self,
81 sources,
82 output_dir=None,
83 macros=None,
84 include_dirs=None,
85 debug=0,
86 extra_preargs=None,
87 extra_postargs=None):
88
89 (output_dir, macros, include_dirs) = \
90 self._fix_compile_args (output_dir, macros, include_dirs)
91 (objects, skip_sources) = self._prep_compile (sources, output_dir)
92
93 if extra_postargs is None:
94 extra_postargs = []
95
96 pp_opts = gen_preprocess_options (macros, include_dirs)
97 compile_opts = extra_preargs or []
98 compile_opts.append ('-c')
99 if debug:
100 compile_opts.extend (self.compile_options_debug)
101 else:
102 compile_opts.extend (self.compile_options)
103
104 for i in range (len (sources)):
105 src = sources[i] ; obj = objects[i]
106 ext = (os.path.splitext (src))[1]
107
108 if skip_sources[src]:
109 self.announce ("skipping %s (%s up-to-date)" % (src, obj))
110 else:
111 if ext in self._c_extensions:
112 input_opt = ""
113 elif ext in self._cpp_extensions:
114 input_opt = "-P"
115
Greg Wardc58c5172000-08-02 01:03:23 +0000116 src = os.path.normpath(src)
117 obj = os.path.normpath(obj)
118
Greg Wardfe9b8182000-06-28 01:20:35 +0000119 output_opt = "-o" + obj
Greg Wardc58c5172000-08-02 01:03:23 +0000120 self.mkpath(os.path.dirname(obj))
Greg Wardfe9b8182000-06-28 01:20:35 +0000121
122 # Compiler command line syntax is: "bcc32 [options] file(s)".
123 # Note that the source file names must appear at the end of
124 # the command line.
Greg Wardfe9b8182000-06-28 01:20:35 +0000125 try:
126 self.spawn ([self.cc] + compile_opts + pp_opts +
127 [input_opt, output_opt] +
128 extra_postargs + [src])
129 except DistutilsExecError, msg:
130 raise CompileError, msg
131
132 return objects
133
134 # compile ()
135
136
137 def create_static_lib (self,
138 objects,
139 output_libname,
140 output_dir=None,
141 debug=0,
142 extra_preargs=None,
143 extra_postargs=None):
144
145 (objects, output_dir) = self._fix_object_args (objects, output_dir)
146 output_filename = \
147 self.library_filename (output_libname, output_dir=output_dir)
148
149 if self._need_link (objects, output_filename):
150 lib_args = [output_filename, '/u'] + objects
151 if debug:
152 pass # XXX what goes here?
153 if extra_preargs:
154 lib_args[:0] = extra_preargs
155 if extra_postargs:
156 lib_args.extend (extra_postargs)
157 try:
158 self.spawn ([self.lib] + lib_args)
159 except DistutilsExecError, msg:
160 raise LibError, msg
161 else:
162 self.announce ("skipping %s (up-to-date)" % output_filename)
163
164 # create_static_lib ()
165
166
167 def link_shared_lib (self,
168 objects,
169 output_libname,
170 output_dir=None,
171 libraries=None,
172 library_dirs=None,
173 runtime_library_dirs=None,
174 export_symbols=None,
175 debug=0,
176 extra_preargs=None,
177 extra_postargs=None,
178 build_temp=None):
179
180 self.link_shared_object (objects,
181 self.shared_library_name(output_libname),
182 output_dir=output_dir,
183 libraries=libraries,
184 library_dirs=library_dirs,
185 runtime_library_dirs=runtime_library_dirs,
186 export_symbols=export_symbols,
187 debug=debug,
188 extra_preargs=extra_preargs,
189 extra_postargs=extra_postargs,
190 build_temp=build_temp)
191
192
193 def link_shared_object (self,
194 objects,
195 output_filename,
196 output_dir=None,
197 libraries=None,
198 library_dirs=None,
199 runtime_library_dirs=None,
200 export_symbols=None,
201 debug=0,
202 extra_preargs=None,
203 extra_postargs=None,
204 build_temp=None):
205
Greg Wardc58c5172000-08-02 01:03:23 +0000206 # XXX this ignores 'build_temp'! should follow the lead of
207 # msvccompiler.py
208
Greg Wardfe9b8182000-06-28 01:20:35 +0000209 (objects, output_dir) = self._fix_object_args (objects, output_dir)
210 (libraries, library_dirs, runtime_library_dirs) = \
211 self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
212
213 if runtime_library_dirs:
214 self.warn ("I don't know what to do with 'runtime_library_dirs': "
215 + str (runtime_library_dirs))
216
217 if output_dir is not None:
218 output_filename = os.path.join (output_dir, output_filename)
219
220 if self._need_link (objects, output_filename):
221
222 if debug:
Greg Wardc58c5172000-08-02 01:03:23 +0000223 ld_args = self.ldflags_shared_debug[:]
Greg Wardfe9b8182000-06-28 01:20:35 +0000224 else:
Greg Wardc58c5172000-08-02 01:03:23 +0000225 ld_args = self.ldflags_shared[:]
Greg Wardfe9b8182000-06-28 01:20:35 +0000226
Greg Wardc58c5172000-08-02 01:03:23 +0000227 # Borland C++ has problems with '/' in paths
228 objects = map(os.path.normpath, objects)
Greg Wardfe9b8182000-06-28 01:20:35 +0000229 startup_obj = 'c0d32'
Greg Wardc58c5172000-08-02 01:03:23 +0000230 objects.insert(0, startup_obj)
Greg Wardfe9b8182000-06-28 01:20:35 +0000231
Greg Wardc58c5172000-08-02 01:03:23 +0000232 # either exchange python15.lib in the python libs directory against
233 # a Borland-like one, or create one with name bcpp_python15.lib
234 # there and remove the pragmas from config.h
235 #libraries.append ('mypylib')
Greg Wardfe9b8182000-06-28 01:20:35 +0000236 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)
Greg Wardc58c5172000-08-02 01:03:23 +0000242 temp_dir = os.path.dirname(objects[0]) # preserve tree structure
243 def_file = os.path.join (temp_dir, '%s.def' % modname)
244 contents = ['EXPORTS']
Greg Wardfe9b8182000-06-28 01:20:35 +0000245 for sym in (export_symbols or []):
Greg Wardc58c5172000-08-02 01:03:23 +0000246 contents.append(' %s=_%s' % (sym, sym))
247 self.execute(write_file, (def_file, contents),
248 "writing %s" % def_file)
Greg Wardfe9b8182000-06-28 01:20:35 +0000249
Greg Wardc58c5172000-08-02 01:03:23 +0000250 # Start building command line flags and options.
251
252 for l in library_dirs:
253 ld_args.append("/L%s" % os.path.normpath(l))
254
255 ld_args.extend(objects) # list of object files
256
257 # name of dll file
258 ld_args.extend([',',output_filename])
259 # no map file and start libraries
260 ld_args.extend([',', ','])
261
262 for lib in libraries:
263 # see if we find it and if there is a bcpp specific lib
264 # (bcpp_xxx.lib)
265 libfile = self.find_library_file(library_dirs, lib, debug)
266 if libfile is None:
267 ld_args.append(lib)
268 # probably a BCPP internal library -- don't warn
269 # self.warn('library %s not found.' % lib)
270 else:
271 # full name which prefers bcpp_xxx.lib over xxx.lib
272 ld_args.append(libfile)
273 # def file for export symbols
274 ld_args.extend([',',def_file])
Greg Wardfe9b8182000-06-28 01:20:35 +0000275
276 if extra_preargs:
277 ld_args[:0] = extra_preargs
278 if extra_postargs:
Greg Wardc58c5172000-08-02 01:03:23 +0000279 ld_args.extend(extra_postargs)
Greg Wardfe9b8182000-06-28 01:20:35 +0000280
281 self.mkpath (os.path.dirname (output_filename))
282 try:
283 self.spawn ([self.link] + ld_args)
284 except DistutilsExecError, msg:
285 raise LinkError, msg
286
287 else:
288 self.announce ("skipping %s (up-to-date)" % output_filename)
289
290 # link_shared_object ()
291
292
293 def link_executable (self,
294 objects,
295 output_progname,
296 output_dir=None,
297 libraries=None,
298 library_dirs=None,
299 runtime_library_dirs=None,
300 debug=0,
301 extra_preargs=None,
302 extra_postargs=None):
303
304 (objects, output_dir) = self._fix_object_args (objects, output_dir)
305 (libraries, library_dirs, runtime_library_dirs) = \
306 self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
307
308 if runtime_library_dirs:
309 self.warn ("I don't know what to do with 'runtime_library_dirs': "
310 + str (runtime_library_dirs))
311
312 lib_opts = gen_lib_options (self,
313 library_dirs, runtime_library_dirs,
314 libraries)
315 output_filename = output_progname + self.exe_extension
316 if output_dir is not None:
317 output_filename = os.path.join (output_dir, output_filename)
318
319 if self._need_link (objects, output_filename):
320
321 if debug:
322 ldflags = self.ldflags_shared_debug[1:]
323 else:
324 ldflags = self.ldflags_shared[1:]
325
326 ld_args = ldflags + lib_opts + \
327 objects + ['/OUT:' + output_filename]
328
329 if extra_preargs:
330 ld_args[:0] = extra_preargs
331 if extra_postargs:
332 ld_args.extend (extra_postargs)
333
334 self.mkpath (os.path.dirname (output_filename))
335 try:
336 self.spawn ([self.link] + ld_args)
337 except DistutilsExecError, msg:
338 raise LinkError, msg
339 else:
340 self.announce ("skipping %s (up-to-date)" % output_filename)
341
342
343 # -- Miscellaneous methods -----------------------------------------
344 # These are all used by the 'gen_lib_options() function, in
345 # ccompiler.py.
346
347 def library_dir_option (self, dir):
348 return "-L" + dir
349
350 def runtime_library_dir_option (self, dir):
351 raise DistutilsPlatformError, \
Greg Wardc58c5172000-08-02 01:03:23 +0000352 ("don't know how to set runtime library search path "
353 "for Borland C++")
Greg Wardfe9b8182000-06-28 01:20:35 +0000354
355 def library_option (self, lib):
356 return self.library_filename (lib)
357
358
Greg Wardc58c5172000-08-02 01:03:23 +0000359 def find_library_file (self, dirs, lib, debug=0):
360 # find library file
361 # bcpp_xxx.lib is better than xxx.lib
362 # and xxx_d.lib is better than xxx.lib if debug is set
Greg Wardfe9b8182000-06-28 01:20:35 +0000363 for dir in dirs:
Greg Wardc58c5172000-08-02 01:03:23 +0000364 if debug:
365 libfile = os.path.join (
366 dir, self.library_filename ("bcpp_" + lib + "_d"))
367 if os.path.exists (libfile):
368 return libfile
369 libfile = os.path.join (
370 dir, self.library_filename ("bcpp_" + lib))
371 if os.path.exists (libfile):
372 return libfile
373 if debug:
374 libfile = os.path.join (
375 dir, self.library_filename(lib + '_d'))
376 if os.path.exists (libfile):
377 return libfile
Greg Wardfe9b8182000-06-28 01:20:35 +0000378 libfile = os.path.join (dir, self.library_filename (lib))
379 if os.path.exists (libfile):
380 return libfile
381
382 else:
383 # Oops, didn't find it in *any* of 'dirs'
384 return None
385