From a3df1d6408d39e40c3ca9dcedf24e9c829881057 Mon Sep 17 00:00:00 2001 From: "Leon Durrenberger (from Dev Box)" Date: Thu, 9 Jan 2025 10:42:32 -0800 Subject: [PATCH 1/6] Added rust script for generate-certificate task --- crates/wdk-build/rust-driver-makefile.toml | 25 ++++++++----- crates/wdk-build/src/cargo_make.rs | 42 ++++++++++++++++++++++ 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/crates/wdk-build/rust-driver-makefile.toml b/crates/wdk-build/rust-driver-makefile.toml index 903af810..7cc3ecb9 100644 --- a/crates/wdk-build/rust-driver-makefile.toml +++ b/crates/wdk-build/rust-driver-makefile.toml @@ -406,17 +406,23 @@ args = [ [tasks.generate-certificate] private = true +condition_script_runner_args = [ + "--base-path", + "${CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY}", +] condition_script = ''' -#!@duckscript +#!@rust -out = exec certmgr.exe -put -s WDRTestCertStore -c -n WDRLocalTestCert ${WDK_BUILD_OUTPUT_DIRECTORY}/WDRLocalTestCert.cer -if eq ${out.code} 0 - echo WDRLocalTestCert found in WDRTestCertStore. Skipping certificate generation. - exit 1 -else - echo WDRLocalTestCert not found in WDRTestCertStore. Generating new certificate. - exit 0 -end +//! ```cargo +//! [dependencies] +//! wdk-build = { path = ".", version = "0.3.0" } +//! anyhow = "1" +//! ``` +#![allow(unused_doc_comments)] + +fn main() -> anyhow::Result<()> { + wdk_build::cargo_make::generate_certificate_condition_script() +} ''' command = "makecert" args = [ @@ -445,6 +451,7 @@ script = ''' //! ```cargo //! [dependencies] //! wdk-build = { path = ".", version = "0.3.0" } +//! anyhow = "1" //! ``` #![allow(unused_doc_comments)] diff --git a/crates/wdk-build/src/cargo_make.rs b/crates/wdk-build/src/cargo_make.rs index 26be9942..09cf0d75 100644 --- a/crates/wdk-build/src/cargo_make.rs +++ b/crates/wdk-build/src/cargo_make.rs @@ -16,6 +16,7 @@ use std::{ env, panic::UnwindSafe, path::{Path, PathBuf}, + process::Command, }; use anyhow::Context; @@ -1007,6 +1008,47 @@ pub fn package_driver_flow_condition_script() -> anyhow::Result<()> { }) } +/// +/// `cargo-make` condition script for `generate-certificate` task in +/// [`rust-driver-makefile.toml`](../rust-driver-makefile.toml) +/// +/// # Errors +/// +/// This functions returns an error whenever it determines that the +/// `generate-certificate` `cargo-make` task should be skipped. This only +/// occurs when the WdrLocalTestCert cannot be added from the build directory +/// to WDRTestCertStore. +/// +pub fn generate_certificate_condition_script() -> anyhow::Result<()> { + condition_script(|| { + let output = Command::new("certmgr") + .args([ + "-put", + "-s", + "WDRTestCertStore", + "-c", + "-n", + "WdrLocalTestCert", + ]) + .arg(get_wdk_build_output_directory().join("WDRLocalTestCert.cer")) + .output() + .expect("Failed to move certificate to build directory."); + + match output.status.code() { + Some(0) => Err(anyhow::anyhow!("WDRLocalTestCert already in WDRTestCertStore. Skipping certificate generation.")), + Some(1) => Ok(()), + Some(_) => { + eprintln!("Unknown status code found from certmgr. Generating certificate."); + Ok(()) + }, + None => { + eprintln!("No status code found from certmgr. Generating certificate."); + Ok(()) + }, + } + }) +} + fn configure_wdf_build_output_dir(target_arg: Option<&String>, cargo_make_cargo_profile: &str) { let cargo_make_crate_custom_triple_target_directory = env::var(CARGO_MAKE_CRATE_CUSTOM_TRIPLE_TARGET_DIRECTORY_ENV_VAR).unwrap_or_else(|_| { From 646558c352dab28655cb611d233b6c4b78d44640 Mon Sep 17 00:00:00 2001 From: "Leon Durrenberger (from Dev Box)" Date: Thu, 9 Jan 2025 10:46:38 -0800 Subject: [PATCH 2/6] remove extraneous anyhow dependency --- crates/wdk-build/rust-driver-makefile.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/wdk-build/rust-driver-makefile.toml b/crates/wdk-build/rust-driver-makefile.toml index 7cc3ecb9..e681d441 100644 --- a/crates/wdk-build/rust-driver-makefile.toml +++ b/crates/wdk-build/rust-driver-makefile.toml @@ -451,7 +451,6 @@ script = ''' //! ```cargo //! [dependencies] //! wdk-build = { path = ".", version = "0.3.0" } -//! anyhow = "1" //! ``` #![allow(unused_doc_comments)] From a8148705e2efaed1d6fdf55c8fd952119a674c7f Mon Sep 17 00:00:00 2001 From: "Leon Durrenberger (from Dev Box)" Date: Thu, 9 Jan 2025 11:01:04 -0800 Subject: [PATCH 3/6] cargo fmt and clippy passing now --- crates/wdk-build/src/cargo_make.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/crates/wdk-build/src/cargo_make.rs b/crates/wdk-build/src/cargo_make.rs index 09cf0d75..910d29c1 100644 --- a/crates/wdk-build/src/cargo_make.rs +++ b/crates/wdk-build/src/cargo_make.rs @@ -1008,17 +1008,19 @@ pub fn package_driver_flow_condition_script() -> anyhow::Result<()> { }) } -/// /// `cargo-make` condition script for `generate-certificate` task in /// [`rust-driver-makefile.toml`](../rust-driver-makefile.toml) -/// +/// /// # Errors -/// -/// This functions returns an error whenever it determines that the +/// +/// This functions returns an error whenever it determines that the /// `generate-certificate` `cargo-make` task should be skipped. This only -/// occurs when the WdrLocalTestCert cannot be added from the build directory -/// to WDRTestCertStore. +/// occurs when the `WdrLocalTestCert` cannot be added from the build directory +/// to `WDRTestCertStore`. /// +/// # Panics +/// +/// Panics if certmgr is unable to be run. pub fn generate_certificate_condition_script() -> anyhow::Result<()> { condition_script(|| { let output = Command::new("certmgr") @@ -1032,19 +1034,21 @@ pub fn generate_certificate_condition_script() -> anyhow::Result<()> { ]) .arg(get_wdk_build_output_directory().join("WDRLocalTestCert.cer")) .output() - .expect("Failed to move certificate to build directory."); + .expect("Failed to run certmgr.exe."); match output.status.code() { - Some(0) => Err(anyhow::anyhow!("WDRLocalTestCert already in WDRTestCertStore. Skipping certificate generation.")), + Some(0) => Err(anyhow::anyhow!( + "WDRLocalTestCert already in WDRTestCertStore. Skipping certificate generation." + )), Some(1) => Ok(()), Some(_) => { eprintln!("Unknown status code found from certmgr. Generating certificate."); Ok(()) - }, + } None => { eprintln!("No status code found from certmgr. Generating certificate."); - Ok(()) - }, + Ok(()) + } } }) } From d0c0c0ec53d1e93c0757eb4d7a8645feeb050885 Mon Sep 17 00:00:00 2001 From: "Leon Durrenberger (from Dev Box)" Date: Thu, 9 Jan 2025 13:01:56 -0800 Subject: [PATCH 4/6] two whitespaces --- crates/wdk-build/src/cargo_make.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/wdk-build/src/cargo_make.rs b/crates/wdk-build/src/cargo_make.rs index 910d29c1..19b10179 100644 --- a/crates/wdk-build/src/cargo_make.rs +++ b/crates/wdk-build/src/cargo_make.rs @@ -1017,9 +1017,9 @@ pub fn package_driver_flow_condition_script() -> anyhow::Result<()> { /// `generate-certificate` `cargo-make` task should be skipped. This only /// occurs when the `WdrLocalTestCert` cannot be added from the build directory /// to `WDRTestCertStore`. -/// +/// /// # Panics -/// +/// /// Panics if certmgr is unable to be run. pub fn generate_certificate_condition_script() -> anyhow::Result<()> { condition_script(|| { From 9dcc00a6dd28b009ecdf57dd5527b46bfb40c773 Mon Sep 17 00:00:00 2001 From: leon-xd Date: Tue, 14 Jan 2025 17:21:08 -0800 Subject: [PATCH 5/6] addressed Melvin's comments --- crates/wdk-build/src/cargo_make.rs | 32 ++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/crates/wdk-build/src/cargo_make.rs b/crates/wdk-build/src/cargo_make.rs index 19b10179..4904db68 100644 --- a/crates/wdk-build/src/cargo_make.rs +++ b/crates/wdk-build/src/cargo_make.rs @@ -1015,15 +1015,18 @@ pub fn package_driver_flow_condition_script() -> anyhow::Result<()> { /// /// This functions returns an error whenever it determines that the /// `generate-certificate` `cargo-make` task should be skipped. This only -/// occurs when the `WdrLocalTestCert` cannot be added from the build directory -/// to `WDRTestCertStore`. +/// occurs when `WdrLocalTestCert` already exists in `WDRTestCertStore`. /// /// # Panics /// -/// Panics if certmgr is unable to be run. +/// Panics if `CARGO_MAKE_CURRENT_TASK_NAME` is not set in the environment. pub fn generate_certificate_condition_script() -> anyhow::Result<()> { condition_script(|| { - let output = Command::new("certmgr") + let cert_save_location = get_wdk_build_output_directory().join("WDRLocalTestCert.cer"); + + let mut command = Command::new("certmgr"); + + command .args([ "-put", "-s", @@ -1032,13 +1035,23 @@ pub fn generate_certificate_condition_script() -> anyhow::Result<()> { "-n", "WdrLocalTestCert", ]) - .arg(get_wdk_build_output_directory().join("WDRLocalTestCert.cer")) - .output() - .expect("Failed to run certmgr.exe."); + .arg(cert_save_location); + + let output = command.output().unwrap_or_else(|err| { + panic!( + "Failed to run certmgr.exe {} due to error: {}", + command + .get_args() + .map(|arg| arg.to_string_lossy()) + .collect::>() + .join(" "), + err + ) + }); match output.status.code() { Some(0) => Err(anyhow::anyhow!( - "WDRLocalTestCert already in WDRTestCertStore. Skipping certificate generation." + "WDRLocalTestCert found in WDRTestCertStore. Skipping certificate generation." )), Some(1) => Ok(()), Some(_) => { @@ -1046,8 +1059,7 @@ pub fn generate_certificate_condition_script() -> anyhow::Result<()> { Ok(()) } None => { - eprintln!("No status code found from certmgr. Generating certificate."); - Ok(()) + unreachable!("Unreachable, no status code found from certmgr."); } } }) From 016442b604a494a7623952bdd48d16298abf3704 Mon Sep 17 00:00:00 2001 From: leon-xd Date: Wed, 15 Jan 2025 11:30:29 -0800 Subject: [PATCH 6/6] added old generation print --- crates/wdk-build/src/cargo_make.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/wdk-build/src/cargo_make.rs b/crates/wdk-build/src/cargo_make.rs index 4904db68..0327a828 100644 --- a/crates/wdk-build/src/cargo_make.rs +++ b/crates/wdk-build/src/cargo_make.rs @@ -1053,9 +1053,14 @@ pub fn generate_certificate_condition_script() -> anyhow::Result<()> { Some(0) => Err(anyhow::anyhow!( "WDRLocalTestCert found in WDRTestCertStore. Skipping certificate generation." )), - Some(1) => Ok(()), + Some(1) => { + println!( + "WDRLocalTestCert not found in WDRTestCertStore. Generating new certificate." + ); + Ok(()) + } Some(_) => { - eprintln!("Unknown status code found from certmgr. Generating certificate."); + eprintln!("Unknown status code found from certmgr. Generating new certificate."); Ok(()) } None => {