2000-03-02 09:57:12 +08:00
|
|
|
"""distutils.command.build_clib
|
2000-02-05 10:23:59 +08:00
|
|
|
|
2000-03-02 09:57:12 +08:00
|
|
|
Implements the Distutils 'build_clib' command, to build a C/C++ library
|
2000-02-05 10:23:59 +08:00
|
|
|
that is included in the module distribution and needed by an extension
|
|
|
|
module."""
|
|
|
|
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
|
2007-04-17 16:48:32 +08:00
|
|
|
import os
|
2000-02-05 10:23:59 +08:00
|
|
|
from distutils.core import Command
|
2010-07-22 20:50:05 +08:00
|
|
|
from distutils.errors import *
|
|
|
|
from distutils.sysconfig import customize_compiler
|
2002-06-05 04:14:43 +08:00
|
|
|
from distutils import log
|
2000-06-24 09:23:37 +08:00
|
|
|
|
2007-08-30 11:52:21 +08:00
|
|
|
def show_compilers():
|
2000-06-24 09:23:37 +08:00
|
|
|
from distutils.ccompiler import show_compilers
|
|
|
|
show_compilers()
|
2000-02-05 10:23:59 +08:00
|
|
|
|
|
|
|
|
2007-08-30 11:52:21 +08:00
|
|
|
class build_clib(Command):
|
2000-02-05 10:23:59 +08:00
|
|
|
|
2000-03-02 09:27:36 +08:00
|
|
|
description = "build C/C++ libraries used by Python extensions"
|
|
|
|
|
2000-02-18 08:25:39 +08:00
|
|
|
user_options = [
|
2010-09-08 06:11:52 +08:00
|
|
|
('build-clib=', 'b',
|
2000-03-02 09:27:36 +08:00
|
|
|
"directory to build C/C++ libraries to"),
|
2010-09-08 06:11:52 +08:00
|
|
|
('build-temp=', 't',
|
2000-03-02 09:27:36 +08:00
|
|
|
"directory to put temporary build by-products"),
|
2000-02-18 08:25:39 +08:00
|
|
|
('debug', 'g',
|
|
|
|
"compile with debugging information"),
|
2000-04-10 08:19:42 +08:00
|
|
|
('force', 'f',
|
2000-05-12 08:33:14 +08:00
|
|
|
"forcibly build everything (ignore file timestamps)"),
|
|
|
|
('compiler=', 'c',
|
|
|
|
"specify the compiler type"),
|
2000-02-18 08:25:39 +08:00
|
|
|
]
|
2000-06-24 08:23:20 +08:00
|
|
|
|
2000-09-25 09:41:15 +08:00
|
|
|
boolean_options = ['debug', 'force']
|
|
|
|
|
2000-06-07 11:00:06 +08:00
|
|
|
help_options = [
|
|
|
|
('help-compiler', None,
|
2000-06-24 08:23:20 +08:00
|
|
|
"list available compilers", show_compilers),
|
2000-10-14 12:06:40 +08:00
|
|
|
]
|
2000-02-05 10:23:59 +08:00
|
|
|
|
2007-08-30 11:52:21 +08:00
|
|
|
def initialize_options(self):
|
2000-03-02 09:27:36 +08:00
|
|
|
self.build_clib = None
|
|
|
|
self.build_temp = None
|
|
|
|
|
2000-02-05 10:23:59 +08: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 10:20:14 +08:00
|
|
|
self.debug = None
|
2000-04-10 08:19:42 +08:00
|
|
|
self.force = 0
|
2000-05-12 08:33:14 +08:00
|
|
|
self.compiler = None
|
2000-02-05 10:23:59 +08:00
|
|
|
|
2000-03-02 09:27:36 +08:00
|
|
|
|
2007-08-30 11:52:21 +08:00
|
|
|
def finalize_options(self):
|
2000-03-02 09:27:36 +08: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-10-01 02:27:54 +08:00
|
|
|
self.set_undefined_options('build',
|
|
|
|
('build_temp', 'build_clib'),
|
|
|
|
('build_temp', 'build_temp'),
|
|
|
|
('compiler', 'compiler'),
|
|
|
|
('debug', 'debug'),
|
|
|
|
('force', 'force'))
|
2000-03-02 09:27:36 +08:00
|
|
|
|
2000-02-05 10:23:59 +08:00
|
|
|
self.libraries = self.distribution.libraries
|
2000-03-02 09:27:36 +08:00
|
|
|
if self.libraries:
|
2000-10-01 02:27:54 +08:00
|
|
|
self.check_library_list(self.libraries)
|
2000-03-02 09:27:36 +08:00
|
|
|
|
2000-02-05 10:23:59 +08:00
|
|
|
if self.include_dirs is None:
|
|
|
|
self.include_dirs = self.distribution.include_dirs or []
|
2007-10-17 02:12:55 +08:00
|
|
|
if isinstance(self.include_dirs, str):
|
2007-04-17 16:48:32 +08:00
|
|
|
self.include_dirs = self.include_dirs.split(os.pathsep)
|
2000-02-05 10:23:59 +08:00
|
|
|
|
|
|
|
# XXX same as for build_ext -- what about 'self.define' and
|
|
|
|
# 'self.undef' ?
|
|
|
|
|
|
|
|
|
2007-08-30 11:52:21 +08:00
|
|
|
def run(self):
|
2000-02-05 10:23:59 +08:00
|
|
|
if not self.libraries:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Yech -- this is cut 'n pasted from build_ext.py!
|
2000-06-24 09:23:37 +08:00
|
|
|
from distutils.ccompiler import new_compiler
|
2000-10-01 02:27:54 +08:00
|
|
|
self.compiler = new_compiler(compiler=self.compiler,
|
|
|
|
dry_run=self.dry_run,
|
|
|
|
force=self.force)
|
2000-06-25 10:10:58 +08:00
|
|
|
customize_compiler(self.compiler)
|
2000-05-12 08:33:14 +08:00
|
|
|
|
2000-02-05 10:23:59 +08:00
|
|
|
if self.include_dirs is not None:
|
2000-10-01 02:27:54 +08:00
|
|
|
self.compiler.set_include_dirs(self.include_dirs)
|
2000-02-05 10:23:59 +08:00
|
|
|
if self.define is not None:
|
|
|
|
# 'define' option is a list of (name,value) tuples
|
|
|
|
for (name,value) in self.define:
|
2000-10-01 02:27:54 +08:00
|
|
|
self.compiler.define_macro(name, value)
|
2000-02-05 10:23:59 +08:00
|
|
|
if self.undef is not None:
|
|
|
|
for macro in self.undef:
|
2000-10-01 02:27:54 +08:00
|
|
|
self.compiler.undefine_macro(macro)
|
2000-02-05 10:23:59 +08:00
|
|
|
|
2000-10-01 02:27:54 +08:00
|
|
|
self.build_libraries(self.libraries)
|
2000-02-05 10:23:59 +08:00
|
|
|
|
|
|
|
|
2007-08-30 11:52:21 +08:00
|
|
|
def check_library_list(self, libraries):
|
2009-05-06 15:26:24 +08:00
|
|
|
"""Ensure that the list of libraries is valid.
|
|
|
|
|
|
|
|
`library` is presumably provided as a command option 'libraries'.
|
|
|
|
This method checks that it is a list of 2-tuples, where the tuples
|
|
|
|
are (library_name, build_info_dict).
|
|
|
|
|
|
|
|
Raise DistutilsSetupError if the structure is invalid anywhere;
|
|
|
|
just returns otherwise.
|
|
|
|
"""
|
2007-08-30 11:52:21 +08:00
|
|
|
if not isinstance(libraries, list):
|
|
|
|
raise DistutilsSetupError(
|
|
|
|
"'libraries' option must be a list of tuples")
|
2000-02-05 10:23:59 +08:00
|
|
|
|
|
|
|
for lib in libraries:
|
2007-08-30 11:52:21 +08:00
|
|
|
if not isinstance(lib, tuple) and len(lib) != 2:
|
|
|
|
raise DistutilsSetupError(
|
|
|
|
"each element of 'libraries' must a 2-tuple")
|
2000-02-05 10:23:59 +08:00
|
|
|
|
2009-05-06 15:26:24 +08:00
|
|
|
name, build_info = lib
|
|
|
|
|
|
|
|
if not isinstance(name, str):
|
2007-08-30 11:52:21 +08:00
|
|
|
raise DistutilsSetupError(
|
|
|
|
"first element of each tuple in 'libraries' "
|
|
|
|
"must be a string (the library name)")
|
2009-05-06 15:26:24 +08:00
|
|
|
|
|
|
|
if '/' in name or (os.sep != '/' and os.sep in name):
|
2007-08-30 11:52:21 +08:00
|
|
|
raise DistutilsSetupError("bad library name '%s': "
|
|
|
|
"may not contain directory separators" % lib[0])
|
2000-02-05 10:23:59 +08:00
|
|
|
|
2009-05-06 15:26:24 +08:00
|
|
|
if not isinstance(build_info, dict):
|
2007-08-30 11:52:21 +08:00
|
|
|
raise DistutilsSetupError(
|
|
|
|
"second element of each tuple in 'libraries' "
|
|
|
|
"must be a dictionary (build info)")
|
2000-02-05 10:23:59 +08:00
|
|
|
|
|
|
|
|
2007-08-30 11:52:21 +08:00
|
|
|
def get_library_names(self):
|
2000-03-02 09:27:36 +08:00
|
|
|
# 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:
|
2000-10-01 02:27:54 +08:00
|
|
|
lib_names.append(lib_name)
|
2000-03-02 09:27:36 +08:00
|
|
|
return lib_names
|
|
|
|
|
|
|
|
|
2007-08-30 11:52:21 +08:00
|
|
|
def get_source_files(self):
|
2001-08-11 04:24:33 +08:00
|
|
|
self.check_library_list(self.libraries)
|
|
|
|
filenames = []
|
|
|
|
for (lib_name, build_info) in self.libraries:
|
|
|
|
sources = build_info.get('sources')
|
2007-08-30 11:52:21 +08:00
|
|
|
if sources is None or not isinstance(sources, (list, tuple)):
|
|
|
|
raise DistutilsSetupError(
|
|
|
|
"in 'libraries' option (library '%s'), "
|
2001-08-11 04:24:33 +08:00
|
|
|
"'sources' must be present and must be "
|
2007-08-30 11:52:21 +08:00
|
|
|
"a list of source filenames" % lib_name)
|
2001-12-07 05:01:19 +08:00
|
|
|
|
2001-08-11 04:24:33 +08:00
|
|
|
filenames.extend(sources)
|
|
|
|
return filenames
|
|
|
|
|
2000-02-05 10:23:59 +08:00
|
|
|
|
2007-08-30 11:52:21 +08:00
|
|
|
def build_libraries(self, libraries):
|
2000-02-05 10:23:59 +08:00
|
|
|
for (lib_name, build_info) in libraries:
|
2000-10-01 02:27:54 +08:00
|
|
|
sources = build_info.get('sources')
|
2007-08-30 11:52:21 +08:00
|
|
|
if sources is None or not isinstance(sources, (list, tuple)):
|
|
|
|
raise DistutilsSetupError(
|
|
|
|
"in 'libraries' option (library '%s'), "
|
|
|
|
"'sources' must be present and must be "
|
|
|
|
"a list of source filenames" % lib_name)
|
2000-10-01 02:27:54 +08:00
|
|
|
sources = list(sources)
|
2000-02-05 10:23:59 +08:00
|
|
|
|
2002-06-05 04:14:43 +08:00
|
|
|
log.info("building '%s' library", lib_name)
|
2000-02-05 10:23:59 +08:00
|
|
|
|
|
|
|
# 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.)
|
2000-10-01 02:27:54 +08:00
|
|
|
macros = build_info.get('macros')
|
|
|
|
include_dirs = build_info.get('include_dirs')
|
|
|
|
objects = self.compiler.compile(sources,
|
|
|
|
output_dir=self.build_temp,
|
|
|
|
macros=macros,
|
|
|
|
include_dirs=include_dirs,
|
|
|
|
debug=self.debug)
|
2000-02-05 10:23:59 +08: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-10-01 02:27:54 +08:00
|
|
|
self.compiler.create_static_lib(objects, lib_name,
|
|
|
|
output_dir=self.build_clib,
|
|
|
|
debug=self.debug)
|