From 272373200c9f85568dc916fbd501779890654ade Mon Sep 17 00:00:00 2001 From: Shravan Vasista Date: Mon, 9 Sep 2024 01:06:42 -0700 Subject: [PATCH] refactor: move infverif task's condition script logic to cargo_make.rs --- .../rust-driver-sample-makefile.toml | 20 +------------- crates/wdk-build/src/cargo_make.rs | 27 ++++++++++++++++++- crates/wdk-build/src/lib.rs | 2 +- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/crates/wdk-build/rust-driver-sample-makefile.toml b/crates/wdk-build/rust-driver-sample-makefile.toml index 9fdadb3a..1406cba1 100644 --- a/crates/wdk-build/rust-driver-sample-makefile.toml +++ b/crates/wdk-build/rust-driver-sample-makefile.toml @@ -41,23 +41,5 @@ condition_script = ''' //! ``` #![allow(unused_doc_comments)] -use core::ops::RangeFrom; - -// This range is inclusive of 25798. FIXME: update with range end after /sample flag is added to InfVerif CLI -const MISSING_SAMPLE_FLAG_WDK_BUILD_NUMBER_RANGE: RangeFrom = 25798..; - -std::panic::catch_unwind(|| { - let wdk_version = std::env::var(wdk_build::cargo_make::WDK_VERSION_ENV_VAR).expect("WDK_BUILD_DETECTED_VERSION should always be set by wdk-build-init cargo make task"); - let wdk_build_number = str::parse::(&wdk_build::utils::get_wdk_version_number(&wdk_version).unwrap()).expect(&format!("Couldn't parse WDK version number! Version number: {}", wdk_version)); - - if MISSING_SAMPLE_FLAG_WDK_BUILD_NUMBER_RANGE.contains(&wdk_build_number) { - // cargo_make will interpret returning an error from the rust-script condition_script as skipping the task - return Err::<(), String>(format!("Skipping InfVerif. InfVerif in WDK Build {wdk_build_number} is bugged and does not contain the /samples flag.").into()); - } - Ok(()) -}).unwrap_or_else(|_|{ - // panic contents would have already been printed to console - eprintln!("condition_script for infverif task panicked while executing. Defaulting to running inverif task."); - Ok(()) -})? +wdk_build::cargo_make::infverif_condition_script()? ''' diff --git a/crates/wdk-build/src/cargo_make.rs b/crates/wdk-build/src/cargo_make.rs index 29f4dc48..1291f4ab 100644 --- a/crates/wdk-build/src/cargo_make.rs +++ b/crates/wdk-build/src/cargo_make.rs @@ -17,13 +17,15 @@ use std::{ path::{Path, PathBuf}, }; +use core::ops::RangeFrom; + use anyhow::Context; use cargo_metadata::{camino::Utf8Path, Metadata, MetadataCommand}; use clap::{Args, Parser}; use crate::{ metadata, - utils::{detect_wdk_content_root, get_latest_windows_sdk_version, PathExt}, + utils::{detect_wdk_content_root, get_latest_windows_sdk_version, get_wdk_version_number, PathExt}, ConfigError, CpuArchitecture, }; @@ -61,6 +63,9 @@ const CARGO_MAKE_CURRENT_TASK_NAME_ENV_VAR: &str = "CARGO_MAKE_CURRENT_TASK_NAME /// `clap` uses an exit code of 2 for usage errors: const CLAP_USAGE_EXIT_CODE: i32 = 2; +// This range is inclusive of 25798. FIXME: update with range end after /sample flag is added to InfVerif CLI +const MISSING_SAMPLE_FLAG_WDK_BUILD_NUMBER_RANGE: RangeFrom = 25798..; + trait ParseCargoArgs { fn parse_cargo_args(&self); } @@ -1052,6 +1057,26 @@ where env::set_var(env_var_name, env_var_value); } +/// `cargo-make` condition script for `infverif` task in +/// [`rust-driver-sample-makefile.toml`](../rust-driver-sample-makefile.toml) +/// +/// # Errors +/// +/// This function returns an error whenever it determines that the +/// `infverif` `cargo-make` task should be skipped (i.e. when the WDK Version is bugged +/// and does not contain /samples flag +pub fn infverif_condition_script() -> anyhow::Result<()> { + condition_script(|| { + let wdk_version = env::var(WDK_VERSION_ENV_VAR).expect("WDK_BUILD_DETECTED_VERSION should always be set by wdk-build-init cargo make task"); + let wdk_build_number = str::parse::(&get_wdk_version_number(&wdk_version).unwrap()).expect(&format!("Couldn't parse WDK version number! Version number: {}", wdk_version)); + if MISSING_SAMPLE_FLAG_WDK_BUILD_NUMBER_RANGE.contains(&wdk_build_number) { + // cargo_make will interpret returning an error from the rust-script condition_script as skipping the task + return Err::<(), anyhow::Error>(anyhow::Error::msg(format!("Skipping InfVerif. InfVerif in WDK Build {} is bugged and does not contain the /samples flag.", wdk_build_number))); + } + Ok(()) + }) +} + #[cfg(test)] mod tests { use crate::ConfigError; diff --git a/crates/wdk-build/src/lib.rs b/crates/wdk-build/src/lib.rs index 64b1a136..8a53fb6c 100644 --- a/crates/wdk-build/src/lib.rs +++ b/crates/wdk-build/src/lib.rs @@ -19,7 +19,7 @@ pub mod cargo_make; pub mod metadata; /// Module for utility code related to the cargo-make experience for building /// drivers. -pub mod utils; +mod utils; mod bindgen;