Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 1 | """Abstract base class for compilers. |
| 2 | |
| 3 | This modules contains CCompiler, an abstract base class that defines the |
| 4 | interface for the compiler abstraction model used by packaging. |
| 5 | """ |
| 6 | |
| 7 | import os |
| 8 | import sys |
| 9 | from shutil import move |
| 10 | from packaging import logger |
| 11 | from packaging.util import split_quoted, execute, newer_group, spawn |
| 12 | from packaging.errors import (CompileError, LinkError, UnknownFileError) |
| 13 | from packaging.compiler import gen_preprocess_options |
| 14 | |
| 15 | |
| 16 | class CCompiler: |
| 17 | """Abstract base class to define the interface that must be implemented |
| 18 | by real compiler classes. Also has some utility methods used by |
| 19 | several compiler classes. |
| 20 | |
| 21 | The basic idea behind a compiler abstraction class is that each |
| 22 | instance can be used for all the compile/link steps in building a |
| 23 | single project. Thus, attributes common to all of those compile and |
| 24 | link steps -- include directories, macros to define, libraries to link |
| 25 | against, etc. -- are attributes of the compiler instance. To allow for |
| 26 | variability in how individual files are treated, most of those |
| 27 | attributes may be varied on a per-compilation or per-link basis. |
| 28 | """ |
| 29 | |
| 30 | # 'name' is a class attribute that identifies this class. It |
| 31 | # keeps code that wants to know what kind of compiler it's dealing with |
| 32 | # from having to import all possible compiler classes just to do an |
| 33 | # 'isinstance'. |
| 34 | name = None |
| 35 | description = None |
| 36 | |
| 37 | # XXX things not handled by this compiler abstraction model: |
| 38 | # * client can't provide additional options for a compiler, |
| 39 | # e.g. warning, optimization, debugging flags. Perhaps this |
| 40 | # should be the domain of concrete compiler abstraction classes |
| 41 | # (UnixCCompiler, MSVCCompiler, etc.) -- or perhaps the base |
| 42 | # class should have methods for the common ones. |
| 43 | # * can't completely override the include or library searchg |
| 44 | # path, ie. no "cc -I -Idir1 -Idir2" or "cc -L -Ldir1 -Ldir2". |
| 45 | # I'm not sure how widely supported this is even by Unix |
| 46 | # compilers, much less on other platforms. And I'm even less |
| 47 | # sure how useful it is; maybe for cross-compiling, but |
| 48 | # support for that is a ways off. (And anyways, cross |
| 49 | # compilers probably have a dedicated binary with the |
| 50 | # right paths compiled in. I hope.) |
| 51 | # * can't do really freaky things with the library list/library |
| 52 | # dirs, e.g. "-Ldir1 -lfoo -Ldir2 -lfoo" to link against |
| 53 | # different versions of libfoo.a in different locations. I |
| 54 | # think this is useless without the ability to null out the |
| 55 | # library search path anyways. |
| 56 | |
| 57 | |
| 58 | # Subclasses that rely on the standard filename generation methods |
| 59 | # implemented below should override these; see the comment near |
| 60 | # those methods ('object_filenames()' et. al.) for details: |
| 61 | src_extensions = None # list of strings |
| 62 | obj_extension = None # string |
| 63 | static_lib_extension = None |
| 64 | shared_lib_extension = None # string |
| 65 | static_lib_format = None # format string |
| 66 | shared_lib_format = None # prob. same as static_lib_format |
| 67 | exe_extension = None # string |
| 68 | |
| 69 | # Default language settings. language_map is used to detect a source |
| 70 | # file or Extension target language, checking source filenames. |
| 71 | # language_order is used to detect the language precedence, when deciding |
| 72 | # what language to use when mixing source types. For example, if some |
| 73 | # extension has two files with ".c" extension, and one with ".cpp", it |
| 74 | # is still linked as c++. |
| 75 | language_map = {".c": "c", |
| 76 | ".cc": "c++", |
| 77 | ".cpp": "c++", |
| 78 | ".cxx": "c++", |
| 79 | ".m": "objc", |
| 80 | } |
| 81 | language_order = ["c++", "objc", "c"] |
| 82 | |
| 83 | def __init__(self, verbose=0, dry_run=False, force=False): |
| 84 | self.dry_run = dry_run |
| 85 | self.force = force |
| 86 | self.verbose = verbose |
| 87 | |
| 88 | # 'output_dir': a common output directory for object, library, |
| 89 | # shared object, and shared library files |
| 90 | self.output_dir = None |
| 91 | |
| 92 | # 'macros': a list of macro definitions (or undefinitions). A |
| 93 | # macro definition is a 2-tuple (name, value), where the value is |
| 94 | # either a string or None (no explicit value). A macro |
| 95 | # undefinition is a 1-tuple (name,). |
| 96 | self.macros = [] |
| 97 | |
| 98 | # 'include_dirs': a list of directories to search for include files |
| 99 | self.include_dirs = [] |
| 100 | |
| 101 | # 'libraries': a list of libraries to include in any link |
| 102 | # (library names, not filenames: eg. "foo" not "libfoo.a") |
| 103 | self.libraries = [] |
| 104 | |
| 105 | # 'library_dirs': a list of directories to search for libraries |
| 106 | self.library_dirs = [] |
| 107 | |
| 108 | # 'runtime_library_dirs': a list of directories to search for |
| 109 | # shared libraries/objects at runtime |
| 110 | self.runtime_library_dirs = [] |
| 111 | |
| 112 | # 'objects': a list of object files (or similar, such as explicitly |
| 113 | # named library files) to include on any link |
| 114 | self.objects = [] |
| 115 | |
| 116 | for key, value in self.executables.items(): |
| 117 | self.set_executable(key, value) |
| 118 | |
| 119 | def set_executables(self, **args): |
| 120 | """Define the executables (and options for them) that will be run |
| 121 | to perform the various stages of compilation. The exact set of |
| 122 | executables that may be specified here depends on the compiler |
| 123 | class (via the 'executables' class attribute), but most will have: |
| 124 | compiler the C/C++ compiler |
| 125 | linker_so linker used to create shared objects and libraries |
| 126 | linker_exe linker used to create binary executables |
| 127 | archiver static library creator |
| 128 | |
| 129 | On platforms with a command line (Unix, DOS/Windows), each of these |
| 130 | is a string that will be split into executable name and (optional) |
| 131 | list of arguments. (Splitting the string is done similarly to how |
| 132 | Unix shells operate: words are delimited by spaces, but quotes and |
| 133 | backslashes can override this. See |
| 134 | 'distutils.util.split_quoted()'.) |
| 135 | """ |
| 136 | |
| 137 | # Note that some CCompiler implementation classes will define class |
| 138 | # attributes 'cpp', 'cc', etc. with hard-coded executable names; |
| 139 | # this is appropriate when a compiler class is for exactly one |
| 140 | # compiler/OS combination (eg. MSVCCompiler). Other compiler |
| 141 | # classes (UnixCCompiler, in particular) are driven by information |
| 142 | # discovered at run-time, since there are many different ways to do |
| 143 | # basically the same things with Unix C compilers. |
| 144 | |
| 145 | for key, value in args.items(): |
| 146 | if key not in self.executables: |
| 147 | raise ValueError("unknown executable '%s' for class %s" % \ |
| 148 | (key, self.__class__.__name__)) |
| 149 | self.set_executable(key, value) |
| 150 | |
| 151 | def set_executable(self, key, value): |
| 152 | if isinstance(value, str): |
| 153 | setattr(self, key, split_quoted(value)) |
| 154 | else: |
| 155 | setattr(self, key, value) |
| 156 | |
| 157 | def _find_macro(self, name): |
| 158 | i = 0 |
| 159 | for defn in self.macros: |
| 160 | if defn[0] == name: |
| 161 | return i |
| 162 | i = i + 1 |
| 163 | return None |
| 164 | |
| 165 | def _check_macro_definitions(self, definitions): |
| 166 | """Ensures that every element of 'definitions' is a valid macro |
| 167 | definition, ie. either (name,value) 2-tuple or a (name,) tuple. Do |
| 168 | nothing if all definitions are OK, raise TypeError otherwise. |
| 169 | """ |
| 170 | for defn in definitions: |
| 171 | if not (isinstance(defn, tuple) and |
| 172 | (len(defn) == 1 or |
| 173 | (len(defn) == 2 and |
| 174 | (isinstance(defn[1], str) or defn[1] is None))) and |
| 175 | isinstance(defn[0], str)): |
| 176 | raise TypeError(("invalid macro definition '%s': " % defn) + \ |
| 177 | "must be tuple (string,), (string, string), or " + \ |
| 178 | "(string, None)") |
| 179 | |
| 180 | |
| 181 | # -- Bookkeeping methods ------------------------------------------- |
| 182 | |
| 183 | def define_macro(self, name, value=None): |
| 184 | """Define a preprocessor macro for all compilations driven by this |
| 185 | compiler object. The optional parameter 'value' should be a |
| 186 | string; if it is not supplied, then the macro will be defined |
| 187 | without an explicit value and the exact outcome depends on the |
| 188 | compiler used (XXX true? does ANSI say anything about this?) |
| 189 | """ |
| 190 | # Delete from the list of macro definitions/undefinitions if |
| 191 | # already there (so that this one will take precedence). |
| 192 | i = self._find_macro(name) |
| 193 | if i is not None: |
| 194 | del self.macros[i] |
| 195 | |
| 196 | defn = (name, value) |
| 197 | self.macros.append(defn) |
| 198 | |
| 199 | def undefine_macro(self, name): |
| 200 | """Undefine a preprocessor macro for all compilations driven by |
| 201 | this compiler object. If the same macro is defined by |
| 202 | 'define_macro()' and undefined by 'undefine_macro()' the last call |
| 203 | takes precedence (including multiple redefinitions or |
| 204 | undefinitions). If the macro is redefined/undefined on a |
| 205 | per-compilation basis (ie. in the call to 'compile()'), then that |
| 206 | takes precedence. |
| 207 | """ |
| 208 | # Delete from the list of macro definitions/undefinitions if |
| 209 | # already there (so that this one will take precedence). |
| 210 | i = self._find_macro(name) |
| 211 | if i is not None: |
| 212 | del self.macros[i] |
| 213 | |
| 214 | undefn = (name,) |
| 215 | self.macros.append(undefn) |
| 216 | |
| 217 | def add_include_dir(self, dir): |
| 218 | """Add 'dir' to the list of directories that will be searched for |
| 219 | header files. The compiler is instructed to search directories in |
| 220 | the order in which they are supplied by successive calls to |
| 221 | 'add_include_dir()'. |
| 222 | """ |
| 223 | self.include_dirs.append(dir) |
| 224 | |
| 225 | def set_include_dirs(self, dirs): |
| 226 | """Set the list of directories that will be searched to 'dirs' (a |
| 227 | list of strings). Overrides any preceding calls to |
| 228 | 'add_include_dir()'; subsequence calls to 'add_include_dir()' add |
| 229 | to the list passed to 'set_include_dirs()'. This does not affect |
| 230 | any list of standard include directories that the compiler may |
| 231 | search by default. |
| 232 | """ |
| 233 | self.include_dirs = dirs[:] |
| 234 | |
| 235 | def add_library(self, libname): |
| 236 | """Add 'libname' to the list of libraries that will be included in |
| 237 | all links driven by this compiler object. Note that 'libname' |
| 238 | should *not* be the name of a file containing a library, but the |
| 239 | name of the library itself: the actual filename will be inferred by |
| 240 | the linker, the compiler, or the compiler class (depending on the |
| 241 | platform). |
| 242 | |
| 243 | The linker will be instructed to link against libraries in the |
| 244 | order they were supplied to 'add_library()' and/or |
| 245 | 'set_libraries()'. It is perfectly valid to duplicate library |
| 246 | names; the linker will be instructed to link against libraries as |
| 247 | many times as they are mentioned. |
| 248 | """ |
| 249 | self.libraries.append(libname) |
| 250 | |
| 251 | def set_libraries(self, libnames): |
| 252 | """Set the list of libraries to be included in all links driven by |
| 253 | this compiler object to 'libnames' (a list of strings). This does |
| 254 | not affect any standard system libraries that the linker may |
| 255 | include by default. |
| 256 | """ |
| 257 | self.libraries = libnames[:] |
| 258 | |
| 259 | |
| 260 | def add_library_dir(self, dir): |
| 261 | """Add 'dir' to the list of directories that will be searched for |
| 262 | libraries specified to 'add_library()' and 'set_libraries()'. The |
| 263 | linker will be instructed to search for libraries in the order they |
| 264 | are supplied to 'add_library_dir()' and/or 'set_library_dirs()'. |
| 265 | """ |
| 266 | self.library_dirs.append(dir) |
| 267 | |
| 268 | def set_library_dirs(self, dirs): |
| 269 | """Set the list of library search directories to 'dirs' (a list of |
| 270 | strings). This does not affect any standard library search path |
| 271 | that the linker may search by default. |
| 272 | """ |
| 273 | self.library_dirs = dirs[:] |
| 274 | |
| 275 | def add_runtime_library_dir(self, dir): |
| 276 | """Add 'dir' to the list of directories that will be searched for |
| 277 | shared libraries at runtime. |
| 278 | """ |
| 279 | self.runtime_library_dirs.append(dir) |
| 280 | |
| 281 | def set_runtime_library_dirs(self, dirs): |
| 282 | """Set the list of directories to search for shared libraries at |
| 283 | runtime to 'dirs' (a list of strings). This does not affect any |
| 284 | standard search path that the runtime linker may search by |
| 285 | default. |
| 286 | """ |
| 287 | self.runtime_library_dirs = dirs[:] |
| 288 | |
| 289 | def add_link_object(self, object): |
| 290 | """Add 'object' to the list of object files (or analogues, such as |
| 291 | explicitly named library files or the output of "resource |
| 292 | compilers") to be included in every link driven by this compiler |
| 293 | object. |
| 294 | """ |
| 295 | self.objects.append(object) |
| 296 | |
| 297 | def set_link_objects(self, objects): |
| 298 | """Set the list of object files (or analogues) to be included in |
| 299 | every link to 'objects'. This does not affect any standard object |
| 300 | files that the linker may include by default (such as system |
| 301 | libraries). |
| 302 | """ |
| 303 | self.objects = objects[:] |
| 304 | |
| 305 | |
| 306 | # -- Private utility methods -------------------------------------- |
| 307 | # (here for the convenience of subclasses) |
| 308 | |
| 309 | # Helper method to prep compiler in subclass compile() methods |
| 310 | def _setup_compile(self, outdir, macros, incdirs, sources, depends, |
| 311 | extra): |
| 312 | """Process arguments and decide which source files to compile.""" |
| 313 | if outdir is None: |
| 314 | outdir = self.output_dir |
| 315 | elif not isinstance(outdir, str): |
| 316 | raise TypeError("'output_dir' must be a string or None") |
| 317 | |
| 318 | if macros is None: |
| 319 | macros = self.macros |
| 320 | elif isinstance(macros, list): |
| 321 | macros = macros + (self.macros or []) |
| 322 | else: |
| 323 | raise TypeError("'macros' (if supplied) must be a list of tuples") |
| 324 | |
| 325 | if incdirs is None: |
| 326 | incdirs = self.include_dirs |
| 327 | elif isinstance(incdirs, (list, tuple)): |
| 328 | incdirs = list(incdirs) + (self.include_dirs or []) |
| 329 | else: |
| 330 | raise TypeError( |
| 331 | "'include_dirs' (if supplied) must be a list of strings") |
| 332 | |
| 333 | if extra is None: |
| 334 | extra = [] |
| 335 | |
| 336 | # Get the list of expected output (object) files |
| 337 | objects = self.object_filenames(sources, |
| 338 | strip_dir=False, |
| 339 | output_dir=outdir) |
| 340 | assert len(objects) == len(sources) |
| 341 | |
| 342 | pp_opts = gen_preprocess_options(macros, incdirs) |
| 343 | |
| 344 | build = {} |
| 345 | for i in range(len(sources)): |
| 346 | src = sources[i] |
| 347 | obj = objects[i] |
| 348 | ext = os.path.splitext(src)[1] |
| 349 | self.mkpath(os.path.dirname(obj)) |
| 350 | build[obj] = (src, ext) |
| 351 | |
| 352 | return macros, objects, extra, pp_opts, build |
| 353 | |
| 354 | def _get_cc_args(self, pp_opts, debug, before): |
Éric Araujo | 25987d0 | 2011-06-01 15:20:44 +0200 | [diff] [blame] | 355 | # works for unixccompiler and cygwinccompiler |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 356 | cc_args = pp_opts + ['-c'] |
| 357 | if debug: |
| 358 | cc_args[:0] = ['-g'] |
| 359 | if before: |
| 360 | cc_args[:0] = before |
| 361 | return cc_args |
| 362 | |
| 363 | def _fix_compile_args(self, output_dir, macros, include_dirs): |
| 364 | """Typecheck and fix-up some of the arguments to the 'compile()' |
| 365 | method, and return fixed-up values. Specifically: if 'output_dir' |
| 366 | is None, replaces it with 'self.output_dir'; ensures that 'macros' |
| 367 | is a list, and augments it with 'self.macros'; ensures that |
| 368 | 'include_dirs' is a list, and augments it with 'self.include_dirs'. |
| 369 | Guarantees that the returned values are of the correct type, |
| 370 | i.e. for 'output_dir' either string or None, and for 'macros' and |
| 371 | 'include_dirs' either list or None. |
| 372 | """ |
| 373 | if output_dir is None: |
| 374 | output_dir = self.output_dir |
| 375 | elif not isinstance(output_dir, str): |
| 376 | raise TypeError("'output_dir' must be a string or None") |
| 377 | |
| 378 | if macros is None: |
| 379 | macros = self.macros |
| 380 | elif isinstance(macros, list): |
| 381 | macros = macros + (self.macros or []) |
| 382 | else: |
| 383 | raise TypeError("'macros' (if supplied) must be a list of tuples") |
| 384 | |
| 385 | if include_dirs is None: |
| 386 | include_dirs = self.include_dirs |
| 387 | elif isinstance(include_dirs, (list, tuple)): |
| 388 | include_dirs = list(include_dirs) + (self.include_dirs or []) |
| 389 | else: |
| 390 | raise TypeError( |
| 391 | "'include_dirs' (if supplied) must be a list of strings") |
| 392 | |
| 393 | return output_dir, macros, include_dirs |
| 394 | |
| 395 | def _fix_object_args(self, objects, output_dir): |
| 396 | """Typecheck and fix up some arguments supplied to various methods. |
| 397 | Specifically: ensure that 'objects' is a list; if output_dir is |
| 398 | None, replace with self.output_dir. Return fixed versions of |
| 399 | 'objects' and 'output_dir'. |
| 400 | """ |
| 401 | if not isinstance(objects, (list, tuple)): |
| 402 | raise TypeError("'objects' must be a list or tuple of strings") |
| 403 | objects = list(objects) |
| 404 | |
| 405 | if output_dir is None: |
| 406 | output_dir = self.output_dir |
| 407 | elif not isinstance(output_dir, str): |
| 408 | raise TypeError("'output_dir' must be a string or None") |
| 409 | |
| 410 | return objects, output_dir |
| 411 | |
| 412 | def _fix_lib_args(self, libraries, library_dirs, runtime_library_dirs): |
| 413 | """Typecheck and fix up some of the arguments supplied to the |
| 414 | 'link_*' methods. Specifically: ensure that all arguments are |
| 415 | lists, and augment them with their permanent versions |
| 416 | (eg. 'self.libraries' augments 'libraries'). Return a tuple with |
| 417 | fixed versions of all arguments. |
| 418 | """ |
| 419 | if libraries is None: |
| 420 | libraries = self.libraries |
| 421 | elif isinstance(libraries, (list, tuple)): |
| 422 | libraries = list(libraries) + (self.libraries or []) |
| 423 | else: |
| 424 | raise TypeError( |
| 425 | "'libraries' (if supplied) must be a list of strings") |
| 426 | |
| 427 | if library_dirs is None: |
| 428 | library_dirs = self.library_dirs |
| 429 | elif isinstance(library_dirs, (list, tuple)): |
| 430 | library_dirs = list(library_dirs) + (self.library_dirs or []) |
| 431 | else: |
| 432 | raise TypeError( |
| 433 | "'library_dirs' (if supplied) must be a list of strings") |
| 434 | |
| 435 | if runtime_library_dirs is None: |
| 436 | runtime_library_dirs = self.runtime_library_dirs |
| 437 | elif isinstance(runtime_library_dirs, (list, tuple)): |
| 438 | runtime_library_dirs = (list(runtime_library_dirs) + |
| 439 | (self.runtime_library_dirs or [])) |
| 440 | else: |
| 441 | raise TypeError("'runtime_library_dirs' (if supplied) " |
| 442 | "must be a list of strings") |
| 443 | |
| 444 | return libraries, library_dirs, runtime_library_dirs |
| 445 | |
| 446 | def _need_link(self, objects, output_file): |
| 447 | """Return true if we need to relink the files listed in 'objects' |
| 448 | to recreate 'output_file'. |
| 449 | """ |
| 450 | if self.force: |
| 451 | return True |
| 452 | else: |
| 453 | if self.dry_run: |
| 454 | newer = newer_group(objects, output_file, missing='newer') |
| 455 | else: |
| 456 | newer = newer_group(objects, output_file) |
| 457 | return newer |
| 458 | |
| 459 | def detect_language(self, sources): |
| 460 | """Detect the language of a given file, or list of files. Uses |
| 461 | language_map, and language_order to do the job. |
| 462 | """ |
| 463 | if not isinstance(sources, list): |
| 464 | sources = [sources] |
| 465 | lang = None |
| 466 | index = len(self.language_order) |
| 467 | for source in sources: |
| 468 | base, ext = os.path.splitext(source) |
| 469 | extlang = self.language_map.get(ext) |
| 470 | try: |
| 471 | extindex = self.language_order.index(extlang) |
| 472 | if extindex < index: |
| 473 | lang = extlang |
| 474 | index = extindex |
| 475 | except ValueError: |
| 476 | pass |
| 477 | return lang |
| 478 | |
| 479 | # -- Worker methods ------------------------------------------------ |
| 480 | # (must be implemented by subclasses) |
| 481 | |
| 482 | def preprocess(self, source, output_file=None, macros=None, |
| 483 | include_dirs=None, extra_preargs=None, extra_postargs=None): |
| 484 | """Preprocess a single C/C++ source file, named in 'source'. |
| 485 | Output will be written to file named 'output_file', or stdout if |
| 486 | 'output_file' not supplied. 'macros' is a list of macro |
| 487 | definitions as for 'compile()', which will augment the macros set |
| 488 | with 'define_macro()' and 'undefine_macro()'. 'include_dirs' is a |
| 489 | list of directory names that will be added to the default list. |
| 490 | |
| 491 | Raises PreprocessError on failure. |
| 492 | """ |
| 493 | pass |
| 494 | |
| 495 | def compile(self, sources, output_dir=None, macros=None, |
| 496 | include_dirs=None, debug=False, extra_preargs=None, |
| 497 | extra_postargs=None, depends=None): |
| 498 | """Compile one or more source files. |
| 499 | |
| 500 | 'sources' must be a list of filenames, most likely C/C++ |
| 501 | files, but in reality anything that can be handled by a |
| 502 | particular compiler and compiler class (eg. MSVCCompiler can |
| 503 | handle resource files in 'sources'). Return a list of object |
| 504 | filenames, one per source filename in 'sources'. Depending on |
| 505 | the implementation, not all source files will necessarily be |
| 506 | compiled, but all corresponding object filenames will be |
| 507 | returned. |
| 508 | |
| 509 | If 'output_dir' is given, object files will be put under it, while |
| 510 | retaining their original path component. That is, "foo/bar.c" |
| 511 | normally compiles to "foo/bar.o" (for a Unix implementation); if |
| 512 | 'output_dir' is "build", then it would compile to |
| 513 | "build/foo/bar.o". |
| 514 | |
| 515 | 'macros', if given, must be a list of macro definitions. A macro |
| 516 | definition is either a (name, value) 2-tuple or a (name,) 1-tuple. |
| 517 | The former defines a macro; if the value is None, the macro is |
| 518 | defined without an explicit value. The 1-tuple case undefines a |
| 519 | macro. Later definitions/redefinitions/ undefinitions take |
| 520 | precedence. |
| 521 | |
| 522 | 'include_dirs', if given, must be a list of strings, the |
| 523 | directories to add to the default include file search path for this |
| 524 | compilation only. |
| 525 | |
| 526 | 'debug' is a boolean; if true, the compiler will be instructed to |
| 527 | output debug symbols in (or alongside) the object file(s). |
| 528 | |
| 529 | 'extra_preargs' and 'extra_postargs' are implementation- dependent. |
| 530 | On platforms that have the notion of a command line (e.g. Unix, |
| 531 | DOS/Windows), they are most likely lists of strings: extra |
| 532 | command-line arguments to prepand/append to the compiler command |
| 533 | line. On other platforms, consult the implementation class |
| 534 | documentation. In any event, they are intended as an escape hatch |
| 535 | for those occasions when the abstract compiler framework doesn't |
| 536 | cut the mustard. |
| 537 | |
| 538 | 'depends', if given, is a list of filenames that all targets |
| 539 | depend on. If a source file is older than any file in |
| 540 | depends, then the source file will be recompiled. This |
| 541 | supports dependency tracking, but only at a coarse |
| 542 | granularity. |
| 543 | |
| 544 | Raises CompileError on failure. |
| 545 | """ |
| 546 | # A concrete compiler class can either override this method |
| 547 | # entirely or implement _compile(). |
| 548 | |
| 549 | macros, objects, extra_postargs, pp_opts, build = \ |
| 550 | self._setup_compile(output_dir, macros, include_dirs, sources, |
| 551 | depends, extra_postargs) |
| 552 | cc_args = self._get_cc_args(pp_opts, debug, extra_preargs) |
| 553 | |
| 554 | for obj in objects: |
| 555 | try: |
| 556 | src, ext = build[obj] |
| 557 | except KeyError: |
| 558 | continue |
| 559 | self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts) |
| 560 | |
| 561 | # Return *all* object filenames, not just the ones we just built. |
| 562 | return objects |
| 563 | |
| 564 | def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts): |
| 565 | """Compile 'src' to product 'obj'.""" |
| 566 | |
| 567 | # A concrete compiler class that does not override compile() |
| 568 | # should implement _compile(). |
| 569 | pass |
| 570 | |
| 571 | def create_static_lib(self, objects, output_libname, output_dir=None, |
| 572 | debug=False, target_lang=None): |
| 573 | """Link a bunch of stuff together to create a static library file. |
| 574 | The "bunch of stuff" consists of the list of object files supplied |
| 575 | as 'objects', the extra object files supplied to |
| 576 | 'add_link_object()' and/or 'set_link_objects()', the libraries |
| 577 | supplied to 'add_library()' and/or 'set_libraries()', and the |
| 578 | libraries supplied as 'libraries' (if any). |
| 579 | |
| 580 | 'output_libname' should be a library name, not a filename; the |
| 581 | filename will be inferred from the library name. 'output_dir' is |
| 582 | the directory where the library file will be put. |
| 583 | |
| 584 | 'debug' is a boolean; if true, debugging information will be |
| 585 | included in the library (note that on most platforms, it is the |
| 586 | compile step where this matters: the 'debug' flag is included here |
| 587 | just for consistency). |
| 588 | |
| 589 | 'target_lang' is the target language for which the given objects |
| 590 | are being compiled. This allows specific linkage time treatment of |
| 591 | certain languages. |
| 592 | |
| 593 | Raises LibError on failure. |
| 594 | """ |
| 595 | pass |
| 596 | |
| 597 | # values for target_desc parameter in link() |
| 598 | SHARED_OBJECT = "shared_object" |
| 599 | SHARED_LIBRARY = "shared_library" |
| 600 | EXECUTABLE = "executable" |
| 601 | |
| 602 | def link(self, target_desc, objects, output_filename, output_dir=None, |
| 603 | libraries=None, library_dirs=None, runtime_library_dirs=None, |
| 604 | export_symbols=None, debug=False, extra_preargs=None, |
| 605 | extra_postargs=None, build_temp=None, target_lang=None): |
| 606 | """Link a bunch of stuff together to create an executable or |
| 607 | shared library file. |
| 608 | |
| 609 | The "bunch of stuff" consists of the list of object files supplied |
| 610 | as 'objects'. 'output_filename' should be a filename. If |
| 611 | 'output_dir' is supplied, 'output_filename' is relative to it |
| 612 | (i.e. 'output_filename' can provide directory components if |
| 613 | needed). |
| 614 | |
| 615 | 'libraries' is a list of libraries to link against. These are |
| 616 | library names, not filenames, since they're translated into |
| 617 | filenames in a platform-specific way (eg. "foo" becomes "libfoo.a" |
| 618 | on Unix and "foo.lib" on DOS/Windows). However, they can include a |
| 619 | directory component, which means the linker will look in that |
| 620 | specific directory rather than searching all the normal locations. |
| 621 | |
| 622 | 'library_dirs', if supplied, should be a list of directories to |
| 623 | search for libraries that were specified as bare library names |
| 624 | (ie. no directory component). These are on top of the system |
| 625 | default and those supplied to 'add_library_dir()' and/or |
| 626 | 'set_library_dirs()'. 'runtime_library_dirs' is a list of |
| 627 | directories that will be embedded into the shared library and used |
| 628 | to search for other shared libraries that *it* depends on at |
| 629 | run-time. (This may only be relevant on Unix.) |
| 630 | |
| 631 | 'export_symbols' is a list of symbols that the shared library will |
| 632 | export. (This appears to be relevant only on Windows.) |
| 633 | |
| 634 | 'debug' is as for 'compile()' and 'create_static_lib()', with the |
| 635 | slight distinction that it actually matters on most platforms (as |
| 636 | opposed to 'create_static_lib()', which includes a 'debug' flag |
| 637 | mostly for form's sake). |
| 638 | |
| 639 | 'extra_preargs' and 'extra_postargs' are as for 'compile()' (except |
| 640 | of course that they supply command-line arguments for the |
| 641 | particular linker being used). |
| 642 | |
| 643 | 'target_lang' is the target language for which the given objects |
| 644 | are being compiled. This allows specific linkage time treatment of |
| 645 | certain languages. |
| 646 | |
| 647 | Raises LinkError on failure. |
| 648 | """ |
| 649 | raise NotImplementedError |
| 650 | |
| 651 | |
| 652 | # Old 'link_*()' methods, rewritten to use the new 'link()' method. |
| 653 | |
| 654 | def link_shared_lib(self, objects, output_libname, output_dir=None, |
| 655 | libraries=None, library_dirs=None, |
| 656 | runtime_library_dirs=None, export_symbols=None, |
| 657 | debug=False, extra_preargs=None, extra_postargs=None, |
| 658 | build_temp=None, target_lang=None): |
| 659 | self.link(CCompiler.SHARED_LIBRARY, objects, |
| 660 | self.library_filename(output_libname, lib_type='shared'), |
| 661 | output_dir, |
| 662 | libraries, library_dirs, runtime_library_dirs, |
| 663 | export_symbols, debug, |
| 664 | extra_preargs, extra_postargs, build_temp, target_lang) |
| 665 | |
| 666 | def link_shared_object(self, objects, output_filename, output_dir=None, |
| 667 | libraries=None, library_dirs=None, |
| 668 | runtime_library_dirs=None, export_symbols=None, |
| 669 | debug=False, extra_preargs=None, extra_postargs=None, |
| 670 | build_temp=None, target_lang=None): |
| 671 | self.link(CCompiler.SHARED_OBJECT, objects, |
| 672 | output_filename, output_dir, |
| 673 | libraries, library_dirs, runtime_library_dirs, |
| 674 | export_symbols, debug, |
| 675 | extra_preargs, extra_postargs, build_temp, target_lang) |
| 676 | |
| 677 | def link_executable(self, objects, output_progname, output_dir=None, |
| 678 | libraries=None, library_dirs=None, |
| 679 | runtime_library_dirs=None, debug=False, |
| 680 | extra_preargs=None, extra_postargs=None, |
| 681 | target_lang=None): |
| 682 | self.link(CCompiler.EXECUTABLE, objects, |
| 683 | self.executable_filename(output_progname), output_dir, |
| 684 | libraries, library_dirs, runtime_library_dirs, None, |
| 685 | debug, extra_preargs, extra_postargs, None, target_lang) |
| 686 | |
| 687 | |
| 688 | # -- Miscellaneous methods ----------------------------------------- |
| 689 | # These are all used by the 'gen_lib_options() function; there is |
| 690 | # no appropriate default implementation so subclasses should |
| 691 | # implement all of these. |
| 692 | |
| 693 | def library_dir_option(self, dir): |
| 694 | """Return the compiler option to add 'dir' to the list of |
| 695 | directories searched for libraries. |
| 696 | """ |
| 697 | raise NotImplementedError |
| 698 | |
| 699 | def runtime_library_dir_option(self, dir): |
| 700 | """Return the compiler option to add 'dir' to the list of |
| 701 | directories searched for runtime libraries. |
| 702 | """ |
| 703 | raise NotImplementedError |
| 704 | |
| 705 | def library_option(self, lib): |
| 706 | """Return the compiler option to add 'dir' to the list of libraries |
| 707 | linked into the shared library or executable. |
| 708 | """ |
| 709 | raise NotImplementedError |
| 710 | |
| 711 | def has_function(self, funcname, includes=None, include_dirs=None, |
| 712 | libraries=None, library_dirs=None): |
| 713 | """Return a boolean indicating whether funcname is supported on |
| 714 | the current platform. The optional arguments can be used to |
| 715 | augment the compilation environment. |
| 716 | """ |
| 717 | |
| 718 | # this can't be included at module scope because it tries to |
| 719 | # import math which might not be available at that point - maybe |
| 720 | # the necessary logic should just be inlined? |
| 721 | import tempfile |
| 722 | if includes is None: |
| 723 | includes = [] |
| 724 | if include_dirs is None: |
| 725 | include_dirs = [] |
| 726 | if libraries is None: |
| 727 | libraries = [] |
| 728 | if library_dirs is None: |
| 729 | library_dirs = [] |
| 730 | fd, fname = tempfile.mkstemp(".c", funcname, text=True) |
Victor Stinner | 21a9c74 | 2011-05-19 15:51:27 +0200 | [diff] [blame] | 731 | with os.fdopen(fd, "w") as f: |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 732 | for incl in includes: |
| 733 | f.write("""#include "%s"\n""" % incl) |
| 734 | f.write("""\ |
| 735 | main (int argc, char **argv) { |
| 736 | %s(); |
| 737 | } |
| 738 | """ % funcname) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 739 | try: |
| 740 | objects = self.compile([fname], include_dirs=include_dirs) |
| 741 | except CompileError: |
| 742 | return False |
| 743 | |
| 744 | try: |
| 745 | self.link_executable(objects, "a.out", |
| 746 | libraries=libraries, |
| 747 | library_dirs=library_dirs) |
| 748 | except (LinkError, TypeError): |
| 749 | return False |
| 750 | return True |
| 751 | |
| 752 | def find_library_file(self, dirs, lib, debug=False): |
| 753 | """Search the specified list of directories for a static or shared |
| 754 | library file 'lib' and return the full path to that file. If |
| 755 | 'debug' is true, look for a debugging version (if that makes sense on |
| 756 | the current platform). Return None if 'lib' wasn't found in any of |
| 757 | the specified directories. |
| 758 | """ |
| 759 | raise NotImplementedError |
| 760 | |
| 761 | # -- Filename generation methods ----------------------------------- |
| 762 | |
| 763 | # The default implementation of the filename generating methods are |
| 764 | # prejudiced towards the Unix/DOS/Windows view of the world: |
| 765 | # * object files are named by replacing the source file extension |
| 766 | # (eg. .c/.cpp -> .o/.obj) |
| 767 | # * library files (shared or static) are named by plugging the |
| 768 | # library name and extension into a format string, eg. |
| 769 | # "lib%s.%s" % (lib_name, ".a") for Unix static libraries |
| 770 | # * executables are named by appending an extension (possibly |
| 771 | # empty) to the program name: eg. progname + ".exe" for |
| 772 | # Windows |
| 773 | # |
| 774 | # To reduce redundant code, these methods expect to find |
| 775 | # several attributes in the current object (presumably defined |
| 776 | # as class attributes): |
| 777 | # * src_extensions - |
| 778 | # list of C/C++ source file extensions, eg. ['.c', '.cpp'] |
| 779 | # * obj_extension - |
| 780 | # object file extension, eg. '.o' or '.obj' |
| 781 | # * static_lib_extension - |
| 782 | # extension for static library files, eg. '.a' or '.lib' |
| 783 | # * shared_lib_extension - |
| 784 | # extension for shared library/object files, eg. '.so', '.dll' |
| 785 | # * static_lib_format - |
| 786 | # format string for generating static library filenames, |
| 787 | # eg. 'lib%s.%s' or '%s.%s' |
| 788 | # * shared_lib_format |
| 789 | # format string for generating shared library filenames |
| 790 | # (probably same as static_lib_format, since the extension |
| 791 | # is one of the intended parameters to the format string) |
| 792 | # * exe_extension - |
| 793 | # extension for executable files, eg. '' or '.exe' |
| 794 | |
| 795 | def object_filenames(self, source_filenames, strip_dir=False, output_dir=''): |
| 796 | if output_dir is None: |
| 797 | output_dir = '' |
| 798 | obj_names = [] |
| 799 | for src_name in source_filenames: |
| 800 | base, ext = os.path.splitext(src_name) |
| 801 | base = os.path.splitdrive(base)[1] # Chop off the drive |
| 802 | base = base[os.path.isabs(base):] # If abs, chop off leading / |
| 803 | if ext not in self.src_extensions: |
| 804 | raise UnknownFileError("unknown file type '%s' (from '%s')" % |
| 805 | (ext, src_name)) |
| 806 | if strip_dir: |
| 807 | base = os.path.basename(base) |
| 808 | obj_names.append(os.path.join(output_dir, |
| 809 | base + self.obj_extension)) |
| 810 | return obj_names |
| 811 | |
| 812 | def shared_object_filename(self, basename, strip_dir=False, output_dir=''): |
| 813 | assert output_dir is not None |
| 814 | if strip_dir: |
| 815 | basename = os.path.basename(basename) |
| 816 | return os.path.join(output_dir, basename + self.shared_lib_extension) |
| 817 | |
| 818 | def executable_filename(self, basename, strip_dir=False, output_dir=''): |
| 819 | assert output_dir is not None |
| 820 | if strip_dir: |
| 821 | basename = os.path.basename(basename) |
| 822 | return os.path.join(output_dir, basename + (self.exe_extension or '')) |
| 823 | |
| 824 | def library_filename(self, libname, lib_type='static', # or 'shared' |
| 825 | strip_dir=False, output_dir=''): |
| 826 | assert output_dir is not None |
| 827 | if lib_type not in ("static", "shared", "dylib"): |
| 828 | raise ValueError( |
| 829 | "'lib_type' must be 'static', 'shared' or 'dylib'") |
| 830 | fmt = getattr(self, lib_type + "_lib_format") |
| 831 | ext = getattr(self, lib_type + "_lib_extension") |
| 832 | |
| 833 | dir, base = os.path.split(libname) |
| 834 | filename = fmt % (base, ext) |
| 835 | if strip_dir: |
| 836 | dir = '' |
| 837 | |
| 838 | return os.path.join(output_dir, dir, filename) |
| 839 | |
| 840 | |
| 841 | # -- Utility methods ----------------------------------------------- |
| 842 | |
| 843 | def execute(self, func, args, msg=None, level=1): |
| 844 | execute(func, args, msg, self.dry_run) |
| 845 | |
| 846 | def spawn(self, cmd): |
| 847 | spawn(cmd, dry_run=self.dry_run) |
| 848 | |
| 849 | def move_file(self, src, dst): |
| 850 | logger.info("moving %r to %r", src, dst) |
| 851 | if self.dry_run: |
| 852 | return |
| 853 | return move(src, dst) |
| 854 | |
| 855 | def mkpath(self, name, mode=0o777): |
| 856 | name = os.path.normpath(name) |
| 857 | if os.path.isdir(name) or name == '': |
| 858 | return |
| 859 | if self.dry_run: |
| 860 | head = '' |
| 861 | for part in name.split(os.sep): |
| 862 | logger.info("created directory %s%s", head, part) |
| 863 | head += part + os.sep |
| 864 | return |
| 865 | os.makedirs(name, mode) |