Skip to content

Commit e0052d1

Browse files
Harden ABI detection for cross-build wrappers (#19)
* Add explicit STACKMAN_ABI override support * Normalize Windows ARM64 ABI token and keep alias compatibility * Add ABI detection diagnostics and target-triple mismatch warning * Use CFLAGS in target-triple probe and soften mismatch warning * Document ABI detection hardening in changelog * Address PR review feedback on changelog and ABI override * Skip target probing when STACKMAN_ABI override is set
1 parent 9a950af commit e0052d1

4 files changed

Lines changed: 84 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
- Added explicit `STACKMAN_ABI` override support in the Makefile and ABI probe script for deterministic cross-build packaging
12+
13+
### Changed
14+
- Normalized Windows ARM64 ABI naming to `win_arm64` and kept compatibility mapping for legacy `win_aarch64` inputs
15+
- ABI probe diagnostics now query compiler target triple and emit clearer mismatch guidance when target intent is not fully propagated through wrapper/toolchain settings
16+
- Target triple probing now prefers `CC` with `CFLAGS` (with fallback probing) so clang-style cross-target flags are reflected in secondary detection
17+
1018
## [1.2.2] - 2026-06-05
1119

1220
### Changed

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ CXX = $(PLATFORM_PREFIX)-g++
1717
LD = $(PLATFORM_PREFIX)-ld
1818
AR = $(PLATFORM_PREFIX)-ar
1919
endif
20-
# run c preprocessor with any cflags to get cross compilation result, then run regular compile in native
20+
# Allow explicit ABI selection for deterministic cross-build packaging.
21+
# If not provided, auto-detect from compiler macros.
22+
ifeq ($(strip $(STACKMAN_ABI)),)
2123
ABI := $(shell sh tools/abiname.sh "$(CC)" "$(CFLAGS)")
24+
else
25+
# Accept legacy alias for backward compatibility.
26+
ABI := $(patsubst win_aarch64,win_arm64,$(strip $(STACKMAN_ABI)))
27+
endif
2228
ifndef ABI
2329
$(error Could not determine platform)
2430
endif

stackman/platforms/platform.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#elif defined(_M_ARM64)
4545
#include "switch_arm64_msvc.h" /* MS Visual Studio on ARM */
4646
#define _STACKMAN_PLATFORM arm64_msvc
47-
#define _STACKMAN_ABI win_aarch64
47+
#define _STACKMAN_ABI win_arm64
4848
#endif
4949

5050

tools/abiname.sh

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,54 @@
1010
# run the provided code.
1111
set -eu
1212
here=$(dirname "$0")
13+
14+
canonicalize_abi() {
15+
case "$1" in
16+
win_aarch64) printf '%s\n' "win_arm64" ;;
17+
*) printf '%s\n' "$1" ;;
18+
esac
19+
}
20+
21+
detect_target_triple() {
22+
# Prefer querying with CFLAGS so explicit target flags (for example
23+
# --target=... in clang-based cross builds) are reflected.
24+
if ${CC} ${CFLAGS} -dumpmachine >/dev/null 2>&1; then
25+
${CC} ${CFLAGS} -dumpmachine
26+
return 0
27+
fi
28+
if ${CC} ${CFLAGS} --print-target-triple >/dev/null 2>&1; then
29+
${CC} ${CFLAGS} --print-target-triple
30+
return 0
31+
fi
32+
# Fall back to no CFLAGS in case unrelated build flags make the query fail.
33+
if ${CC} -dumpmachine >/dev/null 2>&1; then
34+
${CC} -dumpmachine
35+
return 0
36+
fi
37+
if ${CC} --print-target-triple >/dev/null 2>&1; then
38+
${CC} --print-target-triple
39+
return 0
40+
fi
41+
printf '%s\n' ""
42+
}
43+
44+
abi_from_target_triple() {
45+
triple=$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')
46+
case "${triple}" in
47+
*x86_64*apple-darwin*|*x86_64*darwin*) printf '%s\n' "darwin_x86_64" ;;
48+
*arm64*apple-darwin*|*aarch64*apple-darwin*|*arm64*darwin*|*aarch64*darwin*) printf '%s\n' "darwin_arm64" ;;
49+
*x86_64*linux*) printf '%s\n' "sysv_amd64" ;;
50+
*i?86*linux*) printf '%s\n' "sysv_i386" ;;
51+
*aarch64*linux*) printf '%s\n' "aarch64" ;;
52+
*arm*linux*gnueabi*|*armv7*linux*|*armv6*linux*) printf '%s\n' "arm32" ;;
53+
*riscv64*linux*) printf '%s\n' "riscv64" ;;
54+
*aarch64*w64*mingw*|*arm64*windows*|*aarch64*windows*) printf '%s\n' "win_arm64" ;;
55+
*x86_64*w64*mingw*|*x86_64*windows*) printf '%s\n' "win_x64" ;;
56+
*i?86*w64*mingw*|*i?86*windows*) printf '%s\n' "win_x86" ;;
57+
*) printf '%s\n' "" ;;
58+
esac
59+
}
60+
1361
mkdir -p "${here}/tmp"
1462
# Clean up any stale temp files first
1563
rm -f "${here}/tmp"/abiname*.c "${here}/tmp"/abiname*.c.out
@@ -18,8 +66,27 @@ tmp=$(mktemp "${here}/tmp/abinameXXX.c")
1866
#1 create the preprocessed file
1967
CC=${1:-cc}
2068
CFLAGS=${2:-}
69+
ABI_OVERRIDE=${3:-${STACKMAN_ABI:-}}
70+
if [ -n "${ABI_OVERRIDE}" ]; then
71+
abi=$(canonicalize_abi "${ABI_OVERRIDE}")
72+
if [ -n "${STACKMAN_ABI_DEBUG:-}" ] || [ -n "${STACKMAN_ABI_TRACE:-}" ]; then
73+
printf '%s\n' "stackman abiname: cc=${CC} target=override-skip abi=${abi} source=override" >&2
74+
fi
75+
printf '%s\n' "${abi}"
76+
exit 0
77+
fi
78+
target_triple=$(detect_target_triple)
2179
${CC} ${CFLAGS} -I${here}/../stackman -E -o "${tmp}" "${here}/abiname.c"
2280
#2 compile resulting file
2381
cc -o "${tmp}.out" "${tmp}"
2482
#3 run it
25-
"${tmp}.out"
83+
abi=$("${tmp}.out")
84+
abi=$(canonicalize_abi "${abi}")
85+
target_abi=$(abi_from_target_triple "${target_triple}")
86+
if [ -n "${target_abi}" ] && [ "${target_abi}" != "${abi}" ]; then
87+
printf '%s\n' "warning: stackman ABI mismatch: compiler target '${target_triple}' suggests '${target_abi}', macro probe selected '${abi}'. This usually means target intent was not fully propagated (flags/wrapper selection). Consider setting STACKMAN_ABI explicitly for deterministic packaging." >&2
88+
fi
89+
if [ -n "${STACKMAN_ABI_DEBUG:-}" ] || [ -n "${STACKMAN_ABI_TRACE:-}" ]; then
90+
printf '%s\n' "stackman abiname: cc=${CC} target=${target_triple:-unknown} abi=${abi} source=macro" >&2
91+
fi
92+
printf '%s\n' "${abi}"

0 commit comments

Comments
 (0)