aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/distutils/ccompiler.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/distutils/ccompiler.py')
-rw-r--r--Lib/distutils/ccompiler.py281
1 files changed, 153 insertions, 128 deletions
diff --git a/Lib/distutils/ccompiler.py b/Lib/distutils/ccompiler.py
index 7076b933946..c795c958fec 100644
--- a/Lib/distutils/ccompiler.py
+++ b/Lib/distutils/ccompiler.py
@@ -3,18 +3,12 @@
Contains CCompiler, an abstract base class that defines the interface
for the Distutils compiler abstraction model."""
-__revision__ = "$Id$"
-
-import sys
-import os
-import re
-
-from distutils.errors import (CompileError, LinkError, UnknownFileError,
- DistutilsPlatformError, DistutilsModuleError)
+import sys, os, re
+from distutils.errors import *
from distutils.spawn import spawn
from distutils.file_util import move_file
from distutils.dir_util import mkpath
-from distutils.dep_util import newer_group
+from distutils.dep_util import newer_pairwise, newer_group
from distutils.util import split_quoted, execute
from distutils import log
@@ -88,7 +82,7 @@ class CCompiler:
}
language_order = ["c++", "objc", "c"]
- def __init__ (self, verbose=0, dry_run=0, force=0):
+ def __init__(self, verbose=0, dry_run=0, force=0):
self.dry_run = dry_run
self.force = force
self.verbose = verbose
@@ -124,7 +118,7 @@ class CCompiler:
for key in self.executables.keys():
self.set_executable(key, self.executables[key])
- def set_executables(self, **args):
+ def set_executables(self, **kwargs):
"""Define the executables (and options for them) that will be run
to perform the various stages of compilation. The exact set of
executables that may be specified here depends on the compiler
@@ -150,12 +144,11 @@ class CCompiler:
# discovered at run-time, since there are many different ways to do
# basically the same things with Unix C compilers.
- for key in args.keys():
+ for key in kwargs:
if key not in self.executables:
- raise ValueError, \
- "unknown executable '%s' for class %s" % \
- (key, self.__class__.__name__)
- self.set_executable(key, args[key])
+ raise ValueError("unknown executable '%s' for class %s" %
+ (key, self.__class__.__name__))
+ self.set_executable(key, kwargs[key])
def set_executable(self, key, value):
if isinstance(value, str):
@@ -168,7 +161,7 @@ class CCompiler:
for defn in self.macros:
if defn[0] == name:
return i
- i = i + 1
+ i += 1
return None
def _check_macro_definitions(self, definitions):
@@ -178,14 +171,12 @@ class CCompiler:
"""
for defn in definitions:
if not (isinstance(defn, tuple) and
- (len (defn) == 1 or
- (len (defn) == 2 and
- (isinstance(defn[1], str) or defn[1] is None))) and
- isinstance(defn[0], str)):
- raise TypeError, \
- ("invalid macro definition '%s': " % defn) + \
+ (len(defn) in (1, 2) and
+ (isinstance (defn[1], str) or defn[1] is None)) and
+ isinstance (defn[0], str)):
+ raise TypeError(("invalid macro definition '%s': " % defn) + \
"must be tuple (string,), (string, string), or " + \
- "(string, None)"
+ "(string, None)")
# -- Bookkeeping methods -------------------------------------------
@@ -203,8 +194,7 @@ class CCompiler:
if i is not None:
del self.macros[i]
- defn = (name, value)
- self.macros.append (defn)
+ self.macros.append((name, value))
def undefine_macro(self, name):
"""Undefine a preprocessor macro for all compilations driven by
@@ -222,7 +212,7 @@ class CCompiler:
del self.macros[i]
undefn = (name,)
- self.macros.append (undefn)
+ self.macros.append(undefn)
def add_include_dir(self, dir):
"""Add 'dir' to the list of directories that will be searched for
@@ -230,7 +220,7 @@ class CCompiler:
the order in which they are supplied by successive calls to
'add_include_dir()'.
"""
- self.include_dirs.append (dir)
+ self.include_dirs.append(dir)
def set_include_dirs(self, dirs):
"""Set the list of directories that will be searched to 'dirs' (a
@@ -256,7 +246,7 @@ class CCompiler:
names; the linker will be instructed to link against libraries as
many times as they are mentioned.
"""
- self.libraries.append (libname)
+ self.libraries.append(libname)
def set_libraries(self, libnames):
"""Set the list of libraries to be included in all links driven by
@@ -266,7 +256,6 @@ class CCompiler:
"""
self.libraries = libnames[:]
-
def add_library_dir(self, dir):
"""Add 'dir' to the list of directories that will be searched for
libraries specified to 'add_library()' and 'set_libraries()'. The
@@ -324,29 +313,28 @@ class CCompiler:
if outdir is None:
outdir = self.output_dir
elif not isinstance(outdir, str):
- raise TypeError, "'output_dir' must be a string or None"
+ raise TypeError("'output_dir' must be a string or None")
if macros is None:
macros = self.macros
elif isinstance(macros, list):
macros = macros + (self.macros or [])
else:
- raise TypeError, "'macros' (if supplied) must be a list of tuples"
+ raise TypeError("'macros' (if supplied) must be a list of tuples")
if incdirs is None:
incdirs = self.include_dirs
elif isinstance(incdirs, (list, tuple)):
incdirs = list(incdirs) + (self.include_dirs or [])
else:
- raise TypeError, \
- "'include_dirs' (if supplied) must be a list of strings"
+ raise TypeError(
+ "'include_dirs' (if supplied) must be a list of strings")
if extra is None:
extra = []
# Get the list of expected output (object) files
- objects = self.object_filenames(sources,
- strip_dir=0,
+ objects = self.object_filenames(sources, strip_dir=0,
output_dir=outdir)
assert len(objects) == len(sources)
@@ -384,25 +372,41 @@ class CCompiler:
if output_dir is None:
output_dir = self.output_dir
elif not isinstance(output_dir, str):
- raise TypeError, "'output_dir' must be a string or None"
+ raise TypeError("'output_dir' must be a string or None")
if macros is None:
macros = self.macros
elif isinstance(macros, list):
macros = macros + (self.macros or [])
else:
- raise TypeError, "'macros' (if supplied) must be a list of tuples"
+ raise TypeError("'macros' (if supplied) must be a list of tuples")
if include_dirs is None:
include_dirs = self.include_dirs
elif isinstance(include_dirs, (list, tuple)):
- include_dirs = list (include_dirs) + (self.include_dirs or [])
+ include_dirs = list(include_dirs) + (self.include_dirs or [])
else:
- raise TypeError, \
- "'include_dirs' (if supplied) must be a list of strings"
+ raise TypeError(
+ "'include_dirs' (if supplied) must be a list of strings")
return output_dir, macros, include_dirs
+ def _prep_compile(self, sources, output_dir, depends=None):
+ """Decide which souce files must be recompiled.
+
+ Determine the list of object files corresponding to 'sources',
+ and figure out which ones really need to be recompiled.
+ Return a list of all object files and a dictionary telling
+ which source files can be skipped.
+ """
+ # Get the list of expected output (object) files
+ objects = self.object_filenames(sources, output_dir=output_dir)
+ assert len(objects) == len(sources)
+
+ # Return an empty dict for the "which source files can be skipped"
+ # return value to preserve API compatibility.
+ return objects, {}
+
def _fix_object_args(self, objects, output_dir):
"""Typecheck and fix up some arguments supplied to various methods.
Specifically: ensure that 'objects' is a list; if output_dir is
@@ -410,14 +414,13 @@ class CCompiler:
'objects' and 'output_dir'.
"""
if not isinstance(objects, (list, tuple)):
- raise TypeError, \
- "'objects' must be a list or tuple of strings"
- objects = list (objects)
+ raise TypeError("'objects' must be a list or tuple of strings")
+ objects = list(objects)
if output_dir is None:
output_dir = self.output_dir
elif not isinstance(output_dir, str):
- raise TypeError, "'output_dir' must be a string or None"
+ raise TypeError("'output_dir' must be a string or None")
return (objects, output_dir)
@@ -433,26 +436,25 @@ class CCompiler:
elif isinstance(libraries, (list, tuple)):
libraries = list (libraries) + (self.libraries or [])
else:
- raise TypeError, \
- "'libraries' (if supplied) must be a list of strings"
+ raise TypeError(
+ "'libraries' (if supplied) must be a list of strings")
if library_dirs is None:
library_dirs = self.library_dirs
elif isinstance(library_dirs, (list, tuple)):
library_dirs = list (library_dirs) + (self.library_dirs or [])
else:
- raise TypeError, \
- "'library_dirs' (if supplied) must be a list of strings"
+ raise TypeError(
+ "'library_dirs' (if supplied) must be a list of strings")
if runtime_library_dirs is None:
runtime_library_dirs = self.runtime_library_dirs
elif isinstance(runtime_library_dirs, (list, tuple)):
- runtime_library_dirs = (list (runtime_library_dirs) +
+ runtime_library_dirs = (list(runtime_library_dirs) +
(self.runtime_library_dirs or []))
else:
- raise TypeError, \
- "'runtime_library_dirs' (if supplied) " + \
- "must be a list of strings"
+ raise TypeError("'runtime_library_dirs' (if supplied) "
+ "must be a list of strings")
return (libraries, library_dirs, runtime_library_dirs)
@@ -461,7 +463,7 @@ class CCompiler:
to recreate 'output_file'.
"""
if self.force:
- return 1
+ return True
else:
if self.dry_run:
newer = newer_group (objects, output_file, missing='newer')
@@ -489,6 +491,7 @@ class CCompiler:
pass
return lang
+
# -- Worker methods ------------------------------------------------
# (must be implemented by subclasses)
@@ -558,7 +561,6 @@ class CCompiler:
"""
# A concrete compiler class can either override this method
# entirely or implement _compile().
-
macros, objects, extra_postargs, pp_opts, build = \
self._setup_compile(output_dir, macros, include_dirs, sources,
depends, extra_postargs)
@@ -576,7 +578,6 @@ class CCompiler:
def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
"""Compile 'src' to product 'obj'."""
-
# A concrete compiler class that does not override compile()
# should implement _compile().
pass
@@ -607,15 +608,26 @@ class CCompiler:
"""
pass
+
# values for target_desc parameter in link()
SHARED_OBJECT = "shared_object"
SHARED_LIBRARY = "shared_library"
EXECUTABLE = "executable"
- def link(self, 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):
+ def link(self,
+ 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):
"""Link a bunch of stuff together to create an executable or
shared library file.
@@ -664,11 +676,19 @@ class CCompiler:
# Old 'link_*()' methods, rewritten to use the new 'link()' method.
- def link_shared_lib(self, 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):
+ def link_shared_lib(self,
+ 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):
self.link(CCompiler.SHARED_LIBRARY, objects,
self.library_filename(output_libname, lib_type='shared'),
output_dir,
@@ -677,21 +697,37 @@ class CCompiler:
extra_preargs, extra_postargs, build_temp, target_lang)
- def link_shared_object(self, 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):
+ def link_shared_object(self,
+ 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):
self.link(CCompiler.SHARED_OBJECT, objects,
output_filename, output_dir,
libraries, library_dirs, runtime_library_dirs,
export_symbols, debug,
extra_preargs, extra_postargs, build_temp, target_lang)
- def link_executable(self, 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):
+
+ def link_executable(self,
+ 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):
self.link(CCompiler.EXECUTABLE, objects,
self.executable_filename(output_progname), output_dir,
libraries, library_dirs, runtime_library_dirs, None,
@@ -727,7 +763,6 @@ class CCompiler:
the current platform. The optional arguments can be used to
augment the compilation environment.
"""
-
# this can't be included at module scope because it tries to
# import math which might not be available at that point - maybe
# the necessary logic should just be inlined?
@@ -817,8 +852,8 @@ main (int argc, char **argv) {
base = os.path.splitdrive(base)[1] # Chop off the drive
base = base[os.path.isabs(base):] # If abs, chop off leading /
if ext not in self.src_extensions:
- raise UnknownFileError, \
- "unknown file type '%s' (from '%s')" % (ext, src_name)
+ raise UnknownFileError(
+ "unknown file type '%s' (from '%s')" % (ext, src_name))
if strip_dir:
base = os.path.basename(base)
obj_names.append(os.path.join(output_dir,
@@ -828,24 +863,25 @@ main (int argc, char **argv) {
def shared_object_filename(self, basename, strip_dir=0, output_dir=''):
assert output_dir is not None
if strip_dir:
- basename = os.path.basename (basename)
+ basename = os.path.basename(basename)
return os.path.join(output_dir, basename + self.shared_lib_extension)
def executable_filename(self, basename, strip_dir=0, output_dir=''):
assert output_dir is not None
if strip_dir:
- basename = os.path.basename (basename)
+ basename = os.path.basename(basename)
return os.path.join(output_dir, basename + (self.exe_extension or ''))
def library_filename(self, libname, lib_type='static', # or 'shared'
strip_dir=0, output_dir=''):
assert output_dir is not None
if lib_type not in ("static", "shared", "dylib"):
- raise ValueError, "'lib_type' must be \"static\", \"shared\" or \"dylib\""
+ raise ValueError(
+ "'lib_type' must be \"static\", \"shared\" or \"dylib\"")
fmt = getattr(self, lib_type + "_lib_format")
ext = getattr(self, lib_type + "_lib_extension")
- dir, base = os.path.split (libname)
+ dir, base = os.path.split(libname)
filename = fmt % (base, ext)
if strip_dir:
dir = ''
@@ -861,7 +897,7 @@ main (int argc, char **argv) {
def debug_print(self, msg):
from distutils.debug import DEBUG
if DEBUG:
- print msg
+ print(msg)
def warn(self, msg):
sys.stderr.write("warning: %s\n" % msg)
@@ -875,13 +911,10 @@ main (int argc, char **argv) {
def move_file(self, src, dst):
return move_file(src, dst, dry_run=self.dry_run)
- def mkpath(self, name, mode=0777):
+ def mkpath (self, name, mode=0o777):
mkpath(name, mode, dry_run=self.dry_run)
-# class CCompiler
-
-
# Map a sys.platform/os.name ('posix', 'nt') to the default compiler
# type for that platform. Keys are interpreted as re match
# patterns. Order is important; platform mappings are preferred over
@@ -902,15 +935,14 @@ _default_compilers = (
)
def 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.
+ """Determine the default compiler to use for the given platform.
- The default values are os.name and sys.platform in case the
- parameters are not given.
+ 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.
"""
if osname is None:
osname = os.name
@@ -980,7 +1012,7 @@ def new_compiler(plat=None, compiler=None, verbose=0, dry_run=0, force=0):
msg = "don't know how to compile C/C++ code on platform '%s'" % plat
if compiler is not None:
msg = msg + " with '%s' compiler" % compiler
- raise DistutilsPlatformError, msg
+ raise DistutilsPlatformError(msg)
try:
module_name = "distutils." + module_name
@@ -988,13 +1020,13 @@ def new_compiler(plat=None, compiler=None, verbose=0, dry_run=0, force=0):
module = sys.modules[module_name]
klass = vars(module)[class_name]
except ImportError:
- raise DistutilsModuleError, \
+ raise DistutilsModuleError(
"can't compile C/C++ code: unable to load module '%s'" % \
- module_name
+ module_name)
except KeyError:
- raise DistutilsModuleError, \
- ("can't compile C/C++ code: unable to find class '%s' " +
- "in module '%s'") % (class_name, module_name)
+ raise DistutilsModuleError(
+ "can't compile C/C++ code: unable to find class '%s' "
+ "in module '%s'" % (class_name, module_name))
# XXX The None is necessary to preserve backwards compatibility
# with classes that expect verbose to be the first positional
@@ -1023,42 +1055,36 @@ def gen_preprocess_options(macros, include_dirs):
# redundancies like this should probably be the province of
# CCompiler, since the data structures used are inherited from it
# and therefore common to all CCompiler classes.
-
pp_opts = []
for macro in macros:
-
- if not (isinstance(macro, tuple) and
- 1 <= len (macro) <= 2):
- raise TypeError, \
- ("bad macro definition '%s': " +
- "each element of 'macros' list must be a 1- or 2-tuple") % \
- macro
-
- if len (macro) == 1: # undefine this macro
- pp_opts.append ("-U%s" % macro[0])
- elif len (macro) == 2:
+ if not (isinstance(macro, tuple) and 1 <= len(macro) <= 2):
+ raise TypeError(
+ "bad macro definition '%s': "
+ "each element of 'macros' list must be a 1- or 2-tuple"
+ % macro)
+
+ if len(macro) == 1: # undefine this macro
+ pp_opts.append("-U%s" % macro[0])
+ elif len(macro) == 2:
if macro[1] is None: # define with no explicit value
- pp_opts.append ("-D%s" % macro[0])
+ pp_opts.append("-D%s" % macro[0])
else:
# XXX *don't* need to be clever about quoting the
# macro value here, because we're going to avoid the
# shell at all costs when we spawn the command!
- pp_opts.append ("-D%s=%s" % macro)
+ pp_opts.append("-D%s=%s" % macro)
for dir in include_dirs:
- pp_opts.append ("-I%s" % dir)
-
+ pp_opts.append("-I%s" % dir)
return pp_opts
-def gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries):
+def 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).
+ 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).
"""
lib_opts = []
@@ -1068,7 +1094,7 @@ def gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries):
for dir in runtime_library_dirs:
opt = compiler.runtime_library_dir_option(dir)
if isinstance(opt, list):
- lib_opts.extend(opt)
+ lib_opts = lib_opts + opt
else:
lib_opts.append(opt)
@@ -1079,15 +1105,14 @@ def gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries):
# pretty nasty way to arrange your C code.
for lib in libraries:
- lib_dir, lib_name = os.path.split(lib)
- if lib_dir != '':
+ (lib_dir, lib_name) = os.path.split(lib)
+ if lib_dir:
lib_file = compiler.find_library_file([lib_dir], lib_name)
- if lib_file is not None:
+ if lib_file:
lib_opts.append(lib_file)
else:
compiler.warn("no library file corresponding to "
"'%s' found (skipping)" % lib)
else:
- lib_opts.append(compiler.library_option(lib))
-
+ lib_opts.append(compiler.library_option (lib))
return lib_opts