Skip to content

Commit aecbc2e

Browse files
gh-115382: Fix cross compiles when host and target use same SOABI
Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent aab3210 commit aecbc2e

File tree

6 files changed

+19
-3
lines changed

6 files changed

+19
-3
lines changed

Lib/sysconfig/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,20 @@ def _init_posix(vars):
340340
"""Initialize the module as appropriate for POSIX systems."""
341341
# _sysconfigdata is generated at build time, see _generate_posix_vars()
342342
name = _get_sysconfigdata_name()
343-
_temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)
343+
344+
# For cross builds, the path to the target's sysconfigdata must be specified
345+
# so it can be imported. It cannot be in PYTHONPATH, as foreign modules in
346+
# sys.path can cause crashes when loaded by the host interpreter.
347+
# Rely on truthiness as a valueless env variable is still an empty string.
348+
# See OS X note in _generate_posix_vars re _sysconfigdata.
349+
if (path := os.environ.get('_PYTHON_SYSCONFIGDATA_PATH')):
350+
from importlib.machinery import FileFinder, SourceFileLoader, SOURCE_SUFFIXES
351+
from importlib.util import module_from_spec
352+
spec = FileFinder(path, (SourceFileLoader, SOURCE_SUFFIXES)).find_spec(name)
353+
_temp = module_from_spec(spec)
354+
spec.loader.exec_module(_temp)
355+
else:
356+
_temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)
344357
build_time_vars = _temp.build_time_vars
345358
vars.update(build_time_vars)
346359

Lib/test/libregrtest/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ def _add_cross_compile_opts(self, regrtest_opts):
594594
'_PYTHON_PROJECT_BASE',
595595
'_PYTHON_HOST_PLATFORM',
596596
'_PYTHON_SYSCONFIGDATA_NAME',
597+
"_PYTHON_SYSCONFIGDATA_PATH",
597598
'PYTHONPATH'
598599
}
599600
old_environ = os.environ

Lib/test/pythoninfo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ def format_groups(groups):
334334
"_PYTHON_HOST_PLATFORM",
335335
"_PYTHON_PROJECT_BASE",
336336
"_PYTHON_SYSCONFIGDATA_NAME",
337+
"_PYTHON_SYSCONFIGDATA_PATH",
337338
"__PYVENV_LAUNCHER__",
338339

339340
# Sanitizer options
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix cross compile failures when the host and target SOABIs match.

configure

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ AC_ARG_WITH([build-python],
164164
dnl Build Python interpreter is used for regeneration and freezing.
165165
ac_cv_prog_PYTHON_FOR_REGEN=$with_build_python
166166
PYTHON_FOR_FREEZE="$with_build_python"
167-
PYTHON_FOR_BUILD='_PYTHON_PROJECT_BASE=$(abs_builddir) _PYTHON_HOST_PLATFORM=$(_PYTHON_HOST_PLATFORM) PYTHONPATH=$(shell test -f pybuilddir.txt && echo $(abs_builddir)/`cat pybuilddir.txt`:)$(srcdir)/Lib _PYTHON_SYSCONFIGDATA_NAME=_sysconfigdata_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH) '$with_build_python
167+
PYTHON_FOR_BUILD='_PYTHON_PROJECT_BASE=$(abs_builddir) _PYTHON_HOST_PLATFORM=$(_PYTHON_HOST_PLATFORM) PYTHONPATH=$(srcdir)/Lib _PYTHON_SYSCONFIGDATA_NAME=_sysconfigdata_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH) _PYTHON_SYSCONFIGDATA_PATH=$(shell test -f pybuilddir.txt && echo $(abs_builddir)/`cat pybuilddir.txt`) '$with_build_python
168168
AC_MSG_RESULT([$with_build_python])
169169
], [
170170
AS_VAR_IF([cross_compiling], [yes],

0 commit comments

Comments
 (0)