Skip to content

Conversation

@svasista-ms
Copy link
Contributor

@svasista-ms svasista-ms commented Sep 2, 2025

This PR includes changes to the build command to respect other forms of target configuration vis config.toml (build.target) and environment (CARGO_BUILD_TARGET) along with the --target CLI option.

Logical Changes

cargo build already supported the above forms of target specification. What we needed was to ensure packaging step uses the same target as build step and uses the correct output location of binaries.

In this PR is we now run cargo build --message-format=json (instead of cargo build) and extract the output location of binaries from the resulting JSON. To get the target arch we run cargo rustc --print right after cargo build --message-format=json and parse the output for the target_arch field. [1] [2] [3].

Test Changes

Added kmdf-driver-with-target-override test fixture in cargo-wdk/tests and the cases in crates/cargo-wdk/tests/build_command_test.rs [4]

CI Workflow Changes

To support the integration test cases that run cargo wdk build in a cross-compilation setting when using Nuget-based WDK, two additional environment variables have been added:

  1. FullVersionNumber: Denotes the complete version number (including QFE) of the WDK and SDK nuget packages that were downloaded [5]
  2. NugetPackagesRoot: The root directory where the nuget packages required to build a driver are downloaded [6]

The variables are set in the install-wdk action and used in build_command_test where some form of target override is tested. Using NugetPackagesRoot and FullVersionNumber the WDKContentRoot env var is set to point to the correct WDK content based on the target architecture.

Closes #435
Closes #348

- Removed the TargetArch enum and simplified target architecture handling in the CLI.
- Updated the command execution function to accept an optional working directory.
- Enhanced error reporting for command failures, including status codes and stderr output.
- Cleaned up the `run` method in the `WdkBuild` struct to prepare for future enhancements.
- Improved test assertions for driver packaging tasks and added a utility function to determine target folders based on architecture.
- Updated the Cargo configuration for the WDM driver test to specify the target architecture explicitly.
@svasista-ms svasista-ms changed the title refactor: target architecture handling and command execution refactor(cargo-wdk): target architecture handling in BuildAction to respect other forms of target overrides Sep 4, 2025
Copilot AI review requested due to automatic review settings September 25, 2025 03:58
Copy link
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

This PR refactors the cargo-wdk build command to better respect Cargo's target architecture override mechanisms by removing the TargetArch enum and simplifying architecture handling. Instead of detecting architecture via rustc --print host-tuple, the code now uses cargo rustc -- --print cfg to detect the effective target architecture that Cargo will build for, respecting configuration settings from .cargo/config.toml or environment variables.

  • Removed TargetArch enum and related CLI architecture detection logic
  • Updated command execution to support optional working directories and enhanced error reporting
  • Improved driver packaging task error messages and test utilities

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
crates/cargo-wdk/tests/wdm-driver/.cargo/config.toml Added explicit target architecture configuration
crates/cargo-wdk/tests/build_command_test.rs Updated test assertions and added utility for target folder detection
crates/cargo-wdk/src/providers/wdk_build.rs Added placeholder for future WDK build number field
crates/cargo-wdk/src/providers/mod.rs Enhanced command error reporting with status codes and stderr
crates/cargo-wdk/src/providers/exec.rs Added working directory support and improved debug logging
crates/cargo-wdk/src/cli.rs Removed TargetArch enum and simplified build command handling
crates/cargo-wdk/src/actions/mod.rs Removed TargetArch enum definition
crates/cargo-wdk/src/actions/build/tests.rs Updated test cases to use new architecture handling
crates/cargo-wdk/src/actions/build/package_task.rs Changed driver model parameter to reference and minor formatting cleanup
crates/cargo-wdk/src/actions/build/mod.rs Implemented new target architecture detection and package root path construction
crates/cargo-wdk/src/actions/build/error.rs Added new error types for architecture detection
crates/cargo-wdk/src/actions/build/build_task.rs Updated to use simplified architecture handling

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

…ecture and artifact paths for the packaging process

- Added tests for building KMDF drivers with explicit target architecture options.
- Created a new test fixture for `kmdf-driver-with-target-override` to validate target override scenarios
Copilot AI review requested due to automatic review settings September 29, 2025 09:38
Copy link
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 16 out of 17 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

…g logic

- Added new error variants to `BuildActionError` and `BuildTaskError` for better error reporting.
- Enhanced the `build_and_package` function to handle driver binary paths and target architecture more effectively.
- Modified tests to reflect changes in the build process and ensure accurate artifact handling.
- Renamed `Profile::Dev` to `Profile::Debug` for clarity and consistency.
Copilot AI review requested due to automatic review settings October 7, 2025 15:05
Copy link
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 17 out of 18 changed files in this pull request and generated 6 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copilot AI review requested due to automatic review settings October 16, 2025 14:16
Copy link
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 26 out of 38 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copilot AI review requested due to automatic review settings October 16, 2025 19:39
Copy link
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 26 out of 38 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copilot AI review requested due to automatic review settings October 17, 2025 05:04
Copy link
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 26 out of 38 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

crates/cargo-wdk/src/actions/mod.rs:39

  • The profile parsing accepts 'debug' but Cargo's standard development profile is called 'dev'. This creates inconsistency with Cargo conventions.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "debug" => std::result::Result::Ok(Self::Debug),
            "release" => std::result::Result::Ok(Self::Release),
            _ => Err(format!("'{s}' is not a valid profile")),
        }
    }

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

remove retry mechanism for cargo clean in integration tests
improve comments for `get_target_dir_for_packaging` fn
Copilot AI review requested due to automatic review settings December 5, 2025 08:02
Copy link
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 17 out of 18 changed files in this pull request and generated 8 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

