@@ -173,7 +173,9 @@ def joinuser(*args):
173
173
_PY_VERSION = sys .version .split ()[0 ]
174
174
_PY_VERSION_SHORT = f'{ sys .version_info [0 ]} .{ sys .version_info [1 ]} '
175
175
_PY_VERSION_SHORT_NO_DOT = f'{ sys .version_info [0 ]} { sys .version_info [1 ]} '
176
+ _PREFIX = os .path .normpath (sys .prefix )
176
177
_BASE_PREFIX = os .path .normpath (sys .base_prefix )
178
+ _EXEC_PREFIX = os .path .normpath (sys .exec_prefix )
177
179
_BASE_EXEC_PREFIX = os .path .normpath (sys .base_exec_prefix )
178
180
# Mutex guarding initialization of _CONFIG_VARS.
179
181
_CONFIG_VARS_LOCK = threading .RLock ()
@@ -323,14 +325,22 @@ def get_default_scheme():
323
325
324
326
def get_makefile_filename ():
325
327
"""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
+
326
333
if _PYTHON_BUILD :
327
334
return os .path .join (_PROJECT_BASE , "Makefile" )
335
+
328
336
if hasattr (sys , 'abiflags' ):
329
337
config_dir_name = f'config-{ _PY_VERSION_SHORT } { sys .abiflags } '
330
338
else :
331
339
config_dir_name = 'config'
340
+
332
341
if hasattr (sys .implementation , '_multiarch' ):
333
342
config_dir_name += f'-{ sys .implementation ._multiarch } '
343
+
334
344
return os .path .join (get_path ('stdlib' ), config_dir_name , 'Makefile' )
335
345
336
346
@@ -468,29 +478,47 @@ def get_path(name, scheme=get_default_scheme(), vars=None, expand=True):
468
478
def _init_config_vars ():
469
479
global _CONFIG_VARS
470
480
_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
+
471
502
# Normalized versions of prefix and exec_prefix are handy to have;
472
503
# in fact, these are the standard versions used most places in the
473
504
# Distutils.
505
+
474
506
_PREFIX = os .path .normpath (sys .prefix )
475
507
_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
478
510
_CONFIG_VARS ['py_version' ] = _PY_VERSION
479
511
_CONFIG_VARS ['py_version_short' ] = _PY_VERSION_SHORT
480
512
_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
485
517
_CONFIG_VARS ['projectbase' ] = _PROJECT_BASE
486
518
_CONFIG_VARS ['platlibdir' ] = sys .platlibdir
487
519
_CONFIG_VARS ['implementation' ] = _get_implementation ()
488
520
_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
494
522
try :
495
523
_CONFIG_VARS ['py_version_nodot_plat' ] = sys .winver .replace ('.' , '' )
496
524
except AttributeError :
@@ -499,8 +527,6 @@ def _init_config_vars():
499
527
if os .name == 'nt' :
500
528
_init_non_posix (_CONFIG_VARS )
501
529
_CONFIG_VARS ['VPATH' ] = sys ._vpath
502
- if os .name == 'posix' :
503
- _init_posix (_CONFIG_VARS )
504
530
if _HAS_USER_BASE :
505
531
# Setting 'userbase' is done below the call to the
506
532
# init function to enable using 'get_config_var' in
0 commit comments