Skip to content

Commit fd6ac9b

Browse files
FFY00hugovk
authored andcommitted
[3.13] pythonGH-127429: fix sysconfig data generation on cross-builds (pythonGH-127430)
(cherry picked from commit 2950bc5) Co-authored-by: Filipe Laíns 🇵🇸 <lains@riseup.net>
1 parent e285232 commit fd6ac9b

File tree

5 files changed

+49
-19
lines changed

5 files changed

+49
-19
lines changed

Lib/sysconfig/__init__.py

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ def joinuser(*args):
173173
_PY_VERSION = sys.version.split()[0]
174174
_PY_VERSION_SHORT = f'{sys.version_info[0]}.{sys.version_info[1]}'
175175
_PY_VERSION_SHORT_NO_DOT = f'{sys.version_info[0]}{sys.version_info[1]}'
176+
_PREFIX = os.path.normpath(sys.prefix)
176177
_BASE_PREFIX = os.path.normpath(sys.base_prefix)
178+
_EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
177179
_BASE_EXEC_PREFIX = os.path.normpath(sys.base_exec_prefix)
178180
# Mutex guarding initialization of _CONFIG_VARS.
179181
_CONFIG_VARS_LOCK = threading.RLock()
@@ -323,14 +325,22 @@ def get_default_scheme():
323325

324326
def get_makefile_filename():
325327
"""Return the path of the Makefile."""
328+
329+
# GH-127429: When cross-compiling, use the Makefile from the target, instead of the host Python.
330+
if cross_base := os.environ.get('_PYTHON_PROJECT_BASE'):
331+
return os.path.join(cross_base, 'Makefile')
332+
326333
if _PYTHON_BUILD:
327334
return os.path.join(_PROJECT_BASE, "Makefile")
335+
328336
if hasattr(sys, 'abiflags'):
329337
config_dir_name = f'config-{_PY_VERSION_SHORT}{sys.abiflags}'
330338
else:
331339
config_dir_name = 'config'
340+
332341
if hasattr(sys.implementation, '_multiarch'):
333342
config_dir_name += f'-{sys.implementation._multiarch}'
343+
334344
return os.path.join(get_path('stdlib'), config_dir_name, 'Makefile')
335345

336346

