blob: 9aa41adb9db6ebbda6e8af434583ade7aee50850 [file] [log] [blame]
Greg Ward3f81cf71999-07-10 02:03:53 +00001"""distutils.ccompiler
2
3Contains CCompiler, an abstract base class that defines the interface
4for the Distutils compiler abstraction model."""
5
6# created 1999/07/05, Greg Ward
7
Greg Ward3ce77fd2000-03-02 01:49:45 +00008__revision__ = "$Id$"
Greg Ward3f81cf71999-07-10 02:03:53 +00009
Greg Ward802d6b71999-09-29 12:20:55 +000010import sys, os
Greg Ward3f81cf71999-07-10 02:03:53 +000011from types import *
12from copy import copy
13from distutils.errors import *
Greg Warde1aaaa61999-08-14 23:50:50 +000014from distutils.spawn import spawn
Greg Ward32c4a8a2000-03-06 03:40:29 +000015from distutils.util import move_file, mkpath, newer_pairwise, newer_group
Greg Ward3f81cf71999-07-10 02:03:53 +000016
17
18class CCompiler:
19 """Abstract base class to define the interface that must be implemented
Greg Wardc3a43b42000-06-24 18:10:48 +000020 by real compiler classes. Also has some utility methods used by
21 several compiler classes.
Greg Ward3f81cf71999-07-10 02:03:53 +000022
Greg Wardc3a43b42000-06-24 18:10:48 +000023 The basic idea behind a compiler abstraction class is that each
24 instance can be used for all the compile/link steps in building a
25 single project. Thus, attributes common to all of those compile and
26 link steps -- include directories, macros to define, libraries to link
27 against, etc. -- are attributes of the compiler instance. To allow for
28 variability in how individual files are treated, most of those
29 attributes may be varied on a per-compilation or per-link basis.
30 """
Greg Ward3f81cf71999-07-10 02:03:53 +000031
Greg Ward802d6b71999-09-29 12:20:55 +000032 # 'compiler_type' is a class attribute that identifies this class. It
33 # keeps code that wants to know what kind of compiler it's dealing with
34 # from having to import all possible compiler classes just to do an
35 # 'isinstance'. In concrete CCompiler subclasses, 'compiler_type'
36 # should really, really be one of the keys of the 'compiler_class'
37 # dictionary (see below -- used by the 'new_compiler()' factory
38 # function) -- authors of new compiler interface classes are
39 # responsible for updating 'compiler_class'!
40 compiler_type = None
Greg Ward3f81cf71999-07-10 02:03:53 +000041
42 # XXX things not handled by this compiler abstraction model:
43 # * client can't provide additional options for a compiler,
44 # e.g. warning, optimization, debugging flags. Perhaps this
45 # should be the domain of concrete compiler abstraction classes
46 # (UnixCCompiler, MSVCCompiler, etc.) -- or perhaps the base
47 # class should have methods for the common ones.
Greg Ward3f81cf71999-07-10 02:03:53 +000048 # * can't completely override the include or library searchg
49 # path, ie. no "cc -I -Idir1 -Idir2" or "cc -L -Ldir1 -Ldir2".
Greg Warde1aaaa61999-08-14 23:50:50 +000050 # I'm not sure how widely supported this is even by Unix
Greg Ward3f81cf71999-07-10 02:03:53 +000051 # compilers, much less on other platforms. And I'm even less
Greg Warde1aaaa61999-08-14 23:50:50 +000052 # sure how useful it is; maybe for cross-compiling, but
53 # support for that is a ways off. (And anyways, cross
54 # compilers probably have a dedicated binary with the
55 # right paths compiled in. I hope.)
Greg Ward3f81cf71999-07-10 02:03:53 +000056 # * can't do really freaky things with the library list/library
57 # dirs, e.g. "-Ldir1 -lfoo -Ldir2 -lfoo" to link against
58 # different versions of libfoo.a in different locations. I
59 # think this is useless without the ability to null out the
60 # library search path anyways.
Greg Ward3f81cf71999-07-10 02:03:53 +000061
62
Greg Ward32c4a8a2000-03-06 03:40:29 +000063 # Subclasses that rely on the standard filename generation methods
64 # implemented below should override these; see the comment near
65 # those methods ('object_filenames()' et. al.) for details:
66 src_extensions = None # list of strings
67 obj_extension = None # string
68 static_lib_extension = None
69 shared_lib_extension = None # string
70 static_lib_format = None # format string
71 shared_lib_format = None # prob. same as static_lib_format
72 exe_extension = None # string
73
74
Greg Warde1aaaa61999-08-14 23:50:50 +000075 def __init__ (self,
76 verbose=0,
Greg Ward3febd601999-10-03 20:41:02 +000077 dry_run=0,
78 force=0):
Greg Warde1aaaa61999-08-14 23:50:50 +000079
80 self.verbose = verbose
81 self.dry_run = dry_run
Greg Ward3febd601999-10-03 20:41:02 +000082 self.force = force
Greg Ward3f81cf71999-07-10 02:03:53 +000083
Greg Ward9b17cb51999-09-13 03:07:24 +000084 # 'output_dir': a common output directory for object, library,
85 # shared object, and shared library files
86 self.output_dir = None
87
Greg Ward3f81cf71999-07-10 02:03:53 +000088 # 'macros': a list of macro definitions (or undefinitions). A
89 # macro definition is a 2-tuple (name, value), where the value is
90 # either a string or None (no explicit value). A macro
91 # undefinition is a 1-tuple (name,).
92 self.macros = []
93
Greg Ward3f81cf71999-07-10 02:03:53 +000094 # 'include_dirs': a list of directories to search for include files
95 self.include_dirs = []
96
97 # 'libraries': a list of libraries to include in any link
98 # (library names, not filenames: eg. "foo" not "libfoo.a")
99 self.libraries = []
100
101 # 'library_dirs': a list of directories to search for libraries
102 self.library_dirs = []
103
Greg Warde1aaaa61999-08-14 23:50:50 +0000104 # 'runtime_library_dirs': a list of directories to search for
105 # shared libraries/objects at runtime
106 self.runtime_library_dirs = []
107
Greg Ward3f81cf71999-07-10 02:03:53 +0000108 # 'objects': a list of object files (or similar, such as explicitly
109 # named library files) to include on any link
110 self.objects = []
111
112 # __init__ ()
113
114
115 def _find_macro (self, name):
116 i = 0
117 for defn in self.macros:
118 if defn[0] == name:
119 return i
120 i = i + 1
121
122 return None
123
124
125 def _check_macro_definitions (self, definitions):
126 """Ensures that every element of 'definitions' is a valid macro
Greg Wardc3a43b42000-06-24 18:10:48 +0000127 definition, ie. either (name,value) 2-tuple or a (name,) tuple. Do
128 nothing if all definitions are OK, raise TypeError otherwise.
129 """
Greg Ward3f81cf71999-07-10 02:03:53 +0000130 for defn in definitions:
131 if not (type (defn) is TupleType and
132 (len (defn) == 1 or
133 (len (defn) == 2 and
134 (type (defn[1]) is StringType or defn[1] is None))) and
135 type (defn[0]) is StringType):
136 raise TypeError, \
137 ("invalid macro definition '%s': " % defn) + \
138 "must be tuple (string,), (string, string), or " + \
139 "(string, None)"
140
141
142 # -- Bookkeeping methods -------------------------------------------
143
144 def define_macro (self, name, value=None):
Greg Wardc3a43b42000-06-24 18:10:48 +0000145 """Define a preprocessor macro for all compilations driven by this
146 compiler object. The optional parameter 'value' should be a
147 string; if it is not supplied, then the macro will be defined
148 without an explicit value and the exact outcome depends on the
149 compiler used (XXX true? does ANSI say anything about this?)
150 """
Greg Ward3f81cf71999-07-10 02:03:53 +0000151 # Delete from the list of macro definitions/undefinitions if
152 # already there (so that this one will take precedence).
153 i = self._find_macro (name)
154 if i is not None:
155 del self.macros[i]
156
157 defn = (name, value)
158 self.macros.append (defn)
159
160
161 def undefine_macro (self, name):
162 """Undefine a preprocessor macro for all compilations driven by
Greg Wardc3a43b42000-06-24 18:10:48 +0000163 this compiler object. If the same macro is defined by
164 'define_macro()' and undefined by 'undefine_macro()' the last call
165 takes precedence (including multiple redefinitions or
166 undefinitions). If the macro is redefined/undefined on a
167 per-compilation basis (ie. in the call to 'compile()'), then that
168 takes precedence.
169 """
Greg Ward3f81cf71999-07-10 02:03:53 +0000170 # Delete from the list of macro definitions/undefinitions if
171 # already there (so that this one will take precedence).
172 i = self._find_macro (name)
173 if i is not None:
174 del self.macros[i]
175
176 undefn = (name,)
177 self.macros.append (undefn)
178
179
180 def add_include_dir (self, dir):
Greg Wardc3a43b42000-06-24 18:10:48 +0000181 """Add 'dir' to the list of directories that will be searched for
182 header files. The compiler is instructed to search directories in
183 the order in which they are supplied by successive calls to
184 'add_include_dir()'.
185 """
Greg Ward3f81cf71999-07-10 02:03:53 +0000186 self.include_dirs.append (dir)
187
188 def set_include_dirs (self, dirs):
Greg Wardc3a43b42000-06-24 18:10:48 +0000189 """Set the list of directories that will be searched to 'dirs' (a
190 list of strings). Overrides any preceding calls to
191 'add_include_dir()'; subsequence calls to 'add_include_dir()' add
192 to the list passed to 'set_include_dirs()'. This does not affect
193 any list of standard include directories that the compiler may
194 search by default.
195 """
Greg Ward3f81cf71999-07-10 02:03:53 +0000196 self.include_dirs = copy (dirs)
197
198
199 def add_library (self, libname):
Greg Wardc3a43b42000-06-24 18:10:48 +0000200 """Add 'libname' to the list of libraries that will be included in
201 all links driven by this compiler object. Note that 'libname'
202 should *not* be the name of a file containing a library, but the
203 name of the library itself: the actual filename will be inferred by
204 the linker, the compiler, or the compiler class (depending on the
205 platform).
Greg Ward3f81cf71999-07-10 02:03:53 +0000206
Greg Wardc3a43b42000-06-24 18:10:48 +0000207 The linker will be instructed to link against libraries in the
208 order they were supplied to 'add_library()' and/or
209 'set_libraries()'. It is perfectly valid to duplicate library
210 names; the linker will be instructed to link against libraries as
211 many times as they are mentioned.
212 """
Greg Ward3f81cf71999-07-10 02:03:53 +0000213 self.libraries.append (libname)
214
215 def set_libraries (self, libnames):
Greg Wardc3a43b42000-06-24 18:10:48 +0000216 """Set the list of libraries to be included in all links driven by
217 this compiler object to 'libnames' (a list of strings). This does
218 not affect any standard system libraries that the linker may
219 include by default.
220 """
Greg Ward3f81cf71999-07-10 02:03:53 +0000221 self.libraries = copy (libnames)
222
223
224 def add_library_dir (self, dir):
225 """Add 'dir' to the list of directories that will be searched for
Greg Wardc3a43b42000-06-24 18:10:48 +0000226 libraries specified to 'add_library()' and 'set_libraries()'. The
227 linker will be instructed to search for libraries in the order they
228 are supplied to 'add_library_dir()' and/or 'set_library_dirs()'.
229 """
Greg Ward3f81cf71999-07-10 02:03:53 +0000230 self.library_dirs.append (dir)
231
232 def set_library_dirs (self, dirs):
Greg Wardc3a43b42000-06-24 18:10:48 +0000233 """Set the list of library search directories to 'dirs' (a list of
234 strings). This does not affect any standard library search path
235 that the linker may search by default.
236 """
Greg Ward3f81cf71999-07-10 02:03:53 +0000237 self.library_dirs = copy (dirs)
238
239
Greg Warde1aaaa61999-08-14 23:50:50 +0000240 def add_runtime_library_dir (self, dir):
241 """Add 'dir' to the list of directories that will be searched for
Greg Wardc3a43b42000-06-24 18:10:48 +0000242 shared libraries at runtime.
243 """
Greg Warde1aaaa61999-08-14 23:50:50 +0000244 self.runtime_library_dirs.append (dir)
245
246 def set_runtime_library_dirs (self, dirs):
Greg Wardc3a43b42000-06-24 18:10:48 +0000247 """Set the list of directories to search for shared libraries at
248 runtime to 'dirs' (a list of strings). This does not affect any
249 standard search path that the runtime linker may search by
250 default.
251 """
Greg Warde1aaaa61999-08-14 23:50:50 +0000252 self.runtime_library_dirs = copy (dirs)
253
254
Greg Ward3f81cf71999-07-10 02:03:53 +0000255 def add_link_object (self, object):
Greg Wardc3a43b42000-06-24 18:10:48 +0000256 """Add 'object' to the list of object files (or analogues, such as
257 explictly named library files or the output of "resource
258 compilers") to be included in every link driven by this compiler
259 object.
260 """
Greg Ward3f81cf71999-07-10 02:03:53 +0000261 self.objects.append (object)
262
263 def set_link_objects (self, objects):
Greg Wardc3a43b42000-06-24 18:10:48 +0000264 """Set the list of object files (or analogues) to be included in
265 every link to 'objects'. This does not affect any standard object
266 files that the linker may include by default (such as system
267 libraries).
268 """
Greg Ward3f81cf71999-07-10 02:03:53 +0000269 self.objects = copy (objects)
270
271
Greg Ward32c4a8a2000-03-06 03:40:29 +0000272 # -- Priviate utility methods --------------------------------------
273 # (here for the convenience of subclasses)
274
275 def _fix_compile_args (self, output_dir, macros, include_dirs):
Greg Wardc3a43b42000-06-24 18:10:48 +0000276 """Typecheck and fix-up some of the arguments to the 'compile()'
277 method, and return fixed-up values. Specifically: if 'output_dir'
278 is None, replaces it with 'self.output_dir'; ensures that 'macros'
279 is a list, and augments it with 'self.macros'; ensures that
280 'include_dirs' is a list, and augments it with 'self.include_dirs'.
281 Guarantees that the returned values are of the correct type,
282 i.e. for 'output_dir' either string or None, and for 'macros' and
283 'include_dirs' either list or None.
284 """
Greg Ward32c4a8a2000-03-06 03:40:29 +0000285 if output_dir is None:
286 output_dir = self.output_dir
287 elif type (output_dir) is not StringType:
288 raise TypeError, "'output_dir' must be a string or None"
289
290 if macros is None:
291 macros = self.macros
292 elif type (macros) is ListType:
293 macros = macros + (self.macros or [])
294 else:
295 raise TypeError, \
296 "'macros' (if supplied) must be a list of tuples"
297
298 if include_dirs is None:
299 include_dirs = self.include_dirs
300 elif type (include_dirs) in (ListType, TupleType):
301 include_dirs = list (include_dirs) + (self.include_dirs or [])
302 else:
303 raise TypeError, \
304 "'include_dirs' (if supplied) must be a list of strings"
305
306 return (output_dir, macros, include_dirs)
307
308 # _fix_compile_args ()
309
310
311 def _prep_compile (self, sources, output_dir):
Greg Wardc3a43b42000-06-24 18:10:48 +0000312 """Determine the list of object files corresponding to 'sources',
313 and figure out which ones really need to be recompiled. Return a
314 list of all object files and a dictionary telling which source
315 files can be skipped.
316 """
Greg Ward32c4a8a2000-03-06 03:40:29 +0000317 # Get the list of expected output (object) files
318 objects = self.object_filenames (sources,
319 output_dir=output_dir)
320
321 if self.force:
322 skip_source = {} # rebuild everything
323 for source in sources:
324 skip_source[source] = 0
325 else:
326 # Figure out which source files we have to recompile according
327 # to a simplistic check -- we just compare the source and
328 # object file, no deep dependency checking involving header
329 # files.
330 skip_source = {} # rebuild everything
331 for source in sources: # no wait, rebuild nothing
332 skip_source[source] = 1
333
334 (n_sources, n_objects) = newer_pairwise (sources, objects)
Greg Wardc3a43b42000-06-24 18:10:48 +0000335 for source in n_sources: # no really, only rebuild what's
336 skip_source[source] = 0 # out-of-date
Greg Ward32c4a8a2000-03-06 03:40:29 +0000337
338 return (objects, skip_source)
339
340 # _prep_compile ()
341
342
Greg Wardf10f95d2000-03-26 21:37:09 +0000343 def _fix_object_args (self, objects, output_dir):
Greg Wardc3a43b42000-06-24 18:10:48 +0000344 """Typecheck and fix up some arguments supplied to various methods.
345 Specifically: ensure that 'objects' is a list; if output_dir is
346 None, replace with self.output_dir. Return fixed versions of
347 'objects' and 'output_dir'.
348 """
Greg Ward32c4a8a2000-03-06 03:40:29 +0000349 if type (objects) not in (ListType, TupleType):
350 raise TypeError, \
351 "'objects' must be a list or tuple of strings"
352 objects = list (objects)
353
354 if output_dir is None:
355 output_dir = self.output_dir
356 elif type (output_dir) is not StringType:
357 raise TypeError, "'output_dir' must be a string or None"
358
Greg Wardf10f95d2000-03-26 21:37:09 +0000359 return (objects, output_dir)
Greg Ward32c4a8a2000-03-06 03:40:29 +0000360
Greg Ward32c4a8a2000-03-06 03:40:29 +0000361
Greg Wardf10f95d2000-03-26 21:37:09 +0000362 def _fix_lib_args (self, libraries, library_dirs, runtime_library_dirs):
363 """Typecheck and fix up some of the arguments supplied to the
Greg Wardc3a43b42000-06-24 18:10:48 +0000364 'link_*' methods. Specifically: ensure that all arguments are
365 lists, and augment them with their permanent versions
366 (eg. 'self.libraries' augments 'libraries'). Return a tuple with
367 fixed versions of all arguments.
368 """
Greg Wardf10f95d2000-03-26 21:37:09 +0000369 if libraries is None:
370 libraries = self.libraries
371 elif type (libraries) in (ListType, TupleType):
372 libraries = list (libraries) + (self.libraries or [])
Greg Ward32c4a8a2000-03-06 03:40:29 +0000373 else:
Greg Wardf10f95d2000-03-26 21:37:09 +0000374 raise TypeError, \
375 "'libraries' (if supplied) must be a list of strings"
Greg Ward32c4a8a2000-03-06 03:40:29 +0000376
Greg Wardf10f95d2000-03-26 21:37:09 +0000377 if library_dirs is None:
378 library_dirs = self.library_dirs
379 elif type (library_dirs) in (ListType, TupleType):
380 library_dirs = list (library_dirs) + (self.library_dirs or [])
381 else:
382 raise TypeError, \
383 "'library_dirs' (if supplied) must be a list of strings"
384
385 if runtime_library_dirs is None:
386 runtime_library_dirs = self.runtime_library_dirs
387 elif type (runtime_library_dirs) in (ListType, TupleType):
388 runtime_library_dirs = (list (runtime_library_dirs) +
389 (self.runtime_library_dirs or []))
390 else:
391 raise TypeError, \
392 "'runtime_library_dirs' (if supplied) " + \
393 "must be a list of strings"
394
395 return (libraries, library_dirs, runtime_library_dirs)
396
397 # _fix_lib_args ()
Greg Ward32c4a8a2000-03-06 03:40:29 +0000398
399
400 def _need_link (self, objects, output_file):
Greg Wardc3a43b42000-06-24 18:10:48 +0000401 """Return true if we need to relink the files listed in 'objects'
402 to recreate 'output_file'.
403 """
Greg Ward32c4a8a2000-03-06 03:40:29 +0000404 if self.force:
405 return 1
406 else:
407 if self.dry_run:
408 newer = newer_group (objects, output_file, missing='newer')
409 else:
410 newer = newer_group (objects, output_file)
411 return newer
412
413 # _need_link ()
414
415
Greg Ward3f81cf71999-07-10 02:03:53 +0000416 # -- Worker methods ------------------------------------------------
417 # (must be implemented by subclasses)
418
Greg Ward3ff3b032000-06-21 02:58:46 +0000419 def preprocess (self,
420 source,
421 output_file=None,
422 macros=None,
423 include_dirs=None,
424 extra_preargs=None,
425 extra_postargs=None):
426 """Preprocess a single C/C++ source file, named in 'source'.
427 Output will be written to file named 'output_file', or stdout if
428 'output_file' not supplied. 'macros' is a list of macro
429 definitions as for 'compile()', which will augment the macros set
430 with 'define_macro()' and 'undefine_macro()'. 'include_dirs' is a
431 list of directory names that will be added to the default list.
432 """
433 pass
434
Greg Ward3f81cf71999-07-10 02:03:53 +0000435 def compile (self,
436 sources,
Greg Ward9b17cb51999-09-13 03:07:24 +0000437 output_dir=None,
Greg Ward3f81cf71999-07-10 02:03:53 +0000438 macros=None,
Greg Ward0bdd90a1999-12-12 17:19:58 +0000439 include_dirs=None,
Greg Ward3c045a52000-02-09 02:16:14 +0000440 debug=0,
Greg Ward802d6b71999-09-29 12:20:55 +0000441 extra_preargs=None,
442 extra_postargs=None):
Greg Wardc3a43b42000-06-24 18:10:48 +0000443 """Compile one or more C/C++ source files. 'sources' must be a
444 list of strings, each one the name of a C/C++ source file. Return
445 a list of object filenames, one per source filename in 'sources'.
446 Depending on the implementation, not all source files will
447 necessarily be compiled, but all corresponding object filenames
448 will be returned.
Greg Ward32c4a8a2000-03-06 03:40:29 +0000449
Greg Wardc3a43b42000-06-24 18:10:48 +0000450 If 'output_dir' is given, object files will be put under it, while
451 retaining their original path component. That is, "foo/bar.c"
452 normally compiles to "foo/bar.o" (for a Unix implementation); if
453 'output_dir' is "build", then it would compile to
454 "build/foo/bar.o".
Greg Ward3f81cf71999-07-10 02:03:53 +0000455
Greg Wardc3a43b42000-06-24 18:10:48 +0000456 'macros', if given, must be a list of macro definitions. A macro
457 definition is either a (name, value) 2-tuple or a (name,) 1-tuple.
458 The former defines a macro; if the value is None, the macro is
459 defined without an explicit value. The 1-tuple case undefines a
460 macro. Later definitions/redefinitions/ undefinitions take
461 precedence.
Greg Ward3f81cf71999-07-10 02:03:53 +0000462
Greg Wardc3a43b42000-06-24 18:10:48 +0000463 'include_dirs', if given, must be a list of strings, the
464 directories to add to the default include file search path for this
465 compilation only.
Greg Ward3c045a52000-02-09 02:16:14 +0000466
Greg Wardc3a43b42000-06-24 18:10:48 +0000467 'debug' is a boolean; if true, the compiler will be instructed to
468 output debug symbols in (or alongside) the object file(s).
Greg Ward802d6b71999-09-29 12:20:55 +0000469
Greg Wardc3a43b42000-06-24 18:10:48 +0000470 'extra_preargs' and 'extra_postargs' are implementation- dependent.
471 On platforms that have the notion of a command-line (e.g. Unix,
472 DOS/Windows), they are most likely lists of strings: extra
473 command-line arguments to prepand/append to the compiler command
474 line. On other platforms, consult the implementation class
475 documentation. In any event, they are intended as an escape hatch
476 for those occasions when the abstract compiler framework doesn't
477 cut the mustard.
Greg Wardd1517112000-05-30 01:56:44 +0000478
Greg Wardc3a43b42000-06-24 18:10:48 +0000479 Raises CompileError on failure.
480 """
Greg Ward3f81cf71999-07-10 02:03:53 +0000481 pass
482
483
Greg Ward036c8052000-03-10 01:48:32 +0000484 def create_static_lib (self,
485 objects,
486 output_libname,
487 output_dir=None,
488 debug=0):
Greg Wardc3a43b42000-06-24 18:10:48 +0000489 """Link a bunch of stuff together to create a static library file.
490 The "bunch of stuff" consists of the list of object files supplied
491 as 'objects', the extra object files supplied to
492 'add_link_object()' and/or 'set_link_objects()', the libraries
493 supplied to 'add_library()' and/or 'set_libraries()', and the
494 libraries supplied as 'libraries' (if any).
Greg Ward3f81cf71999-07-10 02:03:53 +0000495
Greg Wardc3a43b42000-06-24 18:10:48 +0000496 'output_libname' should be a library name, not a filename; the
497 filename will be inferred from the library name. 'output_dir' is
498 the directory where the library file will be put.
Greg Ward3c045a52000-02-09 02:16:14 +0000499
Greg Wardc3a43b42000-06-24 18:10:48 +0000500 'debug' is a boolean; if true, debugging information will be
501 included in the library (note that on most platforms, it is the
502 compile step where this matters: the 'debug' flag is included here
503 just for consistency).
Greg Wardd1517112000-05-30 01:56:44 +0000504
Greg Wardc3a43b42000-06-24 18:10:48 +0000505 Raises LibError on failure.
506 """
Greg Ward3c045a52000-02-09 02:16:14 +0000507 pass
508
509
510 def link_shared_lib (self,
511 objects,
512 output_libname,
513 output_dir=None,
514 libraries=None,
515 library_dirs=None,
Greg Wardf10f95d2000-03-26 21:37:09 +0000516 runtime_library_dirs=None,
Greg Ward5299b6a2000-05-20 13:23:21 +0000517 export_symbols=None,
Greg Ward3c045a52000-02-09 02:16:14 +0000518 debug=0,
519 extra_preargs=None,
520 extra_postargs=None):
Greg Ward3f81cf71999-07-10 02:03:53 +0000521
Greg Wardc3a43b42000-06-24 18:10:48 +0000522 """Link a bunch of stuff together to create a shared library file.
523 Similar semantics to 'create_static_lib()', with the addition of
524 other libraries to link against and directories to search for them.
525 Also, of course, the type and name of the generated file will
526 almost certainly be different, as will the program used to create
527 it.
Greg Ward3febd601999-10-03 20:41:02 +0000528
Greg Wardc3a43b42000-06-24 18:10:48 +0000529 'libraries' is a list of libraries to link against. These are
530 library names, not filenames, since they're translated into
531 filenames in a platform-specific way (eg. "foo" becomes "libfoo.a"
532 on Unix and "foo.lib" on DOS/Windows). However, they can include a
533 directory component, which means the linker will look in that
534 specific directory rather than searching all the normal locations.
Greg Ward5299b6a2000-05-20 13:23:21 +0000535
Greg Wardc3a43b42000-06-24 18:10:48 +0000536 'library_dirs', if supplied, should be a list of directories to
537 search for libraries that were specified as bare library names
538 (ie. no directory component). These are on top of the system
539 default and those supplied to 'add_library_dir()' and/or
540 'set_library_dirs()'. 'runtime_library_dirs' is a list of
541 directories that will be embedded into the shared library and used
542 to search for other shared libraries that *it* depends on at
543 run-time. (This may only be relevant on Unix.)
Greg Ward802d6b71999-09-29 12:20:55 +0000544
Greg Wardc3a43b42000-06-24 18:10:48 +0000545 'export_symbols' is a list of symbols that the shared library will
546 export. (This appears to be relevant only on Windows.)
Greg Ward3c045a52000-02-09 02:16:14 +0000547
Greg Wardc3a43b42000-06-24 18:10:48 +0000548 'debug' is as for 'compile()' and 'create_static_lib()', with the
549 slight distinction that it actually matters on most platforms (as
550 opposed to 'create_static_lib()', which includes a 'debug' flag
551 mostly for form's sake).
Greg Wardd1517112000-05-30 01:56:44 +0000552
Greg Wardc3a43b42000-06-24 18:10:48 +0000553 'extra_preargs' and 'extra_postargs' are as for 'compile()' (except
554 of course that they supply command-line arguments for the
555 particular linker being used).
Greg Ward3f81cf71999-07-10 02:03:53 +0000556
Greg Wardc3a43b42000-06-24 18:10:48 +0000557 Raises LinkError on failure.
558 """
Greg Ward3f81cf71999-07-10 02:03:53 +0000559 pass
560
561
Greg Ward3f81cf71999-07-10 02:03:53 +0000562 def link_shared_object (self,
563 objects,
564 output_filename,
Greg Ward9b17cb51999-09-13 03:07:24 +0000565 output_dir=None,
Greg Ward3f81cf71999-07-10 02:03:53 +0000566 libraries=None,
Greg Ward26e48ea1999-08-29 18:17:36 +0000567 library_dirs=None,
Greg Wardf10f95d2000-03-26 21:37:09 +0000568 runtime_library_dirs=None,
Greg Ward5299b6a2000-05-20 13:23:21 +0000569 export_symbols=None,
Greg Ward3c045a52000-02-09 02:16:14 +0000570 debug=0,
Greg Ward802d6b71999-09-29 12:20:55 +0000571 extra_preargs=None,
572 extra_postargs=None):
Greg Wardc3a43b42000-06-24 18:10:48 +0000573 """Link a bunch of stuff together to create a shared object file.
574 Much like 'link_shared_lib()', except the output filename is
575 explicitly supplied as 'output_filename'. If 'output_dir' is
576 supplied, 'output_filename' is relative to it
577 (i.e. 'output_filename' can provide directory components if
578 needed).
Greg Wardd1517112000-05-30 01:56:44 +0000579
Greg Wardc3a43b42000-06-24 18:10:48 +0000580 Raises LinkError on failure.
581 """
Greg Ward3f81cf71999-07-10 02:03:53 +0000582 pass
583
Greg Warde1aaaa61999-08-14 23:50:50 +0000584
Greg Ward5baf1c22000-01-09 22:41:02 +0000585 def link_executable (self,
586 objects,
587 output_progname,
588 output_dir=None,
589 libraries=None,
590 library_dirs=None,
Greg Wardf10f95d2000-03-26 21:37:09 +0000591 runtime_library_dirs=None,
Greg Ward3c045a52000-02-09 02:16:14 +0000592 debug=0,
Greg Ward5baf1c22000-01-09 22:41:02 +0000593 extra_preargs=None,
594 extra_postargs=None):
595 """Link a bunch of stuff together to create a binary executable
Greg Wardc3a43b42000-06-24 18:10:48 +0000596 file. The "bunch of stuff" is as for 'link_shared_lib()'.
597 'output_progname' should be the base name of the executable
598 program--e.g. on Unix the same as the output filename, but on
599 DOS/Windows ".exe" will be appended.
Greg Wardd1517112000-05-30 01:56:44 +0000600
Greg Wardc3a43b42000-06-24 18:10:48 +0000601 Raises LinkError on failure.
602 """
Greg Ward5baf1c22000-01-09 22:41:02 +0000603 pass
604
605
606
Greg Wardf7edea72000-05-20 13:31:32 +0000607 # -- Miscellaneous methods -----------------------------------------
608 # These are all used by the 'gen_lib_options() function; there is
609 # no appropriate default implementation so subclasses should
610 # implement all of these.
611
612 def library_dir_option (self, dir):
Greg Wardc3a43b42000-06-24 18:10:48 +0000613 """Return the compiler option to add 'dir' to the list of
614 directories searched for libraries.
615 """
Greg Wardf7edea72000-05-20 13:31:32 +0000616 raise NotImplementedError
617
618 def runtime_library_dir_option (self, dir):
Greg Wardc3a43b42000-06-24 18:10:48 +0000619 """Return the compiler option to add 'dir' to the list of
620 directories searched for runtime libraries.
621 """
Greg Wardf7edea72000-05-20 13:31:32 +0000622 raise NotImplementedError
623
624 def library_option (self, lib):
625 """Return the compiler option to add 'dir' to the list of libraries
Greg Wardc3a43b42000-06-24 18:10:48 +0000626 linked into the shared library or executable.
627 """
Greg Wardf7edea72000-05-20 13:31:32 +0000628 raise NotImplementedError
629
630 def find_library_file (self, dirs, lib):
631 """Search the specified list of directories for a static or shared
Greg Wardc3a43b42000-06-24 18:10:48 +0000632 library file 'lib' and return the full path to that file. Return
633 None if it wasn't found in any of the specified directories.
634 """
Greg Wardf7edea72000-05-20 13:31:32 +0000635 raise NotImplementedError
636
637
Greg Ward32c4a8a2000-03-06 03:40:29 +0000638 # -- Filename generation methods -----------------------------------
Greg Warde1aaaa61999-08-14 23:50:50 +0000639
Greg Ward32c4a8a2000-03-06 03:40:29 +0000640 # The default implementation of the filename generating methods are
641 # prejudiced towards the Unix/DOS/Windows view of the world:
642 # * object files are named by replacing the source file extension
643 # (eg. .c/.cpp -> .o/.obj)
644 # * library files (shared or static) are named by plugging the
645 # library name and extension into a format string, eg.
646 # "lib%s.%s" % (lib_name, ".a") for Unix static libraries
647 # * executables are named by appending an extension (possibly
648 # empty) to the program name: eg. progname + ".exe" for
649 # Windows
650 #
651 # To reduce redundant code, these methods expect to find
652 # several attributes in the current object (presumably defined
653 # as class attributes):
654 # * src_extensions -
655 # list of C/C++ source file extensions, eg. ['.c', '.cpp']
656 # * obj_extension -
657 # object file extension, eg. '.o' or '.obj'
658 # * static_lib_extension -
659 # extension for static library files, eg. '.a' or '.lib'
660 # * shared_lib_extension -
661 # extension for shared library/object files, eg. '.so', '.dll'
662 # * static_lib_format -
663 # format string for generating static library filenames,
664 # eg. 'lib%s.%s' or '%s.%s'
665 # * shared_lib_format
666 # format string for generating shared library filenames
667 # (probably same as static_lib_format, since the extension
668 # is one of the intended parameters to the format string)
669 # * exe_extension -
670 # extension for executable files, eg. '' or '.exe'
Greg Ward9b17cb51999-09-13 03:07:24 +0000671
Greg Ward32c4a8a2000-03-06 03:40:29 +0000672 def object_filenames (self,
673 source_filenames,
674 strip_dir=0,
675 output_dir=''):
676 if output_dir is None: output_dir = ''
677 obj_names = []
678 for src_name in source_filenames:
679 (base, ext) = os.path.splitext (src_name)
680 if ext not in self.src_extensions:
Greg Ward9aa668b2000-06-24 02:22:49 +0000681 raise UnknownFileError, \
682 "unknown file type '%s' (from '%s')" % \
683 (ext, src_name)
Greg Ward32c4a8a2000-03-06 03:40:29 +0000684 if strip_dir:
685 base = os.path.basename (base)
686 obj_names.append (os.path.join (output_dir,
687 base + self.obj_extension))
688 return obj_names
Greg Warde1aaaa61999-08-14 23:50:50 +0000689
Greg Ward32c4a8a2000-03-06 03:40:29 +0000690 # object_filenames ()
Greg Warde1aaaa61999-08-14 23:50:50 +0000691
Greg Warde1aaaa61999-08-14 23:50:50 +0000692
Greg Ward32c4a8a2000-03-06 03:40:29 +0000693 def shared_object_filename (self,
694 basename,
695 strip_dir=0,
696 output_dir=''):
697 if output_dir is None: output_dir = ''
698 if strip_dir:
699 basename = os.path.basename (basename)
700 return os.path.join (output_dir, basename + self.shared_lib_extension)
Greg Warde1aaaa61999-08-14 23:50:50 +0000701
Greg Ward26e48ea1999-08-29 18:17:36 +0000702
Greg Ward32c4a8a2000-03-06 03:40:29 +0000703 def library_filename (self,
704 libname,
705 lib_type='static', # or 'shared'
706 strip_dir=0,
707 output_dir=''):
708
709 if output_dir is None: output_dir = ''
710 if lib_type not in ("static","shared"):
711 raise ValueError, "'lib_type' must be \"static\" or \"shared\""
712 fmt = getattr (self, lib_type + "_lib_format")
713 ext = getattr (self, lib_type + "_lib_extension")
714
715 (dir, base) = os.path.split (libname)
716 filename = fmt % (base, ext)
717 if strip_dir:
718 dir = ''
719
720 return os.path.join (output_dir, dir, filename)
721
Greg Warde1aaaa61999-08-14 23:50:50 +0000722
723 # -- Utility methods -----------------------------------------------
724
Greg Ward9b17cb51999-09-13 03:07:24 +0000725 def announce (self, msg, level=1):
726 if self.verbose >= level:
727 print msg
728
Greg Ward3febd601999-10-03 20:41:02 +0000729 def warn (self, msg):
730 sys.stderr.write ("warning: %s\n" % msg)
731
Greg Warde1aaaa61999-08-14 23:50:50 +0000732 def spawn (self, cmd):
733 spawn (cmd, verbose=self.verbose, dry_run=self.dry_run)
734
Greg Ward9b17cb51999-09-13 03:07:24 +0000735 def move_file (self, src, dst):
736 return move_file (src, dst, verbose=self.verbose, dry_run=self.dry_run)
737
Greg Ward013f0c82000-03-01 14:43:12 +0000738 def mkpath (self, name, mode=0777):
739 mkpath (name, mode, self.verbose, self.dry_run)
740
Greg Warde1aaaa61999-08-14 23:50:50 +0000741
Greg Ward3f81cf71999-07-10 02:03:53 +0000742# class CCompiler
743
744
Greg Ward802d6b71999-09-29 12:20:55 +0000745# Map a platform ('posix', 'nt') to the default compiler type for
746# that platform.
747default_compiler = { 'posix': 'unix',
748 'nt': 'msvc',
749 }
750
751# Map compiler types to (module_name, class_name) pairs -- ie. where to
752# find the code that implements an interface to this compiler. (The module
753# is assumed to be in the 'distutils' package.)
Greg Ward2ff78872000-06-24 00:23:20 +0000754compiler_class = { 'unix': ('unixccompiler', 'UnixCCompiler',
755 "standard UNIX-style compiler"),
756 'msvc': ('msvccompiler', 'MSVCCompiler',
757 "Microsoft Visual C++"),
758 'cygwin': ('cygwinccompiler', 'CygwinCCompiler',
759 "Cygwin port of GNU C Compiler for Win32"),
760 'mingw32': ('cygwinccompiler', 'Mingw32CCompiler',
761 "Mingw32 port of GNU C Compiler for Win32"),
Greg Ward802d6b71999-09-29 12:20:55 +0000762 }
763
Greg Ward9d17a7a2000-06-07 03:00:06 +0000764def show_compilers():
Greg Ward2ff78872000-06-24 00:23:20 +0000765 """Print list of available compilers (used by the "--help-compiler"
766 options to "build", "build_ext", "build_clib").
767 """
768 # XXX this "knows" that the compiler option it's describing is
769 # "--compiler", which just happens to be the case for the three
770 # commands that use it.
Greg Ward9d17a7a2000-06-07 03:00:06 +0000771 from distutils.fancy_getopt import FancyGetopt
Greg Ward2ff78872000-06-24 00:23:20 +0000772 compilers = []
Greg Ward9d17a7a2000-06-07 03:00:06 +0000773 for compiler in compiler_class.keys():
Greg Ward2ff78872000-06-24 00:23:20 +0000774 compilers.append(("compiler="+compiler, None,
775 compiler_class[compiler][2]))
776 compilers.sort()
777 pretty_printer = FancyGetopt(compilers)
Greg Ward9d17a7a2000-06-07 03:00:06 +0000778 pretty_printer.print_help("List of available compilers:")
779
Greg Ward802d6b71999-09-29 12:20:55 +0000780
Greg Warde1aaaa61999-08-14 23:50:50 +0000781def new_compiler (plat=None,
Greg Ward802d6b71999-09-29 12:20:55 +0000782 compiler=None,
Greg Warde1aaaa61999-08-14 23:50:50 +0000783 verbose=0,
Greg Ward3febd601999-10-03 20:41:02 +0000784 dry_run=0,
785 force=0):
Greg Ward802d6b71999-09-29 12:20:55 +0000786 """Generate an instance of some CCompiler subclass for the supplied
Greg Wardc3a43b42000-06-24 18:10:48 +0000787 platform/compiler combination. 'plat' defaults to 'os.name'
788 (eg. 'posix', 'nt'), and 'compiler' defaults to the default compiler
789 for that platform. Currently only 'posix' and 'nt' are supported, and
790 the default compilers are "traditional Unix interface" (UnixCCompiler
791 class) and Visual C++ (MSVCCompiler class). Note that it's perfectly
792 possible to ask for a Unix compiler object under Windows, and a
793 Microsoft compiler object under Unix -- if you supply a value for
794 'compiler', 'plat' is ignored.
795 """
Greg Ward802d6b71999-09-29 12:20:55 +0000796 if plat is None:
797 plat = os.name
798
799 try:
800 if compiler is None:
801 compiler = default_compiler[plat]
802
Greg Ward2ff78872000-06-24 00:23:20 +0000803 (module_name, class_name, long_description) = compiler_class[compiler]
Greg Ward802d6b71999-09-29 12:20:55 +0000804 except KeyError:
805 msg = "don't know how to compile C/C++ code on platform '%s'" % plat
806 if compiler is not None:
807 msg = msg + " with '%s' compiler" % compiler
808 raise DistutilsPlatformError, msg
809
810 try:
811 module_name = "distutils." + module_name
812 __import__ (module_name)
813 module = sys.modules[module_name]
814 klass = vars(module)[class_name]
815 except ImportError:
816 raise DistutilsModuleError, \
817 "can't compile C/C++ code: unable to load module '%s'" % \
818 module_name
819 except KeyError:
820 raise DistutilsModuleError, \
821 ("can't compile C/C++ code: unable to find class '%s' " +
822 "in module '%s'") % (class_name, module_name)
823
Greg Ward3febd601999-10-03 20:41:02 +0000824 return klass (verbose, dry_run, force)
Greg Wardf7a39ec1999-09-08 02:29:08 +0000825
826
Greg Ward0bdd90a1999-12-12 17:19:58 +0000827def gen_preprocess_options (macros, include_dirs):
Greg Wardc3a43b42000-06-24 18:10:48 +0000828 """Generate C pre-processor options (-D, -U, -I) as used by at least
829 two types of compilers: the typical Unix compiler and Visual C++.
830 'macros' is the usual thing, a list of 1- or 2-tuples, where (name,)
831 means undefine (-U) macro 'name', and (name,value) means define (-D)
832 macro 'name' to 'value'. 'include_dirs' is just a list of directory
833 names to be added to the header file search path (-I). Returns a list
834 of command-line options suitable for either Unix compilers or Visual
835 C++.
836 """
Greg Wardf7a39ec1999-09-08 02:29:08 +0000837 # XXX it would be nice (mainly aesthetic, and so we don't generate
838 # stupid-looking command lines) to go over 'macros' and eliminate
839 # redundant definitions/undefinitions (ie. ensure that only the
840 # latest mention of a particular macro winds up on the command
841 # line). I don't think it's essential, though, since most (all?)
842 # Unix C compilers only pay attention to the latest -D or -U
843 # mention of a macro on their command line. Similar situation for
Greg Ward0bdd90a1999-12-12 17:19:58 +0000844 # 'include_dirs'. I'm punting on both for now. Anyways, weeding out
Greg Wardf7a39ec1999-09-08 02:29:08 +0000845 # redundancies like this should probably be the province of
846 # CCompiler, since the data structures used are inherited from it
847 # and therefore common to all CCompiler classes.
848
849 pp_opts = []
850 for macro in macros:
Greg Wardfbf8aff1999-09-21 18:35:09 +0000851
852 if not (type (macro) is TupleType and
853 1 <= len (macro) <= 2):
854 raise TypeError, \
855 ("bad macro definition '%s': " +
856 "each element of 'macros' list must be a 1- or 2-tuple") % \
857 macro
858
Greg Wardf7a39ec1999-09-08 02:29:08 +0000859 if len (macro) == 1: # undefine this macro
860 pp_opts.append ("-U%s" % macro[0])
861 elif len (macro) == 2:
862 if macro[1] is None: # define with no explicit value
863 pp_opts.append ("-D%s" % macro[0])
864 else:
865 # XXX *don't* need to be clever about quoting the
866 # macro value here, because we're going to avoid the
867 # shell at all costs when we spawn the command!
868 pp_opts.append ("-D%s=%s" % macro)
869
Greg Ward0bdd90a1999-12-12 17:19:58 +0000870 for dir in include_dirs:
Greg Wardf7a39ec1999-09-08 02:29:08 +0000871 pp_opts.append ("-I%s" % dir)
872
873 return pp_opts
874
875# gen_preprocess_options ()
876
877
Greg Wardd03f88a2000-03-18 15:19:51 +0000878def gen_lib_options (compiler, library_dirs, runtime_library_dirs, libraries):
Greg Wardf7a39ec1999-09-08 02:29:08 +0000879 """Generate linker options for searching library directories and
Greg Wardc3a43b42000-06-24 18:10:48 +0000880 linking with specific libraries. 'libraries' and 'library_dirs' are,
881 respectively, lists of library names (not filenames!) and search
882 directories. Returns a list of command-line options suitable for use
883 with some compiler (depending on the two format strings passed in).
884 """
Greg Wardf7a39ec1999-09-08 02:29:08 +0000885 lib_opts = []
886
887 for dir in library_dirs:
Greg Ward3febd601999-10-03 20:41:02 +0000888 lib_opts.append (compiler.library_dir_option (dir))
Greg Wardf7a39ec1999-09-08 02:29:08 +0000889
Greg Wardd03f88a2000-03-18 15:19:51 +0000890 for dir in runtime_library_dirs:
891 lib_opts.append (compiler.runtime_library_dir_option (dir))
892
Greg Wardf7a39ec1999-09-08 02:29:08 +0000893 # XXX it's important that we *not* remove redundant library mentions!
894 # sometimes you really do have to say "-lfoo -lbar -lfoo" in order to
895 # resolve all symbols. I just hope we never have to say "-lfoo obj.o
896 # -lbar" to get things to work -- that's certainly a possibility, but a
897 # pretty nasty way to arrange your C code.
898
899 for lib in libraries:
Greg Ward3febd601999-10-03 20:41:02 +0000900 (lib_dir, lib_name) = os.path.split (lib)
901 if lib_dir:
902 lib_file = compiler.find_library_file ([lib_dir], lib_name)
903 if lib_file:
904 lib_opts.append (lib_file)
905 else:
906 compiler.warn ("no library file corresponding to "
907 "'%s' found (skipping)" % lib)
908 else:
909 lib_opts.append (compiler.library_option (lib))
Greg Wardf7a39ec1999-09-08 02:29:08 +0000910
911 return lib_opts
912
Greg Ward32c4a8a2000-03-06 03:40:29 +0000913# gen_lib_options ()