Skip to content

Generate bindings compatible with current rustc version by default #3197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions bindgen/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,66 @@ impl RustTarget {

impl Default for RustTarget {
fn default() -> Self {
// Bindgen from build script: default to generating bindings compatible
// with the Rust version currently performing this build.
#[cfg(not(feature = "__cli"))]
{
use std::env;
use std::iter;
use std::process::Command;
use std::sync::OnceLock;

static CURRENT_RUST: OnceLock<Option<RustTarget>> = OnceLock::new();

if let Some(current_rust) = *CURRENT_RUST.get_or_init(|| {
let is_build_script =
env::var_os("CARGO_CFG_TARGET_ARCH").is_some();
if !is_build_script {
return None;
}

let rustc = env::var_os("RUSTC")?;
let rustc_wrapper = env::var_os("RUSTC_WRAPPER")
.filter(|wrapper| !wrapper.is_empty());
let wrapped_rustc =
rustc_wrapper.iter().chain(iter::once(&rustc));

let mut is_clippy_driver = false;
loop {
let mut wrapped_rustc = wrapped_rustc.clone();
let mut command =
Command::new(wrapped_rustc.next().unwrap());
command.args(wrapped_rustc);
if is_clippy_driver {
command.arg("--rustc");
}
command.arg("--version");

let output = command.output().ok()?;
let string = String::from_utf8(output.stdout).ok()?;

// Version string like "rustc 1.100.0-beta.5 (f0e1d2c3b 2026-10-17)"
let last_line = string.lines().last().unwrap_or(&string);
let (program, rest) = last_line.trim().split_once(' ')?;
if program != "rustc" {
if program.starts_with("clippy") && !is_clippy_driver {
is_clippy_driver = true;
continue;
}
return None;
}

let number = rest.split([' ', '-', '+']).next()?;
break RustTarget::from_str(number).ok();
}
}) {
return current_rust;
}
}

// Bindgen from CLI, or cannot determine compiler version: default to
// generating bindings compatible with the latest stable release of Rust
// that Bindgen knows about.
LATEST_STABLE_RUST
}
}
Expand Down
Loading