Skip to content

Commit d02fd97

Browse files
committed
Add ABI detection diagnostics and target-triple mismatch warning
1 parent d86e7b4 commit d02fd97

1 file changed

Lines changed: 44 additions & 2 deletions

File tree

tools/abiname.sh

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,35 @@ canonicalize_abi() {
1818
esac
1919
}
2020

21+
detect_target_triple() {
22+
if ${CC} -dumpmachine >/dev/null 2>&1; then
23+
${CC} -dumpmachine
24+
return 0
25+
fi
26+
if ${CC} --print-target-triple >/dev/null 2>&1; then
27+
${CC} --print-target-triple
28+
return 0
29+
fi
30+
printf '%s\n' ""
31+
}
32+
33+
abi_from_target_triple() {
34+
triple=$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')
35+
case "${triple}" in
36+
*x86_64*apple-darwin*|*x86_64*darwin*) printf '%s\n' "darwin_x86_64" ;;
37+
*arm64*apple-darwin*|*aarch64*apple-darwin*|*arm64*darwin*|*aarch64*darwin*) printf '%s\n' "darwin_arm64" ;;
38+
*x86_64*linux*) printf '%s\n' "sysv_amd64" ;;
39+
*i?86*linux*) printf '%s\n' "sysv_i386" ;;
40+
*aarch64*linux*) printf '%s\n' "aarch64" ;;
41+
*arm*linux*gnueabi*|*armv7*linux*|*armv6*linux*) printf '%s\n' "arm32" ;;
42+
*riscv64*linux*) printf '%s\n' "riscv64" ;;
43+
*aarch64*w64*mingw*|*arm64*windows*|*aarch64*windows*) printf '%s\n' "win_arm64" ;;
44+
*x86_64*w64*mingw*|*x86_64*windows*) printf '%s\n' "win_x64" ;;
45+
*i?86*w64*mingw*|*i?86*windows*) printf '%s\n' "win_x86" ;;
46+
*) printf '%s\n' "" ;;
47+
esac
48+
}
49+
2150
mkdir -p "${here}/tmp"
2251
# Clean up any stale temp files first
2352
rm -f "${here}/tmp"/abiname*.c "${here}/tmp"/abiname*.c.out
@@ -27,13 +56,26 @@ tmp=$(mktemp "${here}/tmp/abinameXXX.c")
2756
CC=${1:-cc}
2857
CFLAGS=${2:-}
2958
ABI_OVERRIDE=${3:-${STACKMAN_ABI:-}}
59+
target_triple=$(detect_target_triple)
3060
if [ -n "${ABI_OVERRIDE}" ]; then
31-
canonicalize_abi "${ABI_OVERRIDE}"
61+
abi=$(canonicalize_abi "${ABI_OVERRIDE}")
62+
if [ -n "${STACKMAN_ABI_DEBUG:-}" ] || [ -n "${STACKMAN_ABI_TRACE:-}" ]; then
63+
printf '%s\n' "stackman abiname: cc=${CC} target=${target_triple:-unknown} abi=${abi} source=override" >&2
64+
fi
65+
printf '%s\n' "${abi}"
3266
exit 0
3367
fi
3468
${CC} ${CFLAGS} -I${here}/../stackman -E -o "${tmp}" "${here}/abiname.c"
3569
#2 compile resulting file
3670
cc -o "${tmp}.out" "${tmp}"
3771
#3 run it
3872
abi=$("${tmp}.out")
39-
canonicalize_abi "${abi}"
73+
abi=$(canonicalize_abi "${abi}")
74+
target_abi=$(abi_from_target_triple "${target_triple}")
75+
if [ -n "${target_abi}" ] && [ "${target_abi}" != "${abi}" ]; then
76+
printf '%s\n' "warning: stackman ABI mismatch: compiler target '${target_triple}' suggests '${target_abi}', macro probe selected '${abi}'. Consider setting STACKMAN_ABI explicitly." >&2
77+
fi
78+
if [ -n "${STACKMAN_ABI_DEBUG:-}" ] || [ -n "${STACKMAN_ABI_TRACE:-}" ]; then
79+
printf '%s\n' "stackman abiname: cc=${CC} target=${target_triple:-unknown} abi=${abi} source=macro" >&2
80+
fi
81+
printf '%s\n' "${abi}"

0 commit comments

Comments
 (0)