Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions project-template-ios/internal/nsld.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,57 @@ GEN_MODULEMAP $TARGET_ARCH
printf "Generating metadata..."
GEN_METADATA $TARGET_ARCH
DELETE_SWIFT_MODULES_DIR
NS_LD="${NS_LD:-"$TOOLCHAIN_DIR/usr/bin/clang"}"
$NS_LD "$@"

function resolve_clang() {
# 1) If NS_LD is set and executable, honor it.
if [[ -n "$NS_LD" && -x "$NS_LD" ]]; then
echo "$NS_LD"
return 0
fi

# 2) TOOLCHAIN_DIR (if provided)
if [[ -n "$TOOLCHAIN_DIR" && -x "$TOOLCHAIN_DIR/usr/bin/clang" ]]; then
echo "$TOOLCHAIN_DIR/usr/bin/clang"
return 0
fi

# 3) Xcode's DT_TOOLCHAIN_DIR (provided by xcodebuild)
if [[ -n "$DT_TOOLCHAIN_DIR" && -x "$DT_TOOLCHAIN_DIR/usr/bin/clang" ]]; then
echo "$DT_TOOLCHAIN_DIR/usr/bin/clang"
return 0
fi

# 4) xcrun lookup (most reliable within Xcode build env)
local xcrun_clang
xcrun_clang=$(xcrun --find clang 2>/dev/null) || true
if [[ -n "$xcrun_clang" && -x "$xcrun_clang" ]]; then
echo "$xcrun_clang"
return 0
fi

# 5) Xcode default toolchain from xcode-select
local xcode_path
xcode_path=$(xcode-select -p 2>/dev/null) || true
if [[ -n "$xcode_path" && -x "$xcode_path/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" ]]; then
echo "$xcode_path/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang"
return 0
fi

# 6) System fallback
if [[ -x "/usr/bin/clang" ]]; then
echo "/usr/bin/clang"
return 0
fi

return 1
}

CLANG_PATH=$(resolve_clang)
if [[ -z "$CLANG_PATH" ]]; then
echo "NSLD: ERROR: Could not locate a usable clang. TOOLCHAIN_DIR='${TOOLCHAIN_DIR}' DT_TOOLCHAIN_DIR='${DT_TOOLCHAIN_DIR}'."
exit 1
fi

# For visibility downstream, set NS_LD to the resolved path and invoke.
NS_LD="$CLANG_PATH"
"$NS_LD" "$@"