Skip to content

feat: add linker args for VHF lib to wdk-build#653

Open
Alan632 wants to merge 13 commits intomicrosoft:mainfrom
Alan632:vhf_lib_linker_args
Open

feat: add linker args for VHF lib to wdk-build#653
Alan632 wants to merge 13 commits intomicrosoft:mainfrom
Alan632:vhf_lib_linker_args

Conversation

@Alan632
Copy link
Copy Markdown
Contributor

@Alan632 Alan632 commented May 1, 2026

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.rs

  • For each driver type (WDM/KMDF/UMDF) add the correct Vhf lib linker arguments, gated by "hid" feature flag usage in wdk-sys.

Verification

Built a driver and ensured vhf linker args were still in build output after removing the hardcoded line in the driver's build.rs.

image

@Alan632 Alan632 self-assigned this May 1, 2026
Copilot AI review requested due to automatic review settings May 1, 2026 23:38
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 vhf Cargo feature to wdk-build.
  • When vhf is enabled, emit VhfKm.lib for WDM/KMDF builds and VhfUm.lib for 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-commenter
Copy link
Copy Markdown

codecov-commenter commented May 2, 2026

Codecov Report

❌ Patch coverage is 90.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.74%. Comparing base (efa6c11) to head (d22d9db).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
crates/wdk-build/src/lib.rs 90.00% 2 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Alan632 Alan632 marked this pull request as ready for review May 4, 2026 17:47
Comment thread crates/wdk-build/src/lib.rs Outdated
Alan Ngo added 2 commits May 5, 2026 21:50
…adata inspection (automatic) rather than crate features, removed vhf feature from Cargo.toml
Copilot AI review requested due to automatic review settings May 6, 2026 05:06
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 5 comments.

Comment thread crates/wdk-build/src/lib.rs Outdated
Comment thread crates/wdk-build/src/lib.rs Outdated
Comment thread crates/wdk-build/src/lib.rs Outdated
Comment thread crates/wdk-build/src/lib.rs
Comment thread crates/wdk-build/src/lib.rs Outdated
Comment thread crates/wdk-build/src/lib.rs Outdated
Comment thread crates/wdk-build/src/lib.rs Outdated
Comment thread crates/wdk-build/src/lib.rs Outdated
Copilot AI review requested due to automatic review settings May 7, 2026 04:08
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.

Comment thread crates/wdk-build/src/lib.rs
Comment thread crates/wdk-build/src/lib.rs Outdated
Comment thread crates/wdk-build/src/lib.rs Outdated
Comment on lines +1202 to +1206
.enabled_api_subsets
.iter()
.any(|f| f == &ApiSubset::Hid)
{
println!("cargo::rustc-cdylib-link-arg=VhfKm.lib");
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Extended current config unit tests.

Copilot AI review requested due to automatic review settings May 8, 2026 01:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 5 comments.

Comment thread crates/wdk-build/src/lib.rs Outdated
Comment thread crates/wdk-build/src/lib.rs Outdated
Comment thread crates/wdk-build/src/lib.rs Outdated
Comment thread crates/wdk-build/src/lib.rs Outdated
Comment thread crates/wdk-build/src/lib.rs Outdated
Comment thread crates/wdk-build/src/lib.rs Outdated
pub driver_config: DriverConfig,
/// List of features enabled for `wdk-sys` in resolved dependency graph
#[serde(default)]
enabled_api_subsets: Vec<ApiSubset>,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done!

Comment thread crates/wdk-build/src/lib.rs Outdated
Comment thread crates/wdk-build/src/lib.rs Outdated
determined from cargo metadata resolve. Feature-dependent libraries \
will not be linked automatically."
);
Vec::new()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

@Alan632 Alan632 May 9, 2026

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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!

Comment thread crates/wdk-build/src/lib.rs Outdated
/// Build configuration of driver
pub driver_config: DriverConfig,
/// List of features enabled for `wdk-sys` in resolved dependency graph
#[serde(default)]
Copy link
Copy Markdown
Collaborator

@wmmc88 wmmc88 May 8, 2026

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

@Alan632 Alan632 May 9, 2026

Choose a reason for hiding this comment

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

Sounds good and done! I didn't think about what could be implied (the backwards-compat invariant) by adding that attribute.

Comment thread crates/wdk-build/src/lib.rs Outdated
.resolve
.as_ref()
.and_then(|resolve| resolve.nodes.iter().find(|node| node.id == *id))
.map_or_else(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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
Copilot AI review requested due to automatic review settings May 9, 2026 01:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.

cpu_architecture: CpuArchitecture,
/// Build configuration of driver
pub driver_config: DriverConfig,
/// List of features enabled for `wdk-sys` in resolved dependency graph
Comment on lines +1222 to +1226
// 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");
Comment on lines +1258 to +1262
// 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");
Comment on lines +1279 to +1283
// 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");
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.

4 participants