@@ -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
324326def 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):
468478def _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
0 commit comments