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

Introducing the windows-link crate #3450

Merged
merged 8 commits into from
Jan 16, 2025
Merged

Introducing the windows-link crate #3450

merged 8 commits into from
Jan 16, 2025

Conversation

kennykerr
Copy link
Collaborator

@kennykerr kennykerr commented Jan 15, 2025

This update introduces the windows-link crate as a simpler alternative to the windows-targets crate. It provides the exact same link macro but uses raw-dylib unconditionally and thus has no target-specific dependencies to include import libs. This means that the windows-link crate requires Rust 1.71 or later as that was the first version to stabilize raw-dylib for all Windows targets.

The windows-bindgen crate is also updated to default to use windows-link for functions. You can opt-out by using the new --link option and specify a different module for the link macro. For example, the windows-sys crate continues to use windows-targets as it has an older MSRV. It does so by using the following option bindgen arguments:

--link windows_targets

The windows-targets crate is unchanged and will continue to be updated for the time being to support windows-sys. It continues to support the windows_raw_dylib cfg option for opting in to raw-dylib whereas the new windows-link crate ignores the windows_raw_dylib cfg option and uses raw-dylib in all cases.

As an example, consider the following build script.

windows_bindgen::bindgen(
    "--in default --out src/bindings.rs --flat --sys --filter GetTickCount".split_whitespace(),
);

This will produce the following output:

windows_link::link!("kernel32.dll" "system" fn GetTickCount() -> u32);

Add a dependency on the windows-link crate and you're all set!

You can then use the new --link argument only if you need to change this.

windows_bindgen::bindgen(
    "--in default --out src/bindings.rs --flat --sys --filter GetTickCount --link windows_targets".split_whitespace(),
);

This will produce the following altered output to once again use the windows-targets crate.

windows_targets::link!("kernel32.dll" "system" fn GetTickCount() -> u32);

@kennykerr kennykerr requested a review from ChrisDenton January 15, 2025 21:56
Copy link
Collaborator

@ChrisDenton ChrisDenton left a comment

Choose a reason for hiding this comment

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

Looks great! --link is a really useful feature on its own. And the more we can move toward not needing to bundle binary libs the better.

macro_rules! link {
($library:literal $abi:literal $($link_name:literal)? fn $($function:tt)*) => (
#[link(name = $library, kind = "raw-dylib", modifiers = "+verbatim")]
extern "C" {
Copy link
Collaborator

@ChrisDenton ChrisDenton Jan 16, 2025

Choose a reason for hiding this comment

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

This is just a comment but I had to look this up to recall why extern "C" is used instead of $abi. It was rust-lang/rust#110505 which wasn't fixed until 1.77, a fair bit above the current MSRV.

@kennykerr kennykerr merged commit 134e63f into master Jan 16, 2025
78 checks passed
@kennykerr kennykerr deleted the windows-link branch January 16, 2025 14:38
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