module cleanup

This commit is contained in:
Tarek Ziadé 2010-01-11 23:41:32 +00:00
parent 69eb51697c
commit ae7731af45
1 changed files with 69 additions and 148 deletions

View File

@ -9,11 +9,12 @@ import sys
import os import os
import re import re
from distutils.errors import CompileError, LinkError, UnknownFileError from distutils.errors import (CompileError, LinkError, UnknownFileError,
DistutilsPlatformError, DistutilsModuleError)
from distutils.spawn import spawn from distutils.spawn import spawn
from distutils.file_util import move_file from distutils.file_util import move_file
from distutils.dir_util import mkpath from distutils.dir_util import mkpath
from distutils.dep_util import newer_pairwise, newer_group from distutils.dep_util import newer_group
from distutils.util import split_quoted, execute from distutils.util import split_quoted, execute
from distutils import log from distutils import log
@ -87,11 +88,7 @@ class CCompiler:
} }
language_order = ["c++", "objc", "c"] language_order = ["c++", "objc", "c"]
def __init__ (self, def __init__ (self, verbose=0, dry_run=0, force=0):
verbose=0,
dry_run=0,
force=0):
self.dry_run = dry_run self.dry_run = dry_run
self.force = force self.force = force
self.verbose = verbose self.verbose = verbose
@ -127,11 +124,7 @@ class CCompiler:
for key in self.executables.keys(): for key in self.executables.keys():
self.set_executable(key, self.executables[key]) self.set_executable(key, self.executables[key])
# __init__ () def set_executables(self, **args):
def set_executables (self, **args):
"""Define the executables (and options for them) that will be run """Define the executables (and options for them) that will be run
to perform the various stages of compilation. The exact set of to perform the various stages of compilation. The exact set of
executables that may be specified here depends on the compiler executables that may be specified here depends on the compiler
@ -164,26 +157,21 @@ class CCompiler:
(key, self.__class__.__name__) (key, self.__class__.__name__)
self.set_executable(key, args[key]) self.set_executable(key, args[key])
# set_executables ()
def set_executable(self, key, value): def set_executable(self, key, value):
if isinstance(value, str): if isinstance(value, str):
setattr(self, key, split_quoted(value)) setattr(self, key, split_quoted(value))
else: else:
setattr(self, key, value) setattr(self, key, value)
def _find_macro(self, name):
def _find_macro (self, name):
i = 0 i = 0
for defn in self.macros: for defn in self.macros:
if defn[0] == name: if defn[0] == name:
return i return i
i = i + 1 i = i + 1
return None return None
def _check_macro_definitions(self, definitions):
def _check_macro_definitions (self, definitions):
"""Ensures that every element of 'definitions' is a valid macro """Ensures that every element of 'definitions' is a valid macro
definition, ie. either (name,value) 2-tuple or a (name,) tuple. Do definition, ie. either (name,value) 2-tuple or a (name,) tuple. Do
nothing if all definitions are OK, raise TypeError otherwise. nothing if all definitions are OK, raise TypeError otherwise.
@ -202,7 +190,7 @@ class CCompiler:
# -- Bookkeeping methods ------------------------------------------- # -- Bookkeeping methods -------------------------------------------
def define_macro (self, name, value=None): def define_macro(self, name, value=None):
"""Define a preprocessor macro for all compilations driven by this """Define a preprocessor macro for all compilations driven by this
compiler object. The optional parameter 'value' should be a compiler object. The optional parameter 'value' should be a
string; if it is not supplied, then the macro will be defined string; if it is not supplied, then the macro will be defined
@ -218,8 +206,7 @@ class CCompiler:
defn = (name, value) defn = (name, value)
self.macros.append (defn) self.macros.append (defn)
def undefine_macro(self, name):
def undefine_macro (self, name):
"""Undefine a preprocessor macro for all compilations driven by """Undefine a preprocessor macro for all compilations driven by
this compiler object. If the same macro is defined by this compiler object. If the same macro is defined by
'define_macro()' and undefined by 'undefine_macro()' the last call 'define_macro()' and undefined by 'undefine_macro()' the last call
@ -237,8 +224,7 @@ class CCompiler:
undefn = (name,) undefn = (name,)
self.macros.append (undefn) self.macros.append (undefn)
def add_include_dir(self, dir):
def add_include_dir (self, dir):
"""Add 'dir' to the list of directories that will be searched for """Add 'dir' to the list of directories that will be searched for
header files. The compiler is instructed to search directories in header files. The compiler is instructed to search directories in
the order in which they are supplied by successive calls to the order in which they are supplied by successive calls to
@ -246,7 +232,7 @@ class CCompiler:
""" """
self.include_dirs.append (dir) self.include_dirs.append (dir)
def set_include_dirs (self, dirs): def set_include_dirs(self, dirs):
"""Set the list of directories that will be searched to 'dirs' (a """Set the list of directories that will be searched to 'dirs' (a
list of strings). Overrides any preceding calls to list of strings). Overrides any preceding calls to
'add_include_dir()'; subsequence calls to 'add_include_dir()' add 'add_include_dir()'; subsequence calls to 'add_include_dir()' add
@ -256,8 +242,7 @@ class CCompiler:
""" """
self.include_dirs = dirs[:] self.include_dirs = dirs[:]
def add_library(self, libname):
def add_library (self, libname):
"""Add 'libname' to the list of libraries that will be included in """Add 'libname' to the list of libraries that will be included in
all links driven by this compiler object. Note that 'libname' all links driven by this compiler object. Note that 'libname'
should *not* be the name of a file containing a library, but the should *not* be the name of a file containing a library, but the
@ -273,7 +258,7 @@ class CCompiler:
""" """
self.libraries.append (libname) self.libraries.append (libname)
def set_libraries (self, libnames): def set_libraries(self, libnames):
"""Set the list of libraries to be included in all links driven by """Set the list of libraries to be included in all links driven by
this compiler object to 'libnames' (a list of strings). This does this compiler object to 'libnames' (a list of strings). This does
not affect any standard system libraries that the linker may not affect any standard system libraries that the linker may
@ -282,29 +267,28 @@ class CCompiler:
self.libraries = libnames[:] self.libraries = libnames[:]
def add_library_dir (self, dir): def add_library_dir(self, dir):
"""Add 'dir' to the list of directories that will be searched for """Add 'dir' to the list of directories that will be searched for
libraries specified to 'add_library()' and 'set_libraries()'. The libraries specified to 'add_library()' and 'set_libraries()'. The
linker will be instructed to search for libraries in the order they linker will be instructed to search for libraries in the order they
are supplied to 'add_library_dir()' and/or 'set_library_dirs()'. are supplied to 'add_library_dir()' and/or 'set_library_dirs()'.
""" """
self.library_dirs.append (dir) self.library_dirs.append(dir)
def set_library_dirs (self, dirs): def set_library_dirs(self, dirs):
"""Set the list of library search directories to 'dirs' (a list of """Set the list of library search directories to 'dirs' (a list of
strings). This does not affect any standard library search path strings). This does not affect any standard library search path
that the linker may search by default. that the linker may search by default.
""" """
self.library_dirs = dirs[:] self.library_dirs = dirs[:]
def add_runtime_library_dir(self, dir):
def add_runtime_library_dir (self, dir):
"""Add 'dir' to the list of directories that will be searched for """Add 'dir' to the list of directories that will be searched for
shared libraries at runtime. shared libraries at runtime.
""" """
self.runtime_library_dirs.append (dir) self.runtime_library_dirs.append(dir)
def set_runtime_library_dirs (self, dirs): def set_runtime_library_dirs(self, dirs):
"""Set the list of directories to search for shared libraries at """Set the list of directories to search for shared libraries at
runtime to 'dirs' (a list of strings). This does not affect any runtime to 'dirs' (a list of strings). This does not affect any
standard search path that the runtime linker may search by standard search path that the runtime linker may search by
@ -312,16 +296,15 @@ class CCompiler:
""" """
self.runtime_library_dirs = dirs[:] self.runtime_library_dirs = dirs[:]
def add_link_object(self, object):
def add_link_object (self, object):
"""Add 'object' to the list of object files (or analogues, such as """Add 'object' to the list of object files (or analogues, such as
explicitly named library files or the output of "resource explicitly named library files or the output of "resource
compilers") to be included in every link driven by this compiler compilers") to be included in every link driven by this compiler
object. object.
""" """
self.objects.append (object) self.objects.append(object)
def set_link_objects (self, objects): def set_link_objects(self, objects):
"""Set the list of object files (or analogues) to be included in """Set the list of object files (or analogues) to be included in
every link to 'objects'. This does not affect any standard object every link to 'objects'. This does not affect any standard object
files that the linker may include by default (such as system files that the linker may include by default (such as system
@ -388,7 +371,7 @@ class CCompiler:
cc_args[:0] = before cc_args[:0] = before
return cc_args return cc_args
def _fix_compile_args (self, output_dir, macros, include_dirs): def _fix_compile_args(self, output_dir, macros, include_dirs):
"""Typecheck and fix-up some of the arguments to the 'compile()' """Typecheck and fix-up some of the arguments to the 'compile()'
method, and return fixed-up values. Specifically: if 'output_dir' method, and return fixed-up values. Specifically: if 'output_dir'
is None, replaces it with 'self.output_dir'; ensures that 'macros' is None, replaces it with 'self.output_dir'; ensures that 'macros'
@ -400,7 +383,7 @@ class CCompiler:
""" """
if output_dir is None: if output_dir is None:
output_dir = self.output_dir output_dir = self.output_dir
elif type (output_dir) is not StringType: 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: if macros is None:
@ -420,9 +403,7 @@ class CCompiler:
return output_dir, macros, include_dirs return output_dir, macros, include_dirs
# _fix_compile_args () def _fix_object_args(self, objects, output_dir):
def _fix_object_args (self, objects, output_dir):
"""Typecheck and fix up some arguments supplied to various methods. """Typecheck and fix up some arguments supplied to various methods.
Specifically: ensure that 'objects' is a list; if output_dir is Specifically: ensure that 'objects' is a list; if output_dir is
None, replace with self.output_dir. Return fixed versions of None, replace with self.output_dir. Return fixed versions of
@ -440,8 +421,7 @@ class CCompiler:
return (objects, output_dir) return (objects, output_dir)
def _fix_lib_args(self, libraries, library_dirs, runtime_library_dirs):
def _fix_lib_args (self, libraries, library_dirs, runtime_library_dirs):
"""Typecheck and fix up some of the arguments supplied to the """Typecheck and fix up some of the arguments supplied to the
'link_*' methods. Specifically: ensure that all arguments are 'link_*' methods. Specifically: ensure that all arguments are
lists, and augment them with their permanent versions lists, and augment them with their permanent versions
@ -476,10 +456,7 @@ class CCompiler:
return (libraries, library_dirs, runtime_library_dirs) return (libraries, library_dirs, runtime_library_dirs)
# _fix_lib_args () def _need_link(self, objects, output_file):
def _need_link (self, objects, output_file):
"""Return true if we need to relink the files listed in 'objects' """Return true if we need to relink the files listed in 'objects'
to recreate 'output_file'. to recreate 'output_file'.
""" """
@ -492,9 +469,7 @@ class CCompiler:
newer = newer_group (objects, output_file) newer = newer_group (objects, output_file)
return newer return newer
# _need_link () def detect_language(self, sources):
def detect_language (self, sources):
"""Detect the language of a given file, or list of files. Uses """Detect the language of a given file, or list of files. Uses
language_map, and language_order to do the job. language_map, and language_order to do the job.
""" """
@ -514,18 +489,11 @@ class CCompiler:
pass pass
return lang return lang
# detect_language ()
# -- Worker methods ------------------------------------------------ # -- Worker methods ------------------------------------------------
# (must be implemented by subclasses) # (must be implemented by subclasses)
def preprocess (self, def preprocess(self, source, output_file=None, macros=None,
source, include_dirs=None, extra_preargs=None, extra_postargs=None):
output_file=None,
macros=None,
include_dirs=None,
extra_preargs=None,
extra_postargs=None):
"""Preprocess a single C/C++ source file, named in 'source'. """Preprocess a single C/C++ source file, named in 'source'.
Output will be written to file named 'output_file', or stdout if Output will be written to file named 'output_file', or stdout if
'output_file' not supplied. 'macros' is a list of macro 'output_file' not supplied. 'macros' is a list of macro
@ -613,12 +581,8 @@ class CCompiler:
# should implement _compile(). # should implement _compile().
pass pass
def create_static_lib (self, def create_static_lib(self, objects, output_libname, output_dir=None,
objects, debug=0, target_lang=None):
output_libname,
output_dir=None,
debug=0,
target_lang=None):
"""Link a bunch of stuff together to create a static library file. """Link a bunch of stuff together to create a static library file.
The "bunch of stuff" consists of the list of object files supplied The "bunch of stuff" consists of the list of object files supplied
as 'objects', the extra object files supplied to as 'objects', the extra object files supplied to
@ -643,26 +607,15 @@ class CCompiler:
""" """
pass pass
# values for target_desc parameter in link() # values for target_desc parameter in link()
SHARED_OBJECT = "shared_object" SHARED_OBJECT = "shared_object"
SHARED_LIBRARY = "shared_library" SHARED_LIBRARY = "shared_library"
EXECUTABLE = "executable" EXECUTABLE = "executable"
def link (self, def link(self, target_desc, objects, output_filename, output_dir=None,
target_desc, libraries=None, library_dirs=None, runtime_library_dirs=None,
objects, export_symbols=None, debug=0, extra_preargs=None,
output_filename, extra_postargs=None, build_temp=None, target_lang=None):
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 """Link a bunch of stuff together to create an executable or
shared library file. shared library file.
@ -711,19 +664,11 @@ class CCompiler:
# Old 'link_*()' methods, rewritten to use the new 'link()' method. # Old 'link_*()' methods, rewritten to use the new 'link()' method.
def link_shared_lib (self, def link_shared_lib(self, objects, output_libname, output_dir=None,
objects, libraries=None, library_dirs=None,
output_libname, runtime_library_dirs=None, export_symbols=None,
output_dir=None, debug=0, extra_preargs=None, extra_postargs=None,
libraries=None, build_temp=None, target_lang=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.link(CCompiler.SHARED_LIBRARY, objects,
self.library_filename(output_libname, lib_type='shared'), self.library_filename(output_libname, lib_type='shared'),
output_dir, output_dir,
@ -732,37 +677,21 @@ class CCompiler:
extra_preargs, extra_postargs, build_temp, target_lang) extra_preargs, extra_postargs, build_temp, target_lang)
def link_shared_object (self, def link_shared_object(self, objects, output_filename, output_dir=None,
objects, libraries=None, library_dirs=None,
output_filename, runtime_library_dirs=None, export_symbols=None,
output_dir=None, debug=0, extra_preargs=None, extra_postargs=None,
libraries=None, build_temp=None, target_lang=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, self.link(CCompiler.SHARED_OBJECT, objects,
output_filename, output_dir, output_filename, output_dir,
libraries, library_dirs, runtime_library_dirs, libraries, library_dirs, runtime_library_dirs,
export_symbols, debug, export_symbols, debug,
extra_preargs, extra_postargs, build_temp, target_lang) extra_preargs, extra_postargs, build_temp, target_lang)
def link_executable(self, objects, output_progname, output_dir=None,
def link_executable (self, libraries=None, library_dirs=None,
objects, runtime_library_dirs=None, debug=0, extra_preargs=None,
output_progname, extra_postargs=None, target_lang=None):
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.link(CCompiler.EXECUTABLE, objects,
self.executable_filename(output_progname), output_dir, self.executable_filename(output_progname), output_dir,
libraries, library_dirs, runtime_library_dirs, None, libraries, library_dirs, runtime_library_dirs, None,
@ -774,29 +703,26 @@ class CCompiler:
# no appropriate default implementation so subclasses should # no appropriate default implementation so subclasses should
# implement all of these. # implement all of these.
def library_dir_option (self, dir): def library_dir_option(self, dir):
"""Return the compiler option to add 'dir' to the list of """Return the compiler option to add 'dir' to the list of
directories searched for libraries. directories searched for libraries.
""" """
raise NotImplementedError raise NotImplementedError
def runtime_library_dir_option (self, dir): def runtime_library_dir_option(self, dir):
"""Return the compiler option to add 'dir' to the list of """Return the compiler option to add 'dir' to the list of
directories searched for runtime libraries. directories searched for runtime libraries.
""" """
raise NotImplementedError raise NotImplementedError
def library_option (self, lib): def library_option(self, lib):
"""Return the compiler option to add 'dir' to the list of libraries """Return the compiler option to add 'dir' to the list of libraries
linked into the shared library or executable. linked into the shared library or executable.
""" """
raise NotImplementedError raise NotImplementedError
def has_function(self, funcname, def has_function(self, funcname, includes=None, include_dirs=None,
includes=None, libraries=None, library_dirs=None):
include_dirs=None,
libraries=None,
library_dirs=None):
"""Return a boolean indicating whether funcname is supported on """Return a boolean indicating whether funcname is supported on
the current platform. The optional arguments can be used to the current platform. The optional arguments can be used to
augment the compilation environment. augment the compilation environment.
@ -927,28 +853,28 @@ main (int argc, char **argv) {
# -- Utility methods ----------------------------------------------- # -- Utility methods -----------------------------------------------
def announce (self, msg, level=1): def announce(self, msg, level=1):
log.debug(msg) log.debug(msg)
def debug_print (self, msg): def debug_print(self, msg):
from distutils.debug import DEBUG from distutils.debug import DEBUG
if DEBUG: if DEBUG:
print msg print msg
def warn (self, msg): def warn(self, msg):
sys.stderr.write ("warning: %s\n" % msg) sys.stderr.write("warning: %s\n" % msg)
def execute (self, func, args, msg=None, level=1): def execute(self, func, args, msg=None, level=1):
execute(func, args, msg, self.dry_run) execute(func, args, msg, self.dry_run)
def spawn (self, cmd): def spawn(self, cmd):
spawn (cmd, dry_run=self.dry_run) spawn(cmd, dry_run=self.dry_run)
def move_file (self, src, dst): def move_file(self, src, dst):
return move_file (src, dst, dry_run=self.dry_run) return move_file(src, dst, dry_run=self.dry_run)
def mkpath (self, name, mode=0777): def mkpath(self, name, mode=0777):
mkpath (name, mode, dry_run=self.dry_run) mkpath(name, mode, dry_run=self.dry_run)
# class CCompiler # class CCompiler
@ -974,7 +900,6 @@ _default_compilers = (
) )
def get_default_compiler(osname=None, platform=None): def get_default_compiler(osname=None, platform=None):
""" Determine the default compiler to use for the given platform. """ Determine the default compiler to use for the given platform.
osname should be one of the standard Python OS names (i.e. the osname should be one of the standard Python OS names (i.e. the
@ -1030,11 +955,7 @@ def show_compilers():
pretty_printer.print_help("List of available compilers:") pretty_printer.print_help("List of available compilers:")
def new_compiler (plat=None, def new_compiler(plat=None, compiler=None, verbose=0, dry_run=0, force=0):
compiler=None,
verbose=0,
dry_run=0,
force=0):
"""Generate an instance of some CCompiler subclass for the supplied """Generate an instance of some CCompiler subclass for the supplied
platform/compiler combination. 'plat' defaults to 'os.name' platform/compiler combination. 'plat' defaults to 'os.name'
(eg. 'posix', 'nt'), and 'compiler' defaults to the default compiler (eg. 'posix', 'nt'), and 'compiler' defaults to the default compiler
@ -1076,10 +997,10 @@ def new_compiler (plat=None,
# XXX The None is necessary to preserve backwards compatibility # XXX The None is necessary to preserve backwards compatibility
# with classes that expect verbose to be the first positional # with classes that expect verbose to be the first positional
# argument. # argument.
return klass (None, dry_run, force) return klass(None, dry_run, force)
def gen_preprocess_options (macros, include_dirs): def gen_preprocess_options(macros, include_dirs):
"""Generate C pre-processor options (-D, -U, -I) as used by at least """Generate C pre-processor options (-D, -U, -I) as used by at least
two types of compilers: the typical Unix compiler and Visual C++. 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,) 'macros' is the usual thing, a list of 1- or 2-tuples, where (name,)