Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/distutils/ccompiler.py

    r10 r391  
    44for the Distutils compiler abstraction model."""
    55
    6 # This module should be kept compatible with Python 2.1.
    7 
    8 __revision__ = "$Id: ccompiler.py 77425 2010-01-11 22:54:57Z tarek.ziade $"
    9 
    10 import sys, os, re
    11 from types import *
    12 from copy import copy
    13 from distutils.errors import *
     6__revision__ = "$Id$"
     7
     8import sys
     9import os
     10import re
     11
     12from distutils.errors import (CompileError, LinkError, UnknownFileError,
     13                              DistutilsPlatformError, DistutilsModuleError)
    1414from distutils.spawn import spawn
    1515from distutils.file_util import move_file
    1616from distutils.dir_util import mkpath
    17 from distutils.dep_util import newer_pairwise, newer_group
     17from distutils.dep_util import newer_group
    1818from distutils.util import split_quoted, execute
    1919from distutils import log
     20# following import is for backward compatibility
     21from distutils.sysconfig import customize_compiler
    2022
    2123class CCompiler:
     
    8991    language_order = ["c++", "objc", "c"]
    9092
    91     def __init__ (self,
    92                   verbose=0,
    93                   dry_run=0,
    94                   force=0):
    95 
     93    def __init__ (self, verbose=0, dry_run=0, force=0):
    9694        self.dry_run = dry_run
    9795        self.force = force
     
    129127            self.set_executable(key, self.executables[key])
    130128
    131     # __init__ ()
    132 
    133 
    134     def set_executables (self, **args):
    135 
     129    def set_executables(self, **args):
    136130        """Define the executables (and options for them) that will be run
    137131        to perform the various stages of compilation.  The exact set of
     
    166160            self.set_executable(key, args[key])
    167161
    168     # set_executables ()
    169 
    170162    def set_executable(self, key, value):
    171         if type(value) is StringType:
     163        if isinstance(value, str):
    172164            setattr(self, key, split_quoted(value))
    173165        else:
    174166            setattr(self, key, value)
    175167
    176 
    177     def _find_macro (self, name):
     168    def _find_macro(self, name):
    178169        i = 0
    179170        for defn in self.macros:
     
    181172                return i
    182173            i = i + 1
    183 
    184174        return None
    185175
    186 
    187     def _check_macro_definitions (self, definitions):
     176    def _check_macro_definitions(self, definitions):
    188177        """Ensures that every element of 'definitions' is a valid macro
    189178        definition, ie. either (name,value) 2-tuple or a (name,) tuple.  Do
     
    191180        """
    192181        for defn in definitions:
    193             if not (type (defn) is TupleType and
     182            if not (isinstance(defn, tuple) and
    194183                    (len (defn) == 1 or
    195184                     (len (defn) == 2 and
    196                       (type (defn[1]) is StringType or defn[1] is None))) and
    197                     type (defn[0]) is StringType):
     185                      (isinstance(defn[1], str) or defn[1] is None))) and
     186                    isinstance(defn[0], str)):
    198187                raise TypeError, \
    199188                      ("invalid macro definition '%s': " % defn) + \
     
    204193    # -- Bookkeeping methods -------------------------------------------
    205194
    206     def define_macro (self, name, value=None):
     195    def define_macro(self, name, value=None):
    207196        """Define a preprocessor macro for all compilations driven by this
    208197        compiler object.  The optional parameter 'value' should be a
     
    220209        self.macros.append (defn)
    221210
    222 
    223     def undefine_macro (self, name):
     211    def undefine_macro(self, name):
    224212        """Undefine a preprocessor macro for all compilations driven by
    225213        this compiler object.  If the same macro is defined by
     
    239227        self.macros.append (undefn)
    240228
    241 
    242     def add_include_dir (self, dir):
     229    def add_include_dir(self, dir):
    243230        """Add 'dir' to the list of directories that will be searched for
    244231        header files.  The compiler is instructed to search directories in
     
    248235        self.include_dirs.append (dir)
    249236
    250     def set_include_dirs (self, dirs):
     237    def set_include_dirs(self, dirs):
    251238        """Set the list of directories that will be searched to 'dirs' (a
    252239        list of strings).  Overrides any preceding calls to
     
    256243        search by default.
    257244        """
    258         self.include_dirs = copy (dirs)
    259 
    260 
    261     def add_library (self, libname):
     245        self.include_dirs = dirs[:]
     246
     247    def add_library(self, libname):
    262248        """Add 'libname' to the list of libraries that will be included in
    263249        all links driven by this compiler object.  Note that 'libname'
     
    275261        self.libraries.append (libname)
    276262
    277     def set_libraries (self, libnames):
     263    def set_libraries(self, libnames):
    278264        """Set the list of libraries to be included in all links driven by
    279265        this compiler object to 'libnames' (a list of strings).  This does
     
    281267        include by default.
    282268        """
    283         self.libraries = copy (libnames)
    284 
    285 
    286     def add_library_dir (self, dir):
     269        self.libraries = libnames[:]
     270
     271
     272    def add_library_dir(self, dir):
    287273        """Add 'dir' to the list of directories that will be searched for
    288274        libraries specified to 'add_library()' and 'set_libraries()'.  The
     
    290276        are supplied to 'add_library_dir()' and/or 'set_library_dirs()'.
    291277        """
    292         self.library_dirs.append (dir)
    293 
    294     def set_library_dirs (self, dirs):
     278        self.library_dirs.append(dir)
     279
     280    def set_library_dirs(self, dirs):
    295281        """Set the list of library search directories to 'dirs' (a list of
    296282        strings).  This does not affect any standard library search path
    297283        that the linker may search by default.
    298284        """
    299         self.library_dirs = copy (dirs)
    300 
    301 
    302     def add_runtime_library_dir (self, dir):
     285        self.library_dirs = dirs[:]
     286
     287    def add_runtime_library_dir(self, dir):
    303288        """Add 'dir' to the list of directories that will be searched for
    304289        shared libraries at runtime.
    305290        """
    306         self.runtime_library_dirs.append (dir)
    307 
    308     def set_runtime_library_dirs (self, dirs):
     291        self.runtime_library_dirs.append(dir)
     292
     293    def set_runtime_library_dirs(self, dirs):
    309294        """Set the list of directories to search for shared libraries at
    310295        runtime to 'dirs' (a list of strings).  This does not affect any
     
    312297        default.
    313298        """
    314         self.runtime_library_dirs = copy (dirs)
    315 
    316 
    317     def add_link_object (self, object):
     299        self.runtime_library_dirs = dirs[:]
     300
     301    def add_link_object(self, object):
    318302        """Add 'object' to the list of object files (or analogues, such as
    319303        explicitly named library files or the output of "resource
     
    321305        object.
    322306        """
    323         self.objects.append (object)
    324 
    325     def set_link_objects (self, objects):
     307        self.objects.append(object)
     308
     309    def set_link_objects(self, objects):
    326310        """Set the list of object files (or analogues) to be included in
    327311        every link to 'objects'.  This does not affect any standard object
     
    329313        libraries).
    330314        """
    331         self.objects = copy (objects)
     315        self.objects = objects[:]
    332316
    333317
     
    342326        if outdir is None:
    343327            outdir = self.output_dir
    344         elif type(outdir) is not StringType:
     328        elif not isinstance(outdir, str):
    345329            raise TypeError, "'output_dir' must be a string or None"
    346330
    347331        if macros is None:
    348332            macros = self.macros
    349         elif type(macros) is ListType:
     333        elif isinstance(macros, list):
    350334            macros = macros + (self.macros or [])
    351335        else:
     
    354338        if incdirs is None:
    355339            incdirs = self.include_dirs
    356         elif type(incdirs) in (ListType, TupleType):
     340        elif isinstance(incdirs, (list, tuple)):
    357341            incdirs = list(incdirs) + (self.include_dirs or [])
    358342        else:
     
    390374        return cc_args
    391375
    392     def _fix_compile_args (self, output_dir, macros, include_dirs):
     376    def _fix_compile_args(self, output_dir, macros, include_dirs):
    393377        """Typecheck and fix-up some of the arguments to the 'compile()'
    394378        method, and return fixed-up values.  Specifically: if 'output_dir'
     
    402386        if output_dir is None:
    403387            output_dir = self.output_dir
    404         elif type (output_dir) is not StringType:
     388        elif not isinstance(output_dir, str):
    405389            raise TypeError, "'output_dir' must be a string or None"
    406390
    407391        if macros is None:
    408392            macros = self.macros
    409         elif type (macros) is ListType:
     393        elif isinstance(macros, list):
    410394            macros = macros + (self.macros or [])
    411395        else:
     
    414398        if include_dirs is None:
    415399            include_dirs = self.include_dirs
    416         elif type (include_dirs) in (ListType, TupleType):
     400        elif isinstance(include_dirs, (list, tuple)):
    417401            include_dirs = list (include_dirs) + (self.include_dirs or [])
    418402        else:
     
    422406        return output_dir, macros, include_dirs
    423407
    424     # _fix_compile_args ()
    425 
    426     def _prep_compile(self, sources, output_dir, depends=None):
    427         """Decide which souce files must be recompiled.
    428 
    429         Determine the list of object files corresponding to 'sources',
    430         and figure out which ones really need to be recompiled.
    431         Return a list of all object files and a dictionary telling
    432         which source files can be skipped.
    433         """
    434         # Get the list of expected output (object) files
    435         objects = self.object_filenames(sources, output_dir=output_dir)
    436         assert len(objects) == len(sources)
    437 
    438         # Return an empty dict for the "which source files can be skipped"
    439         # return value to preserve API compatibility.
    440         return objects, {}
    441 
    442     def _fix_object_args (self, objects, output_dir):
     408    def _fix_object_args(self, objects, output_dir):
    443409        """Typecheck and fix up some arguments supplied to various methods.
    444410        Specifically: ensure that 'objects' is a list; if output_dir is
     
    446412        'objects' and 'output_dir'.
    447413        """
    448         if type (objects) not in (ListType, TupleType):
     414        if not isinstance(objects, (list, tuple)):
    449415            raise TypeError, \
    450416                  "'objects' must be a list or tuple of strings"
     
    453419        if output_dir is None:
    454420            output_dir = self.output_dir
    455         elif type (output_dir) is not StringType:
     421        elif not isinstance(output_dir, str):
    456422            raise TypeError, "'output_dir' must be a string or None"
    457423
    458424        return (objects, output_dir)
    459425
    460 
    461     def _fix_lib_args (self, libraries, library_dirs, runtime_library_dirs):
     426    def _fix_lib_args(self, libraries, library_dirs, runtime_library_dirs):
    462427        """Typecheck and fix up some of the arguments supplied to the
    463428        'link_*' methods.  Specifically: ensure that all arguments are
     
    468433        if libraries is None:
    469434            libraries = self.libraries
    470         elif type (libraries) in (ListType, TupleType):
     435        elif isinstance(libraries, (list, tuple)):
    471436            libraries = list (libraries) + (self.libraries or [])
    472437        else:
     
    476441        if library_dirs is None:
    477442            library_dirs = self.library_dirs
    478         elif type (library_dirs) in (ListType, TupleType):
     443        elif isinstance(library_dirs, (list, tuple)):
    479444            library_dirs = list (library_dirs) + (self.library_dirs or [])
    480445        else:
     
    484449        if runtime_library_dirs is None:
    485450            runtime_library_dirs = self.runtime_library_dirs
    486         elif type (runtime_library_dirs) in (ListType, TupleType):
     451        elif isinstance(runtime_library_dirs, (list, tuple)):
    487452            runtime_library_dirs = (list (runtime_library_dirs) +
    488453                                    (self.runtime_library_dirs or []))
     
    494459        return (libraries, library_dirs, runtime_library_dirs)
    495460
    496     # _fix_lib_args ()
    497 
    498 
    499     def _need_link (self, objects, output_file):
     461    def _need_link(self, objects, output_file):
    500462        """Return true if we need to relink the files listed in 'objects'
    501463        to recreate 'output_file'.
     
    510472            return newer
    511473
    512     # _need_link ()
    513 
    514     def detect_language (self, sources):
     474    def detect_language(self, sources):
    515475        """Detect the language of a given file, or list of files. Uses
    516476        language_map, and language_order to do the job.
    517477        """
    518         if type(sources) is not ListType:
     478        if not isinstance(sources, list):
    519479            sources = [sources]
    520480        lang = None
     
    532492        return lang
    533493
    534     # detect_language ()
    535 
    536494    # -- Worker methods ------------------------------------------------
    537495    # (must be implemented by subclasses)
    538496
    539     def preprocess (self,
    540                     source,
    541                     output_file=None,
    542                     macros=None,
    543                     include_dirs=None,
    544                     extra_preargs=None,
    545                     extra_postargs=None):
     497    def preprocess(self, source, output_file=None, macros=None,
     498                   include_dirs=None, extra_preargs=None, extra_postargs=None):
    546499        """Preprocess a single C/C++ source file, named in 'source'.
    547500        Output will be written to file named 'output_file', or stdout if
     
    631584        pass
    632585
    633     def create_static_lib (self,
    634                            objects,
    635                            output_libname,
    636                            output_dir=None,
    637                            debug=0,
    638                            target_lang=None):
     586    def create_static_lib(self, objects, output_libname, output_dir=None,
     587                          debug=0, target_lang=None):
    639588        """Link a bunch of stuff together to create a static library file.
    640589        The "bunch of stuff" consists of the list of object files supplied
     
    661610        pass
    662611
    663 
    664612    # values for target_desc parameter in link()
    665613    SHARED_OBJECT = "shared_object"
     
    667615    EXECUTABLE = "executable"
    668616
    669     def link (self,
    670               target_desc,
    671               objects,
    672               output_filename,
    673               output_dir=None,
    674               libraries=None,
    675               library_dirs=None,
    676               runtime_library_dirs=None,
    677               export_symbols=None,
    678               debug=0,
    679               extra_preargs=None,
    680               extra_postargs=None,
    681               build_temp=None,
    682               target_lang=None):
     617    def link(self, target_desc, objects, output_filename, output_dir=None,
     618             libraries=None, library_dirs=None, runtime_library_dirs=None,
     619             export_symbols=None, debug=0, extra_preargs=None,
     620             extra_postargs=None, build_temp=None, target_lang=None):
    683621        """Link a bunch of stuff together to create an executable or
    684622        shared library file.
     
    729667    # Old 'link_*()' methods, rewritten to use the new 'link()' method.
    730668
    731     def link_shared_lib (self,
    732                          objects,
    733                          output_libname,
    734                          output_dir=None,
    735                          libraries=None,
    736                          library_dirs=None,
    737                          runtime_library_dirs=None,
    738                          export_symbols=None,
    739                          debug=0,
    740                          extra_preargs=None,
    741                          extra_postargs=None,
    742                          build_temp=None,
    743                          target_lang=None):
     669    def link_shared_lib(self, objects, output_libname, output_dir=None,
     670                        libraries=None, library_dirs=None,
     671                        runtime_library_dirs=None, export_symbols=None,
     672                        debug=0, extra_preargs=None, extra_postargs=None,
     673                        build_temp=None, target_lang=None):
    744674        self.link(CCompiler.SHARED_LIBRARY, objects,
    745675                  self.library_filename(output_libname, lib_type='shared'),
     
    750680
    751681
    752     def link_shared_object (self,
    753                             objects,
    754                             output_filename,
    755                             output_dir=None,
    756                             libraries=None,
    757                             library_dirs=None,
    758                             runtime_library_dirs=None,
    759                             export_symbols=None,
    760                             debug=0,
    761                             extra_preargs=None,
    762                             extra_postargs=None,
    763                             build_temp=None,
    764                             target_lang=None):
     682    def link_shared_object(self, objects, output_filename, output_dir=None,
     683                           libraries=None, library_dirs=None,
     684                           runtime_library_dirs=None, export_symbols=None,
     685                           debug=0, extra_preargs=None, extra_postargs=None,
     686                           build_temp=None, target_lang=None):
    765687        self.link(CCompiler.SHARED_OBJECT, objects,
    766688                  output_filename, output_dir,
     
    769691                  extra_preargs, extra_postargs, build_temp, target_lang)
    770692
    771 
    772     def link_executable (self,
    773                          objects,
    774                          output_progname,
    775                          output_dir=None,
    776                          libraries=None,
    777                          library_dirs=None,
    778                          runtime_library_dirs=None,
    779                          debug=0,
    780                          extra_preargs=None,
    781                          extra_postargs=None,
    782                          target_lang=None):
     693    def link_executable(self, objects, output_progname, output_dir=None,
     694                        libraries=None, library_dirs=None,
     695                        runtime_library_dirs=None, debug=0, extra_preargs=None,
     696                        extra_postargs=None, target_lang=None):
    783697        self.link(CCompiler.EXECUTABLE, objects,
    784698                  self.executable_filename(output_progname), output_dir,
     
    792706    # implement all of these.
    793707
    794     def library_dir_option (self, dir):
     708    def library_dir_option(self, dir):
    795709        """Return the compiler option to add 'dir' to the list of
    796710        directories searched for libraries.
     
    798712        raise NotImplementedError
    799713
    800     def runtime_library_dir_option (self, dir):
     714    def runtime_library_dir_option(self, dir):
    801715        """Return the compiler option to add 'dir' to the list of
    802716        directories searched for runtime libraries.
     
    804718        raise NotImplementedError
    805719
    806     def library_option (self, lib):
     720    def library_option(self, lib):
    807721        """Return the compiler option to add 'dir' to the list of libraries
    808722        linked into the shared library or executable.
     
    810724        raise NotImplementedError
    811725
    812     def has_function(self, funcname,
    813                      includes=None,
    814                      include_dirs=None,
    815                      libraries=None,
    816                      library_dirs=None):
     726    def has_function(self, funcname, includes=None, include_dirs=None,
     727                     libraries=None, library_dirs=None):
    817728        """Return a boolean indicating whether funcname is supported on
    818729        the current platform.  The optional arguments can be used to
     
    834745        fd, fname = tempfile.mkstemp(".c", funcname, text=True)
    835746        f = os.fdopen(fd, "w")
    836         for incl in includes:
    837             f.write("""#include "%s"\n""" % incl)
    838         f.write("""\
     747        try:
     748            for incl in includes:
     749                f.write("""#include "%s"\n""" % incl)
     750            f.write("""\
    839751main (int argc, char **argv) {
    840752    %s();
    841753}
    842754""" % funcname)
    843         f.close()
     755        finally:
     756            f.close()
    844757        try:
    845758            objects = self.compile([fname], include_dirs=include_dirs)
     
    945858    # -- Utility methods -----------------------------------------------
    946859
    947     def announce (self, msg, level=1):
     860    def announce(self, msg, level=1):
    948861        log.debug(msg)
    949862
    950     def debug_print (self, msg):
     863    def debug_print(self, msg):
    951864        from distutils.debug import DEBUG
    952865        if DEBUG:
    953866            print msg
    954867
    955     def warn (self, msg):
    956         sys.stderr.write ("warning: %s\n" % msg)
    957 
    958     def execute (self, func, args, msg=None, level=1):
     868    def warn(self, msg):
     869        sys.stderr.write("warning: %s\n" % msg)
     870
     871    def execute(self, func, args, msg=None, level=1):
    959872        execute(func, args, msg, self.dry_run)
    960873
    961     def spawn (self, cmd):
    962         spawn (cmd, dry_run=self.dry_run)
    963 
    964     def move_file (self, src, dst):
    965         return move_file (src, dst, dry_run=self.dry_run)
    966 
    967     def mkpath (self, name, mode=0777):
    968         mkpath (name, mode, dry_run=self.dry_run)
     874    def spawn(self, cmd):
     875        spawn(cmd, dry_run=self.dry_run)
     876
     877    def move_file(self, src, dst):
     878        return move_file(src, dst, dry_run=self.dry_run)
     879
     880    def mkpath(self, name, mode=0777):
     881        mkpath(name, mode, dry_run=self.dry_run)
    969882
    970883
     
    989902    ('posix', 'unix'),
    990903    ('nt', 'msvc'),
    991     ('mac', 'mwerks'),
    992904
    993905    )
    994906
    995907def get_default_compiler(osname=None, platform=None):
    996 
    997908    """ Determine the default compiler to use for the given platform.
    998909
     
    1029940                   'bcpp':    ('bcppcompiler', 'BCPPCompiler',
    1030941                               "Borland C++ Compiler"),
    1031                    'mwerks':  ('mwerkscompiler', 'MWerksCompiler',
    1032                                "MetroWerks CodeWarrior"),
    1033942                   'emx':     ('emxccompiler', 'EMXCCompiler',
    1034943                               "EMX port of GNU C Compiler for OS/2"),
     
    1052961
    1053962
    1054 def new_compiler (plat=None,
    1055                   compiler=None,
    1056                   verbose=0,
    1057                   dry_run=0,
    1058                   force=0):
     963def new_compiler(plat=None, compiler=None, verbose=0, dry_run=0, force=0):
    1059964    """Generate an instance of some CCompiler subclass for the supplied
    1060965    platform/compiler combination.  'plat' defaults to 'os.name'
     
    10981003    # with classes that expect verbose to be the first positional
    10991004    # argument.
    1100     return klass (None, dry_run, force)
    1101 
    1102 
    1103 def gen_preprocess_options (macros, include_dirs):
     1005    return klass(None, dry_run, force)
     1006
     1007
     1008def gen_preprocess_options(macros, include_dirs):
    11041009    """Generate C pre-processor options (-D, -U, -I) as used by at least
    11051010    two types of compilers: the typical Unix compiler and Visual C++.
     
    11261031    for macro in macros:
    11271032
    1128         if not (type (macro) is TupleType and
     1033        if not (isinstance(macro, tuple) and
    11291034                1 <= len (macro) <= 2):
    11301035            raise TypeError, \
     
    11491054    return pp_opts
    11501055
    1151 # gen_preprocess_options ()
    1152 
    1153 
    1154 def gen_lib_options (compiler, library_dirs, runtime_library_dirs, libraries):
     1056
     1057def gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries):
    11551058    """Generate linker options for searching library directories and
    1156     linking with specific libraries.  'libraries' and 'library_dirs' are,
    1157     respectively, lists of library names (not filenames!) and search
    1158     directories.  Returns a list of command-line options suitable for use
    1159     with some compiler (depending on the two format strings passed in).
     1059    linking with specific libraries.
     1060
     1061    'libraries' and 'library_dirs' are, respectively, lists of library names
     1062    (not filenames!) and search directories.  Returns a list of command-line
     1063    options suitable for use with some compiler (depending on the two format
     1064    strings passed in).
    11601065    """
    11611066    lib_opts = []
    11621067
    11631068    for dir in library_dirs:
    1164         lib_opts.append (compiler.library_dir_option (dir))
     1069        lib_opts.append(compiler.library_dir_option(dir))
    11651070
    11661071    for dir in runtime_library_dirs:
    1167         opt = compiler.runtime_library_dir_option (dir)
    1168         if type(opt) is ListType:
    1169             lib_opts = lib_opts + opt
     1072        opt = compiler.runtime_library_dir_option(dir)
     1073        if isinstance(opt, list):
     1074            lib_opts.extend(opt)
    11701075        else:
    1171             lib_opts.append (opt)
     1076            lib_opts.append(opt)
    11721077
    11731078    # XXX it's important that we *not* remove redundant library mentions!
     
    11781083
    11791084    for lib in libraries:
    1180         (lib_dir, lib_name) = os.path.split (lib)
    1181         if lib_dir:
    1182             lib_file = compiler.find_library_file ([lib_dir], lib_name)
    1183             if lib_file:
    1184                 lib_opts.append (lib_file)
     1085        lib_dir, lib_name = os.path.split(lib)
     1086        if lib_dir != '':
     1087            lib_file = compiler.find_library_file([lib_dir], lib_name)
     1088            if lib_file is not None:
     1089                lib_opts.append(lib_file)
    11851090            else:
    1186                 compiler.warn ("no library file corresponding to "
    1187                                "'%s' found (skipping)" % lib)
     1091                compiler.warn("no library file corresponding to "
     1092                              "'%s' found (skipping)" % lib)
    11881093        else:
    1189             lib_opts.append (compiler.library_option (lib))
     1094            lib_opts.append(compiler.library_option(lib))
    11901095
    11911096    return lib_opts
    1192 
    1193 # gen_lib_options ()
Note: See TracChangeset for help on using the changeset viewer.