@@ -1917,6 +1917,17 @@ pub struct Bindings {
1917
1917
pub ( crate ) const HOST_TARGET : & ' static str =
1918
1918
include_str ! ( concat!( env!( "OUT_DIR" ) , "/host-target.txt" ) ) ;
1919
1919
1920
+ // Some architecture triplets are different between rust and libclang, see #1211
1921
+ // and duplicates.
1922
+ fn rust_to_clang_target ( rust_target : & str ) -> String {
1923
+ if rust_target. starts_with ( "aarch64-apple-" ) {
1924
+ let mut clang_target = "arm64-apple-" . to_owned ( ) ;
1925
+ clang_target. push_str ( & rust_target[ "aarch64-apple-" . len ( ) ..] ) ;
1926
+ return clang_target;
1927
+ }
1928
+ rust_target. to_owned ( )
1929
+ }
1930
+
1920
1931
/// Returns the effective target, and whether it was explicitly specified on the
1921
1932
/// clang flags.
1922
1933
fn find_effective_target ( clang_args : & [ String ] ) -> ( String , bool ) {
@@ -1937,10 +1948,10 @@ fn find_effective_target(clang_args: &[String]) -> (String, bool) {
1937
1948
1938
1949
// If we're running from a build script, try to find the cargo target.
1939
1950
if let Ok ( t) = env:: var ( "TARGET" ) {
1940
- return ( t , false ) ;
1951
+ return ( rust_to_clang_target ( & t ) , false ) ;
1941
1952
}
1942
1953
1943
- ( HOST_TARGET . to_owned ( ) , false )
1954
+ ( rust_to_clang_target ( HOST_TARGET ) , false )
1944
1955
}
1945
1956
1946
1957
impl Bindings {
@@ -1963,12 +1974,15 @@ impl Bindings {
1963
1974
let ( effective_target, explicit_target) =
1964
1975
find_effective_target ( & options. clang_args ) ;
1965
1976
1966
- // NOTE: The effective_target == HOST_TARGET check wouldn't be sound
1967
- // normally in some cases if we were to call a binary (if you have a
1968
- // 32-bit clang and are building on a 64-bit system for example).
1969
- // But since we rely on opening libclang.so, it has to be the same
1970
- // architecture and thus the check is fine.
1971
- if !( explicit_target || effective_target == HOST_TARGET ) {
1977
+ let is_host_build =
1978
+ rust_to_clang_target ( HOST_TARGET ) == effective_target;
1979
+
1980
+ // NOTE: The is_host_build check wouldn't be sound normally in some
1981
+ // cases if we were to call a binary (if you have a 32-bit clang and are
1982
+ // building on a 64-bit system for example). But since we rely on
1983
+ // opening libclang.so, it has to be the same architecture and thus the
1984
+ // check is fine.
1985
+ if !explicit_target && !is_host_build {
1972
1986
options
1973
1987
. clang_args
1974
1988
. insert ( 0 , format ! ( "--target={}" , effective_target) ) ;
@@ -2088,17 +2102,14 @@ impl Bindings {
2088
2102
let time_phases = options. time_phases ;
2089
2103
let mut context = BindgenContext :: new ( options) ;
2090
2104
2091
- #[ cfg( debug_assertions) ]
2092
- {
2093
- if effective_target == HOST_TARGET {
2094
- assert_eq ! (
2095
- context. target_pointer_size( ) ,
2096
- std:: mem:: size_of:: <* mut ( ) >( ) ,
2097
- "{:?} {:?}" ,
2098
- effective_target,
2099
- HOST_TARGET
2100
- ) ;
2101
- }
2105
+ if is_host_build {
2106
+ debug_assert_eq ! (
2107
+ context. target_pointer_size( ) ,
2108
+ std:: mem:: size_of:: <* mut ( ) >( ) ,
2109
+ "{:?} {:?}" ,
2110
+ effective_target,
2111
+ HOST_TARGET
2112
+ ) ;
2102
2113
}
2103
2114
2104
2115
{
@@ -2109,7 +2120,7 @@ impl Bindings {
2109
2120
let ( items, options) = codegen:: codegen ( context) ;
2110
2121
2111
2122
Ok ( Bindings {
2112
- options : options ,
2123
+ options,
2113
2124
module : quote ! {
2114
2125
#( #items ) *
2115
2126
} ,
0 commit comments