This module provides the abstract base class for the CCompiler classes. A CCompiler instance can be used for all the compile and link steps needed to build a single project. Methods are provided to set options for the compiler -- macro definitions, include directories, link path, libraries and the like.
This module provides the following functions.
compiler, library_dirs, runtime_library_dirs, libraries) |
macros, include_dirs) |
(name,)
means undefine (-U) macro name,
and (name, value)
means define (-D)
macro name to value. include_dirs is just a list of
directory names to be added to the header file search path (-I).
Returns a list of command-line options suitable for either Unix compilers
or Visual C++.
osname, platform) |
osname should be one of the standard Python OS names (i.e. the
ones returned by os.name
) and platform the common value
returned by sys.platform
for the platform in question.
The default values are os.name
and sys.platform
in case the
parameters are not given.
plat=None , compiler=None , verbose=0 , dry_run=0 , force=0 ) |
os.name
(eg. 'posix'
, 'nt'
), and compiler
defaults to the default compiler for that platform. Currently only
'posix'
and 'nt'
are supported, and the default
compilers are ``traditional Unix interface'' (UnixCCompiler
class) and Visual C++(MSVCCompiler class). Note that it's
perfectly possible to ask for a Unix compiler object under Windows,
and a Microsoft compiler object under Unix--if you supply a value
for compiler, plat is ignored.
) |
build
,
build_ext
, build_clib
).
[verbose=0 , dry_run=0 , force=0 ]) |
The abstract base class CCompiler defines the interface that must be implemented by real compiler classes. The class also has some utility methods used by several compiler classes.
The basic idea behind a compiler abstraction class is that each instance can be used for all the compile/link steps in building a single project. Thus, attributes common to all of those compile and link steps -- include directories, macros to define, libraries to link against, etc. -- are attributes of the compiler instance. To allow for variability in how individual files are treated, most of those attributes may be varied on a per-compilation or per-link basis.
The constructor for each subclass creates an instance of the Compiler
object. Flags are verbose (show verbose output), dry_run
(don't actually execute the steps) and force (rebuild
everything, regardless of dependencies). All of these flags default to
0
(off). Note that you probably don't want to instantiate
CCompiler or one of its subclasses directly - use the
distutils.CCompiler.new_compiler() factory function
instead.
The following methods allow you to manually alter compiler options for the instance of the Compiler class.
dir) |
dirs) |
libname) |
Add libname to the list of libraries that will be included in all links driven by this compiler object. Note that libname should *not* be the name of a file containing a library, but the name of the library itself: the actual filename will be inferred by the linker, the compiler, or the compiler class (depending on the platform).
The linker will be instructed to link against libraries in the order they were supplied to add_library() and/or set_libraries(). It is perfectly valid to duplicate library names; the linker will be instructed to link against libraries as many times as they are mentioned.
libnames) |
dir) |
dirs) |
dir) |
dirs) |
name[, value=None ]) |
name) |
object) |
objects) |
The following methods implement methods for autodetection of compiler options, providing some functionality similar to GNU autoconf.
sources) |
dirs, lib[, debug=0 ]) |
None
if lib wasn't found in any of
the specified directories.
funcname [, includes=None , include_dirs=None , libraries=None , library_dirs=None ]) |
dir) |
lib) |
dir) |
**args) |
attribute | description |
---|---|
the C/C++ compiler | |
linker used to create shared objects and libraries | |
linker used to create binary executables | |
static library creator |
On platforms with a command-line (Unix, DOS/Windows), each of these is a string that will be split into executable name and (optional) list of arguments. (Splitting the string is done similarly to how Unix shells operate: words are delimited by spaces, but quotes and backslashes can override this. See distutils.util.split_quoted().)
The following methods invoke stages in the build process.
sources[, output_dir=None , macros=None , include_dirs=None , debug=0 , extra_preargs=None , extra_postargs=None , depends=None ]) |
sources must be a list of filenames, most likely C/C++ files, but in reality anything that can be handled by a particular compiler and compiler class (eg. MSVCCompiler can handle resource files in sources). Return a list of object filenames, one per source filename in sources. Depending on the implementation, not all source files will necessarily be compiled, but all corresponding object filenames will be returned.
If output_dir is given, object files will be put under it, while retaining their original path component. That is, foo/bar.c normally compiles to foo/bar.o (for a Unix implementation); if output_dir is build, then it would compile to build/foo/bar.o.
macros, if given, must be a list of macro definitions. A macro
definition is either a (name, value)
2-tuple or a
(name,)
1-tuple.
The former defines a macro; if the value is None
, the macro is
defined without an explicit value. The 1-tuple case undefines a
macro. Later definitions/redefinitions/undefinitions take
precedence.
include_dirs, if given, must be a list of strings, the directories to add to the default include file search path for this compilation only.
debug is a boolean; if true, the compiler will be instructed to output debug symbols in (or alongside) the object file(s).
extra_preargs and extra_postargs are implementation-dependent. On platforms that have the notion of a command-line (e.g. Unix, DOS/Windows), they are most likely lists of strings: extra command-line arguments to prepend/append to the compiler command line. On other platforms, consult the implementation class documentation. In any event, they are intended as an escape hatch for those occasions when the abstract compiler framework doesn't cut the mustard.
depends, if given, is a list of filenames that all targets depend on. If a source file is older than any file in depends, then the source file will be recompiled. This supports dependency tracking, but only at a coarse granularity.
Raises CompileError on failure.
objects, output_libname[, output_dir=None , debug=0 , target_lang=None ]) |
output_libname should be a library name, not a filename; the filename will be inferred from the library name. output_dir is the directory where the library file will be put. XXX defaults to what?
debug is a boolean; if true, debugging information will be included in the library (note that on most platforms, it is the compile step where this matters: the debug flag is included here just for consistency).
target_lang is the target language for which the given objects are being compiled. This allows specific linkage time treatment of certain languages.
Raises LibError on failure.
target_desc, objects, output_filename[, output_dir=None , libraries=None , library_dirs=None , runtime_library_dirs=None , export_symbols=None , debug=0 , extra_preargs=None , extra_postargs=None , build_temp=None , target_lang=None ]) |
The ``bunch of stuff'' consists of the list of object files supplied as objects. output_filename should be a filename. If output_dir is supplied, output_filename is relative to it (i.e. output_filename can provide directory components if needed).
libraries is a list of libraries to link against. These are library names, not filenames, since they're translated into filenames in a platform-specific way (eg. foo becomes libfoo.a on Unix and foo.lib on DOS/Windows). However, they can include a directory component, which means the linker will look in that specific directory rather than searching all the normal locations.
library_dirs, if supplied, should be a list of directories to search for libraries that were specified as bare library names (ie. no directory component). These are on top of the system default and those supplied to add_library_dir() and/or set_library_dirs(). runtime_library_dirs is a list of directories that will be embedded into the shared library and used to search for other shared libraries that *it* depends on at run-time. (This may only be relevant on Unix.)
export_symbols is a list of symbols that the shared library will export. (This appears to be relevant only on Windows.)
debug is as for compile() and create_static_lib(), with the slight distinction that it actually matters on most platforms (as opposed to create_static_lib(), which includes a debug flag mostly for form's sake).
extra_preargs and extra_postargs are as for compile() (except of course that they supply command-line arguments for the particular linker being used).
target_lang is the target language for which the given objects are being compiled. This allows specific linkage time treatment of certain languages.
Raises LinkError on failure.
objects, output_progname[, output_dir=None , libraries=None , library_dirs=None , runtime_library_dirs=None , debug=0 , extra_preargs=None , extra_postargs=None , target_lang=None ]) |
objects, output_libname[, output_dir=None , libraries=None , library_dirs=None , runtime_library_dirs=None , export_symbols=None , debug=0 , extra_preargs=None , extra_postargs=None , build_temp=None , target_lang=None ]) |
objects, output_filename[, output_dir=None , libraries=None , library_dirs=None , runtime_library_dirs=None , export_symbols=None , debug=0 , extra_preargs=None , extra_postargs=None , build_temp=None , target_lang=None ]) |
source[, output_file=None , macros=None , include_dirs=None , extra_preargs=None , extra_postargs=None ]) |
Raises PreprocessError on failure.
The following utility methods are defined by the CCompiler class, for use by the various concrete subclasses.
basename[, strip_dir=0 , output_dir='' ]) |
libname[, lib_type='static' , strip_dir=0 , output_dir='' ]) |
'static'
will typically
be of the form liblibname.a, while a lib_type of 'dynamic'
will be of the form liblibname.so.
source_filenames[, strip_dir=0 , output_dir='' ]) |
basename[, strip_dir=0 , output_dir='' ]) |
func, args[, msg=None , level=1 ]) |
cmd) |
name[, mode=511 ]) |
Invokes distutils.dir_util.mkpath(). This creates a directory and any missing ancestor directories. XXX see also.
src, dst) |
msg[, level=1 ]) |
msg) |
msg) |
See About this document... for information on suggesting changes.