Skip to content

Commit 78c686f

Browse files
committed
Fixes for cross-compile, handle --host/build.
1 parent 3132786 commit 78c686f

File tree

5 files changed

+133
-45
lines changed

5 files changed

+133
-45
lines changed

Tools/configure/conf_platform.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ def setup_platform_triplet(v):
312312
):
313313
v.MULTIARCH = ""
314314
else:
315-
v.MULTIARCH = pyconf.cmd_output([v.CC, "--print-multiarch"])
315+
rc, v.MULTIARCH = pyconf.cmd_status([v.CC, "--print-multiarch"])
316+
if rc != 0:
317+
v.MULTIARCH = ""
316318

317319
if v.PLATFORM_TRIPLET and v.MULTIARCH:
318320
if v.PLATFORM_TRIPLET != v.MULTIARCH:

Tools/configure/pyconf.py

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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
110110
host: str = "" # e.g. "x86_64-pc-linux-gnu"
111111
host_cpu: str = "" # e.g. "x86_64"
112112
build: 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

15221552
def _fallback_triplet() -> str:

Tools/configure/transpiler/pyconf.awk

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,23 +1813,51 @@ function pyconf_cmd_status(args, result_arr, cmd) {
18131813
system("rm -f " _pyconf_tmpdir "/cmd_out")
18141814
}
18151815

