feat: add linker directive for VHF lib + move static linker directives from wdk-build to wdk-sys#685
feat: add linker directive for VHF lib + move static linker directives from wdk-build to wdk-sys#685Alan632 wants to merge 16 commits into
Conversation
… directives from wdk-build/src/lib.rs into wdk-sys/build.rs
There was a problem hiding this comment.
Pull request overview
This PR updates how native WDK libraries are linked by moving static rustc-link-lib directives out of wdk-build’s configure_binary_build prints and into wdk-sys’s generated bindings via #[link(...)] attributes. It also adds conditional linker directives for the VHF library when the hid feature is enabled, selecting VhfKm vs VhfUm based on driver model.
Changes:
- Emit base driver-model-specific native library link directives into bindgen output (
ntddk.rs/windows.rs) fromwdk-sys/build.rs. - Add
hid-gated#[link]directives forVhfKm(KMDF/WDM) andVhfUm(UMDF). - Remove the corresponding
cargo::rustc-link-lib=static=*emissions fromwdk-build::Config::configure_binary_build(leaving link-arg prints in place).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| crates/wdk-sys/build.rs | Adds a reusable #[link] directive helper and injects conditional link directives into generated bindings (including VHF for hid). |
| crates/wdk-build/src/lib.rs | Stops emitting static rustc-link-lib lines (now handled by wdk-sys), retaining cdylib link-args and documenting future move. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #685 +/- ##
==========================================
+ Coverage 79.93% 80.61% +0.67%
==========================================
Files 26 26
Lines 5633 5829 +196
Branches 5633 5829 +196
==========================================
+ Hits 4503 4699 +196
Misses 1002 1002
Partials 128 128 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…ts by utilizing ApiSubset similar to the headers
…indows-drivers-rs into vhf_lib_linker_args-v2
| .with_codegen_config((CodegenConfig::TYPES | CodegenConfig::VARS).complement()) | ||
| .header_contents(&format!("{outfile_name}-input.h"), &header_contents); | ||
|
|
||
| add_link_directives(builder, &config.link_directives(ApiSubset::Base)) |
There was a problem hiding this comment.
If you mirror the header_contents implementation, you would just have a bindgen_library_link_raw_lines (or similar name) to get the formatted rawlines and just pass that directly to raw_line. That would make it so that you don't need this free function and can continue to chain the builder construction like with header_contents
There was a problem hiding this comment.
That bindgen_library_link_raw_lines would call a config.libraries(api_subsets) just the same as header_contents calls headers
There was a problem hiding this comment.
Redesigned the implementation to closely mirror the header_contents pattern.
…utes out when building non-driver test wdk-sys-tests (wdk-sys-tests does not call configure_binary_build thus no search paths are emitted and ungated link attributes fail the build/test)
…on, library selection, and link directive string creation) -removed raw strings in lieu of types to tightly constrain LinkDirective creation w/o needing assert checks -removed and compacted unneeded functions from original implementation
… attributes to ensure wdk-sys builds correctly when compiled for test - add unit tests - add PartialEq and Eq traits to LinkKind, LinkModifier, and LinkDirective
leon-xd
left a comment
There was a problem hiding this comment.
Great work. Few changes here and there but the overall shape is very solid!
| } | ||
|
|
||
| #[test] | ||
| fn render_static_directive_includes_no_bundle_modifier() { |
| } | ||
|
|
||
| #[test] | ||
| fn base_directives_are_all_static_no_bundle() { |
- add doctest to wdk-sys-tests - add cdylib driver unit test to mixed-package-kmdf-workspace, and update Cargo.toml
…indows-drivers-rs into vhf_lib_linker_args-v2
| //! Like the driver unit test in `tests/mixed-package-kmdf-workspace`, it | ||
| //! references a bindgen generated WDM type (`DRIVER_OBJECT`) via a const | ||
| //! `size_of` — so the doctest actually links `wdk-sys`'s generated bindings | ||
| //! while touching only a type (never a KM function |
| } | ||
|
|
||
| #[test] | ||
| fn render_static_directive_includes_no_bundle_modifier() { |
| } | ||
|
|
||
| #[test] | ||
| fn base_directives_are_all_static_no_bundle() { |
| //! | ||
| //! This is a sample KMDF driver that demonstrates how to use the crates in | ||
| //! windows-driver-rs to create a skeleton of a kmdf driver. | ||
| //! |
| /// [`ApiSubset`] rather than a set like | ||
| /// [`Config::bindgen_header_contents`]. | ||
| /// Each generated bindings file requests only the native libraries | ||
| /// introduced by its own [`ApiSubset`]. This avoids duplicate `#[link]` |
There was a problem hiding this comment.
nit: i think we can delete this entire paragraph, the first sentence is pretty much saying the same thing as the paragraph above and not sure how necessary the second sentence is but i'm fine to keep it if you feel it's necessary
Summary
Adds conditionally compiled linker directives for the VHF library tied to the "hid" feature.
Moves build script emitted static linker directives (
cargo::rustc-link-lib=static=*) from wdk-build/src/lib.rs into bindgen generated files as conditionally compiled attributes. The Rust source insertion happens in wdk-sys/build.rs while the backend logic and Rust source string build lives in wdk-build/src/lib.rs (closely following the pattern the bindgen generated headers use). This allows conditional compilation of linker directives (and eventually link args) without having to rely on cross crate feature signaling, and guards against issues from version drift (in case multiple versions of wdk-build are used in one project).This PR is a redesign of and supersedes PR!653.
Verification
Verified both kmdf and umdf drivers built with "hid" and Vhf functions linked against VhfKm.lib and VhfUm.lib respectively. Additionally, inspected each driver's linker
.mapfile for evidence of the respective Vhf symbols.Bindgen generated files are also inspected post build for the presence of the link attributes.