Back to index

python3.2  3.2.2
Classes | Functions | Variables
distutils.ccompiler Namespace Reference

Classes

class  CCompiler

Functions

def get_default_compiler
def show_compilers
def new_compiler
def gen_preprocess_options
def gen_lib_options

Variables

tuple _default_compilers
dictionary compiler_class

Detailed Description

distutils.ccompiler

Contains CCompiler, an abstract base class that defines the interface
for the Distutils compiler abstraction model.

Function Documentation

def distutils.ccompiler.gen_lib_options (   compiler,
  library_dirs,
  runtime_library_dirs,
  libraries 
)
Generate linker options for searching library directories and
linking with specific libraries.  'libraries' and 'library_dirs' are,
respectively, lists of library names (not filenames!) and search
directories.  Returns a list of command-line options suitable for use
with some compiler (depending on the two format strings passed in).

Definition at line 1082 of file ccompiler.py.

01082 
01083 def gen_lib_options (compiler, library_dirs, runtime_library_dirs, libraries):
01084     """Generate linker options for searching library directories and
01085     linking with specific libraries.  'libraries' and 'library_dirs' are,
01086     respectively, lists of library names (not filenames!) and search
01087     directories.  Returns a list of command-line options suitable for use
01088     with some compiler (depending on the two format strings passed in).
01089     """
01090     lib_opts = []
01091 
01092     for dir in library_dirs:
01093         lib_opts.append(compiler.library_dir_option(dir))
01094 
01095     for dir in runtime_library_dirs:
01096         opt = compiler.runtime_library_dir_option(dir)
01097         if isinstance(opt, list):
01098             lib_opts = lib_opts + opt
01099         else:
01100             lib_opts.append(opt)
01101 
01102     # XXX it's important that we *not* remove redundant library mentions!
01103     # sometimes you really do have to say "-lfoo -lbar -lfoo" in order to
01104     # resolve all symbols.  I just hope we never have to say "-lfoo obj.o
01105     # -lbar" to get things to work -- that's certainly a possibility, but a
01106     # pretty nasty way to arrange your C code.
01107 
01108     for lib in libraries:
01109         (lib_dir, lib_name) = os.path.split(lib)
01110         if lib_dir:
01111             lib_file = compiler.find_library_file([lib_dir], lib_name)
01112             if lib_file:
01113                 lib_opts.append(lib_file)
01114             else:
01115                 compiler.warn("no library file corresponding to "
01116                               "'%s' found (skipping)" % lib)
01117         else:
01118             lib_opts.append(compiler.library_option (lib))
01119     return lib_opts

Here is the caller graph for this function:

