@@ -106,7 +106,7 @@ def __repr__(self) -> str:
106106# ---------------------------------------------------------------------------
107107# Platform identity (set by canonical_host())
108108# ---------------------------------------------------------------------------
109- cross_compiling : bool = False
109+ cross_compiling : bool | str = False
110110host : str = "" # e.g. "x86_64-pc-linux-gnu"
111111host_cpu : str = "" # e.g. "x86_64"
112112build : str = "" # e.g. "x86_64-pc-linux-gnu"
@@ -1481,6 +1481,9 @@ def canonical_host() -> None:
14811481
14821482 Uses config.guess and config.sub from the source tree when available,
14831483 matching autoconf's behaviour. Falls back to Python's platform module.
1484+
1485+ Parses --host=TRIPLET and --build=TRIPLET from sys.argv. When --host
1486+ is given and differs from the build triplet, sets cross_compiling=True.
14841487 """
14851488 global cross_compiling , host , host_cpu , build
14861489
@@ -1490,33 +1493,60 @@ def canonical_host() -> None:
14901493 config_guess = os .path .join (aux_dir , "config.guess" )
14911494 config_sub = os .path .join (aux_dir , "config.sub" )
14921495
1493- if os .path .isfile (config_guess ) and os .access (config_guess , os .X_OK ):
1496+ def _canonicalize (triplet : str ) -> str :
1497+ """Canonicalize a triplet through config.sub if available."""
1498+ if os .path .isfile (config_sub ) and os .access (config_sub , os .X_OK ):
1499+ try :
1500+ return subprocess .check_output (
1501+ [config_sub , triplet ], text = True , stderr = subprocess .DEVNULL
1502+ ).strip ()
1503+ except (subprocess .CalledProcessError , OSError ):
1504+ pass
1505+ return triplet
1506+
1507+ # Parse --host and --build from sys.argv
1508+ host_arg = None
1509+ build_arg = None
1510+ for arg in sys .argv [1 :]:
1511+ if arg .startswith ("--host=" ):
1512+ host_arg = arg .split ("=" , 1 )[1 ]
1513+ elif arg .startswith ("--build=" ):
1514+ build_arg = arg .split ("=" , 1 )[1 ]
1515+
1516+ # Determine build triplet
1517+ if build_arg :
1518+ build = _canonicalize (build_arg )
1519+ elif os .path .isfile (config_guess ) and os .access (config_guess , os .X_OK ):
14941520 try :
14951521 raw = subprocess .check_output (
14961522 [config_guess ], text = True , stderr = subprocess .DEVNULL
14971523 ).strip ()
1498- # Canonicalize through config.sub if available
1499- if os .path .isfile (config_sub ) and os .access (config_sub , os .X_OK ):
1500- raw = subprocess .check_output (
1501- [config_sub , raw ], text = True , stderr = subprocess .DEVNULL
1502- ).strip ()
1503- build = raw
1524+ build = _canonicalize (raw )
15041525 except (subprocess .CalledProcessError , OSError ):
15051526 build = _fallback_triplet ()
15061527 else :
15071528 build = _fallback_triplet ()
15081529
15091530 # Parse the triplet: cpu-vendor-os (autoconf splits on '-')
15101531 parts = build .split ("-" , 2 )
1511- if len (parts ) >= 3 :
1512- build_cpu = parts [0 ]
1532+ build_cpu = parts [0 ]
1533+
1534+ # Determine host triplet and cross-compilation status.
1535+ # Matches autoconf: --host without --build sets cross_compiling="maybe".
1536+ if host_arg :
1537+ host = _canonicalize (host_arg )
1538+ host_parts = host .split ("-" , 2 )
1539+ host_cpu = host_parts [0 ]
1540+ if build_arg :
1541+ cross_compiling = (host != build )
1542+ else :
1543+ # autoconf sets "maybe" when --host is given without --build
1544+ cross_compiling = "maybe" if host != build else False
15131545 else :
1514- build_cpu = parts [0 ]
1515-
1516- # For native builds, host == build
1517- host = build
1518- host_cpu = build_cpu
1519- cross_compiling = False
1546+ # For native builds, host == build
1547+ host = build
1548+ host_cpu = build_cpu
1549+ cross_compiling = False
15201550
15211551
15221552def _fallback_triplet () -> str :
0 commit comments