bpo-45800: Move pyexpat build setup into configure (GH-29547)

Settings for :mod:`pyexpat` C extension are now detected by ``configure``.
The bundled ``expat`` library is built in ``Makefile``.

Signed-off-by: Christian Heimes <christian@python.org>
This commit is contained in:
Christian Heimes 2021-11-14 11:02:24 +02:00 committed by GitHub
parent 25835c518a
commit 464e6616be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 176 additions and 57 deletions

138
setup.py
View file

@ -364,6 +364,57 @@ def find_module_file(module, dirlist):
return os.path.join(dirs[0], module)
def parse_cflags(flags):
"""Parse a string with compiler flags (-I, -D, -U, extra)
Distutils appends extra args to the compiler arguments. Some flags like
-I must appear earlier. Otherwise the pre-processor picks up files
from system inclue directories.
"""
include_dirs = []
define_macros = []
undef_macros = []
extra_compile_args = []
if flags is not None:
# shlex.split(None) reads from stdin
for token in shlex.split(flags):
switch = token[0:2]
value = token[2:]
if switch == '-I':
include_dirs.append(value)
elif switch == '-D':
key, _, val = value.partition("=")
if not val:
val = None
define_macros.append((key, val))
elif switch == '-U':
undef_macros.append(value)
else:
extra_compile_args.append(token)
return include_dirs, define_macros, undef_macros, extra_compile_args
def parse_ldflags(flags):
"""Parse a string with linker flags (-L, -l, extra)"""
library_dirs = []
libraries = []
extra_link_args = []
if flags is not None:
# shlex.split(None) reads from stdin
for token in shlex.split(flags):
switch = token[0:2]
value = token[2:]
if switch == '-L':
library_dirs.append(value)
elif switch == '-l':
libraries.append(value)
else:
extra_link_args.append(token)
return library_dirs, libraries, extra_link_args
class PyBuildExt(build_ext):
def __init__(self, dist):
@ -1469,59 +1520,39 @@ class PyBuildExt(build_ext):
#
# More information on Expat can be found at www.libexpat.org.
#
if '--with-system-expat' in sysconfig.get_config_var("CONFIG_ARGS"):
expat_inc = []
extra_compile_args = []
expat_lib = ['expat']
expat_sources = []
expat_depends = []
else:
expat_inc = [os.path.join(self.srcdir, 'Modules', 'expat')]
extra_compile_args = []
# bpo-44394: libexpat uses isnan() of math.h and needs linkage
# against the libm
expat_lib = ['m']
expat_sources = ['expat/xmlparse.c',
'expat/xmlrole.c',
'expat/xmltok.c']
expat_depends = ['expat/ascii.h',
'expat/asciitab.h',
'expat/expat.h',
'expat/expat_config.h',
'expat/expat_external.h',
'expat/internal.h',
'expat/latin1tab.h',
'expat/utf8tab.h',
'expat/xmlrole.h',
'expat/xmltok.h',
'expat/xmltok_impl.h'
]
cflags = parse_cflags(sysconfig.get_config_var("EXPAT_CFLAGS"))
include_dirs, define_macros, undef_macros, extra_compile_args = cflags
# ldflags includes either system libexpat or full path to
# our static libexpat.a.
ldflags = parse_ldflags(sysconfig.get_config_var("EXPAT_LDFLAGS"))
library_dirs, libraries, extra_link_args = ldflags
cc = sysconfig.get_config_var('CC').split()[0]
ret = run_command(
'"%s" -Werror -Wno-unreachable-code -E -xc /dev/null >/dev/null 2>&1' % cc)
if ret == 0:
extra_compile_args.append('-Wno-unreachable-code')
expat_depends = []
libexpat_a = sysconfig.get_config_var("LIBEXPAT_A")
if libexpat_a:
expat_depends.append(libexpat_a)
self.add(Extension('pyexpat',
include_dirs=include_dirs,
define_macros=define_macros,
undef_macros=undef_macros,
extra_compile_args=extra_compile_args,
include_dirs=expat_inc,
libraries=expat_lib,
sources=['pyexpat.c'] + expat_sources,
library_dirs=library_dirs,
libraries=libraries,
extra_link_args=extra_link_args,
sources=['pyexpat.c'],
depends=expat_depends))
# Fredrik Lundh's cElementTree module. Note that this also
# uses expat (via the CAPI hook in pyexpat).
if os.path.isfile(os.path.join(self.srcdir, 'Modules', '_elementtree.c')):
self.add(Extension('_elementtree',
include_dirs=expat_inc,
libraries=expat_lib,
sources=['_elementtree.c'],
depends=['pyexpat.c', *expat_sources,
*expat_depends]))
else:
self.missing.append('_elementtree')
self.add(Extension('_elementtree',
include_dirs=include_dirs,
define_macros=define_macros,
undef_macros=undef_macros,
extra_compile_args=extra_compile_args,
# no EXPAT_LDFLAGS
sources=['_elementtree.c'],
depends=['pyexpat.c', *expat_depends]))
def detect_multibytecodecs(self):
# Hye-Shik Chang's CJKCodecs modules.
@ -2019,15 +2050,14 @@ class PyBuildExt(build_ext):
# Stefan Krah's _decimal module
sources = ['_decimal/_decimal.c']
depends = ['_decimal/docstrings.h']
define_macros = []
cflags = sysconfig.get_config_var("DECIMAL_CFLAGS")
extra_compile_args = shlex.split(cflags) if cflags else None
cflags = parse_cflags(sysconfig.get_config_var("DECIMAL_CFLAGS"))
include_dirs, define_macros, undef_macros, extra_compile_args = cflags
# ldflags includes either system libmpdec or full path to
# our static libmpdec.a.
ldflags = sysconfig.get_config_var("DECIMAL_LDFLAGS")
extra_link_args = shlex.split(ldflags) if ldflags else None
ldflags = parse_ldflags(sysconfig.get_config_var("DECIMAL_LDFLAGS"))
library_dirs, libraries, extra_link_args = ldflags
libmpdec_a = sysconfig.get_config_var("LIBMPDEC_A")
if libmpdec_a:
depends.append(libmpdec_a)
@ -2035,8 +2065,12 @@ class PyBuildExt(build_ext):
# Uncomment for extra functionality:
#define_macros.append(('EXTRA_FUNCTIONALITY', 1))
self.add(Extension('_decimal',
include_dirs=include_dirs,
define_macros=define_macros,
undef_macros=undef_macros,
extra_compile_args=extra_compile_args,
library_dirs=library_dirs,
libraries=libraries,
extra_link_args=extra_link_args,
sources=sources,
depends=depends))