From 308df892280c9e3d31f9b3866ee5b0891652fb4d Mon Sep 17 00:00:00 2001 From: Ahmed Semih Ozmekik Date: Wed, 4 Nov 2026 18:34:42 +0300 Subject: [PATCH] gh-160924: Don't run the build Python with RUNSHARED when cross-compiling In a cross build the build Python is the --with-build-python interpreter. Running it with RUNSHARED puts the target's build directory on the library path, so a shared build Python of the same version loads the target's libpython, which fails when the target uses another libc. Add RUNSHARED_FOR_BUILD, empty when cross-compiling, and use it for $(PYTHON_FOR_BUILD). RUNSHARED itself is unchanged, so the PGO profile run (gh-141926) and TESTPYTHON still get it. --- Makefile.pre.in | 19 ++++++++++--------- ...-11-04-15-00-00.gh-issue-160924.WQgVVh.rst | 4 ++++ configure | 6 ++++++ configure.ac | 5 +++++ 4 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 Misc/NEWS.d/next/Build/2026-11-04-15-00-00.gh-issue-160924.WQgVVh.rst diff --git a/Makefile.pre.in b/Makefile.pre.in index e74deee353488f..b02419e1785c14 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -242,6 +242,7 @@ LIPO_INTEL64_FLAGS=@LIPO_INTEL64_FLAGS@ # Environment to run shared python without installed libraries RUNSHARED= @RUNSHARED@ +RUNSHARED_FOR_BUILD= @RUNSHARED_FOR_BUILD@ # ensurepip options ENSUREPIP= @ENSUREPIP@ @@ -1013,7 +1014,7 @@ $(BUILDPYTHON): Programs/python.o $(PLATFORM_MAIN_OBJS) $(LINK_PYTHON_DEPS) $(LINKCC) $(PY_CORE_EXE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/python.o $(PLATFORM_MAIN_OBJS) $(LINK_PYTHON_OBJS) $(LIBS) $(MODLIBS) $(SYSLIBS) platform: $(PYTHON_FOR_BUILD_DEPS) pybuilddir.txt - $(RUNSHARED) $(PYTHON_FOR_BUILD) -c 'import sys ; from sysconfig import get_platform ; print("%s-%d.%d" % (get_platform(), *sys.version_info[:2]))' >platform + $(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) -c 'import sys ; from sysconfig import get_platform ; print("%s-%d.%d" % (get_platform(), *sys.version_info[:2]))' >platform # Create build directory and generate the sysconfig build-time data there. # pybuilddir.txt contains the name of the build dir and is used for @@ -1024,7 +1025,7 @@ platform: $(PYTHON_FOR_BUILD_DEPS) pybuilddir.txt # or removed in case of failure. pybuilddir.txt: $(PYTHON_FOR_BUILD_DEPS) @echo "none" > ./pybuilddir.txt - $(RUNSHARED) $(PYTHON_FOR_BUILD) -S -X pathconfig_warnings=0 -m sysconfig --generate-posix-vars ;\ + $(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) -S -X pathconfig_warnings=0 -m sysconfig --generate-posix-vars ;\ if test $$? -ne 0 ; then \ echo "generate-posix-vars failed" ; \ rm -f ./pybuilddir.txt ; \ @@ -1032,7 +1033,7 @@ pybuilddir.txt: $(PYTHON_FOR_BUILD_DEPS) fi $(BUILD_DETAILS): pybuilddir.txt - $(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/generate-build-details.py `cat pybuilddir.txt`/$(BUILD_DETAILS) + $(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/generate-build-details.py `cat pybuilddir.txt`/$(BUILD_DETAILS) # Build static library $(LIBRARY): $(LIBRARY_OBJS) @@ -1604,11 +1605,11 @@ sharedmods: $(SHAREDMODS) pybuilddir.txt .PHONY: checksharedmods checksharedmods: sharedmods $(PYTHON_FOR_BUILD_DEPS) $(BUILDPYTHON) @if [ -n "@MISSING_STDLIB_CONFIG@" ]; then \ - $(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/check_extension_modules.py --generate-missing-stdlib-info --with-missing-stdlib-config="@MISSING_STDLIB_CONFIG@"; \ + $(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/check_extension_modules.py --generate-missing-stdlib-info --with-missing-stdlib-config="@MISSING_STDLIB_CONFIG@"; \ else \ - $(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/check_extension_modules.py --generate-missing-stdlib-info; \ + $(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/check_extension_modules.py --generate-missing-stdlib-info; \ fi - @$(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/check_extension_modules.py + @$(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/check_extension_modules.py .PHONY: rundsymutil rundsymutil: sharedmods $(PYTHON_FOR_BUILD_DEPS) $(BUILDPYTHON) @@ -2436,7 +2437,7 @@ install: @FRAMEWORKINSTALLFIRST@ @INSTALLTARGETS@ @FRAMEWORKINSTALLLAST@ upgrade) ensurepip="--upgrade" ;; \ install|*) ensurepip="" ;; \ esac; \ - $(RUNSHARED) $(PYTHON_FOR_BUILD) -m ensurepip \ + $(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) -m ensurepip \ $$ensurepip --root=$(DESTDIR)/ ; \ fi @@ -2447,7 +2448,7 @@ altinstall: commoninstall upgrade) ensurepip="--altinstall --upgrade" ;; \ install|*) ensurepip="--altinstall" ;; \ esac; \ - $(RUNSHARED) $(PYTHON_FOR_BUILD) -m ensurepip \ + $(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) -m ensurepip \ $$ensurepip --root=$(DESTDIR)/ ; \ fi @@ -3138,7 +3139,7 @@ frameworkinstallunversionedstructure: $(LDLIBRARY) rm -rf $(DESTDIR)$(PYTHONFRAMEWORKPREFIX)/include; \ fi $(INSTALL) -d -m $(DIRMODE) $(DESTDIR)$(PYTHONFRAMEWORKINSTALLDIR) - sed 's/%VERSION%/'"`$(RUNSHARED) $(PYTHON_FOR_BUILD) -c 'import platform; print(platform.python_version())'`"'/g' < $(RESSRCDIR)/Info.plist > $(DESTDIR)$(PYTHONFRAMEWORKINSTALLDIR)/Info.plist + sed 's/%VERSION%/'"`$(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) -c 'import platform; print(platform.python_version())'`"'/g' < $(RESSRCDIR)/Info.plist > $(DESTDIR)$(PYTHONFRAMEWORKINSTALLDIR)/Info.plist $(INSTALL_SHARED) $(LDLIBRARY) $(DESTDIR)$(PYTHONFRAMEWORKPREFIX)/$(LDLIBRARY) $(INSTALL) -d -m $(DIRMODE) $(DESTDIR)$(LIBDIR) $(LN) -fs "../$(LDLIBRARY)" "$(DESTDIR)$(prefix)/lib/libpython$(LDVERSION).dylib" diff --git a/Misc/NEWS.d/next/Build/2026-11-04-15-00-00.gh-issue-160924.WQgVVh.rst b/Misc/NEWS.d/next/Build/2026-11-04-15-00-00.gh-issue-160924.WQgVVh.rst new file mode 100644 index 00000000000000..22e9edae675c46 --- /dev/null +++ b/Misc/NEWS.d/next/Build/2026-11-04-15-00-00.gh-issue-160924.WQgVVh.rst @@ -0,0 +1,4 @@ +Fix cross builds with ``--enable-shared`` when the target uses a different C +library than the build machine, such as musl from glibc. The build Python is +no longer run with ``RUNSHARED``, which could make it load the target's +``libpython``. diff --git a/configure b/configure index f85f487e0249d6..6153efbca6895a 100755 --- a/configure +++ b/configure @@ -1035,6 +1035,7 @@ CONFIG_ARGS SOVERSION VERSION PYTHON_FOR_REGEN +RUNSHARED_FOR_BUILD PYTHON_FOR_BUILD_DEPS FREEZE_MODULE_DEPS FREEZE_MODULE @@ -3863,6 +3864,9 @@ then : FREEZE_MODULE='$(FREEZE_MODULE_BOOTSTRAP)' FREEZE_MODULE_DEPS='$(FREEZE_MODULE_BOOTSTRAP_DEPS)' PYTHON_FOR_BUILD_DEPS='' + # The build Python is a separate interpreter. RUNSHARED would make it + # load the in-tree libpython, which may need a different libc. + RUNSHARED_FOR_BUILD='' else case e in #( e) @@ -3871,6 +3875,7 @@ else case e in #( FREEZE_MODULE='$(PYTHON_FOR_FREEZE) $(srcdir)/Programs/_freeze_module.py' FREEZE_MODULE_DEPS="_bootstrap_python \$(srcdir)/Programs/_freeze_module.py" PYTHON_FOR_BUILD_DEPS='$(BUILDPYTHON)' + RUNSHARED_FOR_BUILD='$(RUNSHARED)' ;; esac @@ -3881,6 +3886,7 @@ fi + for ac_prog in python$PACKAGE_VERSION python3.16 python3.15 python3.14 python3.13 python3.12 python3.11 python3.10 python3 python do # Extract the first word of "$ac_prog", so it can be a program name with args. diff --git a/configure.ac b/configure.ac index 4b8e6d59a5cf46..a7d9f920f9f37b 100644 --- a/configure.ac +++ b/configure.ac @@ -194,6 +194,9 @@ AS_VAR_IF([cross_compiling], [yes], FREEZE_MODULE='$(FREEZE_MODULE_BOOTSTRAP)' FREEZE_MODULE_DEPS='$(FREEZE_MODULE_BOOTSTRAP_DEPS)' PYTHON_FOR_BUILD_DEPS='' + # The build Python is a separate interpreter. RUNSHARED would make it + # load the in-tree libpython, which may need a different libc. + RUNSHARED_FOR_BUILD='' ], [ dnl internal build tools also depend on Programs/_freeze_module and _bootstrap_python. @@ -202,6 +205,7 @@ AS_VAR_IF([cross_compiling], [yes], FREEZE_MODULE='$(PYTHON_FOR_FREEZE) $(srcdir)/Programs/_freeze_module.py' FREEZE_MODULE_DEPS="_bootstrap_python \$(srcdir)/Programs/_freeze_module.py" PYTHON_FOR_BUILD_DEPS='$(BUILDPYTHON)' + RUNSHARED_FOR_BUILD='$(RUNSHARED)' ] ) AC_SUBST([FREEZE_MODULE_BOOTSTRAP]) @@ -209,6 +213,7 @@ AC_SUBST([FREEZE_MODULE_BOOTSTRAP_DEPS]) AC_SUBST([FREEZE_MODULE]) AC_SUBST([FREEZE_MODULE_DEPS]) AC_SUBST([PYTHON_FOR_BUILD_DEPS]) +AC_SUBST([RUNSHARED_FOR_BUILD]) AC_CHECK_PROGS([PYTHON_FOR_REGEN], [python$PACKAGE_VERSION python3.16 python3.15 python3.14 python3.13 python3.12 python3.11 python3.10 python3 python],