run_cargo_clean(&driver_path);
let stdout = run_build_cmd(&driver_path);
#[test]
fn kmdf_driver_with_target_override_cli_wins() {
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

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

The test kmdf_driver_with_target_override_cli_wins sets both CARGO_BUILD_TARGET env var (to X86_64) and --target-arch CLI arg (to ARM64) to verify that CLI wins. However, the test fixture's .cargo/config.toml also has target = "x86_64-pc-windows-msvc". This means there are actually 3 conflicting target specifications, not 2. The test name and logic should clarify that CLI wins over both env and config.toml.

Suggested change
fn kmdf_driver_with_target_override_cli_wins() {
/// This test verifies that the CLI `--target-arch` argument takes precedence over both
/// the `CARGO_BUILD_TARGET` environment variable and the `target` specified in `.cargo/config.toml`.
/// All three sources are set to conflicting values:
/// - `.cargo/config.toml` sets target to x86_64-pc-windows-msvc
/// - `CARGO_BUILD_TARGET` env var is set to either x86_64-pc-windows-msvc or aarch64-pc-windows-msvc
/// - CLI `--target-arch` is set to ARM64
/// The test asserts that the CLI value wins.
fn kmdf_driver_with_target_override_cli_wins_over_env_and_config() {

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

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

@svasista-ms Do add a comment stating the three competing sources of config. The comment can be brief. Doesn't have to be as big as the one suggested by copilot

Copilot AI review requested due to automatic review settings December 23, 2025 16:33
Copy link
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 17 out of 18 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 133 to 138
let wdk_content_root = std::env::var("NugetPackagesRoot")
.ok()
.map(|nuget_package_root| get_nuget_wdk_content_root(target_arch, &nuget_package_root));
let env = wdk_content_root
.as_deref()
.map(|wdk_content_root| [("WDKContentRoot", Some(wdk_content_root))]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Better to extract this into a function and use it everywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @gurry, I moved the NugetPackagesRoot check to the helper function. Renamed the fn to get_wdk_content_root_from_nuget_packages_root. This fn will return None if either of the required environment variables are not set.

@gurry
Copy link
Contributor

gurry commented Dec 26, 2025

❌ Patch coverage is 92.27528% with 55 lines in your changes missing coverage. Please review. ✅ Project coverage is 82.92%.

@svasista-ms looks like the test coverage of the newly added code has some room for improvement. Please have a look

Copilot AI review requested due to automatic review settings December 29, 2025 04:24
Copy link
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 18 out of 19 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +456 to +457
"Driver binary's parent directory is missing. Driver binary (.dll) \
path: {}",
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

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

The comment uses "Driver binary's parent directory is missing" but the parent directory of any file path should always exist. The real issue is that the path is likely a root path or otherwise invalid. Consider updating the error message to be more accurate about the failure condition.

Suggested change
"Driver binary's parent directory is missing. Driver binary (.dll) \
path: {}",
"Cannot determine parent directory for driver binary; path may be a \
root or otherwise invalid. Driver binary (.dll) path: {}",

Copilot uses AI. Check for mistakes.
Copilot AI review requested due to automatic review settings December 29, 2025 09:59
Copy link
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 18 out of 19 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

"./tests/wdk-macros-tests/Cargo.toml",
"./tests/wdk-sys-tests/Cargo.toml",

// Rust Analyzer is disabled on the following test projects because
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

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

The comment at line 25 should have proper sentence capitalization and punctuation. Change "// Rust Analyzer is disabled on the following test projects because" to "// Rust Analyzer is disabled on the following test projects because:"

Suggested change
// Rust Analyzer is disabled on the following test projects because
// Rust Analyzer is disabled on the following test projects because:

Copilot uses AI. Check for mistakes.
Comment on lines +412 to +416
/// is found in the output or its parent folder cannot be determined.
/// - `BuildActionError::NotAbsolute` - If the DLL's parent path could not
/// be made absolute.
/// - `BuildActionError::CannotDetermineTargetDir` - If a cargo message
/// could not be parsed.
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

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

The documentation at lines 411-416 lists the same error variant CannotDetermineTargetDir twice. The first one should be for "no matching DLL file found" and the second for "cargo message parse errors". Consider consolidating these into a single bullet point or making them distinct, such as:

  • BuildActionError::CannotDetermineTargetDir - If no matching DLL file is found in the output, its parent folder cannot be determined, or a cargo message could not be parsed.
Suggested change
/// is found in the output or its parent folder cannot be determined.
/// - `BuildActionError::NotAbsolute` - If the DLL's parent path could not
/// be made absolute.
/// - `BuildActionError::CannotDetermineTargetDir` - If a cargo message
/// could not be parsed.
/// is found in the output, its parent folder cannot be determined, or a
/// cargo message could not be parsed.
/// - `BuildActionError::NotAbsolute` - If the DLL's parent path could not
/// be made absolute.

Copilot uses AI. Check for mistakes.
/// Invokes `cargo rustc -- --print cfg` and finds the `target_arch` value
///
/// # Arguments
/// * `working_dir` -- Working directory from which the command must be
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

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

The double dash in the comment at line 493 should be a single dash for consistency with standard documentation style. Change "* working_dir -- Working directory" to "* working_dir - Working directory".

Suggested change
/// * `working_dir` -- Working directory from which the command must be
/// * `working_dir` - Working directory from which the command must be

Copilot uses AI. Check for mistakes.
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 target overrides from config.toml or env variable [cargo-wdk] Ensure cargo-wdk respect other forms of target tuple/architecture override

4 participants