From 4b441cea9ff585a444864ce3d61d689976af69a3 Mon Sep 17 00:00:00 2001 From: Thomas Vermeilh Date: Wed, 14 Apr 2021 15:17:02 +0200 Subject: [PATCH] Add env var EXTRA_CLANG_ARGS_ Closes #2009 --- README.md | 4 ++++ build.rs | 8 ++++++++ src/lib.rs | 13 +++++++++++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e1ad557e9..60e064ffb 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,10 @@ End-users should set these environment variables to modify `bindgen`'s behavior - Examples: - Specify alternate sysroot: `--sysroot=/path/to/sysroot` - Add include search path with spaces: `-I"/path/with spaces"` +- `BINDGEN_EXTRA_CLANG_ARGS_`: similar to `BINDGEN_EXTRA_CLANG_ARGS`, + but used to set per-target arguments to pass to clang. Useful to set system include + directories in a target-specific way in cross-compilation environments with multiple targets. + Has precedence over `BINDGEN_EXTRA_CLANG_ARGS`. Additionally, `bindgen` uses `libclang` to parse C and C++ header files. To modify how `bindgen` searches for `libclang`, see the [`clang-sys` documentation][clang-sys-env]. diff --git a/build.rs b/build.rs index cb40cb0c1..fcc0bb221 100644 --- a/build.rs +++ b/build.rs @@ -79,4 +79,12 @@ fn main() { println!("cargo:rerun-if-env-changed=LIBCLANG_PATH"); println!("cargo:rerun-if-env-changed=LIBCLANG_STATIC_PATH"); println!("cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS"); + println!( + "cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_{}", + std::env::var("TARGET").unwrap() + ); + println!( + "cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_{}", + std::env::var("TARGET").unwrap().replace("-", "_") + ); } diff --git a/src/lib.rs b/src/lib.rs index 85d555992..385a0b2be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1404,8 +1404,17 @@ impl Builder { /// Generate the Rust bindings using the options built up thus far. pub fn generate(mut self) -> Result { // Add any extra arguments from the environment to the clang command line. - if let Some(extra_clang_args) = - env::var("BINDGEN_EXTRA_CLANG_ARGS").ok() + if let Ok(extra_clang_args) = env::var("TARGET") + .and_then(|target| { + env::var(&format!("BINDGEN_EXTRA_CLANG_ARGS_{}", target)) + .or_else(|_| { + env::var(&format!( + "BINDGEN_EXTRA_CLANG_ARGS_{}", + target.replace("-", "_") + )) + }) + }) + .or_else(|_| env::var("BINDGEN_EXTRA_CLANG_ARGS")) { // Try to parse it with shell quoting. If we fail, make it one single big argument. if let Some(strings) = shlex::split(&extra_clang_args) {