Skip to content

Commit

Permalink
fixup! chore(ffi): avoid hardcoding clang version
Browse files Browse the repository at this point in the history
Address review comments
  • Loading branch information
richvdh committed Dec 11, 2024
1 parent fd2e1fe commit 923fe51
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
12 changes: 7 additions & 5 deletions bindings/matrix-sdk-crypto-ffi/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{env, error::Error};
use std::ffi::OsStr;
use std::path::PathBuf;
use std::process::Command;
use vergen::EmitBuilder;
Expand All @@ -17,24 +16,27 @@ fn setup_x86_64_android_workaround() {
// Configure rust to statically link against the `libclang_rt.builtins` supplied with clang.

// cargo-ndk sets CC_x86_64-linux-android to the path to `clang`, within the Android NDK.
let cc = PathBuf::from(env::var("CC_x86_64-linux-android").expect("CC_x86_64-linux-android not set"));
let clang_path = PathBuf::from(env::var("CC_x86_64-linux-android").expect("CC_x86_64-linux-android not set"));

let toolchain_path = cc
// clang_path should now look something like
// `.../sdk/ndk/28.0.12674087/toolchains/llvm/prebuilt/linux-x86_64/bin/clang`.
// We strip `/bin/clang` from the end to get the toolchain path.
let toolchain_path = clang_path
.ancestors()
.nth(2)
.expect("could not find NDK toolchain path")
.to_str()
.expect("NDK toolchain path is not valid UTF-8");

let clang_version = get_clang_major_version(&cc);
let clang_version = get_clang_major_version(&clang_path);

println!("cargo:rustc-link-search={toolchain_path}/lib/clang/{clang_version}/lib/linux/");
println!("cargo:rustc-link-lib=static=clang_rt.builtins-x86_64-android");
}
}

/// Run the clang binary at `clang_path`, and return its major version number
fn get_clang_major_version(clang_path: impl AsRef<OsStr>) -> String {
fn get_clang_major_version(clang_path: &PathBuf) -> String {
let clang_output = Command::new(clang_path)
.arg("-dumpversion")
.output()
Expand Down
12 changes: 7 additions & 5 deletions bindings/matrix-sdk-ffi/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{env, error::Error};
use std::ffi::OsStr;
use std::path::PathBuf;
use std::process::Command;
use vergen::EmitBuilder;
Expand All @@ -17,24 +16,27 @@ fn setup_x86_64_android_workaround() {
// Configure rust to statically link against the `libclang_rt.builtins` supplied with clang.

// cargo-ndk sets CC_x86_64-linux-android to the path to `clang`, within the Android NDK.
let cc = PathBuf::from(env::var("CC_x86_64-linux-android").expect("CC_x86_64-linux-android not set"));
let clang_path = PathBuf::from(env::var("CC_x86_64-linux-android").expect("CC_x86_64-linux-android not set"));

let toolchain_path = cc
// clang_path should now look something like
// `.../sdk/ndk/28.0.12674087/toolchains/llvm/prebuilt/linux-x86_64/bin/clang`.
// We strip `/bin/clang` from the end to get the toolchain path.
let toolchain_path = clang_path
.ancestors()
.nth(2)
.expect("could not find NDK toolchain path")
.to_str()
.expect("NDK toolchain path is not valid UTF-8");

let clang_version = get_clang_major_version(&cc);
let clang_version = get_clang_major_version(&clang_path);

println!("cargo:rustc-link-search={toolchain_path}/lib/clang/{clang_version}/lib/linux/");
println!("cargo:rustc-link-lib=static=clang_rt.builtins-x86_64-android");
}
}

/// Run the clang binary at `clang_path`, and return its major version number
fn get_clang_major_version(clang_path: impl AsRef<OsStr>) -> String {
fn get_clang_major_version(clang_path: &PathBuf) -> String {
let clang_output = Command::new(clang_path)
.arg("-dumpversion")
.output()
Expand Down

0 comments on commit 923fe51

Please sign in to comment.