1999-08-14 20:57:49 -03:00
|
|
|
"""distutils.command.build_ext
|
|
|
|
|
|
|
|
Implements the Distutils 'build_ext' command, for building extension
|
2000-06-28 11:48:01 -03:00
|
|
|
modules (currently limited to C extensions, should accommodate C++
|
1999-08-14 20:57:49 -03:00
|
|
|
extensions ASAP)."""
|
|
|
|
|
|
|
|
# created 1999/08/09, Greg Ward
|
|
|
|
|
2000-03-01 21:49:45 -04:00
|
|
|
__revision__ = "$Id$"
|
1999-08-14 20:57:49 -03:00
|
|
|
|
|
|
|
import sys, os, string, re
|
|
|
|
from types import *
|
|
|
|
from distutils.core import Command
|
|
|
|
from distutils.errors import *
|
2000-06-24 23:10:46 -03:00
|
|
|
from distutils.sysconfig import customize_compiler
|
2000-05-25 22:31:53 -03:00
|
|
|
from distutils.dep_util import newer_group
|
2000-05-30 22:09:52 -03:00
|
|
|
from distutils.extension import Extension
|
1999-08-14 20:57:49 -03:00
|
|
|
|
1999-09-21 15:27:12 -03:00
|
|
|
# An extension name is just a dot-separated list of Python NAMEs (ie.
|
|
|
|
# the same as a fully-qualified module name).
|
|
|
|
extension_name_re = re.compile \
|
|
|
|
(r'^[a-zA-Z_][a-zA-Z_0-9]*(\.[a-zA-Z_][a-zA-Z_0-9]*)*$')
|
1999-08-14 20:57:49 -03:00
|
|
|
|
|
|
|
|
2000-06-23 22:23:37 -03:00
|
|
|
def show_compilers ():
|
|
|
|
from distutils.ccompiler import show_compilers
|
|
|
|
show_compilers()
|
|
|
|
|
|
|
|
|
2000-02-17 20:13:53 -04:00
|
|
|
class build_ext (Command):
|
1999-08-14 20:57:49 -03:00
|
|
|
|
2000-01-30 14:34:15 -04:00
|
|
|
description = "build C/C++ extensions (compile/link to build directory)"
|
|
|
|
|
1999-08-14 20:57:49 -03:00
|
|
|
# XXX thoughts on how to deal with complex command-line options like
|
|
|
|
# these, i.e. how to make it so fancy_getopt can suck them off the
|
|
|
|
# command line and make it look like setup.py defined the appropriate
|
|
|
|
# lists of tuples of what-have-you.
|
|
|
|
# - each command needs a callback to process its command-line options
|
|
|
|
# - Command.__init__() needs access to its share of the whole
|
|
|
|
# command line (must ultimately come from
|
|
|
|
# Distribution.parse_command_line())
|
|
|
|
# - it then calls the current command class' option-parsing
|
|
|
|
# callback to deal with weird options like -D, which have to
|
|
|
|
# parse the option text and churn out some custom data
|
|
|
|
# structure
|
|
|
|
# - that data structure (in this case, a list of 2-tuples)
|
|
|
|
# will then be present in the command object by the time
|
2000-02-17 20:35:22 -04:00
|
|
|
# we get to finalize_options() (i.e. the constructor
|
1999-08-14 20:57:49 -03:00
|
|
|
# takes care of both command-line and client options
|
2000-02-17 20:35:22 -04:00
|
|
|
# in between initialize_options() and finalize_options())
|
1999-08-14 20:57:49 -03:00
|
|
|
|
2000-02-17 20:25:39 -04:00
|
|
|
user_options = [
|
2000-02-29 21:43:28 -04:00
|
|
|
('build-lib=', 'b',
|
2000-02-17 20:25:39 -04:00
|
|
|
"directory for compiled extension modules"),
|
2000-02-29 21:43:28 -04:00
|
|
|
('build-temp=', 't',
|
|
|
|
"directory for temporary files (build by-products)"),
|
|
|
|
('inplace', 'i',
|
|
|
|
"ignore build-lib and put compiled extensions into the source" +
|
|
|
|
"directory alongside your pure Python modules"),
|
2000-02-17 20:25:39 -04:00
|
|
|
('include-dirs=', 'I',
|
|
|
|
"list of directories to search for header files"),
|
|
|
|
('define=', 'D',
|
|
|
|
"C preprocessor macros to define"),
|
|
|
|
('undef=', 'U',
|
|
|
|
"C preprocessor macros to undefine"),
|
2000-03-26 17:45:14 -04:00
|
|
|
('libraries=', 'l',
|
2000-02-17 20:25:39 -04:00
|
|
|
"external C libraries to link with"),
|
|
|
|
('library-dirs=', 'L',
|
|
|
|
"directories to search for external C libraries"),
|
|
|
|
('rpath=', 'R',
|
|
|
|
"directories to search for shared C libraries at runtime"),
|
|
|
|
('link-objects=', 'O',
|
|
|
|
"extra explicit link objects to include in the link"),
|
|
|
|
('debug', 'g',
|
|
|
|
"compile/link with debugging information"),
|
2000-04-09 21:19:42 -03:00
|
|
|
('force', 'f',
|
2000-05-11 21:34:12 -03:00
|
|
|
"forcibly build everything (ignore file timestamps)"),
|
|
|
|
('compiler=', 'c',
|
|
|
|
"specify the compiler type"),
|
2000-06-26 22:37:10 -03:00
|
|
|
('swig-cpp', None,
|
|
|
|
"make SWIG create C++ files (default is C)"),
|
2000-02-17 20:25:39 -04:00
|
|
|
]
|
2000-06-23 22:23:37 -03:00
|
|
|
|
2000-06-07 00:00:06 -03:00
|
|
|
help_options = [
|
|
|
|
('help-compiler', None,
|
2000-06-23 22:23:37 -03:00
|
|
|
"list available compilers", show_compilers),
|
2000-06-07 00:00:06 -03:00
|
|
|
]
|
1999-08-14 20:57:49 -03:00
|
|
|
|
2000-02-17 20:35:22 -04:00
|
|
|
def initialize_options (self):
|
1999-09-07 23:42:30 -03:00
|
|
|
self.extensions = None
|
2000-02-29 21:43:28 -04:00
|
|
|
self.build_lib = None
|
|
|
|
self.build_temp = None
|
|
|
|
self.inplace = 0
|
1999-09-13 10:55:34 -03:00
|
|
|
self.package = None
|
|
|
|
|
1999-08-14 20:57:49 -03:00
|
|
|
self.include_dirs = None
|
|
|
|
self.define = None
|
|
|
|
self.undef = None
|
2000-03-26 17:45:14 -04:00
|
|
|
self.libraries = None
|
1999-08-14 20:57:49 -03:00
|
|
|
self.library_dirs = None
|
|
|
|
self.rpath = None
|
|
|
|
self.link_objects = None
|
2000-02-08 22:20:14 -04:00
|
|
|
self.debug = None
|
2000-04-09 21:19:42 -03:00
|
|
|
self.force = None
|
2000-05-11 21:34:12 -03:00
|
|
|
self.compiler = None
|
2000-06-26 22:37:10 -03:00
|
|
|
self.swig_cpp = None
|
1999-08-14 20:57:49 -03:00
|
|
|
|
1999-09-29 09:49:35 -03:00
|
|
|
|
2000-02-17 20:35:22 -04:00
|
|
|
def finalize_options (self):
|
2000-01-30 14:34:15 -04:00
|
|
|
from distutils import sysconfig
|
|
|
|
|
2000-02-08 22:20:14 -04:00
|
|
|
self.set_undefined_options ('build',
|
2000-02-29 21:43:28 -04:00
|
|
|
('build_lib', 'build_lib'),
|
|
|
|
('build_temp', 'build_temp'),
|
2000-05-11 21:34:12 -03:00
|
|
|
('compiler', 'compiler'),
|
2000-04-09 21:19:42 -03:00
|
|
|
('debug', 'debug'),
|
|
|
|
('force', 'force'))
|
1999-08-14 20:57:49 -03:00
|
|
|
|
1999-09-13 10:55:34 -03:00
|
|
|
if self.package is None:
|
1999-09-21 15:27:12 -03:00
|
|
|
self.package = self.distribution.ext_package
|
|
|
|
|
|
|
|
self.extensions = self.distribution.ext_modules
|
|
|
|
|
1999-09-13 10:55:34 -03:00
|
|
|
|
1999-08-14 20:57:49 -03:00
|
|
|
# Make sure Python's include directories (for Python.h, config.h,
|
2000-04-13 21:50:49 -03:00
|
|
|
# etc.) are in the include search path.
|
|
|
|
py_include = sysconfig.get_python_inc()
|
|
|
|
plat_py_include = sysconfig.get_python_inc(plat_specific=1)
|
1999-08-14 20:57:49 -03:00
|
|
|
if self.include_dirs is None:
|
1999-09-21 15:27:12 -03:00
|
|
|
self.include_dirs = self.distribution.include_dirs or []
|
1999-09-29 09:49:35 -03:00
|
|
|
if type (self.include_dirs) is StringType:
|
|
|
|
self.include_dirs = string.split (self.include_dirs,
|
|
|
|
os.pathsep)
|
|
|
|
|
2000-03-29 00:13:49 -04:00
|
|
|
# Put the Python "system" include dir at the end, so that
|
|
|
|
# any local include dirs take precedence.
|
|
|
|
self.include_dirs.append (py_include)
|
2000-04-13 21:50:49 -03:00
|
|
|
if plat_py_include != py_include:
|
|
|
|
self.include_dirs.append (plat_py_include)
|
1999-08-14 20:57:49 -03:00
|
|
|
|
2000-03-26 17:45:14 -04:00
|
|
|
if type (self.libraries) is StringType:
|
|
|
|
self.libraries = [self.libraries]
|
2000-03-18 11:21:03 -04:00
|
|
|
|
2000-03-30 23:50:23 -04:00
|
|
|
# Life is easier if we're not forever checking for None, so
|
|
|
|
# simplify these options to empty lists if unset
|
|
|
|
if self.libraries is None:
|
|
|
|
self.libraries = []
|
|
|
|
if self.library_dirs is None:
|
|
|
|
self.library_dirs = []
|
|
|
|
if self.rpath is None:
|
|
|
|
self.rpath = []
|
2000-02-04 22:23:16 -04:00
|
|
|
|
2000-03-30 23:50:23 -04:00
|
|
|
# for extensions under windows use different directories
|
|
|
|
# for Release and Debug builds.
|
|
|
|
# also Python's library directory must be appended to library_dirs
|
|
|
|
if os.name == 'nt':
|
|
|
|
self.library_dirs.append (os.path.join(sys.exec_prefix, 'libs'))
|
2000-06-26 22:43:24 -03:00
|
|
|
self.implib_dir = self.build_temp
|
2000-03-30 23:50:23 -04:00
|
|
|
if self.debug:
|
|
|
|
self.build_temp = os.path.join (self.build_temp, "Debug")
|
|
|
|
else:
|
|
|
|
self.build_temp = os.path.join (self.build_temp, "Release")
|
2000-02-17 20:35:22 -04:00
|
|
|
# finalize_options ()
|
2000-02-04 22:23:16 -04:00
|
|
|
|
1999-08-14 20:57:49 -03:00
|
|
|
|
|
|
|
def run (self):
|
|
|
|
|
2000-01-30 14:34:15 -04:00
|
|
|
from distutils.ccompiler import new_compiler
|
|
|
|
|
2000-05-30 22:09:52 -03:00
|
|
|
# 'self.extensions', as supplied by setup.py, is a list of
|
|
|
|
# Extension instances. See the documentation for Extension (in
|
2000-06-17 20:04:31 -03:00
|
|
|
# distutils.extension) for details.
|
2000-05-30 22:09:52 -03:00
|
|
|
#
|
|
|
|
# For backwards compatibility with Distutils 0.8.2 and earlier, we
|
|
|
|
# also allow the 'extensions' list to be a list of tuples:
|
1999-08-14 20:57:49 -03:00
|
|
|
# (ext_name, build_info)
|
2000-05-30 22:09:52 -03:00
|
|
|
# where build_info is a dictionary containing everything that
|
|
|
|
# Extension instances do except the name, with a few things being
|
|
|
|
# differently named. We convert these 2-tuples to Extension
|
|
|
|
# instances as needed.
|
1999-08-14 20:57:49 -03:00
|
|
|
|
1999-09-07 23:42:30 -03:00
|
|
|
if not self.extensions:
|
|
|
|
return
|
1999-08-14 20:57:49 -03:00
|
|
|
|
2000-03-26 17:45:14 -04:00
|
|
|
# If we were asked to build any C/C++ libraries, make sure that the
|
|
|
|
# directory where we put them is in the library search path for
|
|
|
|
# linking extensions.
|
2000-03-28 22:13:09 -04:00
|
|
|
if self.distribution.has_c_libraries():
|
2000-05-27 14:27:23 -03:00
|
|
|
build_clib = self.get_finalized_command ('build_clib')
|
2000-03-26 17:45:14 -04:00
|
|
|
self.libraries.extend (build_clib.get_library_names() or [])
|
|
|
|
self.library_dirs.append (build_clib.build_clib)
|
|
|
|
|
1999-08-14 20:57:49 -03:00
|
|
|
# Setup the CCompiler object that we'll use to do all the
|
|
|
|
# compiling and linking
|
2000-06-24 23:30:15 -03:00
|
|
|
self.compiler = new_compiler (compiler=self.compiler,
|
2000-05-11 21:34:12 -03:00
|
|
|
verbose=self.verbose,
|
1999-10-03 18:08:42 -03:00
|
|
|
dry_run=self.dry_run,
|
|
|
|
force=self.force)
|
2000-06-24 23:10:46 -03:00
|
|
|
customize_compiler(self.compiler)
|
2000-03-26 17:45:14 -04:00
|
|
|
|
|
|
|
# And make sure that any compile/link-related options (which might
|
|
|
|
# come from the command-line or from the setup script) are set in
|
|
|
|
# that CCompiler object -- that way, they automatically apply to
|
|
|
|
# all compiling and linking done here.
|
1999-08-14 20:57:49 -03: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)
|
2000-03-26 17:45:14 -04:00
|
|
|
if self.libraries is not None:
|
|
|
|
self.compiler.set_libraries (self.libraries)
|
1999-08-14 20:57:49 -03:00
|
|
|
if self.library_dirs is not None:
|
|
|
|
self.compiler.set_library_dirs (self.library_dirs)
|
|
|
|
if self.rpath is not None:
|
|
|
|
self.compiler.set_runtime_library_dirs (self.rpath)
|
|
|
|
if self.link_objects is not None:
|
|
|
|
self.compiler.set_link_objects (self.link_objects)
|
2000-03-01 21:32:21 -04:00
|
|
|
|
2000-03-26 17:45:14 -04:00
|
|
|
# Now actually compile and link everything.
|
2000-03-28 22:13:09 -04:00
|
|
|
self.build_extensions ()
|
|
|
|
|
|
|
|
# run ()
|
1999-08-14 20:57:49 -03:00
|
|
|
|
|
|
|
|
|
|
|
def check_extensions_list (self, extensions):
|
2000-02-04 22:23:16 -04:00
|
|
|
"""Ensure that the list of extensions (presumably provided as a
|
2000-05-30 22:09:52 -03:00
|
|
|
command option 'extensions') is valid, i.e. it is a list of
|
|
|
|
Extension objects. We also support the old-style list of 2-tuples,
|
|
|
|
where the tuples are (ext_name, build_info), which are converted to
|
|
|
|
Extension instances here.
|
|
|
|
|
|
|
|
Raise DistutilsSetupError if the structure is invalid anywhere;
|
|
|
|
just returns otherwise.
|
|
|
|
"""
|
|
|
|
if type(extensions) is not ListType:
|
2000-04-15 19:15:07 -03:00
|
|
|
raise DistutilsSetupError, \
|
2000-05-30 22:09:52 -03:00
|
|
|
"'ext_modules' option must be a list of Extension instances"
|
1999-08-14 20:57:49 -03:00
|
|
|
|
2000-05-30 22:09:52 -03:00
|
|
|
for i in range(len(extensions)):
|
|
|
|
ext = extensions[i]
|
|
|
|
if isinstance(ext, Extension):
|
|
|
|
continue # OK! (assume type-checking done
|
|
|
|
# by Extension constructor)
|
|
|
|
|
|
|
|
(ext_name, build_info) = ext
|
|
|
|
self.warn(("old-style (ext_name, build_info) tuple found in "
|
|
|
|
"ext_modules for extension '%s'"
|
|
|
|
"-- please convert to Extension instance" % ext_name))
|
|
|
|
if type(ext) is not TupleType and len(ext) != 2:
|
2000-04-15 19:15:07 -03:00
|
|
|
raise DistutilsSetupError, \
|
2000-05-30 22:09:52 -03:00
|
|
|
("each element of 'ext_modules' option must be an "
|
|
|
|
"Extension instance or 2-tuple")
|
1999-08-14 20:57:49 -03:00
|
|
|
|
2000-05-30 22:09:52 -03:00
|
|
|
if not (type(ext_name) is StringType and
|
|
|
|
extension_name_re.match(ext_name)):
|
2000-04-15 19:15:07 -03:00
|
|
|
raise DistutilsSetupError, \
|
2000-05-30 22:09:52 -03:00
|
|
|
("first element of each tuple in 'ext_modules' "
|
|
|
|
"must be the extension name (a string)")
|
1999-08-14 20:57:49 -03:00
|
|
|
|
2000-05-30 22:09:52 -03:00
|
|
|
if type(build_info) is not DictionaryType:
|
2000-04-15 19:15:07 -03:00
|
|
|
raise DistutilsSetupError, \
|
2000-05-30 22:09:52 -03:00
|
|
|
("second element of each tuple in 'ext_modules' "
|
|
|
|
"must be a dictionary (build info)")
|
|
|
|
|
|
|
|
# OK, the (ext_name, build_info) dict is type-safe: convert it
|
|
|
|
# to an Extension instance.
|
|
|
|
ext = Extension(ext_name, build_info['sources'])
|
|
|
|
|
|
|
|
# Easy stuff: one-to-one mapping from dict elements to
|
|
|
|
# instance attributes.
|
|
|
|
for key in ('include_dirs',
|
|
|
|
'library_dirs',
|
|
|
|
'libraries',
|
|
|
|
'extra_objects',
|
|
|
|
'extra_compile_args',
|
|
|
|
'extra_link_args'):
|
|
|
|
setattr(ext, key, build_info.get(key))
|
|
|
|
|
|
|
|
# Medium-easy stuff: same syntax/semantics, different names.
|
|
|
|
ext.runtime_library_dirs = build_info.get('rpath')
|
|
|
|
ext.export_symbol_file = build_info.get('def_file')
|
|
|
|
|
|
|
|
# Non-trivial stuff: 'macros' split into 'define_macros'
|
|
|
|
# and 'undef_macros'.
|
|
|
|
macros = build_info.get('macros')
|
|
|
|
if macros:
|
|
|
|
ext.define_macros = []
|
|
|
|
ext.undef_macros = []
|
|
|
|
for macro in macros:
|
|
|
|
if not (type(macro) is TupleType and
|
2000-07-26 22:21:54 -03:00
|
|
|
1 <= len(macro) <= 2):
|
2000-05-30 22:09:52 -03:00
|
|
|
raise DistutilsSetupError, \
|
|
|
|
("'macros' element of build info dict "
|
|
|
|
"must be 1- or 2-tuple")
|
|
|
|
if len(macro) == 1:
|
|
|
|
ext.undef_macros.append(macro[0])
|
|
|
|
elif len(macro) == 2:
|
|
|
|
ext.define_macros.append(macro)
|
|
|
|
|
|
|
|
extensions[i] = ext
|
|
|
|
|
|
|
|
# for extensions
|
1999-08-14 20:57:49 -03:00
|
|
|
|
|
|
|
# check_extensions_list ()
|
|
|
|
|
|
|
|
|
1999-09-29 09:49:35 -03:00
|
|
|
def get_source_files (self):
|
2000-07-14 10:35:07 -03:00
|
|
|
self.check_extensions_list(self.extensions)
|
1999-09-29 09:49:35 -03:00
|
|
|
filenames = []
|
|
|
|
|
|
|
|
# Wouldn't it be neat if we knew the names of header files too...
|
2000-05-30 22:09:52 -03:00
|
|
|
for ext in self.extensions:
|
|
|
|
filenames.extend (ext.sources)
|
1999-09-29 09:49:35 -03:00
|
|
|
|
|
|
|
return filenames
|
|
|
|
|
|
|
|
|
2000-03-28 22:13:09 -04:00
|
|
|
def get_outputs (self):
|
|
|
|
|
|
|
|
# Sanity check the 'extensions' list -- can't assume this is being
|
|
|
|
# done in the same run as a 'build_extensions()' call (in fact, we
|
|
|
|
# can probably assume that it *isn't*!).
|
|
|
|
self.check_extensions_list (self.extensions)
|
|
|
|
|
|
|
|
# And build the list of output (built) filenames. Note that this
|
|
|
|
# ignores the 'inplace' flag, and assumes everything goes in the
|
|
|
|
# "build" tree.
|
|
|
|
outputs = []
|
2000-05-30 22:09:52 -03:00
|
|
|
for ext in self.extensions:
|
|
|
|
fullname = self.get_ext_fullname (ext.name)
|
2000-03-28 22:13:09 -04:00
|
|
|
outputs.append (os.path.join (self.build_lib,
|
|
|
|
self.get_ext_filename(fullname)))
|
|
|
|
return outputs
|
|
|
|
|
|
|
|
# get_outputs ()
|
|
|
|
|
|
|
|
|
2000-03-30 15:47:22 -04:00
|
|
|
def build_extensions (self):
|
1999-08-14 20:57:49 -03:00
|
|
|
|
2000-03-28 22:13:09 -04:00
|
|
|
# First, sanity-check the 'extensions' list
|
|
|
|
self.check_extensions_list (self.extensions)
|
|
|
|
|
2000-05-30 22:09:52 -03:00
|
|
|
for ext in self.extensions:
|
|
|
|
sources = ext.sources
|
1999-12-12 13:01:01 -04:00
|
|
|
if sources is None or type (sources) not in (ListType, TupleType):
|
2000-04-15 19:15:07 -03:00
|
|
|
raise DistutilsSetupError, \
|
2000-02-04 22:23:16 -04:00
|
|
|
("in 'ext_modules' option (extension '%s'), " +
|
2000-02-03 19:07:54 -04:00
|
|
|
"'sources' must be present and must be " +
|
2000-05-30 22:09:52 -03:00
|
|
|
"a list of source filenames") % ext.name
|
1999-12-12 13:01:01 -04:00
|
|
|
sources = list (sources)
|
1999-09-29 09:49:35 -03:00
|
|
|
|
2000-05-30 22:09:52 -03:00
|
|
|
fullname = self.get_ext_fullname (ext.name)
|
2000-05-25 22:31:53 -03:00
|
|
|
if self.inplace:
|
|
|
|
# ignore build-lib -- put the compiled extension into
|
|
|
|
# the source tree along with pure Python modules
|
|
|
|
|
|
|
|
modpath = string.split (fullname, '.')
|
|
|
|
package = string.join (modpath[0:-1], '.')
|
|
|
|
base = modpath[-1]
|
|
|
|
|
2000-05-27 14:27:23 -03:00
|
|
|
build_py = self.get_finalized_command ('build_py')
|
2000-05-25 22:31:53 -03:00
|
|
|
package_dir = build_py.get_package_dir (package)
|
|
|
|
ext_filename = os.path.join (package_dir,
|
|
|
|
self.get_ext_filename(base))
|
|
|
|
else:
|
|
|
|
ext_filename = os.path.join (self.build_lib,
|
|
|
|
self.get_ext_filename(fullname))
|
|
|
|
|
2000-07-07 17:45:21 -03:00
|
|
|
if not (self.force or newer_group(sources, ext_filename, 'newer')):
|
|
|
|
self.announce ("skipping '%s' extension (up-to-date)" %
|
2000-05-30 22:09:52 -03:00
|
|
|
ext.name)
|
2000-07-07 17:45:21 -03:00
|
|
|
continue # 'for' loop over all extensions
|
|
|
|
else:
|
|
|
|
self.announce ("building '%s' extension" % ext.name)
|
2000-02-03 19:07:54 -04:00
|
|
|
|
2000-06-23 21:19:35 -03:00
|
|
|
# First, scan the sources for SWIG definition files (.i), run
|
|
|
|
# SWIG on 'em to create .c files, and modify the sources list
|
|
|
|
# accordingly.
|
|
|
|
sources = self.swig_sources(sources)
|
|
|
|
|
|
|
|
# Next, compile the source code to object files.
|
2000-05-30 22:09:52 -03:00
|
|
|
|
|
|
|
# XXX not honouring 'define_macros' or 'undef_macros' -- the
|
2000-06-28 11:48:01 -03:00
|
|
|
# CCompiler API needs to change to accommodate this, and I
|
2000-05-30 22:09:52 -03:00
|
|
|
# want to do one thing at a time!
|
|
|
|
|
|
|
|
# Two possible sources for extra compiler arguments:
|
|
|
|
# - 'extra_compile_args' in Extension object
|
|
|
|
# - CFLAGS environment variable (not particularly
|
|
|
|
# elegant, but people seem to expect it and I
|
|
|
|
# guess it's useful)
|
|
|
|
# The environment variable should take precedence, and
|
2000-07-26 23:13:20 -03:00
|
|
|
# any sensible compiler will give precedence to later
|
2000-05-30 22:09:52 -03:00
|
|
|
# command line args. Hence we combine them in order:
|
2000-06-24 23:30:15 -03:00
|
|
|
extra_args = ext.extra_compile_args or []
|
2000-05-30 22:09:52 -03:00
|
|
|
|
|
|
|
# XXX and if we support CFLAGS, why not CC (compiler
|
|
|
|
# executable), CPPFLAGS (pre-processor options), and LDFLAGS
|
|
|
|
# (linker options) too?
|
|
|
|
# XXX should we use shlex to properly parse CFLAGS?
|
|
|
|
|
2000-05-12 22:52:14 -03:00
|
|
|
if os.environ.has_key('CFLAGS'):
|
2000-05-30 22:09:52 -03:00
|
|
|
extra_args.extend(string.split(os.environ['CFLAGS']))
|
2000-05-12 22:52:14 -03:00
|
|
|
|
2000-02-29 21:43:28 -04:00
|
|
|
objects = self.compiler.compile (sources,
|
|
|
|
output_dir=self.build_temp,
|
2000-05-30 22:09:52 -03:00
|
|
|
#macros=macros,
|
|
|
|
include_dirs=ext.include_dirs,
|
2000-05-06 10:12:59 -03:00
|
|
|
debug=self.debug,
|
|
|
|
extra_postargs=extra_args)
|
1999-08-14 20:57:49 -03:00
|
|
|
|
1999-09-29 09:49:35 -03:00
|
|
|
# Now link the object files together into a "shared object" --
|
|
|
|
# of course, first we have to figure out all the other things
|
|
|
|
# that go into the mix.
|
2000-05-30 22:09:52 -03:00
|
|
|
if ext.extra_objects:
|
|
|
|
objects.extend (ext.extra_objects)
|
2000-06-24 23:30:15 -03:00
|
|
|
extra_args = ext.extra_link_args or []
|
2000-02-09 22:17:06 -04:00
|
|
|
|
2000-06-24 23:23:11 -03:00
|
|
|
# Bunch of fixing-up we have to do for Microsoft's linker.
|
|
|
|
if self.compiler.compiler_type == 'msvc':
|
|
|
|
self.msvc_prelink_hack(sources, ext, extra_args)
|
2000-02-29 21:43:28 -04:00
|
|
|
|
2000-05-30 22:09:52 -03:00
|
|
|
self.compiler.link_shared_object (
|
|
|
|
objects, ext_filename,
|
|
|
|
libraries=ext.libraries,
|
|
|
|
library_dirs=ext.library_dirs,
|
|
|
|
runtime_library_dirs=ext.runtime_library_dirs,
|
|
|
|
extra_postargs=extra_args,
|
2000-06-27 22:29:37 -03:00
|
|
|
debug=self.debug,
|
|
|
|
build_temp=self.build_temp)
|
1999-08-14 20:57:49 -03:00
|
|
|
|
|
|
|
# build_extensions ()
|
|
|
|
|
|
|
|
|
2000-06-23 21:19:35 -03:00
|
|
|
def swig_sources (self, sources):
|
|
|
|
|
|
|
|
"""Walk the list of source files in 'sources', looking for SWIG
|
|
|
|
interface (.i) files. Run SWIG on all that are found, and
|
|
|
|
return a modified 'sources' list with SWIG source files replaced
|
|
|
|
by the generated C (or C++) files.
|
|
|
|
"""
|
|
|
|
|
|
|
|
new_sources = []
|
|
|
|
swig_sources = []
|
|
|
|
swig_targets = {}
|
|
|
|
|
2000-06-26 22:37:10 -03:00
|
|
|
# XXX this drops generated C/C++ files into the source tree, which
|
2000-06-23 21:19:35 -03:00
|
|
|
# is fine for developers who want to distribute the generated
|
|
|
|
# source -- but there should be an option to put SWIG output in
|
|
|
|
# the temp dir.
|
|
|
|
|
2000-06-26 22:37:10 -03:00
|
|
|
if self.swig_cpp:
|
|
|
|
target_ext = '.cpp'
|
|
|
|
else:
|
|
|
|
target_ext = '.c'
|
|
|
|
|
2000-06-23 21:19:35 -03:00
|
|
|
for source in sources:
|
|
|
|
(base, ext) = os.path.splitext(source)
|
2000-06-24 23:10:46 -03:00
|
|
|
if ext == ".i": # SWIG interface file
|
2000-06-26 22:37:10 -03:00
|
|
|
new_sources.append(base + target_ext)
|
2000-06-24 23:10:46 -03:00
|
|
|
swig_sources.append(source)
|
2000-06-23 21:19:35 -03:00
|
|
|
swig_targets[source] = new_sources[-1]
|
|
|
|
else:
|
|
|
|
new_sources.append(source)
|
|
|
|
|
2000-06-24 23:10:46 -03:00
|
|
|
if not swig_sources:
|
2000-06-23 21:19:35 -03:00
|
|
|
return new_sources
|
|
|
|
|
|
|
|
swig = self.find_swig()
|
2000-06-26 22:37:10 -03:00
|
|
|
swig_cmd = [swig, "-python", "-dnone", "-ISWIG"]
|
|
|
|
if self.swig_cpp:
|
|
|
|
swig_cmd.append ("-c++")
|
2000-06-23 21:19:35 -03:00
|
|
|
|
|
|
|
for source in swig_sources:
|
2000-06-26 22:37:10 -03:00
|
|
|
target = swig_targets[source]
|
|
|
|
self.announce ("swigging %s to %s" % (source, target))
|
|
|
|
self.spawn(swig_cmd + ["-o", target, source])
|
2000-06-23 21:19:35 -03:00
|
|
|
|
|
|
|
return new_sources
|
|
|
|
|
|
|
|
# swig_sources ()
|
|
|
|
|
|
|
|
def find_swig (self):
|
|
|
|
"""Return the name of the SWIG executable. On Unix, this is
|
|
|
|
just "swig" -- it should be in the PATH. Tries a bit harder on
|
|
|
|
Windows.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if os.name == "posix":
|
|
|
|
return "swig"
|
|
|
|
elif os.name == "nt":
|
|
|
|
|
|
|
|
# Look for SWIG in its standard installation directory on
|
|
|
|
# Windows (or so I presume!). If we find it there, great;
|
|
|
|
# if not, act like Unix and assume it's in the PATH.
|
|
|
|
for vers in ("1.3", "1.2", "1.1"):
|
|
|
|
fn = os.path.join("c:\\swig%s" % vers, "swig.exe")
|
|
|
|
if os.path.isfile (fn):
|
|
|
|
return fn
|
|
|
|
else:
|
|
|
|
return "swig.exe"
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise DistutilsPlatformError, \
|
|
|
|
("I don't know how to find (much less run) SWIG "
|
|
|
|
"on platform '%s'") % os.name
|
|
|
|
|
|
|
|
# find_swig ()
|
|
|
|
|
|
|
|
|
2000-06-24 23:23:11 -03:00
|
|
|
# -- Hooks 'n hacks ------------------------------------------------
|
2000-06-17 20:04:31 -03:00
|
|
|
|
2000-06-24 23:23:11 -03:00
|
|
|
def msvc_prelink_hack (self, sources, ext, extra_args):
|
2000-06-17 20:04:31 -03:00
|
|
|
|
|
|
|
# XXX this is a kludge! Knowledge of specific compilers or
|
|
|
|
# platforms really doesn't belong here; in an ideal world, the
|
|
|
|
# CCompiler interface would provide access to everything in a
|
|
|
|
# compiler/linker system needs to build Python extensions, and
|
|
|
|
# we would just do everything nicely and cleanly through that
|
|
|
|
# interface. However, this is a not an ideal world and the
|
|
|
|
# CCompiler interface doesn't handle absolutely everything.
|
|
|
|
# Thus, kludges like this slip in occasionally. (This is no
|
|
|
|
# excuse for committing more platform- and compiler-specific
|
|
|
|
# kludges; they are to be avoided if possible!)
|
2000-06-24 23:23:11 -03:00
|
|
|
|
|
|
|
def_file = ext.export_symbol_file
|
|
|
|
|
|
|
|
if def_file is not None:
|
|
|
|
extra_args.append ('/DEF:' + def_file)
|
|
|
|
else:
|
|
|
|
modname = string.split (ext.name, '.')[-1]
|
|
|
|
extra_args.append('/export:init%s' % modname)
|
|
|
|
|
2000-06-26 22:37:10 -03:00
|
|
|
# The MSVC linker generates .lib and .exp files, which cannot be
|
|
|
|
# suppressed by any linker switches. The .lib files may even be
|
|
|
|
# needed! Make sure they are generated in the temporary build
|
|
|
|
# directory. Since they have different names for debug and release
|
|
|
|
# builds, they can go into the same directory.
|
2000-06-24 23:23:11 -03:00
|
|
|
implib_file = os.path.join (
|
2000-06-26 22:43:24 -03:00
|
|
|
self.implib_dir,
|
2000-06-24 23:23:11 -03:00
|
|
|
self.get_ext_libname (ext.name))
|
|
|
|
extra_args.append ('/IMPLIB:' + implib_file)
|
|
|
|
self.mkpath (os.path.dirname (implib_file))
|
|
|
|
|
|
|
|
# msvc_prelink_hack ()
|
2000-06-17 20:04:31 -03:00
|
|
|
|
|
|
|
|
|
|
|
# -- Name generators -----------------------------------------------
|
|
|
|
# (extension names, filenames, whatever)
|
|
|
|
|
2000-02-29 21:43:28 -04:00
|
|
|
def get_ext_fullname (self, ext_name):
|
|
|
|
if self.package is None:
|
|
|
|
return ext_name
|
|
|
|
else:
|
|
|
|
return self.package + '.' + ext_name
|
|
|
|
|
|
|
|
def get_ext_filename (self, ext_name):
|
2000-06-17 20:04:31 -03:00
|
|
|
"""Convert the name of an extension (eg. "foo.bar") into the name
|
|
|
|
of the file from which it will be loaded (eg. "foo/bar.so", or
|
|
|
|
"foo\bar.pyd").
|
|
|
|
"""
|
|
|
|
|
2000-01-30 14:34:15 -04:00
|
|
|
from distutils import sysconfig
|
1999-09-21 15:27:12 -03:00
|
|
|
ext_path = string.split (ext_name, '.')
|
2000-03-30 23:50:23 -04:00
|
|
|
# extensions in debug_mode are named 'module_d.pyd' under windows
|
|
|
|
if os.name == 'nt' and self.debug:
|
|
|
|
return apply (os.path.join, ext_path) + '_d' + sysconfig.SO
|
2000-01-30 14:34:15 -04:00
|
|
|
return apply (os.path.join, ext_path) + sysconfig.SO
|
1999-08-14 20:57:49 -03:00
|
|
|
|
2000-03-30 23:50:23 -04:00
|
|
|
def get_ext_libname (self, ext_name):
|
|
|
|
# create a filename for the (unneeded) lib-file.
|
|
|
|
# extensions in debug_mode are named 'module_d.pyd' under windows
|
|
|
|
ext_path = string.split (ext_name, '.')
|
|
|
|
if os.name == 'nt' and self.debug:
|
|
|
|
return apply (os.path.join, ext_path) + '_d.lib'
|
|
|
|
return apply (os.path.join, ext_path) + '.lib'
|
|
|
|
|
2000-05-24 22:10:04 -03:00
|
|
|
# class build_ext
|