7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | use std::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | env, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | error::Error, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | path::{Path, PathBuf}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | process::Command, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | use vergen_gitcl::{Emitter, GitclBuilder}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | /// Adds a temporary workaround for an issue with the Rust compiler and Android |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | /// in x86_64 devices: https://github.com/rust-lang/rust/issues/109717. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | /// The workaround is based on: https://github.com/mozilla/application-services/pull/5442 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | /// TODO: I think this is obsolete with cargo-ndk 4.0.0: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | /// https://github.com/bbqsrc/cargo-ndk/commit/03c738653f935416580803b82fc6ad72c10dbebc |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | /// IMPORTANT: if you modify this, make sure to modify |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | /// [../matrix-sdk-ffi/build.rs] too! |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | fn setup_x86_64_android_workaround() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS not set"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH not set"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | if target_arch == "x86_64" && target_os == "android" { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | // Configure rust to statically link against the `libclang_rt.builtins` supplied |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | // with clang. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | // cargo-ndk sets CC_x86_64-linux-android to the path to `clang`, within the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | // Android NDK. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | let clang_path = PathBuf::from( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | env::var("CC_x86_64-linux-android").expect("CC_x86_64-linux-android not set"), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | ); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | // clang_path should now look something like |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | // `.../sdk/ndk/28.0.12674087/toolchains/llvm/prebuilt/linux-x86_64/bin/clang`. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | // We strip `/bin/clang` from the end to get the toolchain path. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | let toolchain_path = clang_path |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | .ancestors() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | .nth(2) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | .expect("could not find NDK toolchain path") |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | .to_str() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | .expect("NDK toolchain path is not valid UTF-8"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | let clang_version = get_clang_major_version(&clang_path); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | println!("cargo:rustc-link-search={toolchain_path}/lib/clang/{clang_version}/lib/linux/"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | println!("cargo:rustc-link-lib=static=clang_rt.builtins-x86_64-android"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | // Ensure we re-run this build script if the location of the toolchain changes. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | println!("cargo:rerun-if-env-changed=CC_x86_64-linux-android"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | /// Run the clang binary at `clang_path`, and return its major version number |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | fn get_clang_major_version(clang_path: &Path) -> String { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | let clang_output = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | Command::new(clang_path).arg("-dumpversion").output().expect("failed to start clang"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | if !clang_output.status.success() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | panic!("failed to run clang: {}", String::from_utf8_lossy(&clang_output.stderr)); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | let clang_version = String::from_utf8(clang_output.stdout).expect("clang output is not utf8"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | clang_version.split('.').next().expect("could not parse clang output").to_owned() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | fn main() -> Result<(), Box<dyn Error>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | setup_x86_64_android_workaround(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | let git_config = GitclBuilder::default().sha(true).describe(true, false, None).build()?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | Emitter::default().add_instructions(&git_config)?.emit()?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 71 | Ok(()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 72 | } |