Skip to content

Commit d20f57b

Browse files
authored
Implement autodetection for default compiler from NDK (#495)
ref #459
1 parent d1a7b8e commit d20f57b

File tree

1 file changed

+87
-24
lines changed

1 file changed

+87
-24
lines changed

src/lib.rs

+87-24
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,15 @@ impl Build {
13701370
cmd.push_opt_unless_duplicate(format!("-O{}", opt_level).into());
13711371
}
13721372

1373+
if cmd.family == ToolFamily::Clang && target.contains("android") {
1374+
// For compatibility with code that doesn't use pre-defined `__ANDROID__` macro.
1375+
// If compiler used via ndk-build or cmake (officially supported build methods)
1376+
// this macros is defined.
1377+
// See https://android.googlesource.com/platform/ndk/+/refs/heads/ndk-release-r21/build/cmake/android.toolchain.cmake#456
1378+
// https://android.googlesource.com/platform/ndk/+/refs/heads/ndk-release-r21/build/core/build-binary.mk#141
1379+
cmd.push_opt_unless_duplicate("-DANDROID".into());
1380+
}
1381+
13731382
if !target.contains("-ios") {
13741383
cmd.push_cc_arg("-ffunction-sections".into());
13751384
cmd.push_cc_arg("-fdata-sections".into());
@@ -1406,7 +1415,11 @@ impl Build {
14061415
// Target flags
14071416
match cmd.family {
14081417
ToolFamily::Clang => {
1409-
cmd.args.push(format!("--target={}", target).into());
1418+
if !(target.contains("android")
1419+
&& android_clang_compiler_uses_target_arg_internally(&cmd.path))
1420+
{
1421+
cmd.args.push(format!("--target={}", target).into());
1422+
}
14101423
}
14111424
ToolFamily::Msvc { clang_cl } => {
14121425
// This is an undocumented flag from MSVC but helps with making
@@ -1974,29 +1987,7 @@ impl Build {
19741987
format!("{}.exe", gnu)
19751988
}
19761989
} else if target.contains("android") {
1977-
let target = target
1978-
.replace("armv7neon", "arm")
1979-
.replace("armv7", "arm")
1980-
.replace("thumbv7neon", "arm")
1981-
.replace("thumbv7", "arm");
1982-
let gnu_compiler = format!("{}-{}", target, gnu);
1983-
let clang_compiler = format!("{}-{}", target, clang);
1984-
// On Windows, the Android clang compiler is provided as a `.cmd` file instead
1985-
// of a `.exe` file. `std::process::Command` won't run `.cmd` files unless the
1986-
// `.cmd` is explicitly appended to the command name, so we do that here.
1987-
let clang_compiler_cmd = format!("{}-{}.cmd", target, clang);
1988-
1989-
// Check if gnu compiler is present
1990-
// if not, use clang
1991-
if Command::new(&gnu_compiler).output().is_ok() {
1992-
gnu_compiler
1993-
} else if host.contains("windows")
1994-
&& Command::new(&clang_compiler_cmd).output().is_ok()
1995-
{
1996-
clang_compiler_cmd
1997-
} else {
1998-
clang_compiler
1999-
}
1990+
autodetect_android_compiler(&target, &host, gnu, clang)
20001991
} else if target.contains("cloudabi") {
20011992
format!("{}-{}", target, traditional)
20021993
} else if target == "wasm32-wasi"
@@ -2722,3 +2713,75 @@ fn command_add_output_file(
27222713
cmd.arg("-o").arg(&dst);
27232714
}
27242715
}
2716+
2717+
// Use by default minimum available API level
2718+
// See note about naming here
2719+
// https://android.googlesource.com/platform/ndk/+/refs/heads/ndk-release-r21/docs/BuildSystemMaintainers.md#Clang
2720+
static NEW_STANDALONE_ANDROID_COMPILERS: [&str; 4] = [
2721+
"aarch64-linux-android21-clang",
2722+
"armv7a-linux-androideabi16-clang",
2723+
"i686-linux-android16-clang",
2724+
"x86_64-linux-android21-clang",
2725+
];
2726+
2727+
// New "standalone" C/C++ cross-compiler executables from recent Android NDK
2728+
// are just shell scripts that call main clang binary (from Android NDK) with
2729+
// proper `--target` argument.
2730+
//
2731+
// For example, armv7a-linux-androideabi16-clang passes
2732+
// `--target=armv7a-linux-androideabi16` to clang.
2733+
// So to construct proper command line check if
2734+
// `--target` argument would be passed or not to clang
2735+
fn android_clang_compiler_uses_target_arg_internally(clang_path: &Path) -> bool {
2736+
NEW_STANDALONE_ANDROID_COMPILERS.iter().any(|x| {
2737+
let x: &OsStr = x.as_ref();
2738+
x == clang_path.as_os_str()
2739+
})
2740+
}
2741+
2742+
fn autodetect_android_compiler(target: &str, host: &str, gnu: &str, clang: &str) -> String {
2743+
let new_clang_key = match target {
2744+
"aarch64-linux-android" => Some("aarch64"),
2745+
"armv7-linux-androideabi" => Some("armv7a"),
2746+
"i686-linux-android" => Some("i686"),
2747+
"x86_64-linux-android" => Some("x86_64"),
2748+
_ => None,
2749+
};
2750+
2751+
let new_clang = new_clang_key
2752+
.map(|key| {
2753+
NEW_STANDALONE_ANDROID_COMPILERS
2754+
.iter()
2755+
.find(|x| x.starts_with(key))
2756+
})
2757+
.unwrap_or(None);
2758+
2759+
if let Some(new_clang) = new_clang {
2760+
if Command::new(new_clang).output().is_ok() {
2761+
return (*new_clang).into();
2762+
}
2763+
}
2764+
2765+
let target = target
2766+
.replace("armv7neon", "arm")
2767+
.replace("armv7", "arm")
2768+
.replace("thumbv7neon", "arm")
2769+
.replace("thumbv7", "arm");
2770+
let gnu_compiler = format!("{}-{}", target, gnu);
2771+
let clang_compiler = format!("{}-{}", target, clang);
2772+
2773+
// On Windows, the Android clang compiler is provided as a `.cmd` file instead
2774+
// of a `.exe` file. `std::process::Command` won't run `.cmd` files unless the
2775+
// `.cmd` is explicitly appended to the command name, so we do that here.
2776+
let clang_compiler_cmd = format!("{}-{}.cmd", target, clang);
2777+
2778+
// Check if gnu compiler is present
2779+
// if not, use clang
2780+
if Command::new(&gnu_compiler).output().is_ok() {
2781+
gnu_compiler
2782+
} else if host.contains("windows") && Command::new(&clang_compiler_cmd).output().is_ok() {
2783+
clang_compiler_cmd
2784+
} else {
2785+
clang_compiler
2786+
}
2787+
}

0 commit comments

Comments
 (0)