Skip to content

Commit 2493539

Browse files
committed
cargo: Forward cflags to bindgen via BINDGEN_EXTRA_CLANG_ARGS
When a crate invokes `bindgen` through a `build.rs` script to generate Rust code files, it should be using the headers that target Android rather than system headers. The include paths for these are already set up in the `cflags` that we pass to `cc-rs` (for `clang`), and can be trivially reused for `bindgen`'s `clang`.
1 parent 2c967e1 commit 2493539

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

xbuild/src/cargo/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,24 @@ impl CargoBuild {
384384
fn cargo_target_env(&mut self, name: &str, value: &str) {
385385
if let Some(triple) = self.triple {
386386
let utarget = triple.replace('-', "_");
387-
let env = format!("CARGO_TARGET_{}_{}", &utarget, name);
387+
let env = format!("CARGO_TARGET_{utarget}_{name}");
388388
self.cmd.env(env.to_uppercase(), value);
389389
} else {
390390
self.cmd.env(name, value);
391391
}
392392
}
393393

394+
/// Configures a cargo target specific environment variable.
395+
fn bindgen_env(&mut self, value: &str) {
396+
if let Some(triple) = self.triple {
397+
let utarget = triple.replace('-', "_");
398+
let env = format!("BINDGEN_EXTRA_CLANG_ARGS_{utarget}");
399+
self.cmd.env(env, value);
400+
} else {
401+
self.cmd.env("BINDGEN_EXTRA_CLANG_ARGS", value);
402+
}
403+
}
404+
394405
/// Configures an environment variable for the `cc` crate.
395406
fn cc_triple_env(&mut self, name: &str, value: &str) {
396407
if let Some(triple) = self.triple {
@@ -465,6 +476,7 @@ impl CargoBuild {
465476

466477
pub fn exec(mut self) -> Result<()> {
467478
self.cargo_target_env("RUSTFLAGS", &self.rust_flags.clone());
479+
self.bindgen_env(&self.c_flags.clone());
468480
self.cc_triple_env("CFLAGS", &self.c_flags.clone());
469481
// These strings already end with a space if they're non-empty:
470482
self.cc_triple_env("CXXFLAGS", &format!("{}{}", self.c_flags, self.cxx_flags));

0 commit comments

Comments
 (0)