home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / BUILD_CLIB.PY < prev    next >
Encoding:
Python Source  |  2000-10-14  |  8.2 KB  |  225 lines

  1. """distutils.command.build_clib
  2.  
  3. Implements the Distutils 'build_clib' command, to build a C/C++ library
  4. that is included in the module distribution and needed by an extension
  5. module."""
  6.  
  7. # created (an empty husk) 1999/12/18, Greg Ward
  8. # fleshed out 2000/02/03-04
  9.  
  10. __revision__ = "$Id: build_clib.py,v 1.22 2000/10/14 04:06:40 gward Exp $"
  11.  
  12.  
  13. # XXX this module has *lots* of code ripped-off quite transparently from
  14. # build_ext.py -- not surprisingly really, as the work required to build
  15. # a static library from a collection of C source files is not really all
  16. # that different from what's required to build a shared object file from
  17. # a collection of C source files.  Nevertheless, I haven't done the
  18. # necessary refactoring to account for the overlap in code between the
  19. # two modules, mainly because a number of subtle details changed in the
  20. # cut 'n paste.  Sigh.
  21.  
  22. import os, string
  23. from types import *
  24. from distutils.core import Command
  25. from distutils.errors import *
  26. from distutils.sysconfig import customize_compiler
  27.  
  28.  
  29. def show_compilers ():
  30.     from distutils.ccompiler import show_compilers
  31.     show_compilers()
  32.  
  33.  
  34. class build_clib (Command):
  35.  
  36.     description = "build C/C++ libraries used by Python extensions"
  37.  
  38.     user_options = [
  39.         ('build-clib', 'b',
  40.          "directory to build C/C++ libraries to"),
  41.         ('build-temp', 't',
  42.          "directory to put temporary build by-products"),
  43.         ('debug', 'g',
  44.          "compile with debugging information"),
  45.         ('force', 'f',
  46.          "forcibly build everything (ignore file timestamps)"),
  47.         ('compiler=', 'c',
  48.          "specify the compiler type"),
  49.         ]
  50.  
  51.     boolean_options = ['debug', 'force']
  52.  
  53.     help_options = [
  54.         ('help-compiler', None,
  55.          "list available compilers", show_compilers),
  56.         ]
  57.  
  58.     def initialize_options (self):
  59.         self.build_clib = None
  60.         self.build_temp = None
  61.  
  62.         # List of libraries to build
  63.         self.libraries = None
  64.  
  65.         # Compilation options for all libraries
  66.         self.include_dirs = None
  67.         self.define = None
  68.         self.undef = None
  69.         self.debug = None
  70.         self.force = 0
  71.         self.compiler = None
  72.  
  73.     # initialize_options()
  74.  
  75.  
  76.     def finalize_options (self):
  77.  
  78.         # This might be confusing: both build-clib and build-temp default
  79.         # to build-temp as defined by the "build" command.  This is because
  80.         # I think that C libraries are really just temporary build
  81.         # by-products, at least from the point of view of building Python
  82.         # extensions -- but I want to keep my options open.
  83.         self.set_undefined_options('build',
  84.                                    ('build_temp', 'build_clib'),
  85.                                    ('build_temp', 'build_temp'),
  86.                                    ('compiler', 'compiler'),
  87.                                    ('debug', 'debug'),
  88.                                    ('force', 'force'))
  89.  
  90.         self.libraries = self.distribution.libraries
  91.         if self.libraries:
  92.             self.check_library_list(self.libraries)
  93.  
  94.         if self.include_dirs is None:
  95.             self.include_dirs = self.distribution.include_dirs or []
  96.         if type(self.include_dirs) is StringType:
  97.             self.include_dirs = string.split(self.include_dirs,
  98.                                              os.pathsep)
  99.  
  100.         # XXX same as for build_ext -- what about 'self.define' and
  101.         # 'self.undef' ?
  102.  
  103.     # finalize_options()
  104.  
  105.  
  106.     def run (self):
  107.  
  108.         if not self.libraries:
  109.             return
  110.  
  111.         # Yech -- this is cut 'n pasted from build_ext.py!
  112.         from distutils.ccompiler import new_compiler
  113.         self.compiler = new_compiler(compiler=self.compiler,
  114.                                      verbose=self.verbose,
  115.                                      dry_run=self.dry_run,
  116.                                      force=self.force)
  117.         customize_compiler(self.compiler)
  118.  
  119.         if self.include_dirs is not None:
  120.             self.compiler.set_include_dirs(self.include_dirs)
  121.         if self.define is not None:
  122.             # 'define' option is a list of (name,value) tuples
  123.             for (name,value) in self.define:
  124.                 self.compiler.define_macro(name, value)
  125.         if self.undef is not None:
  126.             for macro in self.undef:
  127.                 self.compiler.undefine_macro(macro)
  128.  
  129.         self.build_libraries(self.libraries)
  130.  
  131.     # run()
  132.  
  133.  
  134.     def check_library_list (self, libraries):
  135.         """Ensure that the list of libraries (presumably provided as a
  136.            command option 'libraries') is valid, i.e. it is a list of
  137.            2-tuples, where the tuples are (library_name, build_info_dict).
  138.            Raise DistutilsSetupError if the structure is invalid anywhere;
  139.            just returns otherwise."""
  140.  
  141.         # Yechh, blecch, ackk: this is ripped straight out of build_ext.py,
  142.         # with only names changed to protect the innocent!
  143.  
  144.         if type(libraries) is not ListType:
  145.             raise DistutilsSetupError, \
  146.                   "'libraries' option must be a list of tuples"
  147.  
  148.         for lib in libraries:
  149.             if type(lib) is not TupleType and len(lib) != 2:
  150.                 raise DistutilsSetupError, \
  151.                       "each element of 'libraries' must a 2-tuple"
  152.  
  153.             if type(lib[0]) is not StringType:
  154.                 raise DistutilsSetupError, \
  155.                       "first element of each tuple in 'libraries' " + \
  156.                       "must be a string (the library name)"
  157.             if '/' in lib[0] or (os.sep != '/' and os.sep in lib[0]):
  158.                 raise DistutilsSetupError, \
  159.                       ("bad library name '%s': " + 
  160.                        "may not contain directory separators") % \
  161.                       lib[0]
  162.  
  163.             if type(lib[1]) is not DictionaryType:
  164.                 raise DistutilsSetupError, \
  165.                       "second element of each tuple in 'libraries' " + \
  166.                       "must be a dictionary (build info)"
  167.         # for lib
  168.  
  169.     # check_library_list ()
  170.  
  171.  
  172.     def get_library_names (self):
  173.         # Assume the library list is valid -- 'check_library_list()' is
  174.         # called from 'finalize_options()', so it should be!
  175.  
  176.         if not self.libraries:
  177.             return None
  178.  
  179.         lib_names = []
  180.         for (lib_name, build_info) in self.libraries:
  181.             lib_names.append(lib_name)
  182.         return lib_names
  183.  
  184.     # get_library_names ()
  185.  
  186.  
  187.     def build_libraries (self, libraries):
  188.  
  189.         compiler = self.compiler
  190.  
  191.         for (lib_name, build_info) in libraries:
  192.             sources = build_info.get('sources')
  193.             if sources is None or type(sources) not in (ListType, TupleType):
  194.                 raise DistutilsSetupError, \
  195.                       ("in 'libraries' option (library '%s'), " +
  196.                        "'sources' must be present and must be " +
  197.                        "a list of source filenames") % lib_name
  198.             sources = list(sources)
  199.  
  200.             self.announce("building '%s' library" % lib_name)
  201.  
  202.             # First, compile the source code to object files in the library
  203.             # directory.  (This should probably change to putting object
  204.             # files in a temporary build directory.)
  205.             macros = build_info.get('macros')
  206.             include_dirs = build_info.get('include_dirs')
  207.             objects = self.compiler.compile(sources,
  208.                                             output_dir=self.build_temp,
  209.                                             macros=macros,
  210.                                             include_dirs=include_dirs,
  211.                                             debug=self.debug)
  212.  
  213.             # Now "link" the object files together into a static library.
  214.             # (On Unix at least, this isn't really linking -- it just
  215.             # builds an archive.  Whatever.)
  216.             self.compiler.create_static_lib(objects, lib_name,
  217.                                             output_dir=self.build_clib,
  218.                                             debug=self.debug)
  219.  
  220.         # for libraries
  221.  
  222.     # build_libraries ()
  223.  
  224. # class build_lib
  225.