Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: port certificate-generation condition script to Rust #259

Merged
merged 8 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions crates/wdk-build/rust-driver-makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
46 changes: 46 additions & 0 deletions crates/wdk-build/src/cargo_make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::{
env,
panic::UnwindSafe,
path::{Path, PathBuf},
process::Command,
};

use anyhow::Context;
Expand Down Expand Up @@ -1007,6 +1008,51 @@ 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`.
leon-xd marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Panics
///
/// Panics if certmgr is unable to be run.
pub fn generate_certificate_condition_script() -> anyhow::Result<()> {
leon-xd marked this conversation as resolved.
Show resolved Hide resolved
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 run certmgr.exe.");
leon-xd marked this conversation as resolved.
Show resolved Hide resolved

match output.status.code() {
Some(0) => Err(anyhow::anyhow!(
"WDRLocalTestCert already in WDRTestCertStore. Skipping certificate generation."
leon-xd marked this conversation as resolved.
Show resolved Hide resolved
)),
Some(1) => Ok(()),
leon-xd marked this conversation as resolved.
Show resolved Hide resolved
Some(_) => {
eprintln!("Unknown status code found from certmgr. Generating certificate.");
Ok(())
}
None => {
eprintln!("No status code found from certmgr. Generating certificate.");
Ok(())
}
leon-xd marked this conversation as resolved.
Show resolved Hide resolved
}
})
}

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(|_| {
Expand Down
Loading