Skip to content

Commit debc76d

Browse files
authored
feat: improve robustness of linking with clang path checks (#280)
1 parent 4ce18ed commit debc76d

File tree

1 file changed

+54
-2
lines changed
  • project-template-ios/internal

1 file changed

+54
-2
lines changed

project-template-ios/internal/nsld.sh

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,57 @@ GEN_MODULEMAP $TARGET_ARCH
5858
printf "Generating metadata..."
5959
GEN_METADATA $TARGET_ARCH
6060
DELETE_SWIFT_MODULES_DIR
61-
NS_LD="${NS_LD:-"$TOOLCHAIN_DIR/usr/bin/clang"}"
62-
$NS_LD "$@"
61+
62+
function resolve_clang() {
63+
# 1) If NS_LD is set and executable, honor it.
64+
if [[ -n "$NS_LD" && -x "$NS_LD" ]]; then
65+
echo "$NS_LD"
66+
return 0
67+
fi
68+
69+
# 2) TOOLCHAIN_DIR (if provided)
70+
if [[ -n "$TOOLCHAIN_DIR" && -x "$TOOLCHAIN_DIR/usr/bin/clang" ]]; then
71+
echo "$TOOLCHAIN_DIR/usr/bin/clang"
72+
return 0
73+
fi
74+
75+
# 3) Xcode's DT_TOOLCHAIN_DIR (provided by xcodebuild)
76+
if [[ -n "$DT_TOOLCHAIN_DIR" && -x "$DT_TOOLCHAIN_DIR/usr/bin/clang" ]]; then
77+
echo "$DT_TOOLCHAIN_DIR/usr/bin/clang"
78+
return 0
79+
fi
80+
81+
# 4) xcrun lookup (most reliable within Xcode build env)
82+
local xcrun_clang
83+
xcrun_clang=$(xcrun --find clang 2>/dev/null) || true
84+
if [[ -n "$xcrun_clang" && -x "$xcrun_clang" ]]; then
85+
echo "$xcrun_clang"
86+
return 0
87+
fi
88+
89+
# 5) Xcode default toolchain from xcode-select
90+
local xcode_path
91+
xcode_path=$(xcode-select -p 2>/dev/null) || true
92+
if [[ -n "$xcode_path" && -x "$xcode_path/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" ]]; then
93+
echo "$xcode_path/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang"
94+
return 0
95+
fi
96+
97+
# 6) System fallback
98+
if [[ -x "/usr/bin/clang" ]]; then
99+
echo "/usr/bin/clang"
100+
return 0
101+
fi
102+
103+
return 1
104+
}
105+
106+
CLANG_PATH=$(resolve_clang)
107+
if [[ -z "$CLANG_PATH" ]]; then
108+
echo "NSLD: ERROR: Could not locate a usable clang. TOOLCHAIN_DIR='${TOOLCHAIN_DIR}' DT_TOOLCHAIN_DIR='${DT_TOOLCHAIN_DIR}'."
109+
exit 1
110+
fi
111+
112+
# For visibility downstream, set NS_LD to the resolved path and invoke.
113+
NS_LD="$CLANG_PATH"
114+
"$NS_LD" "$@"

0 commit comments

Comments
 (0)