Fix android build on arm macs (#35516)

The prebuilt directory only contains an `darwin-x86_64`
toolchain, but that is perfectly fine, since that also
works.
In General, if there is only one prebuilt toolchain available,
it should be a very safe assumption that it is usable on  the host
platform, especially if the OS matches.

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
This commit is contained in:
Jonathan Schwender 2025-02-18 19:47:11 +01:00 committed by GitHub
parent 7d2437762f
commit b57eba2919
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -149,18 +149,33 @@ class AndroidTarget(CrossBuildTarget):
if os_type not in ["linux", "darwin"]:
raise Exception("Android cross builds are only supported on Linux and macOS.")
llvm_prebuilt = path.join(env['ANDROID_NDK_ROOT'], "toolchains", "llvm", "prebuilt")
cpu_type = platform.machine().lower()
host_suffix = "unknown"
if cpu_type in ["i386", "i486", "i686", "i768", "x86"]:
host_suffix = "x86"
elif cpu_type in ["x86_64", "x86-64", "x64", "amd64"]:
host_suffix = "x86_64"
else:
available_prebuilts = os.listdir(llvm_prebuilt)
available_prebuilts = [prebuilt for prebuilt in available_prebuilts if prebuilt.startswith(os_type)]
# If there is only one prebuilt option available, it's probably the right one for the host
# platform. E.g. on Arm macs, only the x86 prebuilts are available, buts that perfectly fine,
# since there is rosetta.
if len(available_prebuilts) == 1:
host_suffix = available_prebuilts[0].removeprefix(f"{os_type}-")
else:
print(f"Error: Can't determine LLVM prebuilt. Unknown cpu type {cpu_type}.")
print(f"Hint: The LLVM prebuilts folder contains the following entries: {available_prebuilts}")
print("Please open an issue with the above information")
raise Exception("Can't determine LLVM prebuilt directory.")
host = os_type + "-" + host_suffix
host_cc = env.get('HOST_CC') or shutil.which("clang")
host_cxx = env.get('HOST_CXX') or shutil.which("clang++")
llvm_toolchain = path.join(env['ANDROID_NDK_ROOT'], "toolchains", "llvm", "prebuilt", host)
llvm_toolchain = path.join(llvm_prebuilt, host)
env['PATH'] = (env['PATH'] + ':' + path.join(llvm_toolchain, "bin"))
def to_ndk_bin(prog):