Skip to content

Commit f0150ac

Browse files
authored
bpo-45548: Some test modules must be built as shared libs (GH-29268)
Some test cases don't work when test modules are static extensions. Add dependency on Modules/config.c to trigger a rebuild whenever a module build type is changed. ``makesetup`` puts shared extensions into ``Modules/`` directory. Create symlinks from pybuilddir so the extensions can be imported. Note: It is not possible to use the content of pybuilddir.txt as a build target. Makefile evaluates target variables in the first pass. The pybuilddir.txt file does not exist at that point.
1 parent d957521 commit f0150ac

File tree

6 files changed

+41
-13
lines changed

6 files changed

+41
-13
lines changed

Doc/whatsnew/3.11.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,13 @@ Build Changes
507507
except empty tuple singleton.
508508
(Contributed by Christian Heimes in :issue:`45522`)
509509

510+
* ``Modules/Setup`` and ``Modules/makesetup`` have been improved and tied up.
511+
Extension modules can now be built through ``makesetup``. All except some
512+
test modules can be linked statically into main binary or library.
513+
(Contributed by Brett Cannon and Christian Heimes in :issue:`45548`,
514+
:issue:`45570`, :issue:`45571`, and :issue:`43974`.)
515+
516+
510517
C API Changes
511518
=============
512519

Makefile.pre.in

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,8 +705,17 @@ $(DLLLIBRARY) libpython$(LDVERSION).dll.a: $(LIBRARY_OBJS)
705705
fi
706706

707707

708-
oldsharedmods: $(SHAREDMODS)
709-
708+
# create relative links from build/lib.platform/egg.so to Modules/egg.so
709+
# pybuilddir.txt is created too late. We cannot use it in Makefile
710+
# targets. ln --relative is not portable.
711+
oldsharedmods: $(SHAREDMODS) pybuilddir.txt
712+
@target=`cat pybuilddir.txt`; \
713+
$(MKDIR_P) $$target; \
714+
for mod in X $(SHAREDMODS); do \
715+
if test $$mod != X; then \
716+
$(LN) -sf ../../$$mod $$target/`basename $$mod`; \
717+
fi; \
718+
done
710719

711720
Makefile Modules/config.c: Makefile.pre \
712721
$(srcdir)/Modules/config.c.in \
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``Modules/Setup`` and ``Modules/makesetup`` have been improved. The
2+
``Setup`` file now contains working rules for all extensions. Outdated
3+
comments have been removed. Rules defined by ``makesetup`` track
4+
dependencies correctly.

Modules/Setup

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,16 @@ xxsubtype xxsubtype.c # Required for the test suite to pass!
297297

298298
#_xxsubinterpreters _xxsubinterpretersmodule.c
299299
#_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c
300-
#_ctypes_test _ctypes/_ctypes_test.c
301300
#_testbuffer _testbuffer.c
302-
#_testimportmultiple _testimportmultiple.c
303301
#_testinternalcapi _testinternalcapi.c
304-
#_testmultiphase _testmultiphase.c
302+
303+
# Some testing modules MUST be built as shared libraries.
305304

306305
#*shared*
307-
#_testcapi _testcapimodule.c # CANNOT be statically compiled!
306+
#_ctypes_test _ctypes/_ctypes_test.c
307+
#_testcapi _testcapimodule.c
308+
#_testimportmultiple _testimportmultiple.c
309+
#_testmultiphase _testmultiphase.c
308310

309311
# ---
310312
# Uncommenting the following line tells makesetup that all following modules

Modules/makesetup

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' |
241241
cc="$cc \$(PY_BUILTIN_MODULE_CFLAGS)";;
242242
esac
243243
mods_upper=$(echo $mods | tr '[a-z]' '[A-Z]')
244-
rule="$obj: $src \$(MODULE_${mods_upper}_DEPS) \$(PYTHON_HEADERS); $cc $cpps -c $src -o $obj"
244+
# force rebuild when header file or module build flavor (static/shared) is changed
245+
rule="$obj: $src \$(MODULE_${mods_upper}_DEPS) \$(PYTHON_HEADERS) Modules/config.c; $cc $cpps -c $src -o $obj"
245246
echo "$rule" >>$rulesf
246247
done
247248
case $doconfig in

setup.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,13 @@ def update_sources_depends(self):
426426
# re-compile extensions if a header file has been changed
427427
ext.depends.extend(headers)
428428

429-
def remove_configured_extensions(self):
429+
def handle_configured_extensions(self):
430430
# The sysconfig variables built by makesetup that list the already
431431
# built modules and the disabled modules as configured by the Setup
432432
# files.
433-
sysconf_built = sysconfig.get_config_var('MODBUILT_NAMES').split()
434-
sysconf_dis = sysconfig.get_config_var('MODDISABLED_NAMES').split()
433+
sysconf_built = set(sysconfig.get_config_var('MODBUILT_NAMES').split())
434+
sysconf_shared = set(sysconfig.get_config_var('MODSHARED_NAMES').split())
435+
sysconf_dis = set(sysconfig.get_config_var('MODDISABLED_NAMES').split())
435436

436437
mods_built = []
437438
mods_disabled = []
@@ -449,11 +450,15 @@ def remove_configured_extensions(self):
449450
mods_configured]
450451
# Remove the shared libraries built by a previous build.
451452
for ext in mods_configured:
453+
# Don't remove shared extensions which have been built
454+
# by Modules/Setup
455+
if ext.name in sysconf_shared:
456+
continue
452457
fullpath = self.get_ext_fullpath(ext.name)
453-
if os.path.exists(fullpath):
458+
if os.path.lexists(fullpath):
454459
os.unlink(fullpath)
455460

456-
return (mods_built, mods_disabled)
461+
return mods_built, mods_disabled
457462

458463
def set_compiler_executables(self):
459464
# When you run "make CC=altcc" or something similar, you really want
@@ -478,7 +483,7 @@ def build_extensions(self):
478483
self.remove_disabled()
479484

480485
self.update_sources_depends()
481-
mods_built, mods_disabled = self.remove_configured_extensions()
486+
mods_built, mods_disabled = self.handle_configured_extensions()
482487
self.set_compiler_executables()
483488

484489
if LIST_MODULE_NAMES:

0 commit comments

Comments
 (0)