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 18, 2024
1 parent 31237d7 commit dd966ba
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 126 deletions.
45 changes: 45 additions & 0 deletions 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 @@ -478,6 +481,48 @@ pub fn setup_path() -> Result<(), ConfigError> {
Ok(())
}

/// Returns the path to the WDK build output directory for the current
/// cargo-make flow
pub fn get_wdk_build_output_directory() -> PathBuf {
PathBuf::from(std::env::var("WDK_BUILD_OUTPUT_DIRECTORY").expect(
"WDK_BUILD_OUTPUT_DIRECTORY should be set by cargo-make via the env section of \
rust-driver-makefile.toml",
))
}

/// Returns the name of the current cargo package cargo-make is processing
pub fn get_current_package_name() -> String {
std::env::var(CARGO_MAKE_CRATE_FS_NAME_ENV_VAR).expect(&format!(
"{} 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
pub fn copy_to_driver_package_folder<P: AsRef<Path>>(path_to_copy: P) {
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).expect(&format!(
"creation of '{}' folder should succeed",
package_folder_path.display()
));
}

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).expect(&format!(
"copy of '{}' file to '{}' file should succeed",
path_to_copy.display(),
destination_path.display()
));
}

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 dd966ba

Please sign in to comment.