feat: Implement support for link type 'raw-dylib' on Windows #3257
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 ofverbatim
: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 withverbatim
set to falsewindows-raw-dylib-verbatim
tests the new feature withverbatim
set to trueI am happy about your feedback and look forward to get this landed :)
resolves #2833