@@ -468,29 +478,47 @@ def get_path(name, scheme=get_default_scheme(), vars=None, expand=True):
468478
def _init_config_vars():
469479
global _CONFIG_VARS
470480
_CONFIG_VARS = {}
481+
482+
prefix = _PREFIX
483+
exec_prefix = _EXEC_PREFIX
484+
base_prefix = _BASE_PREFIX
485+
base_exec_prefix = _BASE_EXEC_PREFIX
486+
487+
try:
488+
abiflags = sys.abiflags
489+
except AttributeError:
490+
abiflags = ''
491+
492+
if os.name == 'posix':
493+
_init_posix(_CONFIG_VARS)
494+
# If we are cross-compiling, load the prefixes from the Makefile instead.
495+
if '_PYTHON_PROJECT_BASE' in os.environ:
496+
prefix = _CONFIG_VARS['prefix']
497+
exec_prefix = _CONFIG_VARS['exec_prefix']
498+
base_prefix = _CONFIG_VARS['prefix']
499+
base_exec_prefix = _CONFIG_VARS['exec_prefix']
500+
abiflags = _CONFIG_VARS['ABIFLAGS']
501+
471502
# Normalized versions of prefix and exec_prefix are handy to have;
472503
# in fact, these are the standard versions used most places in the
473504
# Distutils.
505+
474506
_PREFIX = os.path.normpath(sys.prefix)
475507
_EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
476-
_CONFIG_VARS['prefix'] = _PREFIX # FIXME: This gets overwriten by _init_posix.
477-
_CONFIG_VARS['exec_prefix'] = _EXEC_PREFIX # FIXME: This gets overwriten by _init_posix.
508+
_CONFIG_VARS['prefix'] = prefix
509+
_CONFIG_VARS['exec_prefix'] = exec_prefix
478510
_CONFIG_VARS['py_version'] = _PY_VERSION
479511
_CONFIG_VARS['py_version_short'] = _PY_VERSION_SHORT
480512
_CONFIG_VARS['py_version_nodot'] = _PY_VERSION_SHORT_NO_DOT
481-
_CONFIG_VARS['installed_base'] = _BASE_PREFIX
482-
_CONFIG_VARS['base'] = _PREFIX
483-
_CONFIG_VARS['installed_platbase'] = _BASE_EXEC_PREFIX
484-
_CONFIG_VARS['platbase'] = _EXEC_PREFIX
513+
_CONFIG_VARS['installed_base'] = base_prefix
514+
_CONFIG_VARS['base'] = prefix
515+
_CONFIG_VARS['installed_platbase'] = base_exec_prefix
516+
_CONFIG_VARS['platbase'] = exec_prefix
485517
_CONFIG_VARS['projectbase'] = _PROJECT_BASE
486518
_CONFIG_VARS['platlibdir'] = sys.platlibdir
487519
_CONFIG_VARS['implementation'] = _get_implementation()
488520
_CONFIG_VARS['implementation_lower'] = _get_implementation().lower()
489-
try:
490-
_CONFIG_VARS['abiflags'] = sys.abiflags
491-
except AttributeError:
492-
# sys.abiflags may not be defined on all platforms.
493-
_CONFIG_VARS['abiflags'] = ''
521+
_CONFIG_VARS['abiflags'] = abiflags
494522
try:
495523
_CONFIG_VARS['py_version_nodot_plat'] = sys.winver.replace('.', '')
496524
except AttributeError:
@@ -499,8 +527,6 @@ def _init_config_vars():
499527
if os.name == 'nt':
500528
_init_non_posix(_CONFIG_VARS)
501529
_CONFIG_VARS['VPATH'] = sys._vpath
502-
if os.name == 'posix':
503-
_init_posix(_CONFIG_VARS)
504530
if _HAS_USER_BASE:
505531
# Setting 'userbase' is done below the call to the
506532
# init function to enable using 'get_config_var' in

Lib/sysconfig/__main__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
_PYTHON_BUILD,
66
_get_sysconfigdata_name,
77
get_config_h_filename,
8+
get_config_var,
89
get_config_vars,
910
get_default_scheme,
1011
get_makefile_filename,
@@ -204,7 +205,7 @@ def _generate_posix_vars():
204205
sys.modules[name] = module
205206

206207
pybuilddir = f'build/lib.{get_platform()}-{get_python_version()}'
207-
if hasattr(sys, "gettotalrefcount"):
208+
if get_config_var('Py_DEBUG') == '1':
208209
pybuilddir += '-pydebug'
209210
os.makedirs(pybuilddir, exist_ok=True)
210211
destfile = os.path.join(pybuilddir, name + '.py')
@@ -215,6 +216,8 @@ def _generate_posix_vars():
215216
f.write('build_time_vars = ')
216217
_print_config_dict(vars, stream=f)
217218

219+
print(f'Written {destfile}')
220+
218221
# Create file used for sys.path fixup -- see Modules/getpath.c
219222
with open('pybuilddir.txt', 'w', encoding='utf8') as f:
220223
f.write(pybuilddir)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed bug where, on cross-builds, the :mod:`sysconfig` POSIX data was being
2+
generated with the host Python's ``Makefile``. The data is now generated from
3+
current build's ``Makefile``.

configure

Lines changed: 2 additions & 4 deletions
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
@@ -1614,8 +1614,8 @@ if test "$cross_compiling" = yes; then
16141614
RUNSHARED=
16151615
fi
16161616

1617+
# HOSTRUNNER - Program to run CPython for the host platform
16171618
AC_MSG_CHECKING([HOSTRUNNER])
1618-
AC_ARG_VAR([HOSTRUNNER], [Program to run CPython for the host platform])
16191619
if test -z "$HOSTRUNNER"
16201620
then
16211621
AS_CASE([$ac_sys_system/$ac_sys_emscripten_target],

0 commit comments

Comments
 (0)