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