def distutils.ccompiler.gen_preprocess_options (   macros,
  include_dirs 
)
Generate C pre-processor options (-D, -U, -I) as used by at least
two types of compilers: the typical Unix compiler and Visual C++.
'macros' is the usual thing, a list of 1- or 2-tuples, where (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++.

Definition at line 1037 of file ccompiler.py.

01037 
01038 def gen_preprocess_options(macros, include_dirs):
01039     """Generate C pre-processor options (-D, -U, -I) as used by at least
01040     two types of compilers: the typical Unix compiler and Visual C++.
01041     'macros' is the usual thing, a list of 1- or 2-tuples, where (name,)
01042     means undefine (-U) macro 'name', and (name,value) means define (-D)
01043     macro 'name' to 'value'.  'include_dirs' is just a list of directory
01044     names to be added to the header file search path (-I).  Returns a list
01045     of command-line options suitable for either Unix compilers or Visual
01046     C++.
01047     """
01048     # XXX it would be nice (mainly aesthetic, and so we don't generate
01049     # stupid-looking command lines) to go over 'macros' and eliminate
01050     # redundant definitions/undefinitions (ie. ensure that only the
01051     # latest mention of a particular macro winds up on the command
01052     # line).  I don't think it's essential, though, since most (all?)
01053     # Unix C compilers only pay attention to the latest -D or -U
01054     # mention of a macro on their command line.  Similar situation for
01055     # 'include_dirs'.  I'm punting on both for now.  Anyways, weeding out
01056     # redundancies like this should probably be the province of
01057     # CCompiler, since the data structures used are inherited from it
01058     # and therefore common to all CCompiler classes.
01059     pp_opts = []
01060     for macro in macros:
01061         if not (isinstance(macro, tuple) and 1 <= len(macro) <= 2):
01062             raise TypeError(
01063                   "bad macro definition '%s': "
01064                   "each element of 'macros' list must be a 1- or 2-tuple"
01065                   % macro)
01066 
01067         if len(macro) == 1:        # undefine this macro
01068             pp_opts.append("-U%s" % macro[0])
01069         elif len(macro) == 2:
01070             if macro[1] is None:    # define with no explicit value
01071                 pp_opts.append("-D%s" % macro[0])
01072             else:
01073                 # XXX *don't* need to be clever about quoting the
01074                 # macro value here, because we're going to avoid the
01075                 # shell at all costs when we spawn the command!
01076                 pp_opts.append("-D%s=%s" % macro)
01077 
01078     for dir in include_dirs:
01079         pp_opts.append("-I%s" % dir)
01080     return pp_opts
01081 

Here is the caller graph for this function:

def distutils.ccompiler.get_default_compiler (   osname = None,
  platform = None 
)
Determine the default compiler to use for the given 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.

Definition at line 937 of file ccompiler.py.

00937 
00938 def get_default_compiler(osname=None, platform=None):
00939     """Determine the default compiler to use for the given platform.
00940 
00941        osname should be one of the standard Python OS names (i.e. the
00942        ones returned by os.name) and platform the common value
00943        returned by sys.platform for the platform in question.
00944 
00945        The default values are os.name and sys.platform in case the
00946        parameters are not given.
00947     """
00948     if osname is None:
00949         osname = os.name
00950     if platform is None:
00951         platform = sys.platform
00952     for pattern, compiler in _default_compilers:
00953         if re.match(pattern, platform) is not None or \
00954            re.match(pattern, osname) is not None:
00955             return compiler
00956     # Default to Unix compiler
00957     return 'unix'
00958 
00959 # Map compiler types to (module_name, class_name) pairs -- ie. where to
00960 # find the code that implements an interface to this compiler.  (The module
# is assumed to be in the 'distutils' package.)

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.ccompiler.new_compiler (   plat = None,
  compiler = None,
  verbose = 0,
  dry_run = 0,
  force = 0 
)
Generate an instance of some CCompiler subclass for the supplied
platform/compiler combination.  'plat' defaults to '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.

Definition at line 992 of file ccompiler.py.

00992 
00993 def new_compiler(plat=None, compiler=None, verbose=0, dry_run=0, force=0):
00994     """Generate an instance of some CCompiler subclass for the supplied
00995     platform/compiler combination.  'plat' defaults to 'os.name'
00996     (eg. 'posix', 'nt'), and 'compiler' defaults to the default compiler
00997     for that platform.  Currently only 'posix' and 'nt' are supported, and
00998     the default compilers are "traditional Unix interface" (UnixCCompiler
00999     class) and Visual C++ (MSVCCompiler class).  Note that it's perfectly
01000     possible to ask for a Unix compiler object under Windows, and a
01001     Microsoft compiler object under Unix -- if you supply a value for
01002     'compiler', 'plat' is ignored.
01003     """
01004     if plat is None:
01005         plat = os.name
01006 
01007     try:
01008         if compiler is None:
01009             compiler = get_default_compiler(plat)
01010 
01011         (module_name, class_name, long_description) = compiler_class[compiler]
01012     except KeyError:
01013         msg = "don't know how to compile C/C++ code on platform '%s'" % plat
01014         if compiler is not None:
01015             msg = msg + " with '%s' compiler" % compiler
01016         raise DistutilsPlatformError(msg)
01017 
01018     try:
01019         module_name = "distutils." + module_name
01020         __import__ (module_name)
01021         module = sys.modules[module_name]
01022         klass = vars(module)[class_name]
01023     except ImportError:
01024         raise DistutilsModuleError(
01025               "can't compile C/C++ code: unable to load module '%s'" % \
01026               module_name)
01027     except KeyError:
01028         raise DistutilsModuleError(
01029                "can't compile C/C++ code: unable to find class '%s' "
01030                "in module '%s'" % (class_name, module_name))
01031 
01032     # XXX The None is necessary to preserve backwards compatibility
01033     # with classes that expect verbose to be the first positional
01034     # argument.
01035     return klass(None, dry_run, force)
01036 

Here is the call graph for this function:

Here is the caller graph for this function:

Print list of available compilers (used by the "--help-compiler"
options to "build", "build_ext", "build_clib").

Definition at line 975 of file ccompiler.py.

00975 
00976 def show_compilers():
00977     """Print list of available compilers (used by the "--help-compiler"
00978     options to "build", "build_ext", "build_clib").
00979     """
00980     # XXX this "knows" that the compiler option it's describing is
00981     # "--compiler", which just happens to be the case for the three
00982     # commands that use it.
00983     from distutils.fancy_getopt import FancyGetopt
00984     compilers = []
00985     for compiler in compiler_class.keys():
00986         compilers.append(("compiler="+compiler, None,
00987                           compiler_class[compiler][2]))
00988     compilers.sort()
00989     pretty_printer = FancyGetopt(compilers)
00990     pretty_printer.print_help("List of available compilers:")
00991 


Variable Documentation

Initial value:
00001 (
00002 
00003     # Platform string mappings
00004 
00005     # on a cygwin built python we can use gcc like an ordinary UNIXish
00006     # compiler
00007     ('cygwin.*', 'unix'),
00008     ('os2emx', 'emx'),
00009 
00010     # OS name mappings
00011     ('posix', 'unix'),
00012     ('nt', 'msvc'),
00013 
00014     )

Definition at line 922 of file ccompiler.py.

Initial value:
00001 { 'unix':    ('unixccompiler', 'UnixCCompiler',
00002                                "standard UNIX-style compiler"),
00003                    'msvc':    ('msvccompiler', 'MSVCCompiler',
00004                                "Microsoft Visual C++"),
00005                    'cygwin':  ('cygwinccompiler', 'CygwinCCompiler',
00006                                "Cygwin port of GNU C Compiler for Win32"),
00007                    'mingw32': ('cygwinccompiler', 'Mingw32CCompiler',
00008                                "Mingw32 port of GNU C Compiler for Win32"),
00009                    'bcpp':    ('bcppcompiler', 'BCPPCompiler',
00010                                "Borland C++ Compiler"),
00011                    'emx':     ('emxccompiler', 'EMXCCompiler',
00012                                "EMX port of GNU C Compiler for OS/2"),
00013                  }

Definition at line 961 of file ccompiler.py.