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