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