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