Skip to content

Commit e1ec636

Browse files
committed
Generate bindings compatible with current rustc version by default
1 parent 97ab915 commit e1ec636

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

bindgen/features.rs

+60
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,66 @@ impl RustTarget {
4848

4949
impl Default for RustTarget {
5050
fn default() -> Self {
51+
// Bindgen from build script: default to generating bindings compatible
52+
// with the Rust version currently performing this build.
53+
#[cfg(not(feature = "__cli"))]
54+
{
55+
use std::env;
56+
use std::iter;
57+
use std::process::Command;
58+
use std::sync::OnceLock;
59+
60+
static CURRENT_RUST: OnceLock<Option<RustTarget>> = OnceLock::new();
61+
62+
if let Some(current_rust) = *CURRENT_RUST.get_or_init(|| {
63+
let is_build_script =
64+
env::var_os("CARGO_CFG_TARGET_ARCH").is_some();
65+
if !is_build_script {
66+
return None;
67+
}
68+
69+
let rustc = env::var_os("RUSTC")?;
70+
let rustc_wrapper = env::var_os("RUSTC_WRAPPER")
71+
.filter(|wrapper| !wrapper.is_empty());
72+
let wrapped_rustc =
73+
rustc_wrapper.iter().chain(iter::once(&rustc));
74+
75+
let mut is_clippy_driver = false;
76+
loop {
77+
let mut wrapped_rustc = wrapped_rustc.clone();
78+
let mut command =
79+
Command::new(wrapped_rustc.next().unwrap());
80+
command.args(wrapped_rustc);
81+
if is_clippy_driver {
82+
command.arg("--rustc");
83+
}
84+
command.arg("--version");
85+
86+
let output = command.output().ok()?;
87+
let string = String::from_utf8(output.stdout).ok()?;
88+
89+
// Version string like "rustc 1.100.0-beta.5 (f0e1d2c3b 2026-10-17)"
90+
let last_line = string.lines().last().unwrap_or(&string);
91+
let (program, rest) = last_line.trim().split_once(' ')?;
92+
if program != "rustc" {
93+
if program.starts_with("clippy") && !is_clippy_driver {
94+
is_clippy_driver = true;
95+
continue;
96+
}
97+
return None;
98+
}
99+
100+
let number = rest.split([' ', '-', '+']).next()?;
101+
break RustTarget::from_str(number).ok();
102+
}
103+
}) {
104+
return current_rust;
105+
}
106+
}
107+
108+
// Bindgen from CLI, or cannot determine compiler version: default to
109+
// generating bindings compatible with the latest stable release of Rust
110+
// that Bindgen knows about.
51111
LATEST_STABLE_RUST
52112
}
53113
}

0 commit comments

Comments
 (0)