Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support android #88

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions rust-bindings/bls-dash-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn handle_command_output(output: Output) {
#[cfg(not(feature = "apple"))]
fn main() {
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();

// TODO: fix build for wasm32 on MacOS
// errors with `error: linking with `rust-lld` failed: exit status: 1`
Expand Down Expand Up @@ -63,12 +64,48 @@ fn main() {

fs::create_dir_all(&bls_dash_build_path).expect("can't create build directory");

let cmake_output = create_cross_cmake_command()

let cmake_command_binding = create_cross_cmake_command();
let mut cmake_command = cmake_command_binding;

cmake_command
.current_dir(&bls_dash_build_path)
.arg("-DBUILD_BLS_PYTHON_BINDINGS=0")
.arg("-DBUILD_BLS_TESTS=0")
.arg("-DBUILD_BLS_BENCHMARKS=0")
.arg("-DBUILD_BLS_JS_BINDINGS=0")
.arg("-DBUILD_BLS_JS_BINDINGS=0");

// configure CMake for Android
if target_os.eq("android") {
let cmake_toolchain_path = env::var("CMAKE_TOOLCHAIN_FILE")
.or_else(|_| env::var("CARGO_NDK_CMAKE_TOOLCHAIN_PATH"))
.expect("Neither CMAKE_TOOLCHAIN_FILE nor CARGO_NDK_CMAKE_TOOLCHAIN_PATH environment variables are set");

let ndk_target = match env::var("CARGO_NDK_TARGET_ARCH") {
Ok(value) => value, // If set, use the value directly.
Err(_) => {
match target_arch.as_str() {
"aarch64" => "arm64-v8a".to_string(),
"arm" => "armeabi-v7a".to_string(),
"x86" => "x86".to_string(),
"x86_64" => "x86_64".to_string(),
_ => panic!("Unsupported target architecture for Android: {}", target_arch),
}
}
};

// Default to android-24 if ANDROID_PLATFORM is not specified
let android_abi = env::var("ANDROID_PLATFORM")
.or_else(|_| env::var("CARGO_NDK_ANDROID_PLATFORM"))
.unwrap_or_else(|_| "android-24".to_string());

cmake_command
.arg(format!("-DANDROID_PLATFORM={}", android_abi))
.arg(format!("-DANDROID_ABI={}", ndk_target))
.arg(format!("-DCMAKE_TOOLCHAIN_FILE={}", cmake_toolchain_path));
Comment on lines +102 to +105
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To support android, we need to specify the platform version (default will be 24, which is Android 7.0) and the ABI which will be one of the 4 specified by the root build process. Lastly, the toolchain is set to the Android toolchain.

}

let cmake_output = cmake_command
.arg("..")
.output()
.expect("can't run cmake");
Expand Down Expand Up @@ -127,7 +164,8 @@ fn main() {
cc.files(cpp_files)
.includes(&include_paths)
.cpp(true)
.flag_if_supported("-std=c++14");
.flag_if_supported("-std=c++14")
.target(&env::var("TARGET").unwrap());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the cc crate does support using the correct compiler based on the CC_* and AR_* environment variables, but it needs to know the target.


let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();

Expand Down
Loading