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 building on Android #112

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .cargo/config.toml
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this file get respected when frida-rust is used as a library, or only when running cargo build from this directory?

Copy link
Author

Choose a reason for hiding this comment

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

Only for local building and building of examples. In the case of using the crate as a library, an user must take care of creating this file in his own crate.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[target.x86_64-linux-android]
linker = "x86_64-linux-android26-clang"

[target.aarch64-linux-android]
linker = "aarch64-linux-android26-clang"
39 changes: 39 additions & 0 deletions examples/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::env;

/// Adds a temporary workaround for an issue with the Rust compiler and Android
/// in x86_64/aarch64 devices: https://github.com/rust-lang/rust/issues/109717.
/// The workaround comes from: https://github.com/mozilla/application-services/pull/5442
fn setup_android_workaround() {
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS not set");
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH not set");
if (target_arch == "x86_64" || target_arch == "aarch64") && target_os == "android" {
let android_ndk_home = env::var("ANDROID_NDK_HOME").expect("ANDROID_NDK_HOME not set");
let build_os = match env::consts::OS {
"linux" => "linux",
"macos" => "darwin",
"windows" => "windows",
_ => panic!(
"Unsupported OS. You must use either Linux, MacOS or Windows to build the crate."
),
};
// NDK r25c
const DEFAULT_CLANG_VERSION: &str = "14.0.7";
let clang_version =
env::var("NDK_CLANG_VERSION").unwrap_or_else(|_| DEFAULT_CLANG_VERSION.to_owned());
// Another workaround for NDK r26
let lib_path = if clang_version == "17" {
"lib"
} else {
"lib64"
};
let linux_x86_64_lib_dir = format!(
"toolchains/llvm/prebuilt/{build_os}-x86_64/{lib_path}/clang/{clang_version}/lib/linux/"
);
println!("cargo:rustc-link-search={android_ndk_home}/{linux_x86_64_lib_dir}");
println!("cargo:rustc-link-lib=static=clang_rt.builtins-{target_arch}-android");
}
}

fn main() {
setup_android_workaround();
}
1 change: 1 addition & 0 deletions examples/core/console_log/build.rs
1 change: 1 addition & 0 deletions examples/core/hello/build.rs
1 change: 1 addition & 0 deletions examples/gum/debug_symbol/build.rs
1 change: 1 addition & 0 deletions examples/gum/fast_interceptor/build.rs
1 change: 1 addition & 0 deletions examples/gum/stalker/build.rs
1 change: 1 addition & 0 deletions examples/gum/stalker_observer/build.rs
27 changes: 21 additions & 6 deletions frida-gum-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,27 @@ fn main() {
let bindings = bindings.clang_arg(format!("-I{include_dir}"));

#[cfg(not(feature = "auto-download"))]
let bindings = if std::env::var("DOCS_RS").is_ok() {
let bindings = if env::var("DOCS_RS").is_ok() {
bindings.clang_arg("-Iinclude")
} else {
bindings
};

#[cfg(target_os = "android")]
{
let android_ndk_home = env::var("ANDROID_NDK_HOME").expect("ANDROID_NDK_HOME not set");
let build_os = match env::consts::OS {
"linux" => "linux",
"macos" => "darwin",
"windows" => "windows",
_ => panic!(
"Unsupported OS. You must use either Linux, MacOS or Windows to build the crate."
),
};
let bindings = bindings.clang_arg(format!(
"--sysroot={android_ndk_home}/toolchains/llvm/prebuilt/{build_os}-x86_64/sysroot/"
));
}
let bindings = bindings
.header_contents("gum.h", "#include \"frida-gum.h\"")
.header("event_sink.h")
Expand Down Expand Up @@ -97,7 +112,7 @@ fn main() {
let mut builder = builder.include(include_dir.clone());

#[cfg(not(feature = "auto-download"))]
let builder = if std::env::var("DOCS_RS").is_ok() {
let builder = if env::var("DOCS_RS").is_ok() {
builder.include("include")
} else {
&mut builder
Expand All @@ -118,7 +133,7 @@ fn main() {
let mut builder = builder.include(include_dir.clone());

#[cfg(not(feature = "auto-download"))]
let builder = if std::env::var("DOCS_RS").is_ok() {
let builder = if env::var("DOCS_RS").is_ok() {
builder.include("include")
} else {
&mut builder
Expand All @@ -136,7 +151,7 @@ fn main() {
let mut builder = builder.include(include_dir.clone());

#[cfg(not(feature = "auto-download"))]
let builder = if std::env::var("DOCS_RS").is_ok() {
let builder = if env::var("DOCS_RS").is_ok() {
builder.include("include")
} else {
&mut builder
Expand All @@ -156,7 +171,7 @@ fn main() {
let mut builder = builder.include(include_dir.clone());

#[cfg(not(feature = "auto-download"))]
let builder = if std::env::var("DOCS_RS").is_ok() {
let builder = if env::var("DOCS_RS").is_ok() {
builder.include("include")
} else {
&mut builder
Expand All @@ -177,7 +192,7 @@ fn main() {
let mut builder = builder.include(include_dir);

#[cfg(not(feature = "auto-download"))]
let builder = if std::env::var("DOCS_RS").is_ok() {
let builder = if env::var("DOCS_RS").is_ok() {
builder.include("include")
} else {
&mut builder
Expand Down
2 changes: 1 addition & 1 deletion frida-gum-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod bindings {

pub use bindings::*;

#[cfg(not(any(target_os = "windows", target_os = "android", target_vendor = "apple",)))]
#[cfg(not(any(target_os = "windows", target_vendor = "apple")))]
pub use _frida_g_object_unref as g_object_unref;

/// A single disassembled CPU instruction.
Expand Down
Loading