Skip to content

Add a mechanism to rerun bindgen with the same user options#2292

Merged
pvdrz merged 2 commits into
rust-lang:masterfrom
ferrous-systems:restart-button
Nov 1, 2022
Merged

Add a mechanism to rerun bindgen with the same user options#2292
pvdrz merged 2 commits into
rust-lang:masterfrom
ferrous-systems:restart-button

Conversation

@pvdrz

@pvdrz pvdrz commented Oct 3, 2022

Copy link
Copy Markdown
Contributor

In some cases, like when processing headers with static inline functions, might be useful to have a mechanism to rerun Bindgen with the same options the user provided.

I'm opening this PR as a first step to solve #1090 and not make a hard to review and huge PR. The idea would be to run bindgen a first time to find any inlined functions, then generate a new .h file that wraps the inlined functions and finally run bindgen again using this generated file as an input.

Blocked by: #2302

@pvdrz pvdrz requested a review from emilio October 3, 2022 19:21
@pvdrz pvdrz changed the title Add an option to rerun bindgen with the same user options Oct 3, 2022

@emilio emilio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused about the ShouldRestart code, can you elaborate?

Generally looks good, with some nits.

Comment thread src/clang.rs
impl UnsavedFile {
/// Construct a new unsaved file with the given `name` and `contents`.
pub fn new(name: &str, contents: &str) -> UnsavedFile {
pub fn new(name: String, contents: String) -> UnsavedFile {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can pass name and contents by value to UnsavedFile::new which means that the calls to CString::new don't have to clone name and contents

Comment thread src/lib.rs Outdated
Comment thread src/lib.rs Outdated
Bindings::generate(self.options, input_unsaved_files)
match Bindings::generate(options, input_unsaved_files) {
Ok(bindings) => Ok(bindings),
Err(GenerateError::ShouldRestart) => self.generate(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? I don't get this. This is dead code and would trivially loop right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually let me do a change that could make this clearer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pvdrz

pvdrz commented Oct 4, 2022

Copy link
Copy Markdown
Contributor Author

so @emilio the idea is that if bindgen finds a static inline function and the generate-static-inline options is enabled, it generates some extra c code that wraps the inline functions. So if you pass a file header.h to bindgen with the following contents:

static inline int foo() { return 42; }

then bindgen would generate an extra headers file header_inlined.h:

int foo_inlined()

with a corresponding header_inlined.c source file:

int foo_inlined() { return foo(); }

then Bindings::generate returns GenerateError::ShouldReturn { header: "header_inlined.h".into() }. Which means that the builder can include this new header file and run Bindings::generate again.

@bors-servo

Copy link
Copy Markdown

☔ The latest upstream changes (presumably #2284) made this pull request unmergeable. Please resolve the merge conflicts.

@pvdrz pvdrz mentioned this pull request Oct 7, 2022
@pvdrz pvdrz marked this pull request as draft October 7, 2022 19:04
@bors-servo

Copy link
Copy Markdown

☔ The latest upstream changes (presumably #2302) made this pull request unmergeable. Please resolve the merge conflicts.

This adds a mechanism so `bindgen` is able to run `Bindings::generate`
multiple times with the same user input if the `generate_static_inline`
option is enabled and `GenerateError::ShouldRestart` is returned by
`Bindings::generate`.

This is done to eventually solve rust-lang#1090 which would require to check for
any static inline functions and generate a new header file to be used as
an extra input and run `Bindings::generate` again.
@pvdrz pvdrz marked this pull request as ready for review October 12, 2022 16:14

@emilio emilio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok / progress, but I'd probably merge this along with the feature using it? Anyways looks good to merge if you want to make progress in-tree.

Comment thread bindgen/lib.rs Outdated
@pvdrz pvdrz merged commit a2fe04c into rust-lang:master Nov 1, 2022
@pvdrz pvdrz deleted the restart-button branch November 1, 2022 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants