2000-03-02 01:57:12 +00:00
|
|
|
"""distutils.command.build_clib
|
2000-02-05 02:23:59 +00:00
|
|
|
|
2000-03-02 01:57:12 +00:00
|
|
|
Implements the Distutils 'build_clib' command, to build a C/C++ library
|
2000-02-05 02:23:59 +00:00
|
|
|
that is included in the module distribution and needed by an extension
|
|
|
|
module."""
|
|
|
|
|
|
|
|
# created (an empty husk) 1999/12/18, Greg Ward
|
|
|
|
# fleshed out 2000/02/03-04
|
|
|
|
|
2000-03-02 01:49:45 +00:00
|
|
|
__revision__ = "$Id$"
|
2000-02-05 02:23:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
# XXX this module has *lots* of code ripped-off quite transparently from
|
|
|
|
# build_ext.py -- not surprisingly really, as the work required to build
|
|
|
|
# a static library from a collection of C source files is not really all
|
|
|
|
# that different from what's required to build a shared object file from
|
|
|
|
# a collection of C source files. Nevertheless, I haven't done the
|
|
|
|
# necessary refactoring to account for the overlap in code between the
|
|
|
|
# two modules, mainly because a number of subtle details changed in the
|
|
|
|
# cut 'n paste. Sigh.
|
|
|
|
|
|
|
|
import os, string
|
|
|
|
from types import *
|
|
|
|
from distutils.core import Command
|
|
|
|
from distutils.errors import *
|
2000-06-07 03:00:06 +00:00
|
|
|
from distutils.ccompiler import new_compiler,show_compilers
|
2000-02-05 02:23:59 +00:00
|
|
|
|
|
|
|
|
2000-03-02 01:57:12 +00:00
|
|
|
class build_clib (Command):
|
2000-02-05 02:23:59 +00:00
|
|
|
|
2000-03-02 01:27:36 +00:00
|
|
|
description = "build C/C++ libraries used by Python extensions"
|
|
|
|
|
2000-02-18 00:25:39 +00:00
|
|
|
user_options = [
|
2000-03-02 01:27:36 +00:00
|
|
|
('build-clib', 'b',
|
|
|
|
"directory to build C/C++ libraries to"),
|
|
|
|
('build-temp', 't',
|
|
|
|
"directory to put temporary build by-products"),
|
2000-02-18 00:25:39 +00:00
|
|
|
('debug', 'g',
|
|
|
|
"compile with debugging information"),
|
2000-04-10 00:19:42 +00:00
|
|
|
('force', 'f',
|
2000-05-12 00:33:14 +00:00
|
|
|
"forcibly build everything (ignore file timestamps)"),
|
|
|
|
('compiler=', 'c',
|
|
|
|
"specify the compiler type"),
|
2000-02-18 00:25:39 +00:00
|
|
|
]
|
2000-06-24 00:23:20 +00:00
|
|
|
|
2000-06-07 03:00:06 +00:00
|
|
|
help_options = [
|
|
|
|
('help-compiler', None,
|
2000-06-24 00:23:20 +00:00
|
|
|
"list available compilers", show_compilers),
|
2000-06-07 03:00:06 +00:00
|
|
|
]
|
2000-02-05 02:23:59 +00:00
|
|
|
|
2000-02-18 00:35:22 +00:00
|
|
|
def initialize_options (self):
|
2000-03-02 01:27:36 +00:00
|
|
|
self.build_clib = None
|
|
|
|
self.build_temp = None
|
|
|
|
|
2000-02-05 02:23:59 +00:00
|
|
|
# List of libraries to build
|
|
|
|
self.libraries = None
|
|
|
|
|
|
|
|
# Compilation options for all libraries
|
|
|
|
self.include_dirs = None
|
|
|
|
self.define = None
|
|
|
|
self.undef = None
|
2000-02-09 02:20:14 +00:00
|
|
|
self.debug = None
|
2000-04-10 00:19:42 +00:00
|
|
|
self.force = 0
|
2000-05-12 00:33:14 +00:00
|
|
|
self.compiler = None
|
2000-02-05 02:23:59 +00:00
|
|
|
|
2000-02-18 00:35:22 +00:00
|
|
|
# initialize_options()
|
2000-02-05 02:23:59 +00:00
|
|
|
|
2000-03-02 01:27:36 +00:00
|
|
|
|
2000-02-18 00:35:22 +00:00
|
|
|
def finalize_options (self):
|
2000-03-02 01:27:36 +00:00
|
|
|
|
|
|
|
# This might be confusing: both build-clib and build-temp default
|
|
|
|
# to build-temp as defined by the "build" command. This is because
|
|
|
|
# I think that C libraries are really just temporary build
|
|
|
|
# by-products, at least from the point of view of building Python
|
|
|
|
# extensions -- but I want to keep my options open.
|
2000-02-09 02:20:14 +00:00
|
|
|
self.set_undefined_options ('build',
|
2000-03-02 01:27:36 +00:00
|
|
|
('build_temp', 'build_clib'),
|
|
|
|
('build_temp', 'build_temp'),
|
2000-05-12 00:33:14 +00:00
|
|
|
('compiler', 'compiler'),
|
2000-04-10 00:19:42 +00:00
|
|
|
('debug', 'debug'),
|
|
|
|
('force', 'force'))
|
2000-03-02 01:27:36 +00:00
|
|
|
|
2000-02-05 02:23:59 +00:00
|
|
|
self.libraries = self.distribution.libraries
|
2000-03-02 01:27:36 +00:00
|
|
|
if self.libraries:
|
|
|
|
self.check_library_list (self.libraries)
|
|
|
|
|
2000-02-05 02:23:59 +00:00
|
|
|
if self.include_dirs is None:
|
|
|
|
self.include_dirs = self.distribution.include_dirs or []
|
|
|
|
if type (self.include_dirs) is StringType:
|
|
|
|
self.include_dirs = string.split (self.include_dirs,
|
|
|
|
os.pathsep)
|
|
|
|
|
|
|
|
# XXX same as for build_ext -- what about 'self.define' and
|
|
|
|
# 'self.undef' ?
|
|
|
|
|
2000-02-18 00:35:22 +00:00
|
|
|
# finalize_options()
|
2000-02-05 02:23:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run (self):
|
|
|
|
|
|
|
|
if not self.libraries:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Yech -- this is cut 'n pasted from build_ext.py!
|
2000-05-12 00:33:14 +00:00
|
|
|
self.compiler = new_compiler (compiler=self.compiler,
|
|
|
|
verbose=self.verbose,
|
2000-02-05 02:23:59 +00:00
|
|
|
dry_run=self.dry_run,
|
|
|
|
force=self.force)
|
2000-05-12 00:33:14 +00:00
|
|
|
|
2000-02-05 02:23:59 +00:00
|
|
|
if self.include_dirs is not None:
|
|
|
|
self.compiler.set_include_dirs (self.include_dirs)
|
|
|
|
if self.define is not None:
|
|
|
|
# 'define' option is a list of (name,value) tuples
|
|
|
|
for (name,value) in self.define:
|
|
|
|
self.compiler.define_macro (name, value)
|
|
|
|
if self.undef is not None:
|
|
|
|
for macro in self.undef:
|
|
|
|
self.compiler.undefine_macro (macro)
|
|
|
|
|
|
|
|
self.build_libraries (self.libraries)
|
|
|
|
|
|
|
|
# run()
|
|
|
|
|
|
|
|
|
|
|
|
def check_library_list (self, libraries):
|
|
|
|
"""Ensure that the list of libraries (presumably provided as a
|
|
|
|
command option 'libraries') is valid, i.e. it is a list of
|
|
|
|
2-tuples, where the tuples are (library_name, build_info_dict).
|
2000-04-15 22:15:07 +00:00
|
|
|
Raise DistutilsSetupError if the structure is invalid anywhere;
|
2000-02-05 02:23:59 +00:00
|
|
|
just returns otherwise."""
|
|
|
|
|
|
|
|
# Yechh, blecch, ackk: this is ripped straight out of build_ext.py,
|
|
|
|
# with only names changed to protect the innocent!
|
|
|
|
|
|
|
|
if type (libraries) is not ListType:
|
2000-04-15 22:15:07 +00:00
|
|
|
raise DistutilsSetupError, \
|
2000-02-05 02:23:59 +00:00
|
|
|
"'libraries' option must be a list of tuples"
|
|
|
|
|
|
|
|
for lib in libraries:
|
|
|
|
if type (lib) is not TupleType and len (lib) != 2:
|
2000-04-15 22:15:07 +00:00
|
|
|
raise DistutilsSetupError, \
|
2000-02-05 02:23:59 +00:00
|
|
|
"each element of 'libraries' must a 2-tuple"
|
|
|
|
|
|
|
|
if type (lib[0]) is not StringType:
|
2000-04-15 22:15:07 +00:00
|
|
|
raise DistutilsSetupError, \
|
2000-02-05 02:23:59 +00:00
|
|
|
"first element of each tuple in 'libraries' " + \
|
|
|
|
"must be a string (the library name)"
|
2000-03-02 01:27:36 +00:00
|
|
|
if '/' in lib[0] or (os.sep != '/' and os.sep in lib[0]):
|
2000-04-15 22:15:07 +00:00
|
|
|
raise DistutilsSetupError, \
|
2000-03-02 01:27:36 +00:00
|
|
|
("bad library name '%s': " +
|
|
|
|
"may not contain directory separators") % \
|
|
|
|
lib[0]
|
|
|
|
|
2000-02-05 02:23:59 +00:00
|
|
|
if type (lib[1]) is not DictionaryType:
|
2000-04-15 22:15:07 +00:00
|
|
|
raise DistutilsSetupError, \
|
2000-02-05 02:23:59 +00:00
|
|
|
"second element of each tuple in 'libraries' " + \
|
|
|
|
"must be a dictionary (build info)"
|
|
|
|
# for lib
|
|
|
|
|
|
|
|
# check_library_list ()
|
|
|
|
|
|
|
|
|
2000-03-02 01:27:36 +00:00
|
|
|
def get_library_names (self):
|
|
|
|
# Assume the library list is valid -- 'check_library_list()' is
|
|
|
|
# called from 'finalize_options()', so it should be!
|
|
|
|
|
|
|
|
if not self.libraries:
|
|
|
|
return None
|
|
|
|
|
|
|
|
lib_names = []
|
|
|
|
for (lib_name, build_info) in self.libraries:
|
|
|
|
lib_names.append (lib_name)
|
|
|
|
return lib_names
|
|
|
|
|
|
|
|
# get_library_names ()
|
|
|
|
|
|
|
|
|
2000-02-05 02:23:59 +00:00
|
|
|
def build_libraries (self, libraries):
|
|
|
|
|
|
|
|
compiler = self.compiler
|
|
|
|
|
|
|
|
for (lib_name, build_info) in libraries:
|
|
|
|
sources = build_info.get ('sources')
|
|
|
|
if sources is None or type (sources) not in (ListType, TupleType):
|
2000-04-15 22:15:07 +00:00
|
|
|
raise DistutilsSetupError, \
|
2000-02-05 02:23:59 +00:00
|
|
|
("in 'libraries' option (library '%s'), " +
|
|
|
|
"'sources' must be present and must be " +
|
|
|
|
"a list of source filenames") % lib_name
|
|
|
|
sources = list (sources)
|
|
|
|
|
|
|
|
self.announce ("building '%s' library" % lib_name)
|
|
|
|
|
|
|
|
# First, compile the source code to object files in the library
|
|
|
|
# directory. (This should probably change to putting object
|
|
|
|
# files in a temporary build directory.)
|
|
|
|
macros = build_info.get ('macros')
|
|
|
|
include_dirs = build_info.get ('include_dirs')
|
|
|
|
objects = self.compiler.compile (sources,
|
2000-03-02 01:27:36 +00:00
|
|
|
output_dir=self.build_temp,
|
2000-02-05 02:23:59 +00:00
|
|
|
macros=macros,
|
|
|
|
include_dirs=include_dirs,
|
2000-02-09 02:20:14 +00:00
|
|
|
debug=self.debug)
|
2000-02-05 02:23:59 +00:00
|
|
|
|
|
|
|
# Now "link" the object files together into a static library.
|
|
|
|
# (On Unix at least, this isn't really linking -- it just
|
|
|
|
# builds an archive. Whatever.)
|
2000-03-10 02:02:44 +00:00
|
|
|
self.compiler.create_static_lib (objects, lib_name,
|
|
|
|
output_dir=self.build_clib,
|
|
|
|
debug=self.debug)
|
2000-02-05 02:23:59 +00:00
|
|
|
|
|
|
|
# for libraries
|
|
|
|
|
|
|
|
# build_libraries ()
|
|
|
|
|
2000-05-25 01:10:04 +00:00
|
|
|
# class build_lib
|