@@ -1370,6 +1370,11 @@ 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 predefined `__ANDROID__`
1375
+ cmd. push_opt_unless_duplicate ( "-DANDROID" . into ( ) ) ;
1376
+ }
1377
+
1373
1378
if !target. contains ( "-ios" ) {
1374
1379
cmd. push_cc_arg ( "-ffunction-sections" . into ( ) ) ;
1375
1380
cmd. push_cc_arg ( "-fdata-sections" . into ( ) ) ;
@@ -1406,7 +1411,12 @@ impl Build {
1406
1411
// Target flags
1407
1412
match cmd. family {
1408
1413
ToolFamily :: Clang => {
1409
- cmd. args . push ( format ! ( "--target={}" , target) . into ( ) ) ;
1414
+ // clang from Android NDK target names are different from rustc.
1415
+ // For example, armv7a-linux-androideabi16-clang passes
1416
+ // --target=armv7a-linux-androideabi16 to clang.
1417
+ if !target. contains ( "android" ) || !new_clang_android_compiler ( & cmd. path ) {
1418
+ cmd. args . push ( format ! ( "--target={}" , target) . into ( ) ) ;
1419
+ }
1410
1420
}
1411
1421
ToolFamily :: Msvc { clang_cl } => {
1412
1422
// This is an undocumented flag from MSVC but helps with making
@@ -1974,29 +1984,7 @@ impl Build {
1974
1984
format ! ( "{}.exe" , gnu)
1975
1985
}
1976
1986
} 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
- }
1987
+ autodetect_android_compiler ( & target, & host, gnu, clang)
2000
1988
} else if target. contains ( "cloudabi" ) {
2001
1989
format ! ( "{}-{}" , target, traditional)
2002
1990
} else if target == "wasm32-wasi"
@@ -2722,3 +2710,64 @@ fn command_add_output_file(
2722
2710
cmd. arg ( "-o" ) . arg ( & dst) ;
2723
2711
}
2724
2712
}
2713
+
2714
+ static NEW_STANDALONE_ANDROID_COMPILERS : [ & str ; 4 ] = [
2715
+ "aarch64-linux-android21-clang" ,
2716
+ "armv7a-linux-androideabi16-clang" ,
2717
+ "i686-linux-android16-clang" ,
2718
+ "x86_64-linux-android21-clang" ,
2719
+ ] ;
2720
+
2721
+ fn new_clang_android_compiler ( clang_path : & Path ) -> bool {
2722
+ NEW_STANDALONE_ANDROID_COMPILERS . iter ( ) . any ( |x| {
2723
+ let x: & OsStr = x. as_ref ( ) ;
2724
+ x == clang_path. as_os_str ( )
2725
+ } )
2726
+ }
2727
+
2728
+ fn autodetect_android_compiler ( target : & str , host : & str , gnu : & str , clang : & str ) -> String {
2729
+ let new_clang_key = match target {
2730
+ "aarch64-linux-android" => Some ( "aarch64" ) ,
2731
+ "armv7-linux-androideabi" => Some ( "armv7a" ) ,
2732
+ "i686-linux-android" => Some ( "i686" ) ,
2733
+ "x86_64-linux-android" => Some ( "x86_64" ) ,
2734
+ _ => None ,
2735
+ } ;
2736
+
2737
+ let new_clang = new_clang_key
2738
+ . map ( |key| {
2739
+ NEW_STANDALONE_ANDROID_COMPILERS
2740
+ . iter ( )
2741
+ . find ( |x| x. starts_with ( key) )
2742
+ } )
2743
+ . unwrap_or ( None ) ;
2744
+
2745
+ if let Some ( new_clang) = new_clang {
2746
+ if Command :: new ( new_clang) . output ( ) . is_ok ( ) {
2747
+ return ( * new_clang) . into ( ) ;
2748
+ }
2749
+ }
2750
+
2751
+ let target = target
2752
+ . replace ( "armv7neon" , "arm" )
2753
+ . replace ( "armv7" , "arm" )
2754
+ . replace ( "thumbv7neon" , "arm" )
2755
+ . replace ( "thumbv7" , "arm" ) ;
2756
+ let gnu_compiler = format ! ( "{}-{}" , target, gnu) ;
2757
+ let clang_compiler = format ! ( "{}-{}" , target, clang) ;
2758
+
2759
+ // On Windows, the Android clang compiler is provided as a `.cmd` file instead
2760
+ // of a `.exe` file. `std::process::Command` won't run `.cmd` files unless the
2761
+ // `.cmd` is explicitly appended to the command name, so we do that here.
2762
+ let clang_compiler_cmd = format ! ( "{}-{}.cmd" , target, clang) ;
2763
+
2764
+ // Check if gnu compiler is present
2765
+ // if not, use clang
2766
+ if Command :: new ( & gnu_compiler) . output ( ) . is_ok ( ) {
2767
+ gnu_compiler
2768
+ } else if host. contains ( "windows" ) && Command :: new ( & clang_compiler_cmd) . output ( ) . is_ok ( ) {
2769
+ clang_compiler_cmd
2770
+ } else {
2771
+ clang_compiler
2772
+ }
2773
+ }
0 commit comments