1816-
function pyconf_canonical_host( result, guess, csub, parts, n) {
1816+
function pyconf_canonical_host( result, guess, csub, parts, n, host_arg, build_arg, canon) {
18171817
guess = _pyconf_srcdir "/config.guess"
18181818
csub = _pyconf_srcdir "/config.sub"
1819-
result = _cmd_output_oneline(guess " 2>/dev/null")
1820-
if (result != "") {
1821-
# Canonicalize through config.sub if available
1822-
result = _cmd_output_oneline(csub " " result " 2>/dev/null")
1819+
1820+
# Check for --host and --build from parsed args (stored by pyconf_parse_args)
1821+
host_arg = V["host"]
1822+
build_arg = V["build"]
1823+
1824+
# Determine build triplet
1825+
if (build_arg != "") {
1826+
canon = _cmd_output_oneline(csub " " build_arg " 2>/dev/null")
1827+
pyconf_build = (canon != "") ? canon : build_arg
1828+
} else {
1829+
result = _cmd_output_oneline(guess " 2>/dev/null")
1830+
if (result != "") {
1831+
# Canonicalize through config.sub if available
1832+
canon = _cmd_output_oneline(csub " " result " 2>/dev/null")
1833+
result = (canon != "") ? canon : result
1834+
}
1835+
if (result == "") {
1836+
# Fallback: uname-based triplet
1837+
result = _cmd_output_oneline("uname -m") "-pc-" tolower(_cmd_output_oneline("uname -s")) "-gnu"
1838+
}
1839+
pyconf_build = result
18231840
}
1824-
if (result == "") {
1825-
# Fallback: uname-based triplet
1826-
result = _cmd_output_oneline("uname -m") "-pc-" tolower(_cmd_output_oneline("uname -s")) "-gnu"
1841+
1842+
# Determine host triplet and cross-compilation status
1843+
if (host_arg != "") {
1844+
canon = _cmd_output_oneline(csub " " host_arg " 2>/dev/null")
1845+
pyconf_host = (canon != "") ? canon : host_arg
1846+
n = split(pyconf_host, parts, "-")
1847+
pyconf_host_cpu = parts[1]
1848+
if (build_arg != "") {
1849+
pyconf_cross_compiling = (pyconf_host != pyconf_build) ? "yes" : "no"
1850+
} else {
1851+
# autoconf sets "maybe" when --host is given without --build
1852+
pyconf_cross_compiling = (pyconf_host != pyconf_build) ? "maybe" : "no"
1853+
}
1854+
} else {
1855+
# For native builds, host == build
1856+
pyconf_host = pyconf_build
1857+
n = split(pyconf_build, parts, "-")
1858+
pyconf_host_cpu = parts[1]
1859+
pyconf_cross_compiling = "no"
18271860
}
1828-
pyconf_build = result
1829-
pyconf_host = result
1830-
n = split(result, parts, "-")
1831-
pyconf_host_cpu = parts[1]
1832-
pyconf_cross_compiling = "no"
18331861
}
18341862

18351863
function pyconf_find_compiler(user_cc, user_cpp, cc, cpp, ver) {

compare-conf.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ echo ""
4747
DIFFS=0
4848
for f in "${FILES[@]}"; do
4949
echo "=== diff $f ==="
50-
if diff -u -wbB "$OLD_DIR/$f" "$NEW_DIR/$f"; then
50+
if diff -u -wb "$OLD_DIR/$f" "$NEW_DIR/$f"; then
5151
echo "(no differences)"
5252
else
5353
DIFFS=$((DIFFS + 1))

configure-new

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,23 +1824,51 @@ function pyconf_cmd_status(args, result_arr, cmd) {
18241824
system("rm -f " _pyconf_tmpdir "/cmd_out")
18251825
}
18261826

1827-
function pyconf_canonical_host( result, guess, csub, parts, n) {
1827+
function pyconf_canonical_host( result, guess, csub, parts, n, host_arg, build_arg, canon) {
18281828
guess = _pyconf_srcdir "/config.guess"
18291829
csub = _pyconf_srcdir "/config.sub"
1830-
result = _cmd_output_oneline(guess " 2>/dev/null")
1831-
if (result != "") {
1832-
# Canonicalize through config.sub if available
1833-
result = _cmd_output_oneline(csub " " result " 2>/dev/null")
1830+
1831+
# Check for --host and --build from parsed args (stored by pyconf_parse_args)
1832+
host_arg = V["host"]
1833+
build_arg = V["build"]
1834+
1835+
# Determine build triplet
1836+
if (build_arg != "") {
1837+
canon = _cmd_output_oneline(csub " " build_arg " 2>/dev/null")
1838+
pyconf_build = (canon != "") ? canon : build_arg
1839+
} else {
1840+
result = _cmd_output_oneline(guess " 2>/dev/null")
1841+
if (result != "") {
1842+
# Canonicalize through config.sub if available
1843+
canon = _cmd_output_oneline(csub " " result " 2>/dev/null")
1844+
result = (canon != "") ? canon : result
1845+
}
1846+
if (result == "") {
1847+
# Fallback: uname-based triplet
1848+
result = _cmd_output_oneline("uname -m") "-pc-" tolower(_cmd_output_oneline("uname -s")) "-gnu"
1849+
}
1850+
pyconf_build = result
1851+
}
1852+
1853+
# Determine host triplet and cross-compilation status
1854+
if (host_arg != "") {
1855+
canon = _cmd_output_oneline(csub " " host_arg " 2>/dev/null")
1856+
pyconf_host = (canon != "") ? canon : host_arg
1857+
n = split(pyconf_host, parts, "-")
1858+
pyconf_host_cpu = parts[1]
1859+
if (build_arg != "") {
1860+
pyconf_cross_compiling = (pyconf_host != pyconf_build) ? "yes" : "no"
1861+
} else {
1862+
# autoconf sets "maybe" when --host is given without --build
1863+
pyconf_cross_compiling = (pyconf_host != pyconf_build) ? "maybe" : "no"
1864+
}
1865+
} else {
1866+
# For native builds, host == build
1867+
pyconf_host = pyconf_build
1868+
n = split(pyconf_build, parts, "-")
1869+
pyconf_host_cpu = parts[1]
1870+
pyconf_cross_compiling = "no"
18341871
}
1835-
if (result == "") {
1836-
# Fallback: uname-based triplet
1837-
result = _cmd_output_oneline("uname -m") "-pc-" tolower(_cmd_output_oneline("uname -s")) "-gnu"
1838-
}
1839-
pyconf_build = result
1840-
pyconf_host = result
1841-
n = split(result, parts, "-")
1842-
pyconf_host_cpu = parts[1]
1843-
pyconf_cross_compiling = "no"
18441872
}
18451873

18461874
function pyconf_find_compiler(user_cc, user_cpp, cc, cpp, ver) {

0 commit comments

Comments
 (0)