From a4189d0e201cb366b70cfa883b54c24a65343d68 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 | 17 ++++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) 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..418811e3d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1405,7 +1405,7 @@ impl Builder { 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() + get_target_dependent_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) { @@ -2557,6 +2557,21 @@ pub fn clang_version() -> ClangVersion { } } +/// Looks for the env var `var_${TARGET}`, and falls back to just `var` when it is not found. +fn get_target_dependent_env_var(var: &str) -> Option { + if let Ok(target) = env::var("TARGET") { + if let Ok(v) = env::var(&format!("{}_{}", var, target)) { + return Some(v); + } + if let Ok(v) = + env::var(&format!("{}_{}", var, target.replace("-", "_"))) + { + return Some(v); + } + } + env::var(var).ok() +} + /// A ParseCallbacks implementation that will act on file includes by echoing a rerun-if-changed /// line ///