Skip to content

Commit c39c47c

Browse files
Orycteropeemilio
authored andcommitted
Add env var EXTRA_CLANG_ARGS_<TARGET>
Closes rust-lang#2009
1 parent d0d0726 commit c39c47c

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ End-users should set these environment variables to modify `bindgen`'s behavior
6060
- Examples:
6161
- Specify alternate sysroot: `--sysroot=/path/to/sysroot`
6262
- Add include search path with spaces: `-I"/path/with spaces"`
63+
- `BINDGEN_EXTRA_CLANG_ARGS_<TARGET>`: similar to `BINDGEN_EXTRA_CLANG_ARGS`,
64+
but used to set per-target arguments to pass to clang. Useful to set system include
65+
directories in a target-specific way in cross-compilation environments with multiple targets.
66+
Has precedence over `BINDGEN_EXTRA_CLANG_ARGS`.
6367

6468
Additionally, `bindgen` uses `libclang` to parse C and C++ header files.
6569
To modify how `bindgen` searches for `libclang`, see the [`clang-sys` documentation][clang-sys-env].

build.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,12 @@ fn main() {
7979
println!("cargo:rerun-if-env-changed=LIBCLANG_PATH");
8080
println!("cargo:rerun-if-env-changed=LIBCLANG_STATIC_PATH");
8181
println!("cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS");
82+
println!(
83+
"cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_{}",
84+
std::env::var("TARGET").unwrap()
85+
);
86+
println!(
87+
"cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_{}",
88+
std::env::var("TARGET").unwrap().replace("-", "_")
89+
);
8290
}

src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,7 @@ impl Builder {
14051405
pub fn generate(mut self) -> Result<Bindings, ()> {
14061406
// Add any extra arguments from the environment to the clang command line.
14071407
if let Some(extra_clang_args) =
1408-
env::var("BINDGEN_EXTRA_CLANG_ARGS").ok()
1408+
get_target_dependent_env_var("BINDGEN_EXTRA_CLANG_ARGS")
14091409
{
14101410
// Try to parse it with shell quoting. If we fail, make it one single big argument.
14111411
if let Some(strings) = shlex::split(&extra_clang_args) {
@@ -2557,6 +2557,21 @@ pub fn clang_version() -> ClangVersion {
25572557
}
25582558
}
25592559

2560+
/// Looks for the env var `var_${TARGET}`, and falls back to just `var` when it is not found.
2561+
fn get_target_dependent_env_var(var: &str) -> Option<String> {
2562+
if let Ok(target) = env::var("TARGET") {
2563+
if let Ok(v) = env::var(&format!("{}_{}", var, target)) {
2564+
return Some(v);
2565+
}
2566+
if let Ok(v) =
2567+
env::var(&format!("{}_{}", var, target.replace("-", "_")))
2568+
{
2569+
return Some(v);
2570+
}
2571+
}
2572+
env::var(var).ok()
2573+
}
2574+
25602575
/// A ParseCallbacks implementation that will act on file includes by echoing a rerun-if-changed
25612576
/// line
25622577
///

0 commit comments

Comments
 (0)