Skip to content

Commit bf2bbed

Browse files
authored
feat: improve NSLD script to resolve clang paths
1 parent 08811e6 commit bf2bbed

File tree

1 file changed

+50
-13
lines changed
  • project-template-ios/internal

1 file changed

+50
-13
lines changed

project-template-ios/internal/nsld.sh

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,56 @@ printf "Generating metadata..."
5959
GEN_METADATA $TARGET_ARCH
6060
DELETE_SWIFT_MODULES_DIR
6161

62-
# Resolve linker: prefer provided NS_LD, otherwise use toolchain clang if present.
63-
DEFAULT_LD="$TOOLCHAIN_DIR/usr/bin/clang"
64-
if [[ -z "$NS_LD" ]]; then
65-
if [[ -x "$DEFAULT_LD" ]]; then
66-
NS_LD="$DEFAULT_LD"
67-
else
68-
echo "NSLD: Skipping link because toolchain clang not found: $DEFAULT_LD (TOOLCHAIN_DIR may be missing)."
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
6987
fi
70-
fi
7188

72-
# If NS_LD was explicitly set to the default path but it's missing, skip as well.
73-
if [[ "$NS_LD" == "$DEFAULT_LD" && ! -x "$NS_LD" ]]; then
74-
echo "NSLD: Skipping link because toolchain clang not found: $NS_LD (TOOLCHAIN_DIR may be missing)."
75-
else
76-
"$NS_LD" "$@"
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
77110
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)