Skip to content

Commit abf594d

Browse files
francesca64alexcrichton
authored andcommitted
Don't use default target triple when building with NDK (#85)
* Don't use default target triple when building with NDK * Apply review feedback * Simpler toolchain file name check
1 parent 3862101 commit abf594d

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ A build dependency for running `cmake` to build a native library
1414
categories = ["development-tools::build-utils"]
1515

1616
[dependencies]
17-
cc = "1.0.32"
17+
cc = { git = "https://github.com/alexcrichton/cc-rs" }

src/lib.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ impl Config {
176176
self
177177
}
178178

179-
/// Disables the target option for this compilation.
179+
/// Disables the cmake target option for this compilation.
180+
///
181+
/// Note that this isn't related to the target triple passed to the compiler!
180182
pub fn no_build_target(&mut self, no_build_target: bool) -> &mut Config {
181183
self.no_build_target = no_build_target;
182184
self
@@ -275,6 +277,18 @@ impl Config {
275277
self
276278
}
277279

280+
// Simple heuristic to determine if we're cross-compiling using the Android
281+
// NDK toolchain file.
282+
fn uses_android_ndk(&self) -> bool {
283+
// `ANDROID_ABI` is the only required flag:
284+
// https://developer.android.com/ndk/guides/cmake#android_abi
285+
self.defined("ANDROID_ABI")
286+
&& self.defines.iter().any(|(flag, value)| {
287+
flag == "CMAKE_TOOLCHAIN_FILE"
288+
&& Path::new(value).file_name() == Some("android.toolchain.cmake".as_ref())
289+
})
290+
}
291+
278292
/// Run this configuration, compiling the library with all the configured
279293
/// options.
280294
///
@@ -293,23 +307,30 @@ impl Config {
293307
};
294308
let host = self.host.clone().unwrap_or_else(|| getenv_unwrap("HOST"));
295309
let msvc = target.contains("msvc");
310+
let ndk = self.uses_android_ndk();
296311
let mut c_cfg = cc::Build::new();
297312
c_cfg
298313
.cargo_metadata(false)
299314
.opt_level(0)
300315
.debug(false)
301-
.target(&target)
302316
.warnings(false)
303-
.host(&host);
317+
.host(&host)
318+
.no_default_flags(ndk);
319+
if !ndk {
320+
c_cfg.target(&target);
321+
}
304322
let mut cxx_cfg = cc::Build::new();
305323
cxx_cfg
306324
.cargo_metadata(false)
307325
.cpp(true)
308326
.opt_level(0)
309327
.debug(false)
310-
.target(&target)
311328
.warnings(false)
312-
.host(&host);
329+
.host(&host)
330+
.no_default_flags(ndk);
331+
if !ndk {
332+
cxx_cfg.target(&target);
333+
}
313334
if let Some(static_crt) = self.static_crt {
314335
c_cfg.static_crt(static_crt);
315336
cxx_cfg.static_crt(static_crt);

0 commit comments

Comments
 (0)