mirror of
https://github.com/python/cpython.git
synced 2025-10-17 12:18:23 +00:00
Overhauled to expect 'self.extensions' (taken from 'ext_modules' in the
setup script) to be a list of Extension instances, rather than a list of of (ext_name, build_info) tuples. This is mostly a simplification, but 'check_extension_list()' got a lot more complicated because of the need to convert the old-style tuples to Extension instances. Temporarily dropped support for defining/undefining macros in the 'extensions' list -- I want to change the interface, but haven't yet made the required changes in CCompiler and friends to support this nicely. Also neatened up the code that merges 'extra_compile_flags' and the CFLAGS environment variable.
This commit is contained in:
parent
3ca54bc846
commit
5322f008c5
1 changed files with 127 additions and 65 deletions
|
@ -13,6 +13,7 @@ from types import *
|
||||||
from distutils.core import Command
|
from distutils.core import Command
|
||||||
from distutils.errors import *
|
from distutils.errors import *
|
||||||
from distutils.dep_util import newer_group
|
from distutils.dep_util import newer_group
|
||||||
|
from distutils.extension import Extension
|
||||||
|
|
||||||
# An extension name is just a dot-separated list of Python NAMEs (ie.
|
# An extension name is just a dot-separated list of Python NAMEs (ie.
|
||||||
# the same as a fully-qualified module name).
|
# the same as a fully-qualified module name).
|
||||||
|
@ -152,13 +153,17 @@ class build_ext (Command):
|
||||||
|
|
||||||
from distutils.ccompiler import new_compiler
|
from distutils.ccompiler import new_compiler
|
||||||
|
|
||||||
# 'self.extensions', as supplied by setup.py, is a list of 2-tuples.
|
# 'self.extensions', as supplied by setup.py, is a list of
|
||||||
# Each tuple is simple:
|
# Extension instances. See the documentation for Extension (in
|
||||||
|
# distutils.core) for details.
|
||||||
|
#
|
||||||
|
# For backwards compatibility with Distutils 0.8.2 and earlier, we
|
||||||
|
# also allow the 'extensions' list to be a list of tuples:
|
||||||
# (ext_name, build_info)
|
# (ext_name, build_info)
|
||||||
# build_info is a dictionary containing everything specific to
|
# where build_info is a dictionary containing everything that
|
||||||
# building this extension. (Info pertaining to all extensions
|
# Extension instances do except the name, with a few things being
|
||||||
# should be handled by general distutils options passed from
|
# differently named. We convert these 2-tuples to Extension
|
||||||
# setup.py down to right here, but that's not taken care of yet.)
|
# instances as needed.
|
||||||
|
|
||||||
if not self.extensions:
|
if not self.extensions:
|
||||||
return
|
return
|
||||||
|
@ -208,32 +213,82 @@ class build_ext (Command):
|
||||||
|
|
||||||
def check_extensions_list (self, extensions):
|
def check_extensions_list (self, extensions):
|
||||||
"""Ensure that the list of extensions (presumably provided as a
|
"""Ensure that the list of extensions (presumably provided as a
|
||||||
command option 'extensions') is valid, i.e. it is a list of
|
command option 'extensions') is valid, i.e. it is a list of
|
||||||
2-tuples, where the tuples are (extension_name, build_info_dict).
|
Extension objects. We also support the old-style list of 2-tuples,
|
||||||
Raise DistutilsSetupError if the structure is invalid anywhere;
|
where the tuples are (ext_name, build_info), which are converted to
|
||||||
just returns otherwise."""
|
Extension instances here.
|
||||||
|
|
||||||
if type (extensions) is not ListType:
|
Raise DistutilsSetupError if the structure is invalid anywhere;
|
||||||
|
just returns otherwise.
|
||||||
|
"""
|
||||||
|
if type(extensions) is not ListType:
|
||||||
raise DistutilsSetupError, \
|
raise DistutilsSetupError, \
|
||||||
"'ext_modules' option must be a list of tuples"
|
"'ext_modules' option must be a list of Extension instances"
|
||||||
|
|
||||||
for ext in extensions:
|
for i in range(len(extensions)):
|
||||||
if type (ext) is not TupleType and len (ext) != 2:
|
ext = extensions[i]
|
||||||
raise DistutilsSetupError, \
|
if isinstance(ext, Extension):
|
||||||
"each element of 'ext_modules' option must be a 2-tuple"
|
continue # OK! (assume type-checking done
|
||||||
|
# by Extension constructor)
|
||||||
|
|
||||||
if not (type (ext[0]) is StringType and
|
(ext_name, build_info) = ext
|
||||||
extension_name_re.match (ext[0])):
|
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:
|
||||||
raise DistutilsSetupError, \
|
raise DistutilsSetupError, \
|
||||||
"first element of each tuple in 'ext_modules' " + \
|
("each element of 'ext_modules' option must be an "
|
||||||
"must be the extension name (a string)"
|
"Extension instance or 2-tuple")
|
||||||
|
|
||||||
if type (ext[1]) is not DictionaryType:
|
if not (type(ext_name) is StringType and
|
||||||
|
extension_name_re.match(ext_name)):
|
||||||
raise DistutilsSetupError, \
|
raise DistutilsSetupError, \
|
||||||
"second element of each tuple in 'ext_modules' " + \
|
("first element of each tuple in 'ext_modules' "
|
||||||
"must be a dictionary (build info)"
|
"must be the extension name (a string)")
|
||||||
|
|
||||||
# end sanity-check for
|
if type(build_info) is not DictionaryType:
|
||||||
|
raise DistutilsSetupError, \
|
||||||
|
("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
|
||||||
|
1 <= len(macros) <= 2):
|
||||||
|
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
|
||||||
|
|
||||||
# check_extensions_list ()
|
# check_extensions_list ()
|
||||||
|
|
||||||
|
@ -243,10 +298,8 @@ class build_ext (Command):
|
||||||
filenames = []
|
filenames = []
|
||||||
|
|
||||||
# Wouldn't it be neat if we knew the names of header files too...
|
# Wouldn't it be neat if we knew the names of header files too...
|
||||||
for (extension_name, build_info) in self.extensions:
|
for ext in self.extensions:
|
||||||
sources = build_info.get ('sources')
|
filenames.extend (ext.sources)
|
||||||
if type (sources) in (ListType, TupleType):
|
|
||||||
filenames.extend (sources)
|
|
||||||
|
|
||||||
return filenames
|
return filenames
|
||||||
|
|
||||||
|
@ -262,8 +315,8 @@ class build_ext (Command):
|
||||||
# ignores the 'inplace' flag, and assumes everything goes in the
|
# ignores the 'inplace' flag, and assumes everything goes in the
|
||||||
# "build" tree.
|
# "build" tree.
|
||||||
outputs = []
|
outputs = []
|
||||||
for (extension_name, build_info) in self.extensions:
|
for ext in self.extensions:
|
||||||
fullname = self.get_ext_fullname (extension_name)
|
fullname = self.get_ext_fullname (ext.name)
|
||||||
outputs.append (os.path.join (self.build_lib,
|
outputs.append (os.path.join (self.build_lib,
|
||||||
self.get_ext_filename(fullname)))
|
self.get_ext_filename(fullname)))
|
||||||
return outputs
|
return outputs
|
||||||
|
@ -276,16 +329,16 @@ class build_ext (Command):
|
||||||
# First, sanity-check the 'extensions' list
|
# First, sanity-check the 'extensions' list
|
||||||
self.check_extensions_list (self.extensions)
|
self.check_extensions_list (self.extensions)
|
||||||
|
|
||||||
for (extension_name, build_info) in self.extensions:
|
for ext in self.extensions:
|
||||||
sources = build_info.get ('sources')
|
sources = ext.sources
|
||||||
if sources is None or type (sources) not in (ListType, TupleType):
|
if sources is None or type (sources) not in (ListType, TupleType):
|
||||||
raise DistutilsSetupError, \
|
raise DistutilsSetupError, \
|
||||||
("in 'ext_modules' option (extension '%s'), " +
|
("in 'ext_modules' option (extension '%s'), " +
|
||||||
"'sources' must be present and must be " +
|
"'sources' must be present and must be " +
|
||||||
"a list of source filenames") % extension_name
|
"a list of source filenames") % ext.name
|
||||||
sources = list (sources)
|
sources = list (sources)
|
||||||
|
|
||||||
fullname = self.get_ext_fullname (extension_name)
|
fullname = self.get_ext_fullname (ext.name)
|
||||||
if self.inplace:
|
if self.inplace:
|
||||||
# ignore build-lib -- put the compiled extension into
|
# ignore build-lib -- put the compiled extension into
|
||||||
# the source tree along with pure Python modules
|
# the source tree along with pure Python modules
|
||||||
|
@ -302,46 +355,54 @@ class build_ext (Command):
|
||||||
ext_filename = os.path.join (self.build_lib,
|
ext_filename = os.path.join (self.build_lib,
|
||||||
self.get_ext_filename(fullname))
|
self.get_ext_filename(fullname))
|
||||||
|
|
||||||
if not newer_group(sources, ext_filename, 'newer'):
|
if not (self.force or newer_group(sources, ext_filename, 'newer')):
|
||||||
self.announce ("skipping '%s' extension (up-to-date)" %
|
self.announce ("skipping '%s' extension (up-to-date)" %
|
||||||
extension_name)
|
ext.name)
|
||||||
continue # 'for' loop over all extensions
|
continue # 'for' loop over all extensions
|
||||||
else:
|
else:
|
||||||
self.announce ("building '%s' extension" % extension_name)
|
self.announce ("building '%s' extension" % ext.name)
|
||||||
|
|
||||||
# First step: compile the source code to object files. This
|
# First step: compile the source code to object files. This
|
||||||
# drops the object files in the current directory, regardless
|
# drops the object files in the current directory, regardless
|
||||||
# of where the source is (may be a bad thing, but that's how a
|
# of where the source is (may be a bad thing, but that's how a
|
||||||
# Makefile.pre.in-based system does it, so at least there's a
|
# Makefile.pre.in-based system does it, so at least there's a
|
||||||
# precedent!)
|
# precedent!)
|
||||||
macros = build_info.get ('macros')
|
|
||||||
include_dirs = build_info.get ('include_dirs')
|
# XXX not honouring 'define_macros' or 'undef_macros' -- the
|
||||||
extra_args = build_info.get ('extra_compile_args')
|
# CCompiler API needs to change to accomodate this, and I
|
||||||
# honor CFLAGS enviroment variable
|
# want to do one thing at a time!
|
||||||
# XXX do we *really* need this? or is it just a hack until
|
|
||||||
# the user can put compiler flags in setup.cfg?
|
# 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
|
||||||
|
# any sensible compiler will give precendence to later
|
||||||
|
# command line args. Hence we combine them in order:
|
||||||
|
extra_args = ext.extra_compile_args
|
||||||
|
|
||||||
|
# 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?
|
||||||
|
|
||||||
if os.environ.has_key('CFLAGS'):
|
if os.environ.has_key('CFLAGS'):
|
||||||
if not extra_args:
|
extra_args.extend(string.split(os.environ['CFLAGS']))
|
||||||
extra_args = []
|
|
||||||
extra_args = string.split(os.environ['CFLAGS']) + extra_args
|
|
||||||
|
|
||||||
objects = self.compiler.compile (sources,
|
objects = self.compiler.compile (sources,
|
||||||
output_dir=self.build_temp,
|
output_dir=self.build_temp,
|
||||||
macros=macros,
|
#macros=macros,
|
||||||
include_dirs=include_dirs,
|
include_dirs=ext.include_dirs,
|
||||||
debug=self.debug,
|
debug=self.debug,
|
||||||
extra_postargs=extra_args)
|
extra_postargs=extra_args)
|
||||||
|
|
||||||
# Now link the object files together into a "shared object" --
|
# Now link the object files together into a "shared object" --
|
||||||
# of course, first we have to figure out all the other things
|
# of course, first we have to figure out all the other things
|
||||||
# that go into the mix.
|
# that go into the mix.
|
||||||
extra_objects = build_info.get ('extra_objects')
|
if ext.extra_objects:
|
||||||
if extra_objects:
|
objects.extend (ext.extra_objects)
|
||||||
objects.extend (extra_objects)
|
extra_args = ext.extra_link_args
|
||||||
libraries = build_info.get ('libraries')
|
|
||||||
library_dirs = build_info.get ('library_dirs')
|
|
||||||
rpath = build_info.get ('rpath')
|
|
||||||
extra_args = build_info.get ('extra_link_args') or []
|
|
||||||
|
|
||||||
# XXX this is a kludge! Knowledge of specific compilers or
|
# XXX this is a kludge! Knowledge of specific compilers or
|
||||||
# platforms really doesn't belong here; in an ideal world, the
|
# platforms really doesn't belong here; in an ideal world, the
|
||||||
|
@ -354,10 +415,10 @@ class build_ext (Command):
|
||||||
# excuse for committing more platform- and compiler-specific
|
# excuse for committing more platform- and compiler-specific
|
||||||
# kludges; they are to be avoided if possible!)
|
# kludges; they are to be avoided if possible!)
|
||||||
if self.compiler.compiler_type == 'msvc':
|
if self.compiler.compiler_type == 'msvc':
|
||||||
def_file = build_info.get ('def_file')
|
def_file = ext.export_symbol_file
|
||||||
if def_file is None:
|
if def_file is None:
|
||||||
source_dir = os.path.dirname (sources[0])
|
source_dir = os.path.dirname (sources[0])
|
||||||
ext_base = (string.split (extension_name, '.'))[-1]
|
ext_base = (string.split (ext.name, '.'))[-1]
|
||||||
def_file = os.path.join (source_dir, "%s.def" % ext_base)
|
def_file = os.path.join (source_dir, "%s.def" % ext_base)
|
||||||
if not os.path.exists (def_file):
|
if not os.path.exists (def_file):
|
||||||
def_file = None
|
def_file = None
|
||||||
|
@ -365,7 +426,7 @@ class build_ext (Command):
|
||||||
if def_file is not None:
|
if def_file is not None:
|
||||||
extra_args.append ('/DEF:' + def_file)
|
extra_args.append ('/DEF:' + def_file)
|
||||||
else:
|
else:
|
||||||
modname = string.split (extension_name, '.')[-1]
|
modname = string.split (ext.name, '.')[-1]
|
||||||
extra_args.append('/export:init%s'%modname)
|
extra_args.append('/export:init%s'%modname)
|
||||||
|
|
||||||
# The MSVC linker generates unneeded .lib and .exp files,
|
# The MSVC linker generates unneeded .lib and .exp files,
|
||||||
|
@ -374,17 +435,18 @@ class build_ext (Command):
|
||||||
# directory.
|
# directory.
|
||||||
implib_file = os.path.join (
|
implib_file = os.path.join (
|
||||||
self.build_temp,
|
self.build_temp,
|
||||||
self.get_ext_libname (extension_name))
|
self.get_ext_libname (ext.name))
|
||||||
extra_args.append ('/IMPLIB:' + implib_file)
|
extra_args.append ('/IMPLIB:' + implib_file)
|
||||||
self.mkpath (os.path.dirname (implib_file))
|
self.mkpath (os.path.dirname (implib_file))
|
||||||
# if MSVC
|
# if MSVC
|
||||||
|
|
||||||
self.compiler.link_shared_object (objects, ext_filename,
|
self.compiler.link_shared_object (
|
||||||
libraries=libraries,
|
objects, ext_filename,
|
||||||
library_dirs=library_dirs,
|
libraries=ext.libraries,
|
||||||
runtime_library_dirs=rpath,
|
library_dirs=ext.library_dirs,
|
||||||
extra_postargs=extra_args,
|
runtime_library_dirs=ext.runtime_library_dirs,
|
||||||
debug=self.debug)
|
extra_postargs=extra_args,
|
||||||
|
debug=self.debug)
|
||||||
|
|
||||||
# build_extensions ()
|
# build_extensions ()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue