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: move infverif task's condition script logic to cargo_make.rs #216

Merged
merged 7 commits into from
Sep 23, 2024
23 changes: 4 additions & 19 deletions crates/wdk-build/rust-driver-sample-makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,11 @@ condition_script = '''
//! ```cargo
//! [dependencies]
//! wdk-build = { path = ".", version = "0.2.0" }
//! anyhow = "1"
//! ```
#![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(())
})?
fn main() -> anyhow::Result<()> {
wdk_build::cargo_make::driver_sample_infverif_condition_script()
}
'''
46 changes: 45 additions & 1 deletion crates/wdk-build/src/cargo_make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//! provide a CLI very close to cargo's own, but only exposes the arguments
//! supported by `rust-driver-makefile.toml`.

use core::ops::RangeFrom;
use std::{
env,
panic::UnwindSafe,
Expand All @@ -23,7 +24,12 @@ 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 +67,10 @@ 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 +1062,40 @@ 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)
///
/// # Panics
/// Panics if `CARGO_MAKE_CURRENT_TASK_NAME` is not set in the environment
pub fn driver_sample_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).expect("Failed to get WDK version number"),
)
.unwrap_or_else(|_| {
panic!("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 {wdk_build_number} is bugged and does \
not contain the /samples flag.",
)));
}
Ok(())
})
}

#[cfg(test)]
mod tests {
use crate::ConfigError;
Expand Down
5 changes: 2 additions & 3 deletions crates/wdk-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ use metadata::TryFromCargoMetadataError;

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
3 changes: 3 additions & 0 deletions crates/wdk-build/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright (c) Microsoft Corporation
// License: MIT OR Apache-2.0

//! Private module for utility code related to the cargo-make experience for
//! building drivers.

use std::{
env,
ffi::CStr,
Expand Down
Loading