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 | |
Marc-André Lemburg | 636b906 | 2001-02-19 09:20:04 +0000 | [diff] [blame] | 10 | import sys, os, re |
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 |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 19 | from distutils import log |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 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. |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 64 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 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 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 83 | self.dry_run = dry_run |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 84 | self.force = force |
Skip Montanaro | 70e1d9b | 2002-10-01 17:39:59 +0000 | [diff] [blame^] | 85 | self.verbose = verbose |
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) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 162 | |
Greg Ward | e5c62bf | 2000-06-25 02:08:18 +0000 | [diff] [blame] | 163 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 164 | def _find_macro (self, name): |
| 165 | i = 0 |
| 166 | for defn in self.macros: |
| 167 | if defn[0] == name: |
| 168 | return i |
| 169 | i = i + 1 |
| 170 | |
| 171 | return None |
| 172 | |
| 173 | |
| 174 | def _check_macro_definitions (self, definitions): |
| 175 | """Ensures that every element of 'definitions' is a valid macro |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 176 | definition, ie. either (name,value) 2-tuple or a (name,) tuple. Do |
| 177 | nothing if all definitions are OK, raise TypeError otherwise. |
| 178 | """ |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 179 | for defn in definitions: |
| 180 | if not (type (defn) is TupleType and |
| 181 | (len (defn) == 1 or |
| 182 | (len (defn) == 2 and |
| 183 | (type (defn[1]) is StringType or defn[1] is None))) and |
| 184 | type (defn[0]) is StringType): |
| 185 | raise TypeError, \ |
| 186 | ("invalid macro definition '%s': " % defn) + \ |
| 187 | "must be tuple (string,), (string, string), or " + \ |
| 188 | "(string, None)" |
| 189 | |
| 190 | |
| 191 | # -- Bookkeeping methods ------------------------------------------- |
| 192 | |
| 193 | def define_macro (self, name, value=None): |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 194 | """Define a preprocessor macro for all compilations driven by this |
| 195 | compiler object. The optional parameter 'value' should be a |
| 196 | string; if it is not supplied, then the macro will be defined |
| 197 | without an explicit value and the exact outcome depends on the |
| 198 | compiler used (XXX true? does ANSI say anything about this?) |
| 199 | """ |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 200 | # Delete from the list of macro definitions/undefinitions if |
| 201 | # already there (so that this one will take precedence). |
| 202 | i = self._find_macro (name) |
| 203 | if i is not None: |
| 204 | del self.macros[i] |
| 205 | |
| 206 | defn = (name, value) |
| 207 | self.macros.append (defn) |
| 208 | |
| 209 | |
| 210 | def undefine_macro (self, name): |
| 211 | """Undefine a preprocessor macro for all compilations driven by |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 212 | this compiler object. If the same macro is defined by |
| 213 | 'define_macro()' and undefined by 'undefine_macro()' the last call |
| 214 | takes precedence (including multiple redefinitions or |
| 215 | undefinitions). If the macro is redefined/undefined on a |
| 216 | per-compilation basis (ie. in the call to 'compile()'), then that |
| 217 | takes precedence. |
| 218 | """ |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 219 | # Delete from the list of macro definitions/undefinitions if |
| 220 | # already there (so that this one will take precedence). |
| 221 | i = self._find_macro (name) |
| 222 | if i is not None: |
| 223 | del self.macros[i] |
| 224 | |
| 225 | undefn = (name,) |
| 226 | self.macros.append (undefn) |
| 227 | |
| 228 | |
| 229 | def add_include_dir (self, dir): |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 230 | """Add 'dir' to the list of directories that will be searched for |
| 231 | header files. The compiler is instructed to search directories in |
| 232 | the order in which they are supplied by successive calls to |
| 233 | 'add_include_dir()'. |
| 234 | """ |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 235 | self.include_dirs.append (dir) |
| 236 | |
| 237 | def set_include_dirs (self, dirs): |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 238 | """Set the list of directories that will be searched to 'dirs' (a |
| 239 | list of strings). Overrides any preceding calls to |
| 240 | 'add_include_dir()'; subsequence calls to 'add_include_dir()' add |
| 241 | to the list passed to 'set_include_dirs()'. This does not affect |
| 242 | any list of standard include directories that the compiler may |
| 243 | search by default. |
| 244 | """ |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 245 | self.include_dirs = copy (dirs) |
| 246 | |
| 247 | |
| 248 | def add_library (self, libname): |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 249 | """Add 'libname' to the list of libraries that will be included in |
| 250 | all links driven by this compiler object. Note that 'libname' |
| 251 | should *not* be the name of a file containing a library, but the |
| 252 | name of the library itself: the actual filename will be inferred by |
| 253 | the linker, the compiler, or the compiler class (depending on the |
| 254 | platform). |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 255 | |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 256 | The linker will be instructed to link against libraries in the |
| 257 | order they were supplied to 'add_library()' and/or |
| 258 | 'set_libraries()'. It is perfectly valid to duplicate library |
| 259 | names; the linker will be instructed to link against libraries as |
| 260 | many times as they are mentioned. |
| 261 | """ |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 262 | self.libraries.append (libname) |
| 263 | |
| 264 | def set_libraries (self, libnames): |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 265 | """Set the list of libraries to be included in all links driven by |
| 266 | this compiler object to 'libnames' (a list of strings). This does |
| 267 | not affect any standard system libraries that the linker may |
| 268 | include by default. |
| 269 | """ |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 270 | self.libraries = copy (libnames) |
| 271 | |
| 272 | |
| 273 | def add_library_dir (self, dir): |
| 274 | """Add 'dir' to the list of directories that will be searched for |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 275 | libraries specified to 'add_library()' and 'set_libraries()'. The |
| 276 | linker will be instructed to search for libraries in the order they |
| 277 | are supplied to 'add_library_dir()' and/or 'set_library_dirs()'. |
| 278 | """ |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 279 | self.library_dirs.append (dir) |
| 280 | |
| 281 | def set_library_dirs (self, dirs): |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 282 | """Set the list of library search directories to 'dirs' (a list of |
| 283 | strings). This does not affect any standard library search path |
| 284 | that the linker may search by default. |
| 285 | """ |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 286 | self.library_dirs = copy (dirs) |
| 287 | |
| 288 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 289 | def add_runtime_library_dir (self, dir): |
| 290 | """Add 'dir' to the list of directories that will be searched for |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 291 | shared libraries at runtime. |
| 292 | """ |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 293 | self.runtime_library_dirs.append (dir) |
| 294 | |
| 295 | def set_runtime_library_dirs (self, dirs): |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 296 | """Set the list of directories to search for shared libraries at |
| 297 | runtime to 'dirs' (a list of strings). This does not affect any |
| 298 | standard search path that the runtime linker may search by |
| 299 | default. |
| 300 | """ |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 301 | self.runtime_library_dirs = copy (dirs) |
| 302 | |
| 303 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 304 | def add_link_object (self, object): |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 305 | """Add 'object' to the list of object files (or analogues, such as |
Greg Ward | 612eb9f | 2000-07-27 02:13:20 +0000 | [diff] [blame] | 306 | explicitly named library files or the output of "resource |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 307 | compilers") to be included in every link driven by this compiler |
| 308 | object. |
| 309 | """ |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 310 | self.objects.append (object) |
| 311 | |
| 312 | def set_link_objects (self, objects): |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 313 | """Set the list of object files (or analogues) to be included in |
| 314 | every link to 'objects'. This does not affect any standard object |
| 315 | files that the linker may include by default (such as system |
| 316 | libraries). |
| 317 | """ |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 318 | self.objects = copy (objects) |
| 319 | |
| 320 | |
Thomas Heller | e650080 | 2002-04-25 17:03:30 +0000 | [diff] [blame] | 321 | # -- Private utility methods -------------------------------------- |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 322 | # (here for the convenience of subclasses) |
| 323 | |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 324 | # Helper method to prep compiler in subclass compile() methods |
| 325 | |
| 326 | def _setup_compile(self, outdir, macros, incdirs, sources, depends, |
| 327 | extra): |
| 328 | """Process arguments and decide which source files to compile. |
| 329 | |
| 330 | Merges _fix_compile_args() and _prep_compile(). |
| 331 | """ |
| 332 | if outdir is None: |
| 333 | outdir = self.output_dir |
| 334 | elif type(outdir) is not StringType: |
| 335 | raise TypeError, "'output_dir' must be a string or None" |
| 336 | |
| 337 | if macros is None: |
| 338 | macros = self.macros |
| 339 | elif type(macros) is ListType: |
| 340 | macros = macros + (self.macros or []) |
| 341 | else: |
| 342 | raise TypeError, "'macros' (if supplied) must be a list of tuples" |
| 343 | |
| 344 | if incdirs is None: |
| 345 | incdirs = self.include_dirs |
| 346 | elif type(incdirs) in (ListType, TupleType): |
| 347 | incdirs = list(incdirs) + (self.include_dirs or []) |
| 348 | else: |
| 349 | raise TypeError, \ |
| 350 | "'include_dirs' (if supplied) must be a list of strings" |
| 351 | |
| 352 | if extra is None: |
| 353 | extra = [] |
| 354 | |
| 355 | # Get the list of expected output (object) files |
| 356 | objects = self.object_filenames(sources, 1, outdir) |
| 357 | assert len(objects) == len(sources) |
| 358 | |
| 359 | # XXX should redo this code to eliminate skip_source entirely. |
| 360 | # XXX instead create build and issue skip messages inline |
| 361 | |
| 362 | if self.force: |
| 363 | skip_source = {} # rebuild everything |
| 364 | for source in sources: |
| 365 | skip_source[source] = 0 |
| 366 | elif depends is None: |
| 367 | # If depends is None, figure out which source files we |
| 368 | # have to recompile according to a simplistic check. We |
| 369 | # just compare the source and object file, no deep |
| 370 | # dependency checking involving header files. |
| 371 | skip_source = {} # rebuild everything |
| 372 | for source in sources: # no wait, rebuild nothing |
| 373 | skip_source[source] = 1 |
| 374 | |
| 375 | n_sources, n_objects = newer_pairwise(sources, objects) |
| 376 | for source in n_sources: # no really, only rebuild what's |
| 377 | skip_source[source] = 0 # out-of-date |
| 378 | else: |
| 379 | # If depends is a list of files, then do a different |
| 380 | # simplistic check. Assume that each object depends on |
| 381 | # its source and all files in the depends list. |
| 382 | skip_source = {} |
| 383 | # L contains all the depends plus a spot at the end for a |
| 384 | # particular source file |
| 385 | L = depends[:] + [None] |
| 386 | for i in range(len(objects)): |
| 387 | source = sources[i] |
| 388 | L[-1] = source |
| 389 | if newer_group(L, objects[i]): |
| 390 | skip_source[source] = 0 |
| 391 | else: |
| 392 | skip_source[source] = 1 |
| 393 | |
| 394 | pp_opts = gen_preprocess_options(macros, incdirs) |
| 395 | |
| 396 | build = {} |
| 397 | for i in range(len(sources)): |
| 398 | src = sources[i] |
| 399 | obj = objects[i] |
| 400 | ext = os.path.splitext(src)[1] |
| 401 | self.mkpath(os.path.dirname(obj)) |
| 402 | if skip_source[src]: |
| 403 | log.debug("skipping %s (%s up-to-date)", src, obj) |
| 404 | else: |
| 405 | build[obj] = src, ext |
| 406 | |
| 407 | return macros, objects, extra, pp_opts, build |
| 408 | |
| 409 | def _get_cc_args(self, pp_opts, debug, before): |
| 410 | # works for unixccompiler, emxccompiler, cygwinccompiler |
| 411 | cc_args = pp_opts + ['-c'] |
| 412 | if debug: |
| 413 | cc_args[:0] = ['-g'] |
| 414 | if before: |
| 415 | cc_args[:0] = before |
| 416 | return cc_args |
| 417 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 418 | def _fix_compile_args (self, output_dir, macros, include_dirs): |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 419 | """Typecheck and fix-up some of the arguments to the 'compile()' |
| 420 | method, and return fixed-up values. Specifically: if 'output_dir' |
| 421 | is None, replaces it with 'self.output_dir'; ensures that 'macros' |
| 422 | is a list, and augments it with 'self.macros'; ensures that |
| 423 | 'include_dirs' is a list, and augments it with 'self.include_dirs'. |
| 424 | Guarantees that the returned values are of the correct type, |
| 425 | i.e. for 'output_dir' either string or None, and for 'macros' and |
| 426 | 'include_dirs' either list or None. |
| 427 | """ |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 428 | if output_dir is None: |
| 429 | output_dir = self.output_dir |
| 430 | elif type (output_dir) is not StringType: |
| 431 | raise TypeError, "'output_dir' must be a string or None" |
| 432 | |
| 433 | if macros is None: |
| 434 | macros = self.macros |
| 435 | elif type (macros) is ListType: |
| 436 | macros = macros + (self.macros or []) |
| 437 | else: |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 438 | raise TypeError, "'macros' (if supplied) must be a list of tuples" |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 439 | |
| 440 | if include_dirs is None: |
| 441 | include_dirs = self.include_dirs |
| 442 | elif type (include_dirs) in (ListType, TupleType): |
| 443 | include_dirs = list (include_dirs) + (self.include_dirs or []) |
| 444 | else: |
| 445 | raise TypeError, \ |
| 446 | "'include_dirs' (if supplied) must be a list of strings" |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 447 | |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 448 | return output_dir, macros, include_dirs |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 449 | |
| 450 | # _fix_compile_args () |
| 451 | |
| 452 | |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 453 | def _prep_compile(self, sources, output_dir, depends=None): |
| 454 | """Decide which souce files must be recompiled. |
| 455 | |
| 456 | Determine the list of object files corresponding to 'sources', |
| 457 | and figure out which ones really need to be recompiled. |
| 458 | Return a list of all object files and a dictionary telling |
| 459 | which source files can be skipped. |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 460 | """ |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 461 | # Get the list of expected output (object) files |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 462 | objects = self.object_filenames(sources, strip_dir=1, |
| 463 | output_dir=output_dir) |
| 464 | assert len(objects) == len(sources) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 465 | |
| 466 | if self.force: |
| 467 | skip_source = {} # rebuild everything |
| 468 | for source in sources: |
| 469 | skip_source[source] = 0 |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 470 | elif depends is None: |
| 471 | # If depends is None, figure out which source files we |
| 472 | # have to recompile according to a simplistic check. We |
| 473 | # just compare the source and object file, no deep |
| 474 | # dependency checking involving header files. |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 475 | skip_source = {} # rebuild everything |
| 476 | for source in sources: # no wait, rebuild nothing |
| 477 | skip_source[source] = 1 |
| 478 | |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 479 | n_sources, n_objects = newer_pairwise(sources, objects) |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 480 | for source in n_sources: # no really, only rebuild what's |
| 481 | skip_source[source] = 0 # out-of-date |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 482 | else: |
| 483 | # If depends is a list of files, then do a different |
| 484 | # simplistic check. Assume that each object depends on |
| 485 | # its source and all files in the depends list. |
| 486 | skip_source = {} |
| 487 | # L contains all the depends plus a spot at the end for a |
| 488 | # particular source file |
| 489 | L = depends[:] + [None] |
| 490 | for i in range(len(objects)): |
| 491 | source = sources[i] |
| 492 | L[-1] = source |
| 493 | if newer_group(L, objects[i]): |
| 494 | skip_source[source] = 0 |
| 495 | else: |
| 496 | skip_source[source] = 1 |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 497 | |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 498 | return objects, skip_source |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 499 | |
| 500 | # _prep_compile () |
| 501 | |
| 502 | |
Greg Ward | f10f95d | 2000-03-26 21:37:09 +0000 | [diff] [blame] | 503 | def _fix_object_args (self, objects, output_dir): |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 504 | """Typecheck and fix up some arguments supplied to various methods. |
| 505 | Specifically: ensure that 'objects' is a list; if output_dir is |
| 506 | None, replace with self.output_dir. Return fixed versions of |
| 507 | 'objects' and 'output_dir'. |
| 508 | """ |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 509 | if type (objects) not in (ListType, TupleType): |
| 510 | raise TypeError, \ |
| 511 | "'objects' must be a list or tuple of strings" |
| 512 | objects = list (objects) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 513 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 514 | if output_dir is None: |
| 515 | output_dir = self.output_dir |
| 516 | elif type (output_dir) is not StringType: |
| 517 | raise TypeError, "'output_dir' must be a string or None" |
| 518 | |
Greg Ward | f10f95d | 2000-03-26 21:37:09 +0000 | [diff] [blame] | 519 | return (objects, output_dir) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 520 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 521 | |
Greg Ward | f10f95d | 2000-03-26 21:37:09 +0000 | [diff] [blame] | 522 | def _fix_lib_args (self, libraries, library_dirs, runtime_library_dirs): |
| 523 | """Typecheck and fix up some of the arguments supplied to the |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 524 | 'link_*' methods. Specifically: ensure that all arguments are |
| 525 | lists, and augment them with their permanent versions |
| 526 | (eg. 'self.libraries' augments 'libraries'). Return a tuple with |
| 527 | fixed versions of all arguments. |
| 528 | """ |
Greg Ward | f10f95d | 2000-03-26 21:37:09 +0000 | [diff] [blame] | 529 | if libraries is None: |
| 530 | libraries = self.libraries |
| 531 | elif type (libraries) in (ListType, TupleType): |
| 532 | libraries = list (libraries) + (self.libraries or []) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 533 | else: |
Greg Ward | f10f95d | 2000-03-26 21:37:09 +0000 | [diff] [blame] | 534 | raise TypeError, \ |
| 535 | "'libraries' (if supplied) must be a list of strings" |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 536 | |
Greg Ward | f10f95d | 2000-03-26 21:37:09 +0000 | [diff] [blame] | 537 | if library_dirs is None: |
| 538 | library_dirs = self.library_dirs |
| 539 | elif type (library_dirs) in (ListType, TupleType): |
| 540 | library_dirs = list (library_dirs) + (self.library_dirs or []) |
| 541 | else: |
| 542 | raise TypeError, \ |
| 543 | "'library_dirs' (if supplied) must be a list of strings" |
| 544 | |
| 545 | if runtime_library_dirs is None: |
| 546 | runtime_library_dirs = self.runtime_library_dirs |
| 547 | elif type (runtime_library_dirs) in (ListType, TupleType): |
| 548 | runtime_library_dirs = (list (runtime_library_dirs) + |
| 549 | (self.runtime_library_dirs or [])) |
| 550 | else: |
| 551 | raise TypeError, \ |
| 552 | "'runtime_library_dirs' (if supplied) " + \ |
| 553 | "must be a list of strings" |
| 554 | |
| 555 | return (libraries, library_dirs, runtime_library_dirs) |
| 556 | |
| 557 | # _fix_lib_args () |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 558 | |
| 559 | |
| 560 | def _need_link (self, objects, output_file): |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 561 | """Return true if we need to relink the files listed in 'objects' |
| 562 | to recreate 'output_file'. |
| 563 | """ |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 564 | if self.force: |
| 565 | return 1 |
| 566 | else: |
| 567 | if self.dry_run: |
| 568 | newer = newer_group (objects, output_file, missing='newer') |
| 569 | else: |
| 570 | newer = newer_group (objects, output_file) |
| 571 | return newer |
| 572 | |
| 573 | # _need_link () |
| 574 | |
| 575 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 576 | # -- Worker methods ------------------------------------------------ |
| 577 | # (must be implemented by subclasses) |
| 578 | |
Greg Ward | 3ff3b03 | 2000-06-21 02:58:46 +0000 | [diff] [blame] | 579 | def preprocess (self, |
| 580 | source, |
| 581 | output_file=None, |
| 582 | macros=None, |
| 583 | include_dirs=None, |
| 584 | extra_preargs=None, |
| 585 | extra_postargs=None): |
| 586 | """Preprocess a single C/C++ source file, named in 'source'. |
| 587 | Output will be written to file named 'output_file', or stdout if |
| 588 | 'output_file' not supplied. 'macros' is a list of macro |
| 589 | definitions as for 'compile()', which will augment the macros set |
| 590 | with 'define_macro()' and 'undefine_macro()'. 'include_dirs' is a |
| 591 | list of directory names that will be added to the default list. |
Greg Ward | e5c62bf | 2000-06-25 02:08:18 +0000 | [diff] [blame] | 592 | |
| 593 | Raises PreprocessError on failure. |
Greg Ward | 3ff3b03 | 2000-06-21 02:58:46 +0000 | [diff] [blame] | 594 | """ |
| 595 | pass |
| 596 | |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 597 | def compile(self, sources, output_dir=None, macros=None, |
| 598 | include_dirs=None, debug=0, extra_preargs=None, |
| 599 | extra_postargs=None, depends=None): |
| 600 | """Compile one or more source files. |
| 601 | |
| 602 | 'sources' must be a list of filenames, most likely C/C++ |
| 603 | files, but in reality anything that can be handled by a |
| 604 | particular compiler and compiler class (eg. MSVCCompiler can |
| 605 | handle resource files in 'sources'). Return a list of object |
| 606 | filenames, one per source filename in 'sources'. Depending on |
| 607 | the implementation, not all source files will necessarily be |
| 608 | compiled, but all corresponding object filenames will be |
| 609 | returned. |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 610 | |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 611 | If 'output_dir' is given, object files will be put under it, while |
| 612 | retaining their original path component. That is, "foo/bar.c" |
| 613 | normally compiles to "foo/bar.o" (for a Unix implementation); if |
| 614 | 'output_dir' is "build", then it would compile to |
| 615 | "build/foo/bar.o". |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 616 | |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 617 | 'macros', if given, must be a list of macro definitions. A macro |
| 618 | definition is either a (name, value) 2-tuple or a (name,) 1-tuple. |
| 619 | The former defines a macro; if the value is None, the macro is |
| 620 | defined without an explicit value. The 1-tuple case undefines a |
| 621 | macro. Later definitions/redefinitions/ undefinitions take |
| 622 | precedence. |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 623 | |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 624 | 'include_dirs', if given, must be a list of strings, the |
| 625 | directories to add to the default include file search path for this |
| 626 | compilation only. |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 627 | |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 628 | 'debug' is a boolean; if true, the compiler will be instructed to |
| 629 | output debug symbols in (or alongside) the object file(s). |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 630 | |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 631 | 'extra_preargs' and 'extra_postargs' are implementation- dependent. |
| 632 | On platforms that have the notion of a command-line (e.g. Unix, |
| 633 | DOS/Windows), they are most likely lists of strings: extra |
| 634 | command-line arguments to prepand/append to the compiler command |
| 635 | line. On other platforms, consult the implementation class |
| 636 | documentation. In any event, they are intended as an escape hatch |
| 637 | for those occasions when the abstract compiler framework doesn't |
| 638 | cut the mustard. |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 639 | |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 640 | 'depends', if given, is a list of filenames that all targets |
| 641 | depend on. If a source file is older than any file in |
| 642 | depends, then the source file will be recompiled. This |
| 643 | supports dependency tracking, but only at a coarse |
| 644 | granularity. |
| 645 | |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 646 | Raises CompileError on failure. |
| 647 | """ |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 648 | |
Jeremy Hylton | 6e08d22 | 2002-06-18 18:42:41 +0000 | [diff] [blame] | 649 | # A concrete compiler class can either override this method |
| 650 | # entirely or implement _compile(). |
| 651 | |
| 652 | macros, objects, extra_postargs, pp_opts, build = \ |
| 653 | self._setup_compile(output_dir, macros, include_dirs, sources, |
| 654 | depends, extra_postargs) |
| 655 | cc_args = self._get_cc_args(pp_opts, debug, extra_preargs) |
| 656 | |
| 657 | for obj, (src, ext) in build.items(): |
| 658 | self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts) |
| 659 | |
| 660 | # Return *all* object filenames, not just the ones we just built. |
| 661 | return objects |
| 662 | |
| 663 | def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts): |
| 664 | """Compile 'src' to product 'obj'.""" |
| 665 | |
| 666 | # A concrete compiler class that does not override compile() |
| 667 | # should implement _compile(). |
| 668 | pass |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 669 | |
Greg Ward | 036c805 | 2000-03-10 01:48:32 +0000 | [diff] [blame] | 670 | def create_static_lib (self, |
| 671 | objects, |
| 672 | output_libname, |
| 673 | output_dir=None, |
| 674 | debug=0): |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 675 | """Link a bunch of stuff together to create a static library file. |
| 676 | The "bunch of stuff" consists of the list of object files supplied |
| 677 | as 'objects', the extra object files supplied to |
| 678 | 'add_link_object()' and/or 'set_link_objects()', the libraries |
| 679 | supplied to 'add_library()' and/or 'set_libraries()', and the |
| 680 | libraries supplied as 'libraries' (if any). |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 681 | |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 682 | 'output_libname' should be a library name, not a filename; the |
| 683 | filename will be inferred from the library name. 'output_dir' is |
| 684 | the directory where the library file will be put. |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 685 | |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 686 | 'debug' is a boolean; if true, debugging information will be |
| 687 | included in the library (note that on most platforms, it is the |
| 688 | compile step where this matters: the 'debug' flag is included here |
| 689 | just for consistency). |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 690 | |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 691 | Raises LibError on failure. |
| 692 | """ |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 693 | pass |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 694 | |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 695 | |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 696 | # values for target_desc parameter in link() |
| 697 | SHARED_OBJECT = "shared_object" |
| 698 | SHARED_LIBRARY = "shared_library" |
| 699 | EXECUTABLE = "executable" |
| 700 | |
| 701 | def link (self, |
| 702 | target_desc, |
| 703 | objects, |
| 704 | output_filename, |
| 705 | output_dir=None, |
| 706 | libraries=None, |
| 707 | library_dirs=None, |
| 708 | runtime_library_dirs=None, |
| 709 | export_symbols=None, |
| 710 | debug=0, |
| 711 | extra_preargs=None, |
| 712 | extra_postargs=None, |
| 713 | build_temp=None): |
| 714 | """Link a bunch of stuff together to create an executable or |
| 715 | shared library file. |
| 716 | |
| 717 | The "bunch of stuff" consists of the list of object files supplied |
| 718 | as 'objects'. 'output_filename' should be a filename. If |
| 719 | 'output_dir' is supplied, 'output_filename' is relative to it |
| 720 | (i.e. 'output_filename' can provide directory components if |
| 721 | needed). |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 722 | |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 723 | 'libraries' is a list of libraries to link against. These are |
| 724 | library names, not filenames, since they're translated into |
| 725 | filenames in a platform-specific way (eg. "foo" becomes "libfoo.a" |
| 726 | on Unix and "foo.lib" on DOS/Windows). However, they can include a |
| 727 | directory component, which means the linker will look in that |
| 728 | specific directory rather than searching all the normal locations. |
Greg Ward | 5299b6a | 2000-05-20 13:23:21 +0000 | [diff] [blame] | 729 | |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 730 | 'library_dirs', if supplied, should be a list of directories to |
| 731 | search for libraries that were specified as bare library names |
| 732 | (ie. no directory component). These are on top of the system |
| 733 | default and those supplied to 'add_library_dir()' and/or |
| 734 | 'set_library_dirs()'. 'runtime_library_dirs' is a list of |
| 735 | directories that will be embedded into the shared library and used |
| 736 | to search for other shared libraries that *it* depends on at |
| 737 | run-time. (This may only be relevant on Unix.) |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 738 | |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 739 | 'export_symbols' is a list of symbols that the shared library will |
| 740 | export. (This appears to be relevant only on Windows.) |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 741 | |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 742 | 'debug' is as for 'compile()' and 'create_static_lib()', with the |
| 743 | slight distinction that it actually matters on most platforms (as |
| 744 | opposed to 'create_static_lib()', which includes a 'debug' flag |
| 745 | mostly for form's sake). |
Greg Ward | d151711 | 2000-05-30 01:56:44 +0000 | [diff] [blame] | 746 | |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 747 | 'extra_preargs' and 'extra_postargs' are as for 'compile()' (except |
| 748 | of course that they supply command-line arguments for the |
| 749 | particular linker being used). |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 750 | |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 751 | Raises LinkError on failure. |
| 752 | """ |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 753 | raise NotImplementedError |
| 754 | |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 755 | |
Greg Ward | 264cf74 | 2000-09-27 02:24:21 +0000 | [diff] [blame] | 756 | # Old 'link_*()' methods, rewritten to use the new 'link()' method. |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 757 | |
| 758 | def link_shared_lib (self, |
| 759 | objects, |
| 760 | output_libname, |
| 761 | output_dir=None, |
| 762 | libraries=None, |
| 763 | library_dirs=None, |
| 764 | runtime_library_dirs=None, |
| 765 | export_symbols=None, |
| 766 | debug=0, |
| 767 | extra_preargs=None, |
| 768 | extra_postargs=None, |
| 769 | build_temp=None): |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 770 | self.link(CCompiler.SHARED_LIBRARY, objects, |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 771 | self.library_filename(output_libname, lib_type='shared'), |
| 772 | output_dir, |
| 773 | libraries, library_dirs, runtime_library_dirs, |
| 774 | export_symbols, debug, |
| 775 | extra_preargs, extra_postargs, build_temp) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 776 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 777 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 778 | def link_shared_object (self, |
| 779 | objects, |
| 780 | output_filename, |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 781 | output_dir=None, |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 782 | libraries=None, |
Greg Ward | 26e48ea | 1999-08-29 18:17:36 +0000 | [diff] [blame] | 783 | library_dirs=None, |
Greg Ward | f10f95d | 2000-03-26 21:37:09 +0000 | [diff] [blame] | 784 | runtime_library_dirs=None, |
Greg Ward | 5299b6a | 2000-05-20 13:23:21 +0000 | [diff] [blame] | 785 | export_symbols=None, |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 786 | debug=0, |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 787 | extra_preargs=None, |
Greg Ward | bfc79d6 | 2000-06-28 01:29:09 +0000 | [diff] [blame] | 788 | extra_postargs=None, |
| 789 | build_temp=None): |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 790 | self.link(CCompiler.SHARED_OBJECT, objects, |
| 791 | output_filename, output_dir, |
| 792 | libraries, library_dirs, runtime_library_dirs, |
| 793 | export_symbols, debug, |
| 794 | extra_preargs, extra_postargs, build_temp) |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 795 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 796 | |
Greg Ward | 5baf1c2 | 2000-01-09 22:41:02 +0000 | [diff] [blame] | 797 | def link_executable (self, |
| 798 | objects, |
| 799 | output_progname, |
| 800 | output_dir=None, |
| 801 | libraries=None, |
| 802 | library_dirs=None, |
Greg Ward | f10f95d | 2000-03-26 21:37:09 +0000 | [diff] [blame] | 803 | runtime_library_dirs=None, |
Greg Ward | 3c045a5 | 2000-02-09 02:16:14 +0000 | [diff] [blame] | 804 | debug=0, |
Greg Ward | 5baf1c2 | 2000-01-09 22:41:02 +0000 | [diff] [blame] | 805 | extra_preargs=None, |
| 806 | extra_postargs=None): |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 807 | self.link(CCompiler.EXECUTABLE, objects, |
Greg Ward | 264cf74 | 2000-09-27 02:24:21 +0000 | [diff] [blame] | 808 | self.executable_filename(output_progname), output_dir, |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 809 | libraries, library_dirs, runtime_library_dirs, None, |
Greg Ward | 264cf74 | 2000-09-27 02:24:21 +0000 | [diff] [blame] | 810 | debug, extra_preargs, extra_postargs, None) |
Greg Ward | 5baf1c2 | 2000-01-09 22:41:02 +0000 | [diff] [blame] | 811 | |
| 812 | |
Greg Ward | f7edea7 | 2000-05-20 13:31:32 +0000 | [diff] [blame] | 813 | # -- Miscellaneous methods ----------------------------------------- |
| 814 | # These are all used by the 'gen_lib_options() function; there is |
| 815 | # no appropriate default implementation so subclasses should |
| 816 | # implement all of these. |
| 817 | |
| 818 | def library_dir_option (self, dir): |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 819 | """Return the compiler option to add 'dir' to the list of |
| 820 | directories searched for libraries. |
| 821 | """ |
Greg Ward | f7edea7 | 2000-05-20 13:31:32 +0000 | [diff] [blame] | 822 | raise NotImplementedError |
| 823 | |
| 824 | def runtime_library_dir_option (self, dir): |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 825 | """Return the compiler option to add 'dir' to the list of |
| 826 | directories searched for runtime libraries. |
| 827 | """ |
Greg Ward | f7edea7 | 2000-05-20 13:31:32 +0000 | [diff] [blame] | 828 | raise NotImplementedError |
| 829 | |
| 830 | def library_option (self, lib): |
| 831 | """Return the compiler option to add 'dir' to the list of libraries |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 832 | linked into the shared library or executable. |
| 833 | """ |
Greg Ward | f7edea7 | 2000-05-20 13:31:32 +0000 | [diff] [blame] | 834 | raise NotImplementedError |
| 835 | |
Greg Ward | e5e6015 | 2000-08-04 01:28:39 +0000 | [diff] [blame] | 836 | def find_library_file (self, dirs, lib, debug=0): |
Greg Ward | f7edea7 | 2000-05-20 13:31:32 +0000 | [diff] [blame] | 837 | """Search the specified list of directories for a static or shared |
Greg Ward | e5e6015 | 2000-08-04 01:28:39 +0000 | [diff] [blame] | 838 | library file 'lib' and return the full path to that file. If |
| 839 | 'debug' true, look for a debugging version (if that makes sense on |
| 840 | the current platform). Return None if 'lib' wasn't found in any of |
| 841 | the specified directories. |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 842 | """ |
Greg Ward | f7edea7 | 2000-05-20 13:31:32 +0000 | [diff] [blame] | 843 | raise NotImplementedError |
| 844 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 845 | # -- Filename generation methods ----------------------------------- |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 846 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 847 | # The default implementation of the filename generating methods are |
| 848 | # prejudiced towards the Unix/DOS/Windows view of the world: |
| 849 | # * object files are named by replacing the source file extension |
| 850 | # (eg. .c/.cpp -> .o/.obj) |
| 851 | # * library files (shared or static) are named by plugging the |
| 852 | # library name and extension into a format string, eg. |
| 853 | # "lib%s.%s" % (lib_name, ".a") for Unix static libraries |
| 854 | # * executables are named by appending an extension (possibly |
| 855 | # empty) to the program name: eg. progname + ".exe" for |
| 856 | # Windows |
| 857 | # |
| 858 | # To reduce redundant code, these methods expect to find |
| 859 | # several attributes in the current object (presumably defined |
| 860 | # as class attributes): |
| 861 | # * src_extensions - |
| 862 | # list of C/C++ source file extensions, eg. ['.c', '.cpp'] |
| 863 | # * obj_extension - |
| 864 | # object file extension, eg. '.o' or '.obj' |
| 865 | # * static_lib_extension - |
| 866 | # extension for static library files, eg. '.a' or '.lib' |
| 867 | # * shared_lib_extension - |
| 868 | # extension for shared library/object files, eg. '.so', '.dll' |
| 869 | # * static_lib_format - |
| 870 | # format string for generating static library filenames, |
| 871 | # eg. 'lib%s.%s' or '%s.%s' |
| 872 | # * shared_lib_format |
| 873 | # format string for generating shared library filenames |
| 874 | # (probably same as static_lib_format, since the extension |
| 875 | # is one of the intended parameters to the format string) |
| 876 | # * exe_extension - |
| 877 | # extension for executable files, eg. '' or '.exe' |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 878 | |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 879 | def object_filenames(self, source_filenames, strip_dir=0, output_dir=''): |
Guido van Rossum | cc3a6df | 2002-10-01 04:14:17 +0000 | [diff] [blame] | 880 | if output_dir is None: |
| 881 | output_dir = '' |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 882 | obj_names = [] |
| 883 | for src_name in source_filenames: |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 884 | base, ext = os.path.splitext(src_name) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 885 | if ext not in self.src_extensions: |
Greg Ward | 9aa668b | 2000-06-24 02:22:49 +0000 | [diff] [blame] | 886 | raise UnknownFileError, \ |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 887 | "unknown file type '%s' (from '%s')" % (ext, src_name) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 888 | if strip_dir: |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 889 | base = os.path.basename(base) |
| 890 | obj_names.append(os.path.join(output_dir, |
| 891 | base + self.obj_extension)) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 892 | return obj_names |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 893 | |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 894 | def shared_object_filename(self, basename, strip_dir=0, output_dir=''): |
| 895 | assert output_dir is not None |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 896 | if strip_dir: |
| 897 | basename = os.path.basename (basename) |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 898 | return os.path.join(output_dir, basename + self.shared_lib_extension) |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 899 | |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 900 | def executable_filename(self, basename, strip_dir=0, output_dir=''): |
| 901 | assert output_dir is not None |
Greg Ward | 4240648 | 2000-09-27 02:08:14 +0000 | [diff] [blame] | 902 | if strip_dir: |
| 903 | basename = os.path.basename (basename) |
| 904 | return os.path.join(output_dir, basename + (self.exe_extension or '')) |
Greg Ward | 26e48ea | 1999-08-29 18:17:36 +0000 | [diff] [blame] | 905 | |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 906 | def library_filename(self, libname, lib_type='static', # or 'shared' |
| 907 | strip_dir=0, output_dir=''): |
| 908 | assert output_dir is not None |
| 909 | if lib_type not in ("static", "shared", "dylib"): |
Jack Jansen | e259e59 | 2001-08-27 15:08:16 +0000 | [diff] [blame] | 910 | raise ValueError, "'lib_type' must be \"static\", \"shared\" or \"dylib\"" |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 911 | fmt = getattr(self, lib_type + "_lib_format") |
| 912 | ext = getattr(self, lib_type + "_lib_extension") |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 913 | |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 914 | dir, base = os.path.split (libname) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 915 | filename = fmt % (base, ext) |
| 916 | if strip_dir: |
| 917 | dir = '' |
| 918 | |
Jeremy Hylton | 59b103c | 2002-06-13 17:26:30 +0000 | [diff] [blame] | 919 | return os.path.join(output_dir, dir, filename) |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 920 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 921 | |
| 922 | # -- Utility methods ----------------------------------------------- |
| 923 | |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 924 | def announce (self, msg, level=1): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 925 | log.debug(msg) |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 926 | |
Greg Ward | f813e59 | 2000-08-04 01:31:13 +0000 | [diff] [blame] | 927 | def debug_print (self, msg): |
Jeremy Hylton | fcd7353 | 2002-09-11 16:31:53 +0000 | [diff] [blame] | 928 | from distutils.debug import DEBUG |
Greg Ward | f813e59 | 2000-08-04 01:31:13 +0000 | [diff] [blame] | 929 | if DEBUG: |
| 930 | print msg |
| 931 | |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 932 | def warn (self, msg): |
| 933 | sys.stderr.write ("warning: %s\n" % msg) |
| 934 | |
Greg Ward | 9dddbb4 | 2000-08-02 01:38:20 +0000 | [diff] [blame] | 935 | def execute (self, func, args, msg=None, level=1): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 936 | execute(func, args, msg, self.dry_run) |
Greg Ward | 9dddbb4 | 2000-08-02 01:38:20 +0000 | [diff] [blame] | 937 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 938 | def spawn (self, cmd): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 939 | spawn (cmd, dry_run=self.dry_run) |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 940 | |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 941 | def move_file (self, src, dst): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 942 | return move_file (src, dst, dry_run=self.dry_run) |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame] | 943 | |
Greg Ward | 013f0c8 | 2000-03-01 14:43:12 +0000 | [diff] [blame] | 944 | def mkpath (self, name, mode=0777): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 945 | mkpath (name, mode, self.dry_run) |
Greg Ward | 013f0c8 | 2000-03-01 14:43:12 +0000 | [diff] [blame] | 946 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 947 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 948 | # class CCompiler |
| 949 | |
| 950 | |
Marc-André Lemburg | 636b906 | 2001-02-19 09:20:04 +0000 | [diff] [blame] | 951 | # Map a sys.platform/os.name ('posix', 'nt') to the default compiler |
| 952 | # type for that platform. Keys are interpreted as re match |
| 953 | # patterns. Order is important; platform mappings are preferred over |
| 954 | # OS names. |
| 955 | _default_compilers = ( |
| 956 | |
| 957 | # Platform string mappings |
Andrew M. Kuchling | a34dbe0 | 2001-02-27 19:13:15 +0000 | [diff] [blame] | 958 | |
| 959 | # on a cygwin built python we can use gcc like an ordinary UNIXish |
| 960 | # compiler |
| 961 | ('cygwin.*', 'unix'), |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 962 | ('os2emx', 'emx'), |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 963 | |
Marc-André Lemburg | 636b906 | 2001-02-19 09:20:04 +0000 | [diff] [blame] | 964 | # OS name mappings |
| 965 | ('posix', 'unix'), |
| 966 | ('nt', 'msvc'), |
| 967 | ('mac', 'mwerks'), |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 968 | |
Marc-André Lemburg | 636b906 | 2001-02-19 09:20:04 +0000 | [diff] [blame] | 969 | ) |
| 970 | |
| 971 | def get_default_compiler(osname=None, platform=None): |
| 972 | |
| 973 | """ Determine the default compiler to use for the given platform. |
| 974 | |
| 975 | osname should be one of the standard Python OS names (i.e. the |
| 976 | ones returned by os.name) and platform the common value |
| 977 | returned by sys.platform for the platform in question. |
| 978 | |
| 979 | The default values are os.name and sys.platform in case the |
| 980 | parameters are not given. |
| 981 | |
| 982 | """ |
| 983 | if osname is None: |
| 984 | osname = os.name |
| 985 | if platform is None: |
| 986 | platform = sys.platform |
| 987 | for pattern, compiler in _default_compilers: |
| 988 | if re.match(pattern, platform) is not None or \ |
| 989 | re.match(pattern, osname) is not None: |
| 990 | return compiler |
| 991 | # Default to Unix compiler |
| 992 | return 'unix' |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 993 | |
| 994 | # Map compiler types to (module_name, class_name) pairs -- ie. where to |
| 995 | # find the code that implements an interface to this compiler. (The module |
| 996 | # is assumed to be in the 'distutils' package.) |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 997 | compiler_class = { 'unix': ('unixccompiler', 'UnixCCompiler', |
| 998 | "standard UNIX-style compiler"), |
| 999 | 'msvc': ('msvccompiler', 'MSVCCompiler', |
| 1000 | "Microsoft Visual C++"), |
| 1001 | 'cygwin': ('cygwinccompiler', 'CygwinCCompiler', |
| 1002 | "Cygwin port of GNU C Compiler for Win32"), |
| 1003 | 'mingw32': ('cygwinccompiler', 'Mingw32CCompiler', |
| 1004 | "Mingw32 port of GNU C Compiler for Win32"), |
Greg Ward | bfc79d6 | 2000-06-28 01:29:09 +0000 | [diff] [blame] | 1005 | 'bcpp': ('bcppcompiler', 'BCPPCompiler', |
| 1006 | "Borland C++ Compiler"), |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 1007 | 'mwerks': ('mwerkscompiler', 'MWerksCompiler', |
| 1008 | "MetroWerks CodeWarrior"), |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 1009 | 'emx': ('emxccompiler', 'EMXCCompiler', |
| 1010 | "EMX port of GNU C Compiler for OS/2"), |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Greg Ward | 9d17a7a | 2000-06-07 03:00:06 +0000 | [diff] [blame] | 1013 | def show_compilers(): |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 1014 | """Print list of available compilers (used by the "--help-compiler" |
| 1015 | options to "build", "build_ext", "build_clib"). |
| 1016 | """ |
| 1017 | # XXX this "knows" that the compiler option it's describing is |
| 1018 | # "--compiler", which just happens to be the case for the three |
| 1019 | # commands that use it. |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 1020 | from distutils.fancy_getopt import FancyGetopt |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 1021 | compilers = [] |
Greg Ward | 9d17a7a | 2000-06-07 03:00:06 +0000 | [diff] [blame] | 1022 | for compiler in compiler_class.keys(): |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 1023 | compilers.append(("compiler="+compiler, None, |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 1024 | compiler_class[compiler][2])) |
| 1025 | compilers.sort() |
| 1026 | pretty_printer = FancyGetopt(compilers) |
Greg Ward | 9d17a7a | 2000-06-07 03:00:06 +0000 | [diff] [blame] | 1027 | pretty_printer.print_help("List of available compilers:") |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 1028 | |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 1029 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 1030 | def new_compiler (plat=None, |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 1031 | compiler=None, |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 1032 | verbose=0, |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 1033 | dry_run=0, |
| 1034 | force=0): |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 1035 | """Generate an instance of some CCompiler subclass for the supplied |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 1036 | platform/compiler combination. 'plat' defaults to 'os.name' |
| 1037 | (eg. 'posix', 'nt'), and 'compiler' defaults to the default compiler |
| 1038 | for that platform. Currently only 'posix' and 'nt' are supported, and |
| 1039 | the default compilers are "traditional Unix interface" (UnixCCompiler |
| 1040 | class) and Visual C++ (MSVCCompiler class). Note that it's perfectly |
| 1041 | possible to ask for a Unix compiler object under Windows, and a |
| 1042 | Microsoft compiler object under Unix -- if you supply a value for |
| 1043 | 'compiler', 'plat' is ignored. |
| 1044 | """ |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 1045 | if plat is None: |
| 1046 | plat = os.name |
| 1047 | |
| 1048 | try: |
| 1049 | if compiler is None: |
Marc-André Lemburg | 636b906 | 2001-02-19 09:20:04 +0000 | [diff] [blame] | 1050 | compiler = get_default_compiler(plat) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 1051 | |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 1052 | (module_name, class_name, long_description) = compiler_class[compiler] |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 1053 | except KeyError: |
| 1054 | msg = "don't know how to compile C/C++ code on platform '%s'" % plat |
| 1055 | if compiler is not None: |
| 1056 | msg = msg + " with '%s' compiler" % compiler |
| 1057 | raise DistutilsPlatformError, msg |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 1058 | |
Greg Ward | 802d6b7 | 1999-09-29 12:20:55 +0000 | [diff] [blame] | 1059 | try: |
| 1060 | module_name = "distutils." + module_name |
| 1061 | __import__ (module_name) |
| 1062 | module = sys.modules[module_name] |
| 1063 | klass = vars(module)[class_name] |
| 1064 | except ImportError: |
| 1065 | raise DistutilsModuleError, \ |
| 1066 | "can't compile C/C++ code: unable to load module '%s'" % \ |
| 1067 | module_name |
| 1068 | except KeyError: |
| 1069 | raise DistutilsModuleError, \ |
| 1070 | ("can't compile C/C++ code: unable to find class '%s' " + |
| 1071 | "in module '%s'") % (class_name, module_name) |
| 1072 | |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 1073 | # XXX The None is necessary to preserve backwards compatibility |
| 1074 | # with classes that expect verbose to be the first positional |
| 1075 | # argument. |
| 1076 | return klass (None, dry_run, force) |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 1077 | |
| 1078 | |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 1079 | def gen_preprocess_options (macros, include_dirs): |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 1080 | """Generate C pre-processor options (-D, -U, -I) as used by at least |
| 1081 | two types of compilers: the typical Unix compiler and Visual C++. |
| 1082 | 'macros' is the usual thing, a list of 1- or 2-tuples, where (name,) |
| 1083 | means undefine (-U) macro 'name', and (name,value) means define (-D) |
| 1084 | macro 'name' to 'value'. 'include_dirs' is just a list of directory |
| 1085 | names to be added to the header file search path (-I). Returns a list |
| 1086 | of command-line options suitable for either Unix compilers or Visual |
| 1087 | C++. |
| 1088 | """ |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 1089 | # XXX it would be nice (mainly aesthetic, and so we don't generate |
| 1090 | # stupid-looking command lines) to go over 'macros' and eliminate |
| 1091 | # redundant definitions/undefinitions (ie. ensure that only the |
| 1092 | # latest mention of a particular macro winds up on the command |
| 1093 | # line). I don't think it's essential, though, since most (all?) |
| 1094 | # Unix C compilers only pay attention to the latest -D or -U |
| 1095 | # mention of a macro on their command line. Similar situation for |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 1096 | # 'include_dirs'. I'm punting on both for now. Anyways, weeding out |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 1097 | # redundancies like this should probably be the province of |
| 1098 | # CCompiler, since the data structures used are inherited from it |
| 1099 | # and therefore common to all CCompiler classes. |
| 1100 | |
| 1101 | pp_opts = [] |
| 1102 | for macro in macros: |
Greg Ward | fbf8aff | 1999-09-21 18:35:09 +0000 | [diff] [blame] | 1103 | |
| 1104 | if not (type (macro) is TupleType and |
| 1105 | 1 <= len (macro) <= 2): |
| 1106 | raise TypeError, \ |
| 1107 | ("bad macro definition '%s': " + |
| 1108 | "each element of 'macros' list must be a 1- or 2-tuple") % \ |
| 1109 | macro |
| 1110 | |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 1111 | if len (macro) == 1: # undefine this macro |
| 1112 | pp_opts.append ("-U%s" % macro[0]) |
| 1113 | elif len (macro) == 2: |
| 1114 | if macro[1] is None: # define with no explicit value |
| 1115 | pp_opts.append ("-D%s" % macro[0]) |
| 1116 | else: |
| 1117 | # XXX *don't* need to be clever about quoting the |
| 1118 | # macro value here, because we're going to avoid the |
| 1119 | # shell at all costs when we spawn the command! |
| 1120 | pp_opts.append ("-D%s=%s" % macro) |
| 1121 | |
Greg Ward | 0bdd90a | 1999-12-12 17:19:58 +0000 | [diff] [blame] | 1122 | for dir in include_dirs: |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 1123 | pp_opts.append ("-I%s" % dir) |
| 1124 | |
| 1125 | return pp_opts |
| 1126 | |
| 1127 | # gen_preprocess_options () |
| 1128 | |
| 1129 | |
Greg Ward | d03f88a | 2000-03-18 15:19:51 +0000 | [diff] [blame] | 1130 | def gen_lib_options (compiler, library_dirs, runtime_library_dirs, libraries): |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 1131 | """Generate linker options for searching library directories and |
Greg Ward | c3a43b4 | 2000-06-24 18:10:48 +0000 | [diff] [blame] | 1132 | linking with specific libraries. 'libraries' and 'library_dirs' are, |
| 1133 | respectively, lists of library names (not filenames!) and search |
| 1134 | directories. Returns a list of command-line options suitable for use |
| 1135 | with some compiler (depending on the two format strings passed in). |
| 1136 | """ |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 1137 | lib_opts = [] |
| 1138 | |
| 1139 | for dir in library_dirs: |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 1140 | lib_opts.append (compiler.library_dir_option (dir)) |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 1141 | |
Greg Ward | d03f88a | 2000-03-18 15:19:51 +0000 | [diff] [blame] | 1142 | for dir in runtime_library_dirs: |
| 1143 | lib_opts.append (compiler.runtime_library_dir_option (dir)) |
| 1144 | |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 1145 | # XXX it's important that we *not* remove redundant library mentions! |
| 1146 | # sometimes you really do have to say "-lfoo -lbar -lfoo" in order to |
| 1147 | # resolve all symbols. I just hope we never have to say "-lfoo obj.o |
| 1148 | # -lbar" to get things to work -- that's certainly a possibility, but a |
| 1149 | # pretty nasty way to arrange your C code. |
| 1150 | |
| 1151 | for lib in libraries: |
Greg Ward | 3febd60 | 1999-10-03 20:41:02 +0000 | [diff] [blame] | 1152 | (lib_dir, lib_name) = os.path.split (lib) |
| 1153 | if lib_dir: |
| 1154 | lib_file = compiler.find_library_file ([lib_dir], lib_name) |
| 1155 | if lib_file: |
| 1156 | lib_opts.append (lib_file) |
| 1157 | else: |
| 1158 | compiler.warn ("no library file corresponding to " |
| 1159 | "'%s' found (skipping)" % lib) |
| 1160 | else: |
| 1161 | lib_opts.append (compiler.library_option (lib)) |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 1162 | |
| 1163 | return lib_opts |
| 1164 | |
Greg Ward | 32c4a8a | 2000-03-06 03:40:29 +0000 | [diff] [blame] | 1165 | # gen_lib_options () |