Skip to content

Commit

Permalink
feature: support multiple drivers (of same type) in same cargo workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
wmmc88 committed Jan 19, 2024
1 parent 31237d7 commit 3dffa38
Show file tree
Hide file tree
Showing 2 changed files with 220 additions and 127 deletions.
65 changes: 64 additions & 1 deletion crates/wdk-build/src/cargo_make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//! a CLI very close to cargo's own, but only exposes the arguments supported by
//! `rust-driver-makefile.toml`. Help text and other `clap::Arg`

use std::path::{Path, PathBuf};

use clap::{Args, Parser};

use crate::{
Expand All @@ -26,6 +28,7 @@ const CARGO_MAKE_CARGO_PROFILE_ENV_VAR: &str = "CARGO_MAKE_CARGO_PROFILE";
const CARGO_MAKE_CRATE_CUSTOM_TRIPLE_TARGET_DIRECTORY_ENV_VAR: &str =
"CARGO_MAKE_CRATE_CUSTOM_TRIPLE_TARGET_DIRECTORY";
const CARGO_MAKE_RUST_DEFAULT_TOOLCHAIN_ENV_VAR: &str = "CARGO_MAKE_RUST_DEFAULT_TOOLCHAIN";
const CARGO_MAKE_CRATE_FS_NAME_ENV_VAR: &str = "CARGO_MAKE_CRATE_FS_NAME";
const WDK_BUILD_OUTPUT_DIRECTORY_ENV_VAR: &str = "WDK_BUILD_OUTPUT_DIRECTORY";

/// `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>
Expand Down Expand Up @@ -408,7 +411,6 @@ pub fn validate_and_forward_args() {
///
/// This function returns a [`ConfigError::WDKContentRootDetectionError`] if the
/// WDK content root directory could not be found.
/// Sets up the path for the WDK build environment.
///
/// # Panics
///
Expand Down Expand Up @@ -478,6 +480,67 @@ pub fn setup_path() -> Result<(), ConfigError> {
Ok(())
}

/// Returns the path to the WDK build output directory for the current
/// cargo-make flow
///
/// # Panics
///
/// This function will panic if the `WDK_BUILD_OUTPUT_DIRECTORY` environment
/// variable is not set
#[must_use]
pub fn get_wdk_build_output_directory() -> PathBuf {
PathBuf::from(
std::env::var("WDK_BUILD_OUTPUT_DIRECTORY")
.expect("WDK_BUILD_OUTPUT_DIRECTORY should have been set by the wdk-build-init task"),
)
}

/// Returns the name of the current cargo package cargo-make is processing
///
/// # Panics
///
/// This function will panic if the `CARGO_MAKE_CRATE_FS_NAME` environment
/// variable is not set
#[must_use]
pub fn get_current_package_name() -> String {
std::env::var(CARGO_MAKE_CRATE_FS_NAME_ENV_VAR).unwrap_or_else(|_| {
panic!(
"{} should be set by cargo-make",
&CARGO_MAKE_CRATE_FS_NAME_ENV_VAR
)
})
}

/// Copies the file or directory at `path_to_copy` to the Driver Package folder
///
/// # Errors
///
/// This function returns a [`ConfigError::WDKBuildOutputDirectoryError`] if the
/// it encouters IO errors while copying the file or creating the directory
///
/// # Panics
///
/// This function will panic if `path_to_copy` does end with a valid file or
/// directory name
pub fn copy_to_driver_package_folder<P: AsRef<Path>>(path_to_copy: P) -> Result<(), ConfigError> {
let path_to_copy = path_to_copy.as_ref();

let package_folder_path =
get_wdk_build_output_directory().join(format!("{}_package", get_current_package_name()));
if !package_folder_path.exists() {
std::fs::create_dir(&package_folder_path)?;
}

let destination_path = package_folder_path.join(
path_to_copy
.file_name()
.expect("path_to_copy should always end with a valid file or directory name"),
);
std::fs::copy(path_to_copy, destination_path)?;

Ok(())
}

fn configure_wdf_build_output_dir(target_arg: &Option<String>) {
let cargo_make_cargo_profile =
std::env::var(CARGO_MAKE_CARGO_PROFILE_ENV_VAR).unwrap_or_else(|_| {
Expand Down
Loading

0 comments on commit 3dffa38

Please sign in to comment.