diff --git a/native/build-native.sh b/native/build-native.sh index 9d62bc47..005ae797 100644 --- a/native/build-native.sh +++ b/native/build-native.sh @@ -27,7 +27,7 @@ detect_host_tag() { HOST_LIB_SUBDIR="lib64" ;; darwin*) - if [[ "$arch" == "arm64" ]]; then + if [[ "$arch" == "arm64" || "$arch" == "aarch64" ]]; then HOST_TAG="darwin-arm64" else HOST_TAG="darwin-x86_64" @@ -47,23 +47,37 @@ detect_host_tag() { ensure_ndk_for_host() { local try_home="$1" - local bin_dir="$try_home/toolchains/llvm/prebuilt/$HOST_TAG/bin" - local lib_root="$try_home/toolchains/llvm/prebuilt/$HOST_TAG" - local lib_dir="$lib_root/$HOST_LIB_SUBDIR" - if [ ! -d "$lib_dir" ]; then - if [ -d "$lib_root/lib64" ]; then - lib_dir="$lib_root/lib64" - elif [ -d "$lib_root/lib" ]; then - lib_dir="$lib_root/lib" + local prebuilt_root="$try_home/toolchains/llvm/prebuilt" + local requested_tag="$HOST_TAG" + local -a candidate_tags=("$requested_tag") + if [[ "$requested_tag" == "darwin-arm64" ]]; then + candidate_tags+=("darwin-x86_64") + fi + + local candidate_tag + for candidate_tag in "${candidate_tags[@]}"; do + local bin_dir="$prebuilt_root/$candidate_tag/bin" + local lib_root="$prebuilt_root/$candidate_tag" + local lib_dir="$lib_root/$HOST_LIB_SUBDIR" + if [ ! -d "$lib_dir" ]; then + if [ -d "$lib_root/lib64" ]; then + lib_dir="$lib_root/lib64" + elif [ -d "$lib_root/lib" ]; then + lib_dir="$lib_root/lib" + fi fi - fi - if [ -d "$bin_dir" ] && [ -d "$lib_dir" ]; then - ANDROID_NDK_HOME="$try_home" - NDK_TOOLCHAIN_DIR="$bin_dir" - NDK_LIB_DIR="$lib_dir" - echo "$bin_dir" - return 0 - fi + if [ -d "$bin_dir" ] && [ -d "$lib_dir" ]; then + HOST_TAG="$candidate_tag" + ANDROID_NDK_HOME="$try_home" + NDK_TOOLCHAIN_DIR="$bin_dir" + NDK_LIB_DIR="$lib_dir" + if [ "$candidate_tag" != "$requested_tag" ]; then + echo "Falling back to NDK host toolchain: $candidate_tag (requested $requested_tag)" >&2 + fi + echo "$bin_dir" + return 0 + fi + done return 1 } @@ -311,4 +325,3 @@ esac cd "$RUST_DIR" rustup run "$TOOLCHAIN" cargo build --release --target "$1" - diff --git a/native/build.gradle.kts b/native/build.gradle.kts index e41c4b76..439c5ded 100644 --- a/native/build.gradle.kts +++ b/native/build.gradle.kts @@ -49,15 +49,29 @@ require(ndkHome.exists()) { "Configured NDK directory $ndkHome does not exist" } val osName = System.getProperty("os.name").lowercase(Locale.ROOT) val osArch = System.getProperty("os.arch").lowercase(Locale.ROOT) -val hostTag = when { +val preferredHostTag = when { osName.contains("windows") -> "windows-x86_64" osName.contains("mac") && osArch.contains("arm") -> "darwin-arm64" osName.contains("mac") -> "darwin-x86_64" else -> "linux-x86_64" } -val toolchainBin = File(ndkHome, "toolchains/llvm/prebuilt/$hostTag/bin") -require(toolchainBin.exists()) { "Unable to locate NDK toolchain under $toolchainBin" } +val prebuiltRoot = File(ndkHome, "toolchains/llvm/prebuilt") +require(prebuiltRoot.exists()) { "Unable to locate NDK prebuilts under $prebuiltRoot" } +val hostTagCandidates = mutableListOf(preferredHostTag) +if (preferredHostTag == "darwin-arm64") { + hostTagCandidates.add("darwin-x86_64") +} +if (preferredHostTag.startsWith("darwin")) { + prebuiltRoot.listFiles() + ?.filter { it.isDirectory && it.name.startsWith("darwin-") } + ?.forEach { hostTagCandidates.add(it.name) } +} +val hostTag = hostTagCandidates + .firstOrNull { tag -> File(prebuiltRoot, "$tag/bin").exists() } + ?: error("Unable to locate NDK toolchain bin directory for $preferredHostTag under $prebuiltRoot") + +val toolchainBin = File(prebuiltRoot, "$hostTag/bin") val isWindowsHost = hostTag.startsWith("windows") val clangSuffix = if (isWindowsHost) ".cmd" else ""