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..aa9460a55 100644 --- a/build.rs +++ b/build.rs @@ -79,4 +79,6 @@ 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..630fb7bbc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1404,8 +1404,9 @@ 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(&format!("BINDGEN_EXTRA_CLANG_ARGS_{}", env::var("TARGET").unwrap())) + .or_else(|_| env::var(&format!("BINDGEN_EXTRA_CLANG_ARGS_{}", env::var("TARGET").unwrap().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) {