feat: add linker args for VHF lib to wdk-build#653
feat: add linker args for VHF lib to wdk-build#653Alan632 wants to merge 13 commits intomicrosoft:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Adds an opt-in vhf feature to wdk-build so driver crates can automatically link the appropriate Virtual HID Framework (VHF) import library without hardcoding linker directives in their own build.rs.
Changes:
- Add a
vhfCargo feature towdk-build. - When
vhfis enabled, emitVhfKm.libfor WDM/KMDF builds andVhfUm.libfor UMDF builds via build script output.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
crates/wdk-build/src/lib.rs |
Emits VHF linker input per driver type under #[cfg(feature = "vhf")]. |
crates/wdk-build/Cargo.toml |
Defines the new vhf feature flag. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #653 +/- ##
==========================================
+ Coverage 78.69% 78.74% +0.04%
==========================================
Files 25 25
Lines 5234 5254 +20
Branches 5234 5254 +20
==========================================
+ Hits 4119 4137 +18
- Misses 995 997 +2
Partials 120 120 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…adata inspection (automatic) rather than crate features, removed vhf feature from Cargo.toml
| .enabled_api_subsets | ||
| .iter() | ||
| .any(|f| f == &ApiSubset::Hid) | ||
| { | ||
| println!("cargo::rustc-cdylib-link-arg=VhfKm.lib"); |
There was a problem hiding this comment.
Extended current config unit tests.
| pub driver_config: DriverConfig, | ||
| /// List of features enabled for `wdk-sys` in resolved dependency graph | ||
| #[serde(default)] | ||
| enabled_api_subsets: Vec<ApiSubset>, |
There was a problem hiding this comment.
This should be a BTreeSet<ApiSubset> rather than a Vec. Each variant should appear at most once, and the three call sites that do iter().any(|f| f == &ApiSubset::Hid) collapse to contains(&ApiSubset::Hid). BTreeSet over HashSet keeps Config's Serialize/Deserialize round-trips deterministic (although we dont actually use this right now)
| determined from cargo metadata resolve. Feature-dependent libraries \ | ||
| will not be linked automatically." | ||
| ); | ||
| Vec::new() |
There was a problem hiding this comment.
the per-feature from_value(Value::String(f.clone())) is doing an unnecessary JSON round-trip and allocates/clones for every feature. Driving ApiSubset's existing Deserialize impl directly from &str via serde::de::IntoDeserializer avoids both:
use serde::de::{IntoDeserializer, value::{Error as ValueError, StrDeserializer}};
node.features
.iter()
.filter_map(|f| {
ApiSubset::deserialize(f.as_str().into_deserializer()).ok()
})
.collect()There was a problem hiding this comment.
actually, this should go further and actually filter some known set of cargo features that aren't APISubsets (ie. default, nightly), instead of relying on a deserialization failure
There was a problem hiding this comment.
To clarify the second point - is the desire to have a set of known features (aka all wdk-sys features) and warn the user if something not in that set is found in the metadata?
If we have a set of known "non ApiSubset" features that we filter out without notifying the user about it, then that sounds similar to silently filtering out via deserialization failure?
There was a problem hiding this comment.
we'd have a set of known non-api-subset features, and if we encounter one of those, it should hard error. anything thats not an api-subset feature and not in the known list should be treated as an unknown feature and we should bail. this is to help make sure at compile time that when there are new api subsets added, they map cleanly to the enum
There was a problem hiding this comment.
wait, we should hard error on encountering non-api-subset features? (aka default, nightly, test-stubs) Shouldn't we just filter those out and only hard error on anything that isn't listed in the [features] section in wdk-sys's Cargo.toml? Otherwise build will fail if we enable a feature like default or nightly in wdk-sys
There was a problem hiding this comment.
I went ahead and changed the implementation to hard error on found features that are neither in the non-api-subset-features list and the ApiSubset enum (eg. an unknown 'new' feature from wdk-sys), which I think is what we're both trying to express 😅 let's discuss offline if needed!
| /// Build configuration of driver | ||
| pub driver_config: DriverConfig, | ||
| /// List of features enabled for `wdk-sys` in resolved dependency graph | ||
| #[serde(default)] |
There was a problem hiding this comment.
Drop #[serde(default)] here. Nothing in the repo actually deserializes Config (all instances come from from_env_auto/new/default/struct literals; the line 554 serialization is metadata::Wdk, not Config), so this attribute has no observable effect today and implies a backwards-compat invariant the codebase doesn't uphold.
There was a problem hiding this comment.
Sounds good and done! I didn't think about what could be implied (the backwards-compat invariant) by adding that attribute.
| .resolve | ||
| .as_ref() | ||
| .and_then(|resolve| resolve.nodes.iter().find(|node| node.id == *id)) | ||
| .map_or_else( |
There was a problem hiding this comment.
This shouldn't be a warning. Two coherent options: (1) if wdk-sys missing from the build graph is invalid, return a ConfigError from from_env_auto() on None for wdk_sys_package_id so the build fails fast instead of silently omitting feature-gated linker args; (2) if it's a valid state, drop the warning entirely.
(2) is the right call. wdk-build can legitimately be consumed without wdk-sys -- for example, a crate that only wants the cfg plumbing it emits (driver_model__driver_type, driver_model__kmdf_version_major, etc.) without pulling in the FFI bindings (think a higher-level safe wrapper that re-exports from a separately-versioned wdk-sys, or a utility crate that just gates code on driver type). In that case enabled_api_subsets correctly should be empty and from_env_auto() should succeed silently -- the warning is flagging a valid usage pattern as if it were broken.
There was a problem hiding this comment.
That makes sense, we now use expect to panic in case of a true build error.
…, changed enabled_api_subsets type to a BTreeSet, updated tests
| cpu_architecture: CpuArchitecture, | ||
| /// Build configuration of driver | ||
| pub driver_config: DriverConfig, | ||
| /// List of features enabled for `wdk-sys` in resolved dependency graph |
| // Link against VhfKm.lib (Virtual HID Framework, kernel-mode) when | ||
| // the "hid" feature is enabled in wdk-sys. Required by drivers that | ||
| // create virtual HID devices. | ||
| if self.enabled_api_subsets.contains(&ApiSubset::Hid) { | ||
| println!("cargo::rustc-cdylib-link-arg=VhfKm.lib"); |
| // Link against VhfKm.lib (Virtual HID Framework, kernel-mode) when | ||
| // the "hid" feature is enabled in wdk-sys. Required by drivers that | ||
| // create virtual HID devices. | ||
| if self.enabled_api_subsets.contains(&ApiSubset::Hid) { | ||
| println!("cargo::rustc-cdylib-link-arg=VhfKm.lib"); |
| // Link against VhfUm.lib (Virtual HID Framework, user-mode) when | ||
| // the "hid" feature is enabled in wdk-sys. Required by drivers that | ||
| // create virtual HID devices. | ||
| if self.enabled_api_subsets.contains(&ApiSubset::Hid) { | ||
| println!("cargo::rustc-cdylib-link-arg=VhfUm.lib"); |
Description
Add linker arguments for the VHF library to the build script output for each driver type so drivers using the VHF don't have to hardcode the output in their own build scripts.
wdk-build/lib.rsVerification
Built a driver and ensured vhf linker args were still in build
outputafter removing the hardcoded line in the driver'sbuild.rs.