Skip to content
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

Use track_caller to make debugging bindgen build script errors easier #3383

Merged
merged 3 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
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
32 changes: 23 additions & 9 deletions crates/libs/bindgen/src/io.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
use std::io::BufRead;

#[track_caller]
pub fn read_file_lines(path: &str) -> Vec<String> {
let file = std::io::BufReader::new(
std::fs::File::open(path).unwrap_or_else(|_| panic!("failed to open file `{path}`")),
);
let Ok(file) = std::fs::File::open(path) else {
panic!("failed to open file `{path}`")
};

file.lines()
.map(|line| line.unwrap_or_else(|_| panic!("failed to read file lines `{path}`")))
.collect()
let file = std::io::BufReader::new(file);
let mut lines = vec![];

for line in file.lines() {
let Ok(line) = line else {
panic!("failed to read file lines `{path}`");
};

lines.push(line);
}

lines
}

#[track_caller]
pub fn write_to_file<C: AsRef<[u8]>>(path: &str, contents: C) {
if let Some(parent) = std::path::Path::new(path).parent() {
std::fs::create_dir_all(parent)
.unwrap_or_else(|_| panic!("failed to create directory `{path}`"));
if std::fs::create_dir_all(parent).is_err() {
panic!("failed to create directory `{path}`");
}
}

std::fs::write(path, contents).unwrap_or_else(|_| panic!("failed to write file `{path}`"));
if std::fs::write(path, contents).is_err() {
panic!("failed to write file `{path}`");
}
}
20 changes: 15 additions & 5 deletions crates/libs/bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,19 @@ enum ArgKind {
Derive,
}

#[track_caller]
fn expand_args<I, S>(args: I) -> Vec<String>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
// This function is needed to avoid a recursion limit in the Rust compiler.
#[track_caller]
fn from_string(result: &mut Vec<String>, value: &str) {
expand_args(result, value.split_whitespace().map(|arg| arg.to_string()))
}

#[track_caller]
fn expand_args<I, S>(result: &mut Vec<String>, args: I)
where
I: IntoIterator<Item = S>,
Expand Down Expand Up @@ -241,7 +244,9 @@ where
result
}

#[track_caller]
fn expand_input(input: &[&str]) -> Vec<File> {
#[track_caller]
fn expand_input(result: &mut Vec<String>, input: &str) {
let path = std::path::Path::new(input);

Expand Down Expand Up @@ -295,12 +300,17 @@ fn expand_input(input: &[&str]) -> Vec<File> {
.collect();
}

input.extend(paths.iter().map(|path| {
let bytes =
std::fs::read(path).unwrap_or_else(|_| panic!("failed to read binary file `{path}`"));
for path in &paths {
let Ok(bytes) = std::fs::read(path) else {
panic!("failed to read binary file `{path}`");
};

File::new(bytes).unwrap_or_else(|| panic!("failed to read .winmd format `{path}`"))
}));
let Some(file) = File::new(bytes) else {
panic!("failed to read .winmd format `{path}`");
};

input.push(file);
}

input
}
Expand Down
2 changes: 2 additions & 0 deletions crates/libs/bindgen/src/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl Writer {
clone
}

#[track_caller]
pub fn write(&self, tree: TypeTree) {
if self.config.package {
self.write_package(&tree);
Expand All @@ -28,6 +29,7 @@ impl Writer {
}
}

#[track_caller]
fn write_file(&self, tree: TypeTree) {
let tokens = if self.config.flat {
self.write_flat(tree)
Expand Down
Loading