@@ -176,7 +176,9 @@ impl Config {
176
176
self
177
177
}
178
178
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!
180
182
pub fn no_build_target ( & mut self , no_build_target : bool ) -> & mut Config {
181
183
self . no_build_target = no_build_target;
182
184
self
@@ -275,6 +277,18 @@ impl Config {
275
277
self
276
278
}
277
279
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
+
278
292
/// Run this configuration, compiling the library with all the configured
279
293
/// options.
280
294
///
@@ -293,23 +307,30 @@ impl Config {
293
307
} ;
294
308
let host = self . host . clone ( ) . unwrap_or_else ( || getenv_unwrap ( "HOST" ) ) ;
295
309
let msvc = target. contains ( "msvc" ) ;
310
+ let ndk = self . uses_android_ndk ( ) ;
296
311
let mut c_cfg = cc:: Build :: new ( ) ;
297
312
c_cfg
298
313
. cargo_metadata ( false )
299
314
. opt_level ( 0 )
300
315
. debug ( false )
301
- . target ( & target)
302
316
. warnings ( false )
303
- . host ( & host) ;
317
+ . host ( & host)
318
+ . no_default_flags ( ndk) ;
319
+ if !ndk {
320
+ c_cfg. target ( & target) ;
321
+ }
304
322
let mut cxx_cfg = cc:: Build :: new ( ) ;
305
323
cxx_cfg
306
324
. cargo_metadata ( false )
307
325
. cpp ( true )
308
326
. opt_level ( 0 )
309
327
. debug ( false )
310
- . target ( & target)
311
328
. warnings ( false )
312
- . host ( & host) ;
329
+ . host ( & host)
330
+ . no_default_flags ( ndk) ;
331
+ if !ndk {
332
+ cxx_cfg. target ( & target) ;
333
+ }
313
334
if let Some ( static_crt) = self . static_crt {
314
335
c_cfg. static_crt ( static_crt) ;
315
336
cxx_cfg. static_crt ( static_crt) ;
0 commit comments