-"""distutils.command.build_clib\r
-\r
-Implements the Distutils 'build_clib' command, to build a C/C++ library\r
-that is included in the module distribution and needed by an extension\r
-module."""\r
-\r
-__revision__ = "$Id$"\r
-\r
-\r
-# XXX this module has *lots* of code ripped-off quite transparently from\r
-# build_ext.py -- not surprisingly really, as the work required to build\r
-# a static library from a collection of C source files is not really all\r
-# that different from what's required to build a shared object file from\r
-# a collection of C source files. Nevertheless, I haven't done the\r
-# necessary refactoring to account for the overlap in code between the\r
-# two modules, mainly because a number of subtle details changed in the\r
-# cut 'n paste. Sigh.\r
-\r
-import os\r
-from distutils.core import Command\r
-from distutils.errors import DistutilsSetupError\r
-from distutils.ccompiler import customize_compiler\r
-from distutils import log\r
-\r
-def show_compilers():\r
- from distutils.ccompiler import show_compilers\r
- show_compilers()\r
-\r
-\r
-class build_clib(Command):\r
-\r
- description = "build C/C++ libraries used by Python extensions"\r
-\r
- user_options = [\r
- ('build-clib=', 'b',\r
- "directory to build C/C++ libraries to"),\r
- ('build-temp=', 't',\r
- "directory to put temporary build by-products"),\r
- ('debug', 'g',\r
- "compile with debugging information"),\r
- ('force', 'f',\r
- "forcibly build everything (ignore file timestamps)"),\r
- ('compiler=', 'c',\r
- "specify the compiler type"),\r
- ]\r
-\r
- boolean_options = ['debug', 'force']\r
-\r
- help_options = [\r
- ('help-compiler', None,\r
- "list available compilers", show_compilers),\r
- ]\r
-\r
- def initialize_options(self):\r
- self.build_clib = None\r
- self.build_temp = None\r
-\r
- # List of libraries to build\r
- self.libraries = None\r
-\r
- # Compilation options for all libraries\r
- self.include_dirs = None\r
- self.define = None\r
- self.undef = None\r
- self.debug = None\r
- self.force = 0\r
- self.compiler = None\r
-\r
-\r
- def finalize_options(self):\r
- # This might be confusing: both build-clib and build-temp default\r
- # to build-temp as defined by the "build" command. This is because\r
- # I think that C libraries are really just temporary build\r
- # by-products, at least from the point of view of building Python\r
- # extensions -- but I want to keep my options open.\r
- self.set_undefined_options('build',\r
- ('build_temp', 'build_clib'),\r
- ('build_temp', 'build_temp'),\r
- ('compiler', 'compiler'),\r
- ('debug', 'debug'),\r
- ('force', 'force'))\r
-\r
- self.libraries = self.distribution.libraries\r
- if self.libraries:\r
- self.check_library_list(self.libraries)\r
-\r
- if self.include_dirs is None:\r
- self.include_dirs = self.distribution.include_dirs or []\r
- if isinstance(self.include_dirs, str):\r
- self.include_dirs = self.include_dirs.split(os.pathsep)\r
-\r
- # XXX same as for build_ext -- what about 'self.define' and\r
- # 'self.undef' ?\r
-\r
- def run(self):\r
- if not self.libraries:\r
- return\r
-\r
- # Yech -- this is cut 'n pasted from build_ext.py!\r
- from distutils.ccompiler import new_compiler\r
- self.compiler = new_compiler(compiler=self.compiler,\r
- dry_run=self.dry_run,\r
- force=self.force)\r
- customize_compiler(self.compiler)\r
-\r
- if self.include_dirs is not None:\r
- self.compiler.set_include_dirs(self.include_dirs)\r
- if self.define is not None:\r
- # 'define' option is a list of (name,value) tuples\r
- for (name,value) in self.define:\r
- self.compiler.define_macro(name, value)\r
- if self.undef is not None:\r
- for macro in self.undef:\r
- self.compiler.undefine_macro(macro)\r
-\r
- self.build_libraries(self.libraries)\r
-\r
-\r
- def check_library_list(self, libraries):\r
- """Ensure that the list of libraries is valid.\r
-\r
- `library` is presumably provided as a command option 'libraries'.\r
- This method checks that it is a list of 2-tuples, where the tuples\r
- are (library_name, build_info_dict).\r
-\r
- Raise DistutilsSetupError if the structure is invalid anywhere;\r
- just returns otherwise.\r
- """\r
- if not isinstance(libraries, list):\r
- raise DistutilsSetupError, \\r
- "'libraries' option must be a list of tuples"\r
-\r
- for lib in libraries:\r
- if not isinstance(lib, tuple) and len(lib) != 2:\r
- raise DistutilsSetupError, \\r
- "each element of 'libraries' must a 2-tuple"\r
-\r
- name, build_info = lib\r
-\r
- if not isinstance(name, str):\r
- raise DistutilsSetupError, \\r
- "first element of each tuple in 'libraries' " + \\r
- "must be a string (the library name)"\r
- if '/' in name or (os.sep != '/' and os.sep in name):\r
- raise DistutilsSetupError, \\r
- ("bad library name '%s': " +\r
- "may not contain directory separators") % \\r
- lib[0]\r
-\r
- if not isinstance(build_info, dict):\r
- raise DistutilsSetupError, \\r
- "second element of each tuple in 'libraries' " + \\r
- "must be a dictionary (build info)"\r
-\r
- def get_library_names(self):\r
- # Assume the library list is valid -- 'check_library_list()' is\r
- # called from 'finalize_options()', so it should be!\r
- if not self.libraries:\r
- return None\r
-\r
- lib_names = []\r
- for (lib_name, build_info) in self.libraries:\r
- lib_names.append(lib_name)\r
- return lib_names\r
-\r
-\r
- def get_source_files(self):\r
- self.check_library_list(self.libraries)\r
- filenames = []\r
- for (lib_name, build_info) in self.libraries:\r
- sources = build_info.get('sources')\r
- if sources is None or not isinstance(sources, (list, tuple)):\r
- raise DistutilsSetupError, \\r
- ("in 'libraries' option (library '%s'), "\r
- "'sources' must be present and must be "\r
- "a list of source filenames") % lib_name\r
-\r
- filenames.extend(sources)\r
- return filenames\r
-\r
- def build_libraries(self, libraries):\r
- for (lib_name, build_info) in libraries:\r
- sources = build_info.get('sources')\r
- if sources is None or not isinstance(sources, (list, tuple)):\r
- raise DistutilsSetupError, \\r
- ("in 'libraries' option (library '%s'), " +\r
- "'sources' must be present and must be " +\r
- "a list of source filenames") % lib_name\r
- sources = list(sources)\r
-\r
- log.info("building '%s' library", lib_name)\r
-\r
- # First, compile the source code to object files in the library\r
- # directory. (This should probably change to putting object\r
- # files in a temporary build directory.)\r
- macros = build_info.get('macros')\r
- include_dirs = build_info.get('include_dirs')\r
- objects = self.compiler.compile(sources,\r
- output_dir=self.build_temp,\r
- macros=macros,\r
- include_dirs=include_dirs,\r
- debug=self.debug)\r
-\r
- # Now "link" the object files together into a static library.\r
- # (On Unix at least, this isn't really linking -- it just\r
- # builds an archive. Whatever.)\r
- self.compiler.create_static_lib(objects, lib_name,\r
- output_dir=self.build_clib,\r
- debug=self.debug)\r