PyXR

c:\python24\lib \ distutils \ command \ build_clib.py



0001 """distutils.command.build_clib
0002 
0003 Implements the Distutils 'build_clib' command, to build a C/C++ library
0004 that is included in the module distribution and needed by an extension
0005 module."""
0006 
0007 # This module should be kept compatible with Python 1.5.2.
0008 
0009 __revision__ = "$Id: build_clib.py,v 1.27 2002/11/19 13:12:28 akuchling Exp $"
0010 
0011 
0012 # XXX this module has *lots* of code ripped-off quite transparently from
0013 # build_ext.py -- not surprisingly really, as the work required to build
0014 # a static library from a collection of C source files is not really all
0015 # that different from what's required to build a shared object file from
0016 # a collection of C source files.  Nevertheless, I haven't done the
0017 # necessary refactoring to account for the overlap in code between the
0018 # two modules, mainly because a number of subtle details changed in the
0019 # cut 'n paste.  Sigh.
0020 
0021 import os, string
0022 from types import *
0023 from distutils.core import Command
0024 from distutils.errors import *
0025 from distutils.sysconfig import customize_compiler
0026 from distutils import log
0027 
0028 def show_compilers ():
0029     from distutils.ccompiler import show_compilers
0030     show_compilers()
0031 
0032 
0033 class build_clib (Command):
0034 
0035     description = "build C/C++ libraries used by Python extensions"
0036 
0037     user_options = [
0038         ('build-clib', 'b',
0039          "directory to build C/C++ libraries to"),
0040         ('build-temp', 't',
0041          "directory to put temporary build by-products"),
0042         ('debug', 'g',
0043          "compile with debugging information"),
0044         ('force', 'f',
0045          "forcibly build everything (ignore file timestamps)"),
0046         ('compiler=', 'c',
0047          "specify the compiler type"),
0048         ]
0049 
0050     boolean_options = ['debug', 'force']
0051 
0052     help_options = [
0053         ('help-compiler', None,
0054          "list available compilers", show_compilers),
0055         ]
0056 
0057     def initialize_options (self):
0058         self.build_clib = None
0059         self.build_temp = None
0060 
0061         # List of libraries to build
0062         self.libraries = None
0063 
0064         # Compilation options for all libraries
0065         self.include_dirs = None
0066         self.define = None
0067         self.undef = None
0068         self.debug = None
0069         self.force = 0
0070         self.compiler = None
0071 
0072     # initialize_options()
0073 
0074 
0075     def finalize_options (self):
0076 
0077         # This might be confusing: both build-clib and build-temp default
0078         # to build-temp as defined by the "build" command.  This is because
0079         # I think that C libraries are really just temporary build
0080         # by-products, at least from the point of view of building Python
0081         # extensions -- but I want to keep my options open.
0082         self.set_undefined_options('build',
0083                                    ('build_temp', 'build_clib'),
0084                                    ('build_temp', 'build_temp'),
0085                                    ('compiler', 'compiler'),
0086                                    ('debug', 'debug'),
0087                                    ('force', 'force'))
0088 
0089         self.libraries = self.distribution.libraries
0090         if self.libraries:
0091             self.check_library_list(self.libraries)
0092 
0093         if self.include_dirs is None:
0094             self.include_dirs = self.distribution.include_dirs or []
0095         if type(self.include_dirs) is StringType:
0096             self.include_dirs = string.split(self.include_dirs,
0097                                              os.pathsep)
0098 
0099         # XXX same as for build_ext -- what about 'self.define' and
0100         # 'self.undef' ?
0101 
0102     # finalize_options()
0103 
0104 
0105     def run (self):
0106 
0107         if not self.libraries:
0108             return
0109 
0110         # Yech -- this is cut 'n pasted from build_ext.py!
0111         from distutils.ccompiler import new_compiler
0112         self.compiler = new_compiler(compiler=self.compiler,
0113                                      dry_run=self.dry_run,
0114                                      force=self.force)
0115         customize_compiler(self.compiler)
0116 
0117         if self.include_dirs is not None:
0118             self.compiler.set_include_dirs(self.include_dirs)
0119         if self.define is not None:
0120             # 'define' option is a list of (name,value) tuples
0121             for (name,value) in self.define:
0122                 self.compiler.define_macro(name, value)
0123         if self.undef is not None:
0124             for macro in self.undef:
0125                 self.compiler.undefine_macro(macro)
0126 
0127         self.build_libraries(self.libraries)
0128 
0129     # run()
0130 
0131 
0132     def check_library_list (self, libraries):
0133         """Ensure that the list of libraries (presumably provided as a
0134            command option 'libraries') is valid, i.e. it is a list of
0135            2-tuples, where the tuples are (library_name, build_info_dict).
0136            Raise DistutilsSetupError if the structure is invalid anywhere;
0137            just returns otherwise."""
0138 
0139         # Yechh, blecch, ackk: this is ripped straight out of build_ext.py,
0140         # with only names changed to protect the innocent!
0141 
0142         if type(libraries) is not ListType:
0143             raise DistutilsSetupError, \
0144                   "'libraries' option must be a list of tuples"
0145 
0146         for lib in libraries:
0147             if type(lib) is not TupleType and len(lib) != 2:
0148                 raise DistutilsSetupError, \
0149                       "each element of 'libraries' must a 2-tuple"
0150 
0151             if type(lib[0]) is not StringType:
0152                 raise DistutilsSetupError, \
0153                       "first element of each tuple in 'libraries' " + \
0154                       "must be a string (the library name)"
0155             if '/' in lib[0] or (os.sep != '/' and os.sep in lib[0]):
0156                 raise DistutilsSetupError, \
0157                       ("bad library name '%s': " +
0158                        "may not contain directory separators") % \
0159                       lib[0]
0160 
0161             if type(lib[1]) is not DictionaryType:
0162                 raise DistutilsSetupError, \
0163                       "second element of each tuple in 'libraries' " + \
0164                       "must be a dictionary (build info)"
0165         # for lib
0166 
0167     # check_library_list ()
0168 
0169 
0170     def get_library_names (self):
0171         # Assume the library list is valid -- 'check_library_list()' is
0172         # called from 'finalize_options()', so it should be!
0173 
0174         if not self.libraries:
0175             return None
0176 
0177         lib_names = []
0178         for (lib_name, build_info) in self.libraries:
0179             lib_names.append(lib_name)
0180         return lib_names
0181 
0182     # get_library_names ()
0183 
0184 
0185     def get_source_files (self):
0186         self.check_library_list(self.libraries)
0187         filenames = []
0188         for (lib_name, build_info) in self.libraries:
0189             sources = build_info.get('sources')
0190             if (sources is None or
0191                 type(sources) not in (ListType, TupleType) ):
0192                 raise DistutilsSetupError, \
0193                       ("in 'libraries' option (library '%s'), "
0194                        "'sources' must be present and must be "
0195                        "a list of source filenames") % lib_name
0196 
0197             filenames.extend(sources)
0198 
0199         return filenames
0200     # get_source_files ()
0201 
0202 
0203     def build_libraries (self, libraries):
0204 
0205         for (lib_name, build_info) in libraries:
0206             sources = build_info.get('sources')
0207             if sources is None or type(sources) not in (ListType, TupleType):
0208                 raise DistutilsSetupError, \
0209                       ("in 'libraries' option (library '%s'), " +
0210                        "'sources' must be present and must be " +
0211                        "a list of source filenames") % lib_name
0212             sources = list(sources)
0213 
0214             log.info("building '%s' library", lib_name)
0215 
0216             # First, compile the source code to object files in the library
0217             # directory.  (This should probably change to putting object
0218             # files in a temporary build directory.)
0219             macros = build_info.get('macros')
0220             include_dirs = build_info.get('include_dirs')
0221             objects = self.compiler.compile(sources,
0222                                             output_dir=self.build_temp,
0223                                             macros=macros,
0224                                             include_dirs=include_dirs,
0225                                             debug=self.debug)
0226 
0227             # Now "link" the object files together into a static library.
0228             # (On Unix at least, this isn't really linking -- it just
0229             # builds an archive.  Whatever.)
0230             self.compiler.create_static_lib(objects, lib_name,
0231                                             output_dir=self.build_clib,
0232                                             debug=self.debug)
0233 
0234         # for libraries
0235 
0236     # build_libraries ()
0237 
0238 # class build_lib
0239 

Generated by PyXR 0.9.4
SourceForge.net Logo