Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 1 | """distutils.ccompiler |
| 2 | |
| 3 | Contains CCompiler, an abstract base class that defines the interface |
| 4 | for the Distutils compiler abstraction model.""" |
| 5 | |
| 6 | # created 1999/07/05, Greg Ward |
| 7 | |
| 8 | __rcsid__ = "$Id$" |
| 9 | |
| 10 | import os |
| 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 | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame^] | 15 | from distutils.util import move_file |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 16 | |
| 17 | |
| 18 | class CCompiler: |
| 19 | """Abstract base class to define the interface that must be implemented |
| 20 | by real compiler abstraction classes. Might have some use as a |
| 21 | place for shared code, but it's not yet clear what code can be |
| 22 | shared between compiler abstraction models for different platforms. |
| 23 | |
| 24 | The basic idea behind a compiler abstraction class is that each |
| 25 | instance can be used for all the compile/link steps in building |
| 26 | a single project. Thus, attributes common to all of those compile |
| 27 | and link steps -- include directories, macros to define, libraries |
| 28 | to link against, etc. -- are attributes of the compiler instance. |
| 29 | To allow for variability in how individual files are treated, |
| 30 | most (all?) of those attributes may be varied on a per-compilation |
| 31 | or per-link basis.""" |
| 32 | |
| 33 | |
| 34 | # XXX things not handled by this compiler abstraction model: |
| 35 | # * client can't provide additional options for a compiler, |
| 36 | # e.g. warning, optimization, debugging flags. Perhaps this |
| 37 | # should be the domain of concrete compiler abstraction classes |
| 38 | # (UnixCCompiler, MSVCCompiler, etc.) -- or perhaps the base |
| 39 | # class should have methods for the common ones. |
| 40 | # * can't put output files (object files, libraries, whatever) |
| 41 | # into a separate directory from their inputs. Should this be |
| 42 | # handled by an 'output_dir' attribute of the whole object, or a |
| 43 | # parameter to the compile/link_* methods, or both? |
| 44 | # * can't completely override the include or library searchg |
| 45 | # path, ie. no "cc -I -Idir1 -Idir2" or "cc -L -Ldir1 -Ldir2". |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 46 | # I'm not sure how widely supported this is even by Unix |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 47 | # compilers, much less on other platforms. And I'm even less |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 48 | # sure how useful it is; maybe for cross-compiling, but |
| 49 | # support for that is a ways off. (And anyways, cross |
| 50 | # compilers probably have a dedicated binary with the |
| 51 | # right paths compiled in. I hope.) |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 52 | # * can't do really freaky things with the library list/library |
| 53 | # dirs, e.g. "-Ldir1 -lfoo -Ldir2 -lfoo" to link against |
| 54 | # different versions of libfoo.a in different locations. I |
| 55 | # think this is useless without the ability to null out the |
| 56 | # library search path anyways. |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 57 | |
| 58 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 59 | def __init__ (self, |
| 60 | verbose=0, |
| 61 | dry_run=0): |
| 62 | |
| 63 | self.verbose = verbose |
| 64 | self.dry_run = dry_run |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 65 | |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame^] | 66 | # 'output_dir': a common output directory for object, library, |
| 67 | # shared object, and shared library files |
| 68 | self.output_dir = None |
| 69 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 70 | # 'macros': a list of macro definitions (or undefinitions). A |
| 71 | # macro definition is a 2-tuple (name, value), where the value is |
| 72 | # either a string or None (no explicit value). A macro |
| 73 | # undefinition is a 1-tuple (name,). |
| 74 | self.macros = [] |
| 75 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 76 | # 'include_dirs': a list of directories to search for include files |
| 77 | self.include_dirs = [] |
| 78 | |
| 79 | # 'libraries': a list of libraries to include in any link |
| 80 | # (library names, not filenames: eg. "foo" not "libfoo.a") |
| 81 | self.libraries = [] |
| 82 | |
| 83 | # 'library_dirs': a list of directories to search for libraries |
| 84 | self.library_dirs = [] |
| 85 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 86 | # 'runtime_library_dirs': a list of directories to search for |
| 87 | # shared libraries/objects at runtime |
| 88 | self.runtime_library_dirs = [] |
| 89 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 90 | # 'objects': a list of object files (or similar, such as explicitly |
| 91 | # named library files) to include on any link |
| 92 | self.objects = [] |
| 93 | |
| 94 | # __init__ () |
| 95 | |
| 96 | |
| 97 | def _find_macro (self, name): |
| 98 | i = 0 |
| 99 | for defn in self.macros: |
| 100 | if defn[0] == name: |
| 101 | return i |
| 102 | i = i + 1 |
| 103 | |
| 104 | return None |
| 105 | |
| 106 | |
| 107 | def _check_macro_definitions (self, definitions): |
| 108 | """Ensures that every element of 'definitions' is a valid macro |
| 109 | definition, ie. either (name,value) 2-tuple or a (name,) |
| 110 | tuple. Do nothing if all definitions are OK, raise |
| 111 | TypeError otherwise.""" |
| 112 | |
| 113 | for defn in definitions: |
| 114 | if not (type (defn) is TupleType and |
| 115 | (len (defn) == 1 or |
| 116 | (len (defn) == 2 and |
| 117 | (type (defn[1]) is StringType or defn[1] is None))) and |
| 118 | type (defn[0]) is StringType): |
| 119 | raise TypeError, \ |
| 120 | ("invalid macro definition '%s': " % defn) + \ |
| 121 | "must be tuple (string,), (string, string), or " + \ |
| 122 | "(string, None)" |
| 123 | |
| 124 | |
| 125 | # -- Bookkeeping methods ------------------------------------------- |
| 126 | |
| 127 | def define_macro (self, name, value=None): |
| 128 | """Define a preprocessor macro for all compilations driven by |
| 129 | this compiler object. The optional parameter 'value' should be |
| 130 | a string; if it is not supplied, then the macro will be defined |
| 131 | without an explicit value and the exact outcome depends on the |
| 132 | compiler used (XXX true? does ANSI say anything about this?)""" |
| 133 | |
| 134 | # Delete from the list of macro definitions/undefinitions if |
| 135 | # already there (so that this one will take precedence). |
| 136 | i = self._find_macro (name) |
| 137 | if i is not None: |
| 138 | del self.macros[i] |
| 139 | |
| 140 | defn = (name, value) |
| 141 | self.macros.append (defn) |
| 142 | |
| 143 | |
| 144 | def undefine_macro (self, name): |
| 145 | """Undefine a preprocessor macro for all compilations driven by |
| 146 | this compiler object. If the same macro is defined by |
| 147 | 'define_macro()' and undefined by 'undefine_macro()' the last |
| 148 | call takes precedence (including multiple redefinitions or |
| 149 | undefinitions). If the macro is redefined/undefined on a |
| 150 | per-compilation basis (ie. in the call to 'compile()'), then |
| 151 | that takes precedence.""" |
| 152 | |
| 153 | # Delete from the list of macro definitions/undefinitions if |
| 154 | # already there (so that this one will take precedence). |
| 155 | i = self._find_macro (name) |
| 156 | if i is not None: |
| 157 | del self.macros[i] |
| 158 | |
| 159 | undefn = (name,) |
| 160 | self.macros.append (undefn) |
| 161 | |
| 162 | |
| 163 | def add_include_dir (self, dir): |
| 164 | """Add 'dir' to the list of directories that will be searched |
| 165 | for header files. The compiler is instructed to search |
| 166 | directories in the order in which they are supplied by |
| 167 | successive calls to 'add_include_dir()'.""" |
| 168 | self.include_dirs.append (dir) |
| 169 | |
| 170 | def set_include_dirs (self, dirs): |
| 171 | """Set the list of directories that will be searched to 'dirs' |
| 172 | (a list of strings). Overrides any preceding calls to |
| 173 | 'add_include_dir()'; subsequence calls to 'add_include_dir()' |
| 174 | add to the list passed to 'set_include_dirs()'. This does |
| 175 | not affect any list of standard include directories that |
| 176 | the compiler may search by default.""" |
| 177 | self.include_dirs = copy (dirs) |
| 178 | |
| 179 | |
| 180 | def add_library (self, libname): |
| 181 | """Add 'libname' to the list of libraries that will be included |
| 182 | in all links driven by this compiler object. Note that |
| 183 | 'libname' should *not* be the name of a file containing a |
| 184 | library, but the name of the library itself: the actual filename |
| 185 | will be inferred by the linker, the compiler, or the compiler |
| 186 | abstraction class (depending on the platform). |
| 187 | |
| 188 | The linker will be instructed to link against libraries in the |
| 189 | order they were supplied to 'add_library()' and/or |
| 190 | 'set_libraries()'. It is perfectly valid to duplicate library |
| 191 | names; the linker will be instructed to link against libraries |
| 192 | as many times as they are mentioned.""" |
| 193 | self.libraries.append (libname) |
| 194 | |
| 195 | def set_libraries (self, libnames): |
| 196 | """Set the list of libraries to be included in all links driven |
| 197 | by this compiler object to 'libnames' (a list of strings). |
| 198 | This does not affect any standard system libraries that the |
| 199 | linker may include by default.""" |
| 200 | |
| 201 | self.libraries = copy (libnames) |
| 202 | |
| 203 | |
| 204 | def add_library_dir (self, dir): |
| 205 | """Add 'dir' to the list of directories that will be searched for |
| 206 | libraries specified to 'add_library()' and 'set_libraries()'. |
| 207 | The linker will be instructed to search for libraries in the |
| 208 | order they are supplied to 'add_library_dir()' and/or |
| 209 | 'set_library_dirs()'.""" |
| 210 | self.library_dirs.append (dir) |
| 211 | |
| 212 | def set_library_dirs (self, dirs): |
| 213 | """Set the list of library search directories to 'dirs' (a list |
| 214 | of strings). This does not affect any standard library |
| 215 | search path that the linker may search by default.""" |
| 216 | self.library_dirs = copy (dirs) |
| 217 | |
| 218 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 219 | def add_runtime_library_dir (self, dir): |
| 220 | """Add 'dir' to the list of directories that will be searched for |
| 221 | shared libraries at runtime.""" |
| 222 | self.runtime_library_dirs.append (dir) |
| 223 | |
| 224 | def set_runtime_library_dirs (self, dirs): |
| 225 | """Set the list of directories to search for shared libraries |
| 226 | at runtime to 'dirs' (a list of strings). This does not affect |
| 227 | any standard search path that the runtime linker may search by |
| 228 | default.""" |
| 229 | self.runtime_library_dirs = copy (dirs) |
| 230 | |
| 231 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 232 | def add_link_object (self, object): |
| 233 | """Add 'object' to the list of object files (or analogues, such |
| 234 | as explictly named library files or the output of "resource |
| 235 | compilers") to be included in every link driven by this |
| 236 | compiler object.""" |
| 237 | self.objects.append (object) |
| 238 | |
| 239 | def set_link_objects (self, objects): |
| 240 | """Set the list of object files (or analogues) to be included |
| 241 | in every link to 'objects'. This does not affect any |
| 242 | standard object files that the linker may include by default |
| 243 | (such as system libraries).""" |
| 244 | self.objects = copy (objects) |
| 245 | |
| 246 | |
| 247 | # -- Worker methods ------------------------------------------------ |
| 248 | # (must be implemented by subclasses) |
| 249 | |
| 250 | def compile (self, |
| 251 | sources, |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame^] | 252 | output_dir=None, |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 253 | macros=None, |
| 254 | includes=None): |
| 255 | """Compile one or more C/C++ source files. 'sources' must be |
| 256 | a list of strings, each one the name of a C/C++ source |
| 257 | file. Return a list of the object filenames generated |
| 258 | (one for each source filename in 'sources'). |
| 259 | |
| 260 | 'macros', if given, must be a list of macro definitions. A |
| 261 | macro definition is either a (name, value) 2-tuple or a (name,) |
| 262 | 1-tuple. The former defines a macro; if the value is None, the |
| 263 | macro is defined without an explicit value. The 1-tuple case |
| 264 | undefines a macro. Later definitions/redefinitions/ |
| 265 | undefinitions take precedence. |
| 266 | |
| 267 | 'includes', if given, must be a list of strings, the directories |
| 268 | to add to the default include file search path for this |
| 269 | compilation only.""" |
| 270 | pass |
| 271 | |
| 272 | |
| 273 | # XXX this is kind of useless without 'link_binary()' or |
| 274 | # 'link_executable()' or something -- or maybe 'link_static_lib()' |
| 275 | # should not exist at all, and we just have 'link_binary()'? |
| 276 | def link_static_lib (self, |
| 277 | objects, |
| 278 | output_libname, |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame^] | 279 | output_dir=None, |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 280 | libraries=None, |
| 281 | library_dirs=None): |
| 282 | """Link a bunch of stuff together to create a static library |
| 283 | file. The "bunch of stuff" consists of the list of object |
| 284 | files supplied as 'objects', the extra object files supplied |
| 285 | to 'add_link_object()' and/or 'set_link_objects()', the |
| 286 | libraries supplied to 'add_library()' and/or |
| 287 | 'set_libraries()', and the libraries supplied as 'libraries' |
| 288 | (if any). |
| 289 | |
| 290 | 'output_libname' should be a library name, not a filename; |
| 291 | the filename will be inferred from the library name. |
| 292 | |
| 293 | 'library_dirs', if supplied, should be a list of additional |
| 294 | directories to search on top of the system default and those |
| 295 | supplied to 'add_library_dir()' and/or 'set_library_dirs()'.""" |
| 296 | |
| 297 | pass |
| 298 | |
| 299 | |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 300 | # XXX passing in 'build_info' here is a kludge to deal with the |
| 301 | # oddities of one particular compiler (Visual C++). For some reason, |
| 302 | # it needs to be told about ".def" files, and currently the |
| 303 | # 'build_info' hash allows this through a 'def_file' element. The link |
| 304 | # methods for VC++ look for 'def_file' and transform it into the |
| 305 | # appropriate command-line options. The current code is objectionable |
| 306 | # for a number of reasons: 1) if the link methods take 'build_info', |
| 307 | # why bother passing in libraries, library_dirs, etc.? 2) if the link |
| 308 | # methods do it, why not the compile methods? 3) build_info is part of |
| 309 | # the interface between setup.py and the 'build_ext' command -- it |
| 310 | # should stop there and not be propagated down into the compiler |
| 311 | # classes! and 4) I don't like elevating a platform- and |
| 312 | # compiler-specific oddity to "first-class" status in 'build_info' (oh |
| 313 | # well, at least it's not being reified in the compiler classes -- that |
| 314 | # would be really gross). |
| 315 | # |
| 316 | # Possible solutions: |
| 317 | # - just pass build_info to all the compile/link methods, |
| 318 | # never mind all those other parameters and screw the |
| 319 | # integrity of the interfaces |
| 320 | # - add a mechanism for passing platform-specific and/or |
| 321 | # compiler-specific compiler/linker options from setup.py |
| 322 | # straight through to the appropriate compiler class |
| 323 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 324 | def link_shared_lib (self, |
| 325 | objects, |
| 326 | output_libname, |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame^] | 327 | output_dir=None, |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 328 | libraries=None, |
Greg Ward | 26e48ea | 1999-08-29 18:17:36 +0000 | [diff] [blame] | 329 | library_dirs=None, |
| 330 | build_info=None): |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 331 | """Link a bunch of stuff together to create a shared library |
| 332 | file. Has the same effect as 'link_static_lib()' except |
| 333 | that the filename inferred from 'output_libname' will most |
| 334 | likely be different, and the type of file generated will |
| 335 | almost certainly be different.""" |
| 336 | pass |
| 337 | |
| 338 | def link_shared_object (self, |
| 339 | objects, |
| 340 | output_filename, |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame^] | 341 | output_dir=None, |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 342 | libraries=None, |
Greg Ward | 26e48ea | 1999-08-29 18:17:36 +0000 | [diff] [blame] | 343 | library_dirs=None, |
| 344 | build_info=None): |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 345 | """Link a bunch of stuff together to create a shared object |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame^] | 346 | file. Much like 'link_shared_lib()', except the output filename |
| 347 | is explicitly supplied as 'output_filename'. If 'output_dir' is |
| 348 | supplied, 'output_filename' is relative to it |
| 349 | (i.e. 'output_filename' can provide directoriy components if |
| 350 | needed).""" |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 351 | pass |
| 352 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 353 | |
| 354 | # -- Filename mangling methods ------------------------------------- |
| 355 | |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame^] | 356 | # General principle for the filename-mangling methods: by default, |
| 357 | # don't include a directory component, no matter what the caller |
| 358 | # supplies. Eg. for UnixCCompiler, a source file of "foo/bar/baz.c" |
| 359 | # becomes "baz.o" or "baz.so", etc. (That way, it's easiest for the |
| 360 | # caller to decide where it wants to put/find the output file.) The |
| 361 | # 'output_dir' parameter overrides this, of course -- the directory |
| 362 | # component of the input filenames is replaced by 'output_dir'. |
| 363 | |
| 364 | def object_filenames (self, source_filenames, output_dir=None): |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 365 | """Return the list of object filenames corresponding to each |
| 366 | specified source filename.""" |
| 367 | pass |
| 368 | |
Greg Ward | 26e48ea | 1999-08-29 18:17:36 +0000 | [diff] [blame] | 369 | def shared_object_filename (self, source_filename): |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 370 | """Return the shared object filename corresponding to a |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame^] | 371 | specified source filename (assuming the same directory).""" |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 372 | pass |
| 373 | |
Greg Ward | 26e48ea | 1999-08-29 18:17:36 +0000 | [diff] [blame] | 374 | def library_filename (self, libname): |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 375 | """Return the static library filename corresponding to the |
| 376 | specified library name.""" |
| 377 | |
| 378 | pass |
| 379 | |
Greg Ward | 26e48ea | 1999-08-29 18:17:36 +0000 | [diff] [blame] | 380 | def shared_library_filename (self, libname): |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 381 | """Return the shared library filename corresponding to the |
| 382 | specified library name.""" |
| 383 | pass |
| 384 | |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame^] | 385 | # XXX ugh -- these should go! |
Greg Ward | 26e48ea | 1999-08-29 18:17:36 +0000 | [diff] [blame] | 386 | def object_name (self, inname): |
| 387 | """Given a name with no extension, return the name + object extension""" |
| 388 | return inname + self._obj_ext |
| 389 | |
| 390 | def shared_library_name (self, inname): |
| 391 | """Given a name with no extension, return the name + shared object extension""" |
| 392 | return inname + self._shared_lib_ext |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 393 | |
| 394 | # -- Utility methods ----------------------------------------------- |
| 395 | |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame^] | 396 | def announce (self, msg, level=1): |
| 397 | if self.verbose >= level: |
| 398 | print msg |
| 399 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 400 | def spawn (self, cmd): |
| 401 | spawn (cmd, verbose=self.verbose, dry_run=self.dry_run) |
| 402 | |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame^] | 403 | def move_file (self, src, dst): |
| 404 | return move_file (src, dst, verbose=self.verbose, dry_run=self.dry_run) |
| 405 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 406 | |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 407 | # class CCompiler |
| 408 | |
| 409 | |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 410 | def new_compiler (plat=None, |
| 411 | verbose=0, |
| 412 | dry_run=0): |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 413 | """Generate a CCompiler instance for platform 'plat' (or the |
| 414 | current platform, if 'plat' not supplied). Really instantiates |
| 415 | some concrete subclass of CCompiler, of course.""" |
| 416 | |
| 417 | if plat is None: plat = os.name |
| 418 | if plat == 'posix': |
| 419 | from unixccompiler import UnixCCompiler |
Greg Ward | e1aaaa6 | 1999-08-14 23:50:50 +0000 | [diff] [blame] | 420 | return UnixCCompiler (verbose, dry_run) |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 421 | elif plat == 'nt': |
Greg Ward | 26e48ea | 1999-08-29 18:17:36 +0000 | [diff] [blame] | 422 | from msvccompiler import MSVCCompiler |
Greg Ward | 9b17cb5 | 1999-09-13 03:07:24 +0000 | [diff] [blame^] | 423 | return MSVCCompiler (verbose, dry_run) |
Greg Ward | 3f81cf7 | 1999-07-10 02:03:53 +0000 | [diff] [blame] | 424 | else: |
| 425 | raise DistutilsPlatformError, \ |
| 426 | "don't know how to compile C/C++ code on platform %s" % plat |
Greg Ward | f7a39ec | 1999-09-08 02:29:08 +0000 | [diff] [blame] | 427 | |
| 428 | |
| 429 | def gen_preprocess_options (macros, includes): |
| 430 | """Generate C pre-processor options (-D, -U, -I) as used by at |
| 431 | least two types of compilers: the typical Unix compiler and Visual |
| 432 | C++. 'macros' is the usual thing, a list of 1- or 2-tuples, where |
| 433 | (name,) means undefine (-U) macro 'name', and (name,value) means |
| 434 | define (-D) macro 'name' to 'value'. 'includes' is just a list of |
| 435 | directory names to be added to the header file search path (-I). |
| 436 | Returns a list of command-line options suitable for either |
| 437 | Unix compilers or Visual C++.""" |
| 438 | |
| 439 | # XXX it would be nice (mainly aesthetic, and so we don't generate |
| 440 | # stupid-looking command lines) to go over 'macros' and eliminate |
| 441 | # redundant definitions/undefinitions (ie. ensure that only the |
| 442 | # latest mention of a particular macro winds up on the command |
| 443 | # line). I don't think it's essential, though, since most (all?) |
| 444 | # Unix C compilers only pay attention to the latest -D or -U |
| 445 | # mention of a macro on their command line. Similar situation for |
| 446 | # 'includes'. I'm punting on both for now. Anyways, weeding out |
| 447 | # redundancies like this should probably be the province of |
| 448 | # CCompiler, since the data structures used are inherited from it |
| 449 | # and therefore common to all CCompiler classes. |
| 450 | |
| 451 | pp_opts = [] |
| 452 | for macro in macros: |
| 453 | if len (macro) == 1: # undefine this macro |
| 454 | pp_opts.append ("-U%s" % macro[0]) |
| 455 | elif len (macro) == 2: |
| 456 | if macro[1] is None: # define with no explicit value |
| 457 | pp_opts.append ("-D%s" % macro[0]) |
| 458 | else: |
| 459 | # XXX *don't* need to be clever about quoting the |
| 460 | # macro value here, because we're going to avoid the |
| 461 | # shell at all costs when we spawn the command! |
| 462 | pp_opts.append ("-D%s=%s" % macro) |
| 463 | |
| 464 | for dir in includes: |
| 465 | pp_opts.append ("-I%s" % dir) |
| 466 | |
| 467 | return pp_opts |
| 468 | |
| 469 | # gen_preprocess_options () |
| 470 | |
| 471 | |
| 472 | def gen_lib_options (libraries, library_dirs, lib_format, dir_format): |
| 473 | """Generate linker options for searching library directories and |
| 474 | linking with specific libraries. 'libraries' and 'library_dirs' |
| 475 | are, respectively, lists of library names (not filenames!) and |
| 476 | search directories. 'lib_format' is a format string with exactly |
| 477 | one "%s", into which will be plugged each library name in turn; |
| 478 | 'dir_format' is similar, but directory names will be plugged into |
| 479 | it. Returns a list of command-line options suitable for use with |
| 480 | some compiler (depending on the two format strings passed in).""" |
| 481 | |
| 482 | lib_opts = [] |
| 483 | |
| 484 | for dir in library_dirs: |
| 485 | lib_opts.append (dir_format % dir) |
| 486 | |
| 487 | # XXX it's important that we *not* remove redundant library mentions! |
| 488 | # sometimes you really do have to say "-lfoo -lbar -lfoo" in order to |
| 489 | # resolve all symbols. I just hope we never have to say "-lfoo obj.o |
| 490 | # -lbar" to get things to work -- that's certainly a possibility, but a |
| 491 | # pretty nasty way to arrange your C code. |
| 492 | |
| 493 | for lib in libraries: |
| 494 | lib_opts.append (lib_format % lib) |
| 495 | |
| 496 | return lib_opts |
| 497 | |
| 498 | # _gen_lib_options () |