Skip to content

Commit

Permalink
refactor: move infverif task's condition script logic to cargo_make.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
svasista-ms committed Sep 9, 2024
1 parent 659b077 commit 2723732
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
20 changes: 1 addition & 19 deletions crates/wdk-build/rust-driver-sample-makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> = 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::<u32>(&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()?
'''
27 changes: 26 additions & 1 deletion crates/wdk-build/src/cargo_make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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: <https://github.com/clap-rs/clap/blob/14fd853fb9c5b94e371170bbd0ca2bf28ef3abff/clap_builder/src/util/mod.rs#L30C18-L30C28>
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<u32> = 25798..;

trait ParseCargoArgs {
fn parse_cargo_args(&self);
}
Expand Down Expand Up @@ -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::<u32>(&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;
Expand Down
2 changes: 1 addition & 1 deletion crates/wdk-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 2723732

Please sign in to comment.