Skip to content

feat: Implement support for link type 'raw-dylib' on Windows #3257

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

Conversation

doxblek
Copy link

@doxblek doxblek commented Jul 30, 2025

Hi,

I am creating a Node Addon in Rust. For several reasons, I don't want to use a sophisticated solution like napi.rs, but generate Rust bindings for the Node API using bindgen on my own.

To make it run on Windows, it is necessary to provide an import library for symbols that are resolved only at runtime (such as the napi_* functions and types of the Node API.
In the Rust documentation, I noticed the raw-dylib kind of the #[link] attribute, which in addition to the +verbatim modifier (which allows me to set the library to link against "node.exe" instead of "node.dll") perfectly fits to my use case, because it auto-generates the needed import library. By that I don't need to deal will linker flags etc.

Unfortunately, bindgen currently does not support to emit #[link] attributes of that kind – though I found a related issue.

Therefore, I decided to contribute the feature. As I am new to the code base of bindgen, I closely related my feature to the existing --wasm-import-module-name flag which similarly provides a #[link] attribute.

Changes

Builder

I added a function windows_link_as_raw_dylib() to the builder, that takes the module name (similar to --wasm-import-module-name) and a boolean flag whether the module name should be treated verbatim.

The unsafe extern "C" block of a function is then provided with the attribute depending on the value of verbatim:

// verbatim == false
#[cfg_attr(windows, link(name = "<name>", kind = "raw-dylib"))]
unsafe extern "C" {
    pub fn my_function();
}

// verbatim == true
#[cfg_attr(windows, link(name = "<name>", kind = "raw-dylib", modifiers = "+verbatim"))]
unsafe extern "C" {
    pub fn my_function();
}

The attribute is set to only affect builds on Windows, because the "raw-dylib" feature is only supported on Windows (see Rust Documentation

Commandline Options

I added two options:

  • --windows-link-as-raw-dylib enables the linking with the "raw-dylib" kind and takes the name of the DLL/EXE. By its own, it does not treat the name verbatim
  • --windows-link-as-raw-dylib-verbatim can be set in addition to make the name specified in --windows-link-as-raw-dylib be treated verbatim. The flag must not be set without --windows-link-as-raw-dylib. This is ensure via clap.

Tests

I added two tests:

  • windows-raw-dylib tests the new feature with verbatim set to false
  • windows-raw-dylib-verbatim tests the new feature with verbatim set to true

I am happy about your feedback and look forward to get this landed :)

resolves #2833

@doxblek
Copy link
Author

doxblek commented Jul 30, 2025

r? @emilio @pvdrz

@rustbot
Copy link
Collaborator

rustbot commented Jul 30, 2025

Error: The feature assign is not enabled in this repository.
To enable it add its section in the triagebot.toml in the root of the repository.

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

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.

Support for raw-dylib for Windows
3 participants