Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 1 | """distutils.ccompiler |
| 2 | |
| 3 | Contains CCompiler, an abstract base class that defines the interface |
| 4 | for the Distutils compiler abstraction model.""" |
| 5 | |
| 6 | # created 1999/07/05, Greg Ward |
| 7 | |
Greg Ward | 3ce77fd | 2000-03-02 01:49:45 +0000 | [diff] [blame] | 8 | __revision__ = "$Id$" |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 9 | |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 10 | import sys, os |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 11 | from types import * |
| 12 | from copy import copy |
| 13 | from distutils.errors import * |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 14 | from distutils.spawn import spawn |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 15 | from distutils.util import move_file, mkpath, newer_pairwise, newer_group |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 16 | |
| 17 | |
| 18 | class CCompiler: |
| 19 | """Abstract base class to define the interface that must be implemented |
| 20 | by real compiler abstraction classes. Might have some use as a |
| 21 | place for shared code, but it's not yet clear what code can be |
| 22 | shared between compiler abstraction models for different platforms. |
| 23 | |
| 24 | The basic idea behind a compiler abstraction class is that each |
| 25 | instance can be used for all the compile/link steps in building |
| 26 | a single project. Thus, attributes common to all of those compile |
| 27 | and link steps -- include directories, macros to define, libraries |
| 28 | to link against, etc. -- are attributes of the compiler instance. |
| 29 | To allow for variability in how individual files are treated, |
| 30 | most (all?) of those attributes may be varied on a per-compilation |
| 31 | or per-link basis.""" |
| 32 | |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 33 | # 'compiler_type' is a class attribute that identifies this class. It |
| 34 | # keeps code that wants to know what kind of compiler it's dealing with |
| 35 | # from having to import all possible compiler classes just to do an |
| 36 | # 'isinstance'. In concrete CCompiler subclasses, 'compiler_type' |
| 37 | # should really, really be one of the keys of the 'compiler_class' |
| 38 | # dictionary (see below -- used by the 'new_compiler()' factory |
| 39 | # function) -- authors of new compiler interface classes are |
| 40 | # responsible for updating 'compiler_class'! |
| 41 | compiler_type = None |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 42 | |
| 43 | # XXX things not handled by this compiler abstraction model: |
| 44 | # * client can't provide additional options for a compiler, |
| 45 | # e.g. warning, optimization, debugging flags. Perhaps this |
| 46 | # should be the domain of concrete compiler abstraction classes |
| 47 | # (UnixCCompiler, MSVCCompiler, etc.) -- or perhaps the base |
| 48 | # class should have methods for the common ones. |
| 49 | # * can't put output files (object files, libraries, whatever) |
| 50 | # into a separate directory from their inputs. Should this be |
| 51 | # handled by an 'output_dir' attribute of the whole object, or a |
| 52 | # parameter to the compile/link_* methods, or both? |
| 53 | # * can't completely override the include or library searchg |
| 54 | # path, ie. no "cc -I -Idir1 -Idir2" or "cc -L -Ldir1 -Ldir2". |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 55 | # I'm not sure how widely supported this is even by Unix |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 56 | # compilers, much less on other platforms. And I'm even less |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 57 | # sure how useful it is; maybe for cross-compiling, but |
| 58 | # support for that is a ways off. (And anyways, cross |
| 59 | # compilers probably have a dedicated binary with the |
| 60 | # right paths compiled in. I hope.) |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 61 | # * can't do really freaky things with the library list/library |
| 62 | # dirs, e.g. "-Ldir1 -lfoo -Ldir2 -lfoo" to link against |
| 63 | # different versions of libfoo.a in different locations. I |
| 64 | # think this is useless without the ability to null out the |
| 65 | # library search path anyways. |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 66 | |
| 67 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 68 | # Subclasses that rely on the standard filename generation methods |
| 69 | # implemented below should override these; see the comment near |
| 70 | # those methods ('object_filenames()' et. al.) for details: |
| 71 | src_extensions = None # list of strings |
| 72 | obj_extension = None # string |
| 73 | static_lib_extension = None |
| 74 | shared_lib_extension = None # string |
| 75 | static_lib_format = None # format string |
| 76 | shared_lib_format = None # prob. same as static_lib_format |
| 77 | exe_extension = None # string |
| 78 | |
| 79 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 80 | def __init__ (self, |
| 81 | verbose=0, |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 82 | dry_run=0, |
| 83 | force=0): |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 84 | |
| 85 | self.verbose = verbose |
| 86 | self.dry_run = dry_run |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 87 | self.force = force |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 88 | |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 89 | # 'output_dir': a common output directory for object, library, |
| 90 | # shared object, and shared library files |
| 91 | self.output_dir = None |
| 92 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 93 | # 'macros': a list of macro definitions (or undefinitions). A |
| 94 | # macro definition is a 2-tuple (name, value), where the value is |
| 95 | # either a string or None (no explicit value). A macro |
| 96 | # undefinition is a 1-tuple (name,). |
| 97 | self.macros = [] |
| 98 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 99 | # 'include_dirs': a list of directories to search for include files |
| 100 | self.include_dirs = [] |
| 101 | |
| 102 | # 'libraries': a list of libraries to include in any link |
| 103 | # (library names, not filenames: eg. "foo" not "libfoo.a") |
| 104 | self.libraries = [] |
| 105 | |
| 106 | # 'library_dirs': a list of directories to search for libraries |
| 107 | self.library_dirs = [] |
| 108 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 109 | # 'runtime_library_dirs': a list of directories to search for |
| 110 | # shared libraries/objects at runtime |
| 111 | self.runtime_library_dirs = [] |
| 112 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 113 | # 'objects': a list of object files (or similar, such as explicitly |
| 114 | # named library files) to include on any link |
| 115 | self.objects = [] |
| 116 | |
| 117 | # __init__ () |
| 118 | |
| 119 | |
| 120 | def _find_macro (self, name): |
| 121 | i = 0 |
| 122 | for defn in self.macros: |
| 123 | if defn[0] == name: |
| 124 | return i |
| 125 | i = i + 1 |
| 126 | |
| 127 | return None |
| 128 | |
| 129 | |
| 130 | def _check_macro_definitions (self, definitions): |
| 131 | """Ensures that every element of 'definitions' is a valid macro |
| 132 | definition, ie. either (name,value) 2-tuple or a (name,) |
| 133 | tuple. Do nothing if all definitions are OK, raise |
| 134 | TypeError otherwise.""" |
| 135 | |
| 136 | for defn in definitions: |
| 137 | if not (type (defn) is TupleType and |
| 138 | (len (defn) == 1 or |
| 139 | (len (defn) == 2 and |
| 140 | (type (defn[1]) is StringType or defn[1] is None))) and |
| 141 | type (defn[0]) is StringType): |
| 142 | raise TypeError, \ |
| 143 | ("invalid macro definition '%s': " % defn) + \ |
| 144 | "must be tuple (string,), (string, string), or " + \ |
| 145 | "(string, None)" |
| 146 | |
| 147 | |
| 148 | # -- Bookkeeping methods ------------------------------------------- |
| 149 | |
| 150 | def define_macro (self, name, value=None): |
| 151 | """Define a preprocessor macro for all compilations driven by |
| 152 | this compiler object. The optional parameter 'value' should be |
| 153 | a string; if it is not supplied, then the macro will be defined |
| 154 | without an explicit value and the exact outcome depends on the |
| 155 | compiler used (XXX true? does ANSI say anything about this?)""" |
| 156 | |
| 157 | # Delete from the list of macro definitions/undefinitions if |
| 158 | # already there (so that this one will take precedence). |
| 159 | i = self._find_macro (name) |
| 160 | if i is not None: |
| 161 | del self.macros[i] |
| 162 | |
| 163 | defn = (name, value) |
| 164 | self.macros.append (defn) |
| 165 | |
| 166 | |
| 167 | def undefine_macro (self, name): |
| 168 | """Undefine a preprocessor macro for all compilations driven by |
| 169 | this compiler object. If the same macro is defined by |
| 170 | 'define_macro()' and undefined by 'undefine_macro()' the last |
| 171 | call takes precedence (including multiple redefinitions or |
| 172 | undefinitions). If the macro is redefined/undefined on a |
| 173 | per-compilation basis (ie. in the call to 'compile()'), then |
| 174 | that takes precedence.""" |
| 175 | |
| 176 | # Delete from the list of macro definitions/undefinitions if |
| 177 | # already there (so that this one will take precedence). |
| 178 | i = self._find_macro (name) |
| 179 | if i is not None: |
| 180 | del self.macros[i] |
| 181 | |
| 182 | undefn = (name,) |
| 183 | self.macros.append (undefn) |
| 184 | |
| 185 | |
| 186 | def add_include_dir (self, dir): |
| 187 | """Add 'dir' to the list of directories that will be searched |
| 188 | for header files. The compiler is instructed to search |
| 189 | directories in the order in which they are supplied by |
| 190 | successive calls to 'add_include_dir()'.""" |
| 191 | self.include_dirs.append (dir) |
| 192 | |
| 193 | def set_include_dirs (self, dirs): |
| 194 | """Set the list of directories that will be searched to 'dirs' |
| 195 | (a list of strings). Overrides any preceding calls to |
| 196 | 'add_include_dir()'; subsequence calls to 'add_include_dir()' |
| 197 | add to the list passed to 'set_include_dirs()'. This does |
| 198 | not affect any list of standard include directories that |
| 199 | the compiler may search by default.""" |
| 200 | self.include_dirs = copy (dirs) |
| 201 | |
| 202 | |
| 203 | def add_library (self, libname): |
| 204 | """Add 'libname' to the list of libraries that will be included |
| 205 | in all links driven by this compiler object. Note that |
| 206 | 'libname' should *not* be the name of a file containing a |
| 207 | library, but the name of the library itself: the actual filename |
| 208 | will be inferred by the linker, the compiler, or the compiler |
| 209 | abstraction class (depending on the platform). |
| 210 | |
| 211 | The linker will be instructed to link against libraries in the |
| 212 | order they were supplied to 'add_library()' and/or |
| 213 | 'set_libraries()'. It is perfectly valid to duplicate library |
| 214 | names; the linker will be instructed to link against libraries |
| 215 | as many times as they are mentioned.""" |
| 216 | self.libraries.append (libname) |
| 217 | |
| 218 | def set_libraries (self, libnames): |
| 219 | """Set the list of libraries to be included in all links driven |
| 220 | by this compiler object to 'libnames' (a list of strings). |
| 221 | This does not affect any standard system libraries that the |
| 222 | linker may include by default.""" |
| 223 | |
| 224 | self.libraries = copy (libnames) |
| 225 | |
| 226 | |
| 227 | def add_library_dir (self, dir): |
| 228 | """Add 'dir' to the list of directories that will be searched for |
| 229 | libraries specified to 'add_library()' and 'set_libraries()'. |
| 230 | The linker will be instructed to search for libraries in the |
| 231 | order they are supplied to 'add_library_dir()' and/or |
| 232 | 'set_library_dirs()'.""" |
| 233 | self.library_dirs.append (dir) |
| 234 | |
| 235 | def set_library_dirs (self, dirs): |
| 236 | """Set the list of library search directories to 'dirs' (a list |
| 237 | of strings). This does not affect any standard library |
| 238 | search path that the linker may search by default.""" |
| 239 | self.library_dirs = copy (dirs) |
| 240 | |
| 241 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 242 | def add_runtime_library_dir (self, dir): |
| 243 | """Add 'dir' to the list of directories that will be searched for |
| 244 | shared libraries at runtime.""" |
| 245 | self.runtime_library_dirs.append (dir) |
| 246 | |
| 247 | def set_runtime_library_dirs (self, dirs): |
| 248 | """Set the list of directories to search for shared libraries |
| 249 | at runtime to 'dirs' (a list of strings). This does not affect |
| 250 | any standard search path that the runtime linker may search by |
| 251 | default.""" |
| 252 | self.runtime_library_dirs = copy (dirs) |
| 253 | |
| 254 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 255 | def add_link_object (self, object): |
| 256 | """Add 'object' to the list of object files (or analogues, such |
| 257 | as explictly named library files or the output of "resource |
| 258 | compilers") to be included in every link driven by this |
| 259 | compiler object.""" |
| 260 | self.objects.append (object) |
| 261 | |
| 262 | def set_link_objects (self, objects): |
| 263 | """Set the list of object files (or analogues) to be included |
| 264 | in every link to 'objects'. This does not affect any |
| 265 | standard object files that the linker may include by default |
| 266 | (such as system libraries).""" |
| 267 | self.objects = copy (objects) |
| 268 | |
| 269 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 270 | # -- Priviate utility methods -------------------------------------- |
| 271 | # (here for the convenience of subclasses) |
| 272 | |
| 273 | def _fix_compile_args (self, output_dir, macros, include_dirs): |
| 274 | """Typecheck and fix-up some of the arguments to the 'compile()' method, |
| 275 | and return fixed-up values. Specifically: if 'output_dir' is |
| 276 | None, replaces it with 'self.output_dir'; ensures that 'macros' |
| 277 | is a list, and augments it with 'self.macros'; ensures that |
| 278 | 'include_dirs' is a list, and augments it with |
| 279 | 'self.include_dirs'. Guarantees that the returned values are of |
| 280 | the correct type, i.e. for 'output_dir' either string or None, |
| 281 | and for 'macros' and 'include_dirs' either list or None.""" |
| 282 | |
| 283 | if output_dir is None: |
| 284 | output_dir = self.output_dir |
| 285 | elif type (output_dir) is not StringType: |
| 286 | raise TypeError, "'output_dir' must be a string or None" |
| 287 | |
| 288 | if macros is None: |
| 289 | macros = self.macros |
| 290 | elif type (macros) is ListType: |
| 291 | macros = macros + (self.macros or []) |
| 292 | else: |
| 293 | raise TypeError, \ |
| 294 | "'macros' (if supplied) must be a list of tuples" |
| 295 | |
| 296 | if include_dirs is None: |
| 297 | include_dirs = self.include_dirs |
| 298 | elif type (include_dirs) in (ListType, TupleType): |
| 299 | include_dirs = list (include_dirs) + (self.include_dirs or []) |
| 300 | else: |
| 301 | raise TypeError, \ |
| 302 | "'include_dirs' (if supplied) must be a list of strings" |
| 303 | |
| 304 | return (output_dir, macros, include_dirs) |
| 305 | |
| 306 | # _fix_compile_args () |
| 307 | |
| 308 | |
| 309 | def _prep_compile (self, sources, output_dir): |
| 310 | """Determine the list of object files corresponding to 'sources', and |
| 311 | figure out which ones really need to be recompiled. Return a list |
| 312 | of all object files and a dictionary telling which source files can |
| 313 | be skipped.""" |
| 314 | |
| 315 | # Get the list of expected output (object) files |
| 316 | objects = self.object_filenames (sources, |
| 317 | output_dir=output_dir) |
| 318 | |
| 319 | if self.force: |
| 320 | skip_source = {} # rebuild everything |
| 321 | for source in sources: |
| 322 | skip_source[source] = 0 |
| 323 | else: |
| 324 | # Figure out which source files we have to recompile according |
| 325 | # to a simplistic check -- we just compare the source and |
| 326 | # object file, no deep dependency checking involving header |
| 327 | # files. |
| 328 | skip_source = {} # rebuild everything |
| 329 | for source in sources: # no wait, rebuild nothing |
| 330 | skip_source[source] = 1 |
| 331 | |
| 332 | (n_sources, n_objects) = newer_pairwise (sources, objects) |
| 333 | for source in n_sources: # no really, only rebuild what's out-of-date |
| 334 | skip_source[source] = 0 |
| 335 | |
| 336 | return (objects, skip_source) |
| 337 | |
| 338 | # _prep_compile () |
| 339 | |
| 340 | |
Greg Ward | f10f95d | 2000-03-26 21:37:09 +0000 | [diff] [blame] | 341 | def _fix_object_args (self, objects, output_dir): |
| 342 | """Typecheck and fix up some arguments supplied to various |
| 343 | methods. Specifically: ensure that 'objects' is a list; if |
| 344 | output_dir is None, replace with self.output_dir. Return fixed |
| 345 | versions of 'objects' and 'output_dir'.""" |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 346 | |
| 347 | if type (objects) not in (ListType, TupleType): |
| 348 | raise TypeError, \ |
| 349 | "'objects' must be a list or tuple of strings" |
| 350 | objects = list (objects) |
| 351 | |
| 352 | if output_dir is None: |
| 353 | output_dir = self.output_dir |
| 354 | elif type (output_dir) is not StringType: |
| 355 | raise TypeError, "'output_dir' must be a string or None" |
| 356 | |
Greg Ward | f10f95d | 2000-03-26 21:37:09 +0000 | [diff] [blame] | 357 | return (objects, output_dir) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 358 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 359 | |
Greg Ward | f10f95d | 2000-03-26 21:37:09 +0000 | [diff] [blame] | 360 | def _fix_lib_args (self, libraries, library_dirs, runtime_library_dirs): |
| 361 | """Typecheck and fix up some of the arguments supplied to the |
| 362 | 'link_*' methods. Specifically: ensure that all arguments are |
| 363 | lists, and augment them with their permanent versions |
| 364 | (eg. 'self.libraries' augments 'libraries'). Return a tuple |
| 365 | with fixed versions of all arguments.""" |
| 366 | |
| 367 | if libraries is None: |
| 368 | libraries = self.libraries |
| 369 | elif type (libraries) in (ListType, TupleType): |
| 370 | libraries = list (libraries) + (self.libraries or []) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 371 | else: |
Greg Ward | f10f95d | 2000-03-26 21:37:09 +0000 | [diff] [blame] | 372 | raise TypeError, \ |
| 373 | "'libraries' (if supplied) must be a list of strings" |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 374 | |
Greg Ward | f10f95d | 2000-03-26 21:37:09 +0000 | [diff] [blame] | 375 | if library_dirs is None: |
| 376 | library_dirs = self.library_dirs |
| 377 | elif type (library_dirs) in (ListType, TupleType): |
| 378 | library_dirs = list (library_dirs) + (self.library_dirs or []) |
| 379 | else: |
| 380 | raise TypeError, \ |
| 381 | "'library_dirs' (if supplied) must be a list of strings" |
| 382 | |
| 383 | if runtime_library_dirs is None: |
| 384 | runtime_library_dirs = self.runtime_library_dirs |
| 385 | elif type (runtime_library_dirs) in (ListType, TupleType): |
| 386 | runtime_library_dirs = (list (runtime_library_dirs) + |
| 387 | (self.runtime_library_dirs or [])) |
| 388 | else: |
| 389 | raise TypeError, \ |
| 390 | "'runtime_library_dirs' (if supplied) " + \ |
| 391 | "must be a list of strings" |
| 392 | |
| 393 | return (libraries, library_dirs, runtime_library_dirs) |
| 394 | |
| 395 | # _fix_lib_args () |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 396 | |
| 397 | |
| 398 | def _need_link (self, objects, output_file): |
| 399 | """Return true if we need to relink the files listed in 'objects' to |
| 400 | recreate 'output_file'.""" |
| 401 | |
| 402 | if self.force: |
| 403 | return 1 |
| 404 | else: |
| 405 | if self.dry_run: |
| 406 | newer = newer_group (objects, output_file, missing='newer') |
| 407 | else: |
| 408 | newer = newer_group (objects, output_file) |
| 409 | return newer |
| 410 | |
| 411 | # _need_link () |
| 412 | |
| 413 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 414 | # -- Worker methods ------------------------------------------------ |
| 415 | # (must be implemented by subclasses) |
| 416 | |
Greg Ward | 3ff3b03 | 2000-06-21 02:58:46 +0000 | [diff] [blame] | 417 | def preprocess (self, |
| 418 | source, |
| 419 | output_file=None, |
| 420 | macros=None, |
| 421 | include_dirs=None, |
| 422 | extra_preargs=None, |
| 423 | extra_postargs=None): |
| 424 | """Preprocess a single C/C++ source file, named in 'source'. |
| 425 | Output will be written to file named 'output_file', or stdout if |
| 426 | 'output_file' not supplied. 'macros' is a list of macro |
| 427 | definitions as for 'compile()', which will augment the macros set |
| 428 | with 'define_macro()' and 'undefine_macro()'. 'include_dirs' is a |
| 429 | list of directory names that will be added to the default list. |
| 430 | """ |
| 431 | pass |
| 432 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 433 | def compile (self, |
| 434 | sources, |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 435 | output_dir=None, |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 436 | macros=None, |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 437 | include_dirs=None, |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 438 | debug=0, |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 439 | extra_preargs=None, |
| 440 | extra_postargs=None): |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 441 | """Compile one or more C/C++ source files. 'sources' must be |
| 442 | a list of strings, each one the name of a C/C++ source |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 443 | file. Return a list of object filenames, one per source |
| 444 | filename in 'sources'. Depending on the implementation, |
| 445 | not all source files will necessarily be compiled, but |
| 446 | all corresponding object filenames will be returned. |
| 447 | |
| 448 | If 'output_dir' is given, object files will be put under it, |
| 449 | while retaining their original path component. That is, |
| 450 | "foo/bar.c" normally compiles to "foo/bar.o" (for a Unix |
| 451 | implementation); if 'output_dir' is "build", then it would |
| 452 | compile to "build/foo/bar.o". |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 453 | |
| 454 | 'macros', if given, must be a list of macro definitions. A |
| 455 | macro definition is either a (name, value) 2-tuple or a (name,) |
| 456 | 1-tuple. The former defines a macro; if the value is None, the |
| 457 | macro is defined without an explicit value. The 1-tuple case |
| 458 | undefines a macro. Later definitions/redefinitions/ |
| 459 | undefinitions take precedence. |
| 460 | |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 461 | 'include_dirs', if given, must be a list of strings, the |
| 462 | directories to add to the default include file search path for |
| 463 | this compilation only. |
| 464 | |
| 465 | 'debug' is a boolean; if true, the compiler will be instructed |
| 466 | to output debug symbols in (or alongside) the object file(s). |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 467 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 468 | 'extra_preargs' and 'extra_postargs' are implementation- |
| 469 | dependent. On platforms that have the notion of a command-line |
| 470 | (e.g. Unix, DOS/Windows), they are most likely lists of strings: |
| 471 | extra command-line arguments to prepand/append to the compiler |
| 472 | command line. On other platforms, consult the implementation |
| 473 | class documentation. In any event, they are intended as an |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 474 | escape hatch for those occasions when the abstract compiler |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 475 | framework doesn't cut the mustard. |
| 476 | |
| 477 | Raises CompileError on failure.""" |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 478 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 479 | pass |
| 480 | |
| 481 | |
Greg Ward | 036c805 | 2000-03-10 01:48:32 +0000 | [diff] [blame] | 482 | def create_static_lib (self, |
| 483 | objects, |
| 484 | output_libname, |
| 485 | output_dir=None, |
| 486 | debug=0): |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 487 | """Link a bunch of stuff together to create a static library |
| 488 | file. The "bunch of stuff" consists of the list of object |
| 489 | files supplied as 'objects', the extra object files supplied |
| 490 | to 'add_link_object()' and/or 'set_link_objects()', the |
| 491 | libraries supplied to 'add_library()' and/or |
| 492 | 'set_libraries()', and the libraries supplied as 'libraries' |
| 493 | (if any). |
| 494 | |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 495 | 'output_libname' should be a library name, not a filename; the |
| 496 | filename will be inferred from the library name. 'output_dir' |
| 497 | is the directory where the library file will be put. |
| 498 | |
| 499 | 'debug' is a boolean; if true, debugging information will be |
| 500 | included in the library (note that on most platforms, it is the |
| 501 | compile step where this matters: the 'debug' flag is included |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 502 | here just for consistency). |
| 503 | |
| 504 | Raises LibError on failure.""" |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 505 | |
| 506 | pass |
| 507 | |
| 508 | |
| 509 | def link_shared_lib (self, |
| 510 | objects, |
| 511 | output_libname, |
| 512 | output_dir=None, |
| 513 | libraries=None, |
| 514 | library_dirs=None, |
Greg Ward | f10f95d | 2000-03-26 21:37:09 +0000 | [diff] [blame] | 515 | runtime_library_dirs=None, |
Greg Ward | 5299b6a | 2000-05-20 13:23:21 +0000 | [diff] [blame] | 516 | export_symbols=None, |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 517 | debug=0, |
| 518 | extra_preargs=None, |
| 519 | extra_postargs=None): |
| 520 | """Link a bunch of stuff together to create a shared library |
Greg Ward | 036c805 | 2000-03-10 01:48:32 +0000 | [diff] [blame] | 521 | file. Similar semantics to 'create_static_lib()', with the |
| 522 | addition of other libraries to link against and directories to |
| 523 | search for them. Also, of course, the type and name of |
| 524 | the generated file will almost certainly be different, as will |
| 525 | the program used to create it. |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 526 | |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 527 | 'libraries' is a list of libraries to link against. These are |
| 528 | library names, not filenames, since they're translated into |
| 529 | filenames in a platform-specific way (eg. "foo" becomes |
| 530 | "libfoo.a" on Unix and "foo.lib" on DOS/Windows). However, they |
| 531 | can include a directory component, which means the linker will |
| 532 | look in that specific directory rather than searching all the |
| 533 | normal locations. |
| 534 | |
| 535 | 'library_dirs', if supplied, should be a list of directories to |
| 536 | search for libraries that were specified as bare library names |
| 537 | (ie. no directory component). These are on top of the system |
| 538 | default and those supplied to 'add_library_dir()' and/or |
Greg Ward | 5299b6a | 2000-05-20 13:23:21 +0000 | [diff] [blame] | 539 | 'set_library_dirs()'. 'runtime_library_dirs' is a list of |
| 540 | directories that will be embedded into the shared library and |
| 541 | used to search for other shared libraries that *it* depends on |
| 542 | at run-time. (This may only be relevant on Unix.) |
| 543 | |
| 544 | 'export_symbols' is a list of symbols that the shared library |
| 545 | will export. (This appears to be relevant only on Windows.) |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 546 | |
Greg Ward | 036c805 | 2000-03-10 01:48:32 +0000 | [diff] [blame] | 547 | 'debug' is as for 'compile()' and 'create_static_lib()', with the |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 548 | slight distinction that it actually matters on most platforms |
Greg Ward | 036c805 | 2000-03-10 01:48:32 +0000 | [diff] [blame] | 549 | (as opposed to 'create_static_lib()', which includes a 'debug' |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 550 | flag mostly for form's sake). |
| 551 | |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 552 | 'extra_preargs' and 'extra_postargs' are as for 'compile()' |
| 553 | (except of course that they supply command-line arguments |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 554 | for the particular linker being used). |
| 555 | |
| 556 | Raises LinkError on failure.""" |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 557 | |
| 558 | pass |
| 559 | |
| 560 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 561 | def link_shared_object (self, |
| 562 | objects, |
| 563 | output_filename, |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 564 | output_dir=None, |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 565 | libraries=None, |
Greg Ward | 26e48ea | 1999-08-29 18:17:36 +0000 | [diff] [blame] | 566 | library_dirs=None, |
Greg Ward | f10f95d | 2000-03-26 21:37:09 +0000 | [diff] [blame] | 567 | runtime_library_dirs=None, |
Greg Ward | 5299b6a | 2000-05-20 13:23:21 +0000 | [diff] [blame] | 568 | export_symbols=None, |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 569 | debug=0, |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 570 | extra_preargs=None, |
| 571 | extra_postargs=None): |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 572 | """Link a bunch of stuff together to create a shared object |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 573 | file. Much like 'link_shared_lib()', except the output filename |
| 574 | is explicitly supplied as 'output_filename'. If 'output_dir' is |
| 575 | supplied, 'output_filename' is relative to it |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 576 | (i.e. 'output_filename' can provide directory components if |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 577 | needed). |
| 578 | |
| 579 | Raises LinkError on failure.""" |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 580 | pass |
| 581 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 582 | |
Greg Ward | 5baf1c2 | 2000-01-09 22:41:02 +0000 | [diff] [blame] | 583 | def link_executable (self, |
| 584 | objects, |
| 585 | output_progname, |
| 586 | output_dir=None, |
| 587 | libraries=None, |
| 588 | library_dirs=None, |
Greg Ward | f10f95d | 2000-03-26 21:37:09 +0000 | [diff] [blame] | 589 | runtime_library_dirs=None, |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 590 | debug=0, |
Greg Ward | 5baf1c2 | 2000-01-09 22:41:02 +0000 | [diff] [blame] | 591 | extra_preargs=None, |
| 592 | extra_postargs=None): |
| 593 | """Link a bunch of stuff together to create a binary executable |
Greg Ward | 036c805 | 2000-03-10 01:48:32 +0000 | [diff] [blame] | 594 | file. The "bunch of stuff" is as for 'link_shared_lib()'. |
Greg Ward | 5baf1c2 | 2000-01-09 22:41:02 +0000 | [diff] [blame] | 595 | 'output_progname' should be the base name of the executable |
| 596 | program--e.g. on Unix the same as the output filename, but |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 597 | on DOS/Windows ".exe" will be appended. |
| 598 | |
| 599 | Raises LinkError on failure.""" |
Greg Ward | 5baf1c2 | 2000-01-09 22:41:02 +0000 | [diff] [blame] | 600 | pass |
| 601 | |
| 602 | |
| 603 | |
Greg Ward | f7edea7 | 2000-05-20 13:31:32 +0000 | [diff] [blame] | 604 | # -- Miscellaneous methods ----------------------------------------- |
| 605 | # These are all used by the 'gen_lib_options() function; there is |
| 606 | # no appropriate default implementation so subclasses should |
| 607 | # implement all of these. |
| 608 | |
| 609 | def library_dir_option (self, dir): |
| 610 | """Return the compiler option to add 'dir' to the list of directories |
| 611 | searched for libraries.""" |
| 612 | raise NotImplementedError |
| 613 | |
| 614 | def runtime_library_dir_option (self, dir): |
| 615 | """Return the compiler option to add 'dir' to the list of directories |
| 616 | searched for runtime libraries.""" |
| 617 | raise NotImplementedError |
| 618 | |
| 619 | def library_option (self, lib): |
| 620 | """Return the compiler option to add 'dir' to the list of libraries |
| 621 | linked into the shared library or executable.""" |
| 622 | raise NotImplementedError |
| 623 | |
| 624 | def find_library_file (self, dirs, lib): |
| 625 | """Search the specified list of directories for a static or shared |
| 626 | library file 'lib' and return the full path to that file. Return |
| 627 | None if it wasn't found in any of the specified directories.""" |
| 628 | raise NotImplementedError |
| 629 | |
| 630 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 631 | # -- Filename generation methods ----------------------------------- |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 632 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 633 | # The default implementation of the filename generating methods are |
| 634 | # prejudiced towards the Unix/DOS/Windows view of the world: |
| 635 | # * object files are named by replacing the source file extension |
| 636 | # (eg. .c/.cpp -> .o/.obj) |
| 637 | # * library files (shared or static) are named by plugging the |
| 638 | # library name and extension into a format string, eg. |
| 639 | # "lib%s.%s" % (lib_name, ".a") for Unix static libraries |
| 640 | # * executables are named by appending an extension (possibly |
| 641 | # empty) to the program name: eg. progname + ".exe" for |
| 642 | # Windows |
| 643 | # |
| 644 | # To reduce redundant code, these methods expect to find |
| 645 | # several attributes in the current object (presumably defined |
| 646 | # as class attributes): |
| 647 | # * src_extensions - |
| 648 | # list of C/C++ source file extensions, eg. ['.c', '.cpp'] |
| 649 | # * obj_extension - |
| 650 | # object file extension, eg. '.o' or '.obj' |
| 651 | # * static_lib_extension - |
| 652 | # extension for static library files, eg. '.a' or '.lib' |
| 653 | # * shared_lib_extension - |
| 654 | # extension for shared library/object files, eg. '.so', '.dll' |
| 655 | # * static_lib_format - |
| 656 | # format string for generating static library filenames, |
| 657 | # eg. 'lib%s.%s' or '%s.%s' |
| 658 | # * shared_lib_format |
| 659 | # format string for generating shared library filenames |
| 660 | # (probably same as static_lib_format, since the extension |
| 661 | # is one of the intended parameters to the format string) |
| 662 | # * exe_extension - |
| 663 | # extension for executable files, eg. '' or '.exe' |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 664 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 665 | def object_filenames (self, |
| 666 | source_filenames, |
| 667 | strip_dir=0, |
| 668 | output_dir=''): |
| 669 | if output_dir is None: output_dir = '' |
| 670 | obj_names = [] |
| 671 | for src_name in source_filenames: |
| 672 | (base, ext) = os.path.splitext (src_name) |
| 673 | if ext not in self.src_extensions: |
| 674 | continue |
| 675 | if strip_dir: |
| 676 | base = os.path.basename (base) |
| 677 | obj_names.append (os.path.join (output_dir, |
| 678 | base + self.obj_extension)) |
| 679 | return obj_names |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 680 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 681 | # object_filenames () |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 682 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 683 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 684 | def shared_object_filename (self, |
| 685 | basename, |
| 686 | strip_dir=0, |
| 687 | output_dir=''): |
| 688 | if output_dir is None: output_dir = '' |
| 689 | if strip_dir: |
| 690 | basename = os.path.basename (basename) |
| 691 | return os.path.join (output_dir, basename + self.shared_lib_extension) |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 692 | |
Greg Ward | 26e48ea | 1999-08-29 18:17:36 +0000 | [diff] [blame] | 693 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 694 | def library_filename (self, |
| 695 | libname, |
| 696 | lib_type='static', # or 'shared' |
| 697 | strip_dir=0, |
| 698 | output_dir=''): |
| 699 | |
| 700 | if output_dir is None: output_dir = '' |
| 701 | if lib_type not in ("static","shared"): |
| 702 | raise ValueError, "'lib_type' must be \"static\" or \"shared\"" |
| 703 | fmt = getattr (self, lib_type + "_lib_format") |
| 704 | ext = getattr (self, lib_type + "_lib_extension") |
| 705 | |
| 706 | (dir, base) = os.path.split (libname) |
| 707 | filename = fmt % (base, ext) |
| 708 | if strip_dir: |
| 709 | dir = '' |
| 710 | |
| 711 | return os.path.join (output_dir, dir, filename) |
| 712 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 713 | |
| 714 | # -- Utility methods ----------------------------------------------- |
| 715 | |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 716 | def announce (self, msg, level=1): |
| 717 | if self.verbose >= level: |
| 718 | print msg |
| 719 | |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 720 | def warn (self, msg): |
| 721 | sys.stderr.write ("warning: %s\n" % msg) |
| 722 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 723 | def spawn (self, cmd): |
| 724 | spawn (cmd, verbose=self.verbose, dry_run=self.dry_run) |
| 725 | |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 726 | def move_file (self, src, dst): |
| 727 | return move_file (src, dst, verbose=self.verbose, dry_run=self.dry_run) |
| 728 | |
Greg Ward | 013f0c8 | 2000-03-01 14:43:12 +0000 | [diff] [blame] | 729 | def mkpath (self, name, mode=0777): |
| 730 | mkpath (name, mode, self.verbose, self.dry_run) |
| 731 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 732 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 733 | # class CCompiler |
| 734 | |
| 735 | |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 736 | # Map a platform ('posix', 'nt') to the default compiler type for |
| 737 | # that platform. |
| 738 | default_compiler = { 'posix': 'unix', |
| 739 | 'nt': 'msvc', |
| 740 | } |
| 741 | |
| 742 | # Map compiler types to (module_name, class_name) pairs -- ie. where to |
| 743 | # find the code that implements an interface to this compiler. (The module |
| 744 | # is assumed to be in the 'distutils' package.) |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame^] | 745 | compiler_class = { 'unix': ('unixccompiler', 'UnixCCompiler', |
| 746 | "standard UNIX-style compiler"), |
| 747 | 'msvc': ('msvccompiler', 'MSVCCompiler', |
| 748 | "Microsoft Visual C++"), |
| 749 | 'cygwin': ('cygwinccompiler', 'CygwinCCompiler', |
| 750 | "Cygwin port of GNU C Compiler for Win32"), |
| 751 | 'mingw32': ('cygwinccompiler', 'Mingw32CCompiler', |
| 752 | "Mingw32 port of GNU C Compiler for Win32"), |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Greg Ward | 9d17a7a | 2000-06-07 03:00:06 +0000 | [diff] [blame] | 755 | def show_compilers(): |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame^] | 756 | """Print list of available compilers (used by the "--help-compiler" |
| 757 | options to "build", "build_ext", "build_clib"). |
| 758 | """ |
| 759 | # XXX this "knows" that the compiler option it's describing is |
| 760 | # "--compiler", which just happens to be the case for the three |
| 761 | # commands that use it. |
Greg Ward | 9d17a7a | 2000-06-07 03:00:06 +0000 | [diff] [blame] | 762 | from distutils.fancy_getopt import FancyGetopt |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame^] | 763 | compilers = [] |
Greg Ward | 9d17a7a | 2000-06-07 03:00:06 +0000 | [diff] [blame] | 764 | for compiler in compiler_class.keys(): |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame^] | 765 | compilers.append(("compiler="+compiler, None, |
| 766 | compiler_class[compiler][2])) |
| 767 | compilers.sort() |
| 768 | pretty_printer = FancyGetopt(compilers) |
Greg Ward | 9d17a7a | 2000-06-07 03:00:06 +0000 | [diff] [blame] | 769 | pretty_printer.print_help("List of available compilers:") |
| 770 | |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 771 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 772 | def new_compiler (plat=None, |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 773 | compiler=None, |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 774 | verbose=0, |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 775 | dry_run=0, |
| 776 | force=0): |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 777 | |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 778 | """Generate an instance of some CCompiler subclass for the supplied |
| 779 | platform/compiler combination. 'plat' defaults to 'os.name' |
| 780 | (eg. 'posix', 'nt'), and 'compiler' defaults to the default |
| 781 | compiler for that platform. Currently only 'posix' and 'nt' |
| 782 | are supported, and the default compilers are "traditional Unix |
| 783 | interface" (UnixCCompiler class) and Visual C++ (MSVCCompiler |
| 784 | class). Note that it's perfectly possible to ask for a Unix |
| 785 | compiler object under Windows, and a Microsoft compiler object |
| 786 | under Unix -- if you supply a value for 'compiler', 'plat' |
| 787 | is ignored.""" |
| 788 | |
| 789 | if plat is None: |
| 790 | plat = os.name |
| 791 | |
| 792 | try: |
| 793 | if compiler is None: |
| 794 | compiler = default_compiler[plat] |
| 795 | |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame^] | 796 | (module_name, class_name, long_description) = compiler_class[compiler] |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 797 | except KeyError: |
| 798 | msg = "don't know how to compile C/C++ code on platform '%s'" % plat |
| 799 | if compiler is not None: |
| 800 | msg = msg + " with '%s' compiler" % compiler |
| 801 | raise DistutilsPlatformError, msg |
| 802 | |
| 803 | try: |
| 804 | module_name = "distutils." + module_name |
| 805 | __import__ (module_name) |
| 806 | module = sys.modules[module_name] |
| 807 | klass = vars(module)[class_name] |
| 808 | except ImportError: |
| 809 | raise DistutilsModuleError, \ |
| 810 | "can't compile C/C++ code: unable to load module '%s'" % \ |
| 811 | module_name |
| 812 | except KeyError: |
| 813 | raise DistutilsModuleError, \ |
| 814 | ("can't compile C/C++ code: unable to find class '%s' " + |
| 815 | "in module '%s'") % (class_name, module_name) |
| 816 | |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 817 | return klass (verbose, dry_run, force) |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 818 | |
| 819 | |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 820 | def gen_preprocess_options (macros, include_dirs): |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 821 | """Generate C pre-processor options (-D, -U, -I) as used by at |
| 822 | least two types of compilers: the typical Unix compiler and Visual |
| 823 | C++. 'macros' is the usual thing, a list of 1- or 2-tuples, where |
| 824 | (name,) means undefine (-U) macro 'name', and (name,value) means |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 825 | define (-D) macro 'name' to 'value'. 'include_dirs' is just a list of |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 826 | directory names to be added to the header file search path (-I). |
| 827 | Returns a list of command-line options suitable for either |
| 828 | Unix compilers or Visual C++.""" |
| 829 | |
| 830 | # XXX it would be nice (mainly aesthetic, and so we don't generate |
| 831 | # stupid-looking command lines) to go over 'macros' and eliminate |
| 832 | # redundant definitions/undefinitions (ie. ensure that only the |
| 833 | # latest mention of a particular macro winds up on the command |
| 834 | # line). I don't think it's essential, though, since most (all?) |
| 835 | # Unix C compilers only pay attention to the latest -D or -U |
| 836 | # mention of a macro on their command line. Similar situation for |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 837 | # 'include_dirs'. I'm punting on both for now. Anyways, weeding out |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 838 | # redundancies like this should probably be the province of |
| 839 | # CCompiler, since the data structures used are inherited from it |
| 840 | # and therefore common to all CCompiler classes. |
| 841 | |
| 842 | pp_opts = [] |
| 843 | for macro in macros: |
Greg Ward | fbf8aff | 1999-09-21 18:35:09 +0000 | [diff] [blame] | 844 | |
| 845 | if not (type (macro) is TupleType and |
| 846 | 1 <= len (macro) <= 2): |
| 847 | raise TypeError, \ |
| 848 | ("bad macro definition '%s': " + |
| 849 | "each element of 'macros' list must be a 1- or 2-tuple") % \ |
| 850 | macro |
| 851 | |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 852 | if len (macro) == 1: # undefine this macro |
| 853 | pp_opts.append ("-U%s" % macro[0]) |
| 854 | elif len (macro) == 2: |
| 855 | if macro[1] is None: # define with no explicit value |
| 856 | pp_opts.append ("-D%s" % macro[0]) |
| 857 | else: |
| 858 | # XXX *don't* need to be clever about quoting the |
| 859 | # macro value here, because we're going to avoid the |
| 860 | # shell at all costs when we spawn the command! |
| 861 | pp_opts.append ("-D%s=%s" % macro) |
| 862 | |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 863 | for dir in include_dirs: |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 864 | pp_opts.append ("-I%s" % dir) |
| 865 | |
| 866 | return pp_opts |
| 867 | |
| 868 | # gen_preprocess_options () |
| 869 | |
| 870 | |
Greg Ward | d03f88a | 2000-03-18 15:19:51 +0000 | [diff] [blame] | 871 | def gen_lib_options (compiler, library_dirs, runtime_library_dirs, libraries): |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 872 | """Generate linker options for searching library directories and |
| 873 | linking with specific libraries. 'libraries' and 'library_dirs' |
| 874 | are, respectively, lists of library names (not filenames!) and |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 875 | search directories. Returns a list of command-line options suitable |
| 876 | for use with some compiler (depending on the two format strings |
| 877 | passed in).""" |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 878 | |
| 879 | lib_opts = [] |
| 880 | |
| 881 | for dir in library_dirs: |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 882 | lib_opts.append (compiler.library_dir_option (dir)) |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 883 | |
Greg Ward | d03f88a | 2000-03-18 15:19:51 +0000 | [diff] [blame] | 884 | for dir in runtime_library_dirs: |
| 885 | lib_opts.append (compiler.runtime_library_dir_option (dir)) |
| 886 | |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 887 | # XXX it's important that we *not* remove redundant library mentions! |
| 888 | # sometimes you really do have to say "-lfoo -lbar -lfoo" in order to |
| 889 | # resolve all symbols. I just hope we never have to say "-lfoo obj.o |
| 890 | # -lbar" to get things to work -- that's certainly a possibility, but a |
| 891 | # pretty nasty way to arrange your C code. |
| 892 | |
| 893 | for lib in libraries: |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 894 | (lib_dir, lib_name) = os.path.split (lib) |
| 895 | if lib_dir: |
| 896 | lib_file = compiler.find_library_file ([lib_dir], lib_name) |
| 897 | if lib_file: |
| 898 | lib_opts.append (lib_file) |
| 899 | else: |
| 900 | compiler.warn ("no library file corresponding to " |
| 901 | "'%s' found (skipping)" % lib) |
| 902 | else: |
| 903 | lib_opts.append (compiler.library_option (lib)) |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 904 | |
| 905 | return lib_opts |
| 906 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 907 | # gen_lib_options () |