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

Add optional warnings for windows-bindgen to improve diagnostics #3498

Merged
merged 2 commits into from
Feb 20, 2025

Conversation

kennykerr
Copy link
Collaborator

@kennykerr kennykerr commented Feb 19, 2025

This adds the ability to retrieve any warnings that the bindgen function may decide to collect and return as part of code generation that may or may not be an error depending on the context. For example, let's say you're calling bindgen as follows from your build script:

fn main() {
    windows_bindgen::bindgen([
        "--out",
        "src/bindings.rs",
        "--filter",
        "Windows.Win32.System.Com.IPersistFile",
    ]);
}

All goes well until you realize that the IPersistFile.Load method was omitted due to a missing dependency. This may or may not be what you wanted. If not, it can be quite tedious to chase down all of the missing dependencies for a larger filter that may span multiple interfaces with many dozens or even hundreds of methods.

Fortunately, windows-bindgen can gather these up and return this information for you:

fn main() {
    let warnings = windows_bindgen::bindgen([
        "--out",
        "src/bindings.rs",
        "--filter",
        "Windows.Win32.System.Com.IPersistFile",
    ]);

    if !warnings.is_empty() {
        panic!("{warnings}");
    }
}

The resulting Warnings type is just a collection of strings. It also provides a convenient unwrap method to make your build scripts a bit more concise if you prefer:

fn main() {
    windows_bindgen::bindgen([
        "--out",
        "src/bindings.rs",
        "--filter",
        "Windows.Win32.System.Com.IPersistFile",
    ]).unwrap();
}

Either way, here's what the panic message contains:

  thread 'main' panicked at build.rs:7:8:
  skipping `Windows.Win32.System.Com.IPersistFile.Load` due to missing dependencies:
    Windows.Win32.System.Com.STGM

You can then decide to add STGM to your filter and the Load method will be generated and the "warning" disappears:

fn main() {
    windows_bindgen::bindgen([
        "--out",
        "src/bindings.rs",
        "--filter",
        "Windows.Win32.System.Com.IPersistFile",
        "Windows.Win32.System.Com.STGM",
    ]).unwrap();
}

The solution is generic enough that other warnings can conceivably be added in future. And no you can't turn off some warnings. 🙃 Use it for diagnostics and then turn it off if you'd like to skip some but not all methods. For anything more elaborate, feel free to enumerate the warnings yourself.

@riverar
Copy link
Collaborator

riverar commented Feb 19, 2025

Very cool, this will save a bunch of time, thanks!

@kennykerr kennykerr merged commit ed36c28 into master Feb 20, 2025
90 checks passed
@kennykerr kennykerr deleted the warnings branch February 20, 2025 14:28
@kennykerr kennykerr mentioned this pull request Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants