@@ -1370,6 +1370,15 @@ impl Build {
1370
1370
cmd. push_opt_unless_duplicate ( format ! ( "-O{}" , opt_level) . into ( ) ) ;
1371
1371
}
1372
1372
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
+
1373
1382
if !target. contains ( "-ios" ) {
1374
1383
cmd. push_cc_arg ( "-ffunction-sections" . into ( ) ) ;
1375
1384
cmd. push_cc_arg ( "-fdata-sections" . into ( ) ) ;
@@ -1406,7 +1415,11 @@ impl Build {
1406
1415
// Target flags
1407
1416
match cmd. family {
1408
1417
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
+ }
1410
1423
}
1411
1424
ToolFamily :: Msvc { clang_cl } => {
1412
1425
// This is an undocumented flag from MSVC but helps with making
@@ -1974,29 +1987,7 @@ impl Build {
1974
1987
format ! ( "{}.exe" , gnu)
1975
1988
}
1976
1989
} 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)
2000
1991
} else if target. contains ( "cloudabi" ) {
2001
1992
format ! ( "{}-{}" , target, traditional)
2002
1993
} else if target == "wasm32-wasi"
@@ -2722,3 +2713,75 @@ fn command_add_output_file(
2722
2713
cmd. arg ( "-o" ) . arg ( & dst) ;
2723
2714
}
2724
2715
}
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