Skip to content

Commit

Permalink
Add env var EXTRA_CLANG_ARGS_<TARGET>
Browse files Browse the repository at this point in the history
Closes #2009
  • Loading branch information
Orycterope committed Apr 14, 2021
1 parent d0d0726 commit 4b441ce
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -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_<TARGET>`: 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].
Expand Down
8 changes: 8 additions & 0 deletions build.rs
Expand Up @@ -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("-", "_")
);
}
13 changes: 11 additions & 2 deletions src/lib.rs
Expand Up @@ -1404,8 +1404,17 @@ impl Builder {
/// Generate the Rust bindings using the options built up thus far.
pub fn generate(mut self) -> Result<Bindings, ()> {
// 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) {
Expand Down

0 comments on commit 4b441ce

Please sign in to comment.