@@ -65,8 +65,14 @@ fn apply_patches(code_dir: &PathBuf) {
65
65
}
66
66
67
67
fn compile_lib ( code_dir : & Path ) {
68
- cc:: Build :: new ( )
69
- . compiler ( "clang" )
68
+ let mut builder = cc:: Build :: new ( ) ;
69
+
70
+ // android ndk has its own clang compiler, so we can't use the default one
71
+ if !is_cargo_ndk ( ) {
72
+ builder. compiler ( "clang" ) ;
73
+ }
74
+
75
+ builder
70
76
. files (
71
77
[
72
78
// extensions.c has included quickjs.c
@@ -105,16 +111,34 @@ fn compile_lib(code_dir: &Path) {
105
111
fn do_bindgen ( ) {
106
112
let out_path = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
107
113
108
- bindgen:: Builder :: default ( )
114
+ let builder = bindgen:: Builder :: default ( )
109
115
. header ( "embed/extensions.h" )
110
116
. allowlist_item ( "js_.+" )
111
117
. allowlist_item ( "JS.+" )
112
118
. clang_arg ( "-std=c11" )
113
- . clang_arg ( format ! ( "-I{}" , "embed/quickjs" ) )
119
+ . clang_arg ( format ! ( "-I{}" , "embed/quickjs" ) ) ;
120
+
121
+ // detect if we are cross-compiling for android using cargo-ndk
122
+ let builder = if is_cargo_ndk ( ) {
123
+ let target = env:: var ( "TARGET" ) . unwrap ( ) ;
124
+ let ndk_sysroot_path = env:: var ( "CARGO_NDK_SYSROOT_PATH" ) . unwrap ( ) ;
125
+ builder
126
+ . clang_arg ( format ! ( "--sysroot={ndk_sysroot_path}" ) )
127
+ . clang_arg ( format ! ( "--target={}" , target) )
128
+ } else {
129
+ builder
130
+ } ;
131
+
132
+ builder
114
133
. default_enum_style ( bindgen:: EnumVariation :: Consts { } )
115
134
. parse_callbacks ( Box :: new ( bindgen:: CargoCallbacks :: new ( ) ) )
116
135
. generate ( )
117
136
. expect ( "Unable to generate bindings" )
118
137
. write_to_file ( out_path. join ( "bindings.rs" ) )
119
138
. expect ( "Couldn't write bindings!" ) ;
120
139
}
140
+
141
+ fn is_cargo_ndk ( ) -> bool {
142
+ // cargo-ndk sets this variable so we use it to detect if we are cross-compiling for android
143
+ env:: var ( "CARGO_NDK_ANDROID_PLATFORM" ) . is_ok ( )
144
+ }
0 commit comments