Skip to content

Commit 2950bc5

Browse files
authored
GH-127429: fix sysconfig data generation on cross-builds (#127430)
1 parent e271340 commit 2950bc5

File tree

6 files changed

+49
-20
lines changed

6 files changed

+49
-20
lines changed

.github/workflows/jit.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ jobs:
104104
105105
# The `find` line is required as a result of https://github.com/actions/runner-images/issues/9966.
106106
# This is a bug in the macOS runner image where the pre-installed Python is installed in the same
107-
# directory as the Homebrew Python, which causes the build to fail for macos-13. This line removes
107+
# directory as the Homebrew Python, which causes the build to fail for macos-13. This line removes
108108
# the symlink to the pre-installed Python so that the Homebrew Python is used instead.
109109
- name: Native macOS
110110
if: runner.os == 'macOS'

Lib/sysconfig/__init__.py

+36-13
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,22 @@ def get_default_scheme():
318318

319319
def get_makefile_filename():
320320
"""Return the path of the Makefile."""
321+
322+
# GH-127429: When cross-compiling, use the Makefile from the target, instead of the host Python.
323+
if cross_base := os.environ.get('_PYTHON_PROJECT_BASE'):
324+
return os.path.join(cross_base, 'Makefile')
325+
321326
if _PYTHON_BUILD:
322327
return os.path.join(_PROJECT_BASE, "Makefile")
328+
323329
if hasattr(sys, 'abiflags'):
324330
config_dir_name = f'config-{_PY_VERSION_SHORT}{sys.abiflags}'
325331
else:
326332
config_dir_name = 'config'
333+
327334
if hasattr(sys.implementation, '_multiarch'):
328335
config_dir_name += f'-{sys.implementation._multiarch}'
336+
329337
return os.path.join(get_path('stdlib'), config_dir_name, 'Makefile')
330338

331339

@@ -464,27 +472,44 @@ def get_path(name, scheme=get_default_scheme(), vars=None, expand=True):
464472
def _init_config_vars():
465473
global _CONFIG_VARS
466474
_CONFIG_VARS = {}
475+
476+
prefix = _PREFIX
477+
exec_prefix = _EXEC_PREFIX
478+
base_prefix = _BASE_PREFIX
479+
base_exec_prefix = _BASE_EXEC_PREFIX
480+
481+
try:
482+
abiflags = sys.abiflags
483+
except AttributeError:
484+
abiflags = ''
485+
486+
if os.name == 'posix':
487+
_init_posix(_CONFIG_VARS)
488+
# If we are cross-compiling, load the prefixes from the Makefile instead.
489+
if '_PYTHON_PROJECT_BASE' in os.environ:
490+
prefix = _CONFIG_VARS['prefix']
491+
exec_prefix = _CONFIG_VARS['exec_prefix']
492+
base_prefix = _CONFIG_VARS['prefix']
493+
base_exec_prefix = _CONFIG_VARS['exec_prefix']
494+
abiflags = _CONFIG_VARS['ABIFLAGS']
495+
467496
# Normalized versions of prefix and exec_prefix are handy to have;
468497
# in fact, these are the standard versions used most places in the
469498
# Distutils.
470-
_CONFIG_VARS['prefix'] = _PREFIX
471-
_CONFIG_VARS['exec_prefix'] = _EXEC_PREFIX
499+
_CONFIG_VARS['prefix'] = prefix
500+
_CONFIG_VARS['exec_prefix'] = exec_prefix
472501
_CONFIG_VARS['py_version'] = _PY_VERSION
473502
_CONFIG_VARS['py_version_short'] = _PY_VERSION_SHORT
474503
_CONFIG_VARS['py_version_nodot'] = _PY_VERSION_SHORT_NO_DOT
475-
_CONFIG_VARS['installed_base'] = _BASE_PREFIX
476-
_CONFIG_VARS['base'] = _PREFIX
477-
_CONFIG_VARS['installed_platbase'] = _BASE_EXEC_PREFIX
478-
_CONFIG_VARS['platbase'] = _EXEC_PREFIX
504+
_CONFIG_VARS['installed_base'] = base_prefix
505+
_CONFIG_VARS['base'] = prefix
506+
_CONFIG_VARS['installed_platbase'] = base_exec_prefix
507+
_CONFIG_VARS['platbase'] = exec_prefix
479508
_CONFIG_VARS['projectbase'] = _PROJECT_BASE
480509
_CONFIG_VARS['platlibdir'] = sys.platlibdir
481510
_CONFIG_VARS['implementation'] = _get_implementation()
482511
_CONFIG_VARS['implementation_lower'] = _get_implementation().lower()
483-
try:
484-
_CONFIG_VARS['abiflags'] = sys.abiflags
485-
except AttributeError:
486-
# sys.abiflags may not be defined on all platforms.
487-
_CONFIG_VARS['abiflags'] = ''
512+
_CONFIG_VARS['abiflags'] = abiflags
488513
try:
489514
_CONFIG_VARS['py_version_nodot_plat'] = sys.winver.replace('.', '')
490515
except AttributeError:
@@ -493,8 +518,6 @@ def _init_config_vars():
493518
if os.name == 'nt':
494519
_init_non_posix(_CONFIG_VARS)
495520
_CONFIG_VARS['VPATH'] = sys._vpath
496-
if os.name == 'posix':
497-
_init_posix(_CONFIG_VARS)
498521
if _HAS_USER_BASE:
499522
# Setting 'userbase' is done below the call to the
500523
# init function to enable using 'get_config_var' in

Lib/sysconfig/__main__.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
_PYTHON_BUILD,
88
_get_sysconfigdata_name,
99
get_config_h_filename,
10+
get_config_var,
1011
get_config_vars,
1112
get_default_scheme,
1213
get_makefile_filename,
@@ -161,7 +162,7 @@ def _print_config_dict(d, stream):
161162

162163
def _get_pybuilddir():
163164
pybuilddir = f'build/lib.{get_platform()}-{get_python_version()}'
164-
if hasattr(sys, "gettotalrefcount"):
165+
if get_config_var('Py_DEBUG') == '1':
165166
pybuilddir += '-pydebug'
166167
return pybuilddir
167168

@@ -229,11 +230,15 @@ def _generate_posix_vars():
229230
f.write('build_time_vars = ')
230231
_print_config_dict(vars, stream=f)
231232

233+
print(f'Written {destfile}')
234+
232235
# Write a JSON file with the output of sysconfig.get_config_vars
233236
jsonfile = os.path.join(pybuilddir, _get_json_data_name())
234237
with open(jsonfile, 'w') as f:
235238
json.dump(get_config_vars(), f, indent=2)
236239

240+
print(f'Written {jsonfile}')
241+
237242
# Create file used for sys.path fixup -- see Modules/getpath.c
238243
with open('pybuilddir.txt', 'w', encoding='utf8') as f:
239244
f.write(pybuilddir)
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

+2-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

+1-1
Original file line numberDiff line numberDiff line change
@@ -1609,8 +1609,8 @@ if test "$cross_compiling" = yes; then
16091609
RUNSHARED=
16101610
fi
16111611

1612+
# HOSTRUNNER - Program to run CPython for the host platform
16121613
AC_MSG_CHECKING([HOSTRUNNER])
1613-
AC_ARG_VAR([HOSTRUNNER], [Program to run CPython for the host platform])
16141614
if test -z "$HOSTRUNNER"
16151615
then
16161616
AS_CASE([$ac_sys_system],

0 commit comments

Comments
 (0)