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 | |
| 8 | __rcsid__ = "$Id$" |
| 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 | 013f0c8 | 2000-03-01 14:43:12 +0000 | [diff] [blame^] | 15 | from distutils.util import move_file, mkpath |
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 | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 68 | def __init__ (self, |
| 69 | verbose=0, |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 70 | dry_run=0, |
| 71 | force=0): |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 72 | |
| 73 | self.verbose = verbose |
| 74 | self.dry_run = dry_run |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 75 | self.force = force |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 76 | |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 77 | # 'output_dir': a common output directory for object, library, |
| 78 | # shared object, and shared library files |
| 79 | self.output_dir = None |
| 80 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 81 | # 'macros': a list of macro definitions (or undefinitions). A |
| 82 | # macro definition is a 2-tuple (name, value), where the value is |
| 83 | # either a string or None (no explicit value). A macro |
| 84 | # undefinition is a 1-tuple (name,). |
| 85 | self.macros = [] |
| 86 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 87 | # 'include_dirs': a list of directories to search for include files |
| 88 | self.include_dirs = [] |
| 89 | |
| 90 | # 'libraries': a list of libraries to include in any link |
| 91 | # (library names, not filenames: eg. "foo" not "libfoo.a") |
| 92 | self.libraries = [] |
| 93 | |
| 94 | # 'library_dirs': a list of directories to search for libraries |
| 95 | self.library_dirs = [] |
| 96 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 97 | # 'runtime_library_dirs': a list of directories to search for |
| 98 | # shared libraries/objects at runtime |
| 99 | self.runtime_library_dirs = [] |
| 100 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 101 | # 'objects': a list of object files (or similar, such as explicitly |
| 102 | # named library files) to include on any link |
| 103 | self.objects = [] |
| 104 | |
| 105 | # __init__ () |
| 106 | |
| 107 | |
| 108 | def _find_macro (self, name): |
| 109 | i = 0 |
| 110 | for defn in self.macros: |
| 111 | if defn[0] == name: |
| 112 | return i |
| 113 | i = i + 1 |
| 114 | |
| 115 | return None |
| 116 | |
| 117 | |
| 118 | def _check_macro_definitions (self, definitions): |
| 119 | """Ensures that every element of 'definitions' is a valid macro |
| 120 | definition, ie. either (name,value) 2-tuple or a (name,) |
| 121 | tuple. Do nothing if all definitions are OK, raise |
| 122 | TypeError otherwise.""" |
| 123 | |
| 124 | for defn in definitions: |
| 125 | if not (type (defn) is TupleType and |
| 126 | (len (defn) == 1 or |
| 127 | (len (defn) == 2 and |
| 128 | (type (defn[1]) is StringType or defn[1] is None))) and |
| 129 | type (defn[0]) is StringType): |
| 130 | raise TypeError, \ |
| 131 | ("invalid macro definition '%s': " % defn) + \ |
| 132 | "must be tuple (string,), (string, string), or " + \ |
| 133 | "(string, None)" |
| 134 | |
| 135 | |
| 136 | # -- Bookkeeping methods ------------------------------------------- |
| 137 | |
| 138 | def define_macro (self, name, value=None): |
| 139 | """Define a preprocessor macro for all compilations driven by |
| 140 | this compiler object. The optional parameter 'value' should be |
| 141 | a string; if it is not supplied, then the macro will be defined |
| 142 | without an explicit value and the exact outcome depends on the |
| 143 | compiler used (XXX true? does ANSI say anything about this?)""" |
| 144 | |
| 145 | # Delete from the list of macro definitions/undefinitions if |
| 146 | # already there (so that this one will take precedence). |
| 147 | i = self._find_macro (name) |
| 148 | if i is not None: |
| 149 | del self.macros[i] |
| 150 | |
| 151 | defn = (name, value) |
| 152 | self.macros.append (defn) |
| 153 | |
| 154 | |
| 155 | def undefine_macro (self, name): |
| 156 | """Undefine a preprocessor macro for all compilations driven by |
| 157 | this compiler object. If the same macro is defined by |
| 158 | 'define_macro()' and undefined by 'undefine_macro()' the last |
| 159 | call takes precedence (including multiple redefinitions or |
| 160 | undefinitions). If the macro is redefined/undefined on a |
| 161 | per-compilation basis (ie. in the call to 'compile()'), then |
| 162 | that takes precedence.""" |
| 163 | |
| 164 | # Delete from the list of macro definitions/undefinitions if |
| 165 | # already there (so that this one will take precedence). |
| 166 | i = self._find_macro (name) |
| 167 | if i is not None: |
| 168 | del self.macros[i] |
| 169 | |
| 170 | undefn = (name,) |
| 171 | self.macros.append (undefn) |
| 172 | |
| 173 | |
| 174 | def add_include_dir (self, dir): |
| 175 | """Add 'dir' to the list of directories that will be searched |
| 176 | for header files. The compiler is instructed to search |
| 177 | directories in the order in which they are supplied by |
| 178 | successive calls to 'add_include_dir()'.""" |
| 179 | self.include_dirs.append (dir) |
| 180 | |
| 181 | def set_include_dirs (self, dirs): |
| 182 | """Set the list of directories that will be searched to 'dirs' |
| 183 | (a list of strings). Overrides any preceding calls to |
| 184 | 'add_include_dir()'; subsequence calls to 'add_include_dir()' |
| 185 | add to the list passed to 'set_include_dirs()'. This does |
| 186 | not affect any list of standard include directories that |
| 187 | the compiler may search by default.""" |
| 188 | self.include_dirs = copy (dirs) |
| 189 | |
| 190 | |
| 191 | def add_library (self, libname): |
| 192 | """Add 'libname' to the list of libraries that will be included |
| 193 | in all links driven by this compiler object. Note that |
| 194 | 'libname' should *not* be the name of a file containing a |
| 195 | library, but the name of the library itself: the actual filename |
| 196 | will be inferred by the linker, the compiler, or the compiler |
| 197 | abstraction class (depending on the platform). |
| 198 | |
| 199 | The linker will be instructed to link against libraries in the |
| 200 | order they were supplied to 'add_library()' and/or |
| 201 | 'set_libraries()'. It is perfectly valid to duplicate library |
| 202 | names; the linker will be instructed to link against libraries |
| 203 | as many times as they are mentioned.""" |
| 204 | self.libraries.append (libname) |
| 205 | |
| 206 | def set_libraries (self, libnames): |
| 207 | """Set the list of libraries to be included in all links driven |
| 208 | by this compiler object to 'libnames' (a list of strings). |
| 209 | This does not affect any standard system libraries that the |
| 210 | linker may include by default.""" |
| 211 | |
| 212 | self.libraries = copy (libnames) |
| 213 | |
| 214 | |
| 215 | def add_library_dir (self, dir): |
| 216 | """Add 'dir' to the list of directories that will be searched for |
| 217 | libraries specified to 'add_library()' and 'set_libraries()'. |
| 218 | The linker will be instructed to search for libraries in the |
| 219 | order they are supplied to 'add_library_dir()' and/or |
| 220 | 'set_library_dirs()'.""" |
| 221 | self.library_dirs.append (dir) |
| 222 | |
| 223 | def set_library_dirs (self, dirs): |
| 224 | """Set the list of library search directories to 'dirs' (a list |
| 225 | of strings). This does not affect any standard library |
| 226 | search path that the linker may search by default.""" |
| 227 | self.library_dirs = copy (dirs) |
| 228 | |
| 229 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 230 | def add_runtime_library_dir (self, dir): |
| 231 | """Add 'dir' to the list of directories that will be searched for |
| 232 | shared libraries at runtime.""" |
| 233 | self.runtime_library_dirs.append (dir) |
| 234 | |
| 235 | def set_runtime_library_dirs (self, dirs): |
| 236 | """Set the list of directories to search for shared libraries |
| 237 | at runtime to 'dirs' (a list of strings). This does not affect |
| 238 | any standard search path that the runtime linker may search by |
| 239 | default.""" |
| 240 | self.runtime_library_dirs = copy (dirs) |
| 241 | |
| 242 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 243 | def add_link_object (self, object): |
| 244 | """Add 'object' to the list of object files (or analogues, such |
| 245 | as explictly named library files or the output of "resource |
| 246 | compilers") to be included in every link driven by this |
| 247 | compiler object.""" |
| 248 | self.objects.append (object) |
| 249 | |
| 250 | def set_link_objects (self, objects): |
| 251 | """Set the list of object files (or analogues) to be included |
| 252 | in every link to 'objects'. This does not affect any |
| 253 | standard object files that the linker may include by default |
| 254 | (such as system libraries).""" |
| 255 | self.objects = copy (objects) |
| 256 | |
| 257 | |
| 258 | # -- Worker methods ------------------------------------------------ |
| 259 | # (must be implemented by subclasses) |
| 260 | |
| 261 | def compile (self, |
| 262 | sources, |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 263 | output_dir=None, |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 264 | macros=None, |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 265 | include_dirs=None, |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 266 | debug=0, |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 267 | extra_preargs=None, |
| 268 | extra_postargs=None): |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 269 | """Compile one or more C/C++ source files. 'sources' must be |
| 270 | a list of strings, each one the name of a C/C++ source |
| 271 | file. Return a list of the object filenames generated |
| 272 | (one for each source filename in 'sources'). |
| 273 | |
| 274 | 'macros', if given, must be a list of macro definitions. A |
| 275 | macro definition is either a (name, value) 2-tuple or a (name,) |
| 276 | 1-tuple. The former defines a macro; if the value is None, the |
| 277 | macro is defined without an explicit value. The 1-tuple case |
| 278 | undefines a macro. Later definitions/redefinitions/ |
| 279 | undefinitions take precedence. |
| 280 | |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 281 | 'include_dirs', if given, must be a list of strings, the |
| 282 | directories to add to the default include file search path for |
| 283 | this compilation only. |
| 284 | |
| 285 | 'debug' is a boolean; if true, the compiler will be instructed |
| 286 | to output debug symbols in (or alongside) the object file(s). |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 287 | |
| 288 | 'extra_preargs' and 'extra_postargs' are optional lists of extra |
| 289 | command-line arguments that will be, respectively, prepended or |
| 290 | appended to the generated command line immediately before |
| 291 | execution. These will most likely be peculiar to the particular |
| 292 | platform and compiler being worked with, but are a necessary |
| 293 | escape hatch for those occasions when the abstract compiler |
| 294 | framework doesn't cut the mustard.""" |
| 295 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 296 | pass |
| 297 | |
| 298 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 299 | def link_static_lib (self, |
| 300 | objects, |
| 301 | output_libname, |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 302 | output_dir=None, |
| 303 | debug=0): |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 304 | """Link a bunch of stuff together to create a static library |
| 305 | file. The "bunch of stuff" consists of the list of object |
| 306 | files supplied as 'objects', the extra object files supplied |
| 307 | to 'add_link_object()' and/or 'set_link_objects()', the |
| 308 | libraries supplied to 'add_library()' and/or |
| 309 | 'set_libraries()', and the libraries supplied as 'libraries' |
| 310 | (if any). |
| 311 | |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 312 | 'output_libname' should be a library name, not a filename; the |
| 313 | filename will be inferred from the library name. 'output_dir' |
| 314 | is the directory where the library file will be put. |
| 315 | |
| 316 | 'debug' is a boolean; if true, debugging information will be |
| 317 | included in the library (note that on most platforms, it is the |
| 318 | compile step where this matters: the 'debug' flag is included |
| 319 | here just for consistency).""" |
| 320 | |
| 321 | pass |
| 322 | |
| 323 | |
| 324 | def link_shared_lib (self, |
| 325 | objects, |
| 326 | output_libname, |
| 327 | output_dir=None, |
| 328 | libraries=None, |
| 329 | library_dirs=None, |
| 330 | debug=0, |
| 331 | extra_preargs=None, |
| 332 | extra_postargs=None): |
| 333 | """Link a bunch of stuff together to create a shared library |
| 334 | file. Has the same effect as 'link_static_lib()' except |
| 335 | that the filename inferred from 'output_libname' will most |
| 336 | likely be different, and the type of file generated will |
| 337 | almost certainly be different |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 338 | |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 339 | 'libraries' is a list of libraries to link against. These are |
| 340 | library names, not filenames, since they're translated into |
| 341 | filenames in a platform-specific way (eg. "foo" becomes |
| 342 | "libfoo.a" on Unix and "foo.lib" on DOS/Windows). However, they |
| 343 | can include a directory component, which means the linker will |
| 344 | look in that specific directory rather than searching all the |
| 345 | normal locations. |
| 346 | |
| 347 | 'library_dirs', if supplied, should be a list of directories to |
| 348 | search for libraries that were specified as bare library names |
| 349 | (ie. no directory component). These are on top of the system |
| 350 | default and those supplied to 'add_library_dir()' and/or |
| 351 | 'set_library_dirs()'. |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 352 | |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 353 | 'debug' is as for 'compile()' and 'link_static_lib()', with the |
| 354 | slight distinction that it actually matters on most platforms |
| 355 | (as opposed to 'link_static_lib()', which includes a 'debug' |
| 356 | flag mostly for form's sake). |
| 357 | |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 358 | 'extra_preargs' and 'extra_postargs' are as for 'compile()' |
| 359 | (except of course that they supply command-line arguments |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 360 | for the particular linker being used).""" |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 361 | |
| 362 | pass |
| 363 | |
| 364 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 365 | def link_shared_object (self, |
| 366 | objects, |
| 367 | output_filename, |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 368 | output_dir=None, |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 369 | libraries=None, |
Greg Ward | 26e48ea | 1999-08-29 18:17:36 +0000 | [diff] [blame] | 370 | library_dirs=None, |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 371 | debug=0, |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 372 | extra_preargs=None, |
| 373 | extra_postargs=None): |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 374 | """Link a bunch of stuff together to create a shared object |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 375 | file. Much like 'link_shared_lib()', except the output filename |
| 376 | is explicitly supplied as 'output_filename'. If 'output_dir' is |
| 377 | supplied, 'output_filename' is relative to it |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 378 | (i.e. 'output_filename' can provide directory components if |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 379 | needed).""" |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 380 | pass |
| 381 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 382 | |
Greg Ward | 5baf1c2 | 2000-01-09 22:41:02 +0000 | [diff] [blame] | 383 | def link_executable (self, |
| 384 | objects, |
| 385 | output_progname, |
| 386 | output_dir=None, |
| 387 | libraries=None, |
| 388 | library_dirs=None, |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 389 | debug=0, |
Greg Ward | 5baf1c2 | 2000-01-09 22:41:02 +0000 | [diff] [blame] | 390 | extra_preargs=None, |
| 391 | extra_postargs=None): |
| 392 | """Link a bunch of stuff together to create a binary executable |
| 393 | file. The "bunch of stuff" is as for 'link_static_lib()'. |
| 394 | 'output_progname' should be the base name of the executable |
| 395 | program--e.g. on Unix the same as the output filename, but |
| 396 | on DOS/Windows ".exe" will be appended.""" |
| 397 | pass |
| 398 | |
| 399 | |
| 400 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 401 | # -- Filename mangling methods ------------------------------------- |
| 402 | |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 403 | # General principle for the filename-mangling methods: by default, |
| 404 | # don't include a directory component, no matter what the caller |
| 405 | # supplies. Eg. for UnixCCompiler, a source file of "foo/bar/baz.c" |
| 406 | # becomes "baz.o" or "baz.so", etc. (That way, it's easiest for the |
| 407 | # caller to decide where it wants to put/find the output file.) The |
| 408 | # 'output_dir' parameter overrides this, of course -- the directory |
| 409 | # component of the input filenames is replaced by 'output_dir'. |
| 410 | |
| 411 | def object_filenames (self, source_filenames, output_dir=None): |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 412 | """Return the list of object filenames corresponding to each |
| 413 | specified source filename.""" |
| 414 | pass |
| 415 | |
Greg Ward | 26e48ea | 1999-08-29 18:17:36 +0000 | [diff] [blame] | 416 | def shared_object_filename (self, source_filename): |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 417 | """Return the shared object filename corresponding to a |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 418 | specified source filename (assuming the same directory).""" |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 419 | pass |
| 420 | |
Greg Ward | 26e48ea | 1999-08-29 18:17:36 +0000 | [diff] [blame] | 421 | def library_filename (self, libname): |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 422 | """Return the static library filename corresponding to the |
| 423 | specified library name.""" |
| 424 | |
| 425 | pass |
| 426 | |
Greg Ward | 26e48ea | 1999-08-29 18:17:36 +0000 | [diff] [blame] | 427 | def shared_library_filename (self, libname): |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 428 | """Return the shared library filename corresponding to the |
| 429 | specified library name.""" |
| 430 | pass |
| 431 | |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 432 | # XXX ugh -- these should go! |
Greg Ward | 26e48ea | 1999-08-29 18:17:36 +0000 | [diff] [blame] | 433 | def object_name (self, inname): |
| 434 | """Given a name with no extension, return the name + object extension""" |
| 435 | return inname + self._obj_ext |
| 436 | |
| 437 | def shared_library_name (self, inname): |
| 438 | """Given a name with no extension, return the name + shared object extension""" |
| 439 | return inname + self._shared_lib_ext |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 440 | |
| 441 | # -- Utility methods ----------------------------------------------- |
| 442 | |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 443 | def announce (self, msg, level=1): |
| 444 | if self.verbose >= level: |
| 445 | print msg |
| 446 | |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 447 | def warn (self, msg): |
| 448 | sys.stderr.write ("warning: %s\n" % msg) |
| 449 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 450 | def spawn (self, cmd): |
| 451 | spawn (cmd, verbose=self.verbose, dry_run=self.dry_run) |
| 452 | |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 453 | def move_file (self, src, dst): |
| 454 | return move_file (src, dst, verbose=self.verbose, dry_run=self.dry_run) |
| 455 | |
Greg Ward | 013f0c8 | 2000-03-01 14:43:12 +0000 | [diff] [blame^] | 456 | def mkpath (self, name, mode=0777): |
| 457 | mkpath (name, mode, self.verbose, self.dry_run) |
| 458 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 459 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 460 | # class CCompiler |
| 461 | |
| 462 | |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 463 | # Map a platform ('posix', 'nt') to the default compiler type for |
| 464 | # that platform. |
| 465 | default_compiler = { 'posix': 'unix', |
| 466 | 'nt': 'msvc', |
| 467 | } |
| 468 | |
| 469 | # Map compiler types to (module_name, class_name) pairs -- ie. where to |
| 470 | # find the code that implements an interface to this compiler. (The module |
| 471 | # is assumed to be in the 'distutils' package.) |
| 472 | compiler_class = { 'unix': ('unixccompiler', 'UnixCCompiler'), |
| 473 | 'msvc': ('msvccompiler', 'MSVCCompiler'), |
| 474 | } |
| 475 | |
| 476 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 477 | def new_compiler (plat=None, |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 478 | compiler=None, |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 479 | verbose=0, |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 480 | dry_run=0, |
| 481 | force=0): |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 482 | |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 483 | """Generate an instance of some CCompiler subclass for the supplied |
| 484 | platform/compiler combination. 'plat' defaults to 'os.name' |
| 485 | (eg. 'posix', 'nt'), and 'compiler' defaults to the default |
| 486 | compiler for that platform. Currently only 'posix' and 'nt' |
| 487 | are supported, and the default compilers are "traditional Unix |
| 488 | interface" (UnixCCompiler class) and Visual C++ (MSVCCompiler |
| 489 | class). Note that it's perfectly possible to ask for a Unix |
| 490 | compiler object under Windows, and a Microsoft compiler object |
| 491 | under Unix -- if you supply a value for 'compiler', 'plat' |
| 492 | is ignored.""" |
| 493 | |
| 494 | if plat is None: |
| 495 | plat = os.name |
| 496 | |
| 497 | try: |
| 498 | if compiler is None: |
| 499 | compiler = default_compiler[plat] |
| 500 | |
| 501 | (module_name, class_name) = compiler_class[compiler] |
| 502 | except KeyError: |
| 503 | msg = "don't know how to compile C/C++ code on platform '%s'" % plat |
| 504 | if compiler is not None: |
| 505 | msg = msg + " with '%s' compiler" % compiler |
| 506 | raise DistutilsPlatformError, msg |
| 507 | |
| 508 | try: |
| 509 | module_name = "distutils." + module_name |
| 510 | __import__ (module_name) |
| 511 | module = sys.modules[module_name] |
| 512 | klass = vars(module)[class_name] |
| 513 | except ImportError: |
| 514 | raise DistutilsModuleError, \ |
| 515 | "can't compile C/C++ code: unable to load module '%s'" % \ |
| 516 | module_name |
| 517 | except KeyError: |
| 518 | raise DistutilsModuleError, \ |
| 519 | ("can't compile C/C++ code: unable to find class '%s' " + |
| 520 | "in module '%s'") % (class_name, module_name) |
| 521 | |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 522 | return klass (verbose, dry_run, force) |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 523 | |
| 524 | |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 525 | def gen_preprocess_options (macros, include_dirs): |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 526 | """Generate C pre-processor options (-D, -U, -I) as used by at |
| 527 | least two types of compilers: the typical Unix compiler and Visual |
| 528 | C++. 'macros' is the usual thing, a list of 1- or 2-tuples, where |
| 529 | (name,) means undefine (-U) macro 'name', and (name,value) means |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 530 | 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] | 531 | directory names to be added to the header file search path (-I). |
| 532 | Returns a list of command-line options suitable for either |
| 533 | Unix compilers or Visual C++.""" |
| 534 | |
| 535 | # XXX it would be nice (mainly aesthetic, and so we don't generate |
| 536 | # stupid-looking command lines) to go over 'macros' and eliminate |
| 537 | # redundant definitions/undefinitions (ie. ensure that only the |
| 538 | # latest mention of a particular macro winds up on the command |
| 539 | # line). I don't think it's essential, though, since most (all?) |
| 540 | # Unix C compilers only pay attention to the latest -D or -U |
| 541 | # mention of a macro on their command line. Similar situation for |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 542 | # 'include_dirs'. I'm punting on both for now. Anyways, weeding out |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 543 | # redundancies like this should probably be the province of |
| 544 | # CCompiler, since the data structures used are inherited from it |
| 545 | # and therefore common to all CCompiler classes. |
| 546 | |
| 547 | pp_opts = [] |
| 548 | for macro in macros: |
Greg Ward | fbf8aff | 1999-09-21 18:35:09 +0000 | [diff] [blame] | 549 | |
| 550 | if not (type (macro) is TupleType and |
| 551 | 1 <= len (macro) <= 2): |
| 552 | raise TypeError, \ |
| 553 | ("bad macro definition '%s': " + |
| 554 | "each element of 'macros' list must be a 1- or 2-tuple") % \ |
| 555 | macro |
| 556 | |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 557 | if len (macro) == 1: # undefine this macro |
| 558 | pp_opts.append ("-U%s" % macro[0]) |
| 559 | elif len (macro) == 2: |
| 560 | if macro[1] is None: # define with no explicit value |
| 561 | pp_opts.append ("-D%s" % macro[0]) |
| 562 | else: |
| 563 | # XXX *don't* need to be clever about quoting the |
| 564 | # macro value here, because we're going to avoid the |
| 565 | # shell at all costs when we spawn the command! |
| 566 | pp_opts.append ("-D%s=%s" % macro) |
| 567 | |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 568 | for dir in include_dirs: |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 569 | pp_opts.append ("-I%s" % dir) |
| 570 | |
| 571 | return pp_opts |
| 572 | |
| 573 | # gen_preprocess_options () |
| 574 | |
| 575 | |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 576 | def gen_lib_options (compiler, library_dirs, libraries): |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 577 | """Generate linker options for searching library directories and |
| 578 | linking with specific libraries. 'libraries' and 'library_dirs' |
| 579 | are, respectively, lists of library names (not filenames!) and |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 580 | search directories. Returns a list of command-line options suitable |
| 581 | for use with some compiler (depending on the two format strings |
| 582 | passed in).""" |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 583 | |
| 584 | lib_opts = [] |
| 585 | |
| 586 | for dir in library_dirs: |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 587 | lib_opts.append (compiler.library_dir_option (dir)) |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 588 | |
| 589 | # XXX it's important that we *not* remove redundant library mentions! |
| 590 | # sometimes you really do have to say "-lfoo -lbar -lfoo" in order to |
| 591 | # resolve all symbols. I just hope we never have to say "-lfoo obj.o |
| 592 | # -lbar" to get things to work -- that's certainly a possibility, but a |
| 593 | # pretty nasty way to arrange your C code. |
| 594 | |
| 595 | for lib in libraries: |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 596 | (lib_dir, lib_name) = os.path.split (lib) |
| 597 | if lib_dir: |
| 598 | lib_file = compiler.find_library_file ([lib_dir], lib_name) |
| 599 | if lib_file: |
| 600 | lib_opts.append (lib_file) |
| 601 | else: |
| 602 | compiler.warn ("no library file corresponding to " |
| 603 | "'%s' found (skipping)" % lib) |
| 604 | else: |
| 605 | lib_opts.append (compiler.library_option (lib)) |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 606 | |
| 607 | return lib_opts |
| 608 | |
| 609 | # _gen_lib_options () |