From 0fc2bccd44545c8852145a27587de3b0c8919e35 Mon Sep 17 00:00:00 2001 From: Zhang Junyang Date: Sun, 9 Feb 2025 21:05:32 +0800 Subject: [PATCH] Let OSDK forward commands to the base crate if it is kernel --- osdk/src/cli.rs | 11 ++++++--- osdk/src/commands/mod.rs | 51 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/osdk/src/cli.rs b/osdk/src/cli.rs index e46279c84..9b937c7e3 100644 --- a/osdk/src/cli.rs +++ b/osdk/src/cli.rs @@ -8,7 +8,8 @@ use crate::{ arch::Arch, commands::{ execute_build_command, execute_debug_command, execute_forwarded_command, - execute_new_command, execute_profile_command, execute_run_command, execute_test_command, + execute_forwarded_command_on_each_crate, execute_new_command, execute_profile_command, + execute_run_command, execute_test_command, }, config::{ manifest::{ProjectType, TomlManifest}, @@ -55,8 +56,12 @@ pub fn main() { OsdkSubcommand::Test(test_args) => { execute_test_command(&load_config(&test_args.common_args), test_args); } - OsdkSubcommand::Check(args) => execute_forwarded_command("check", &args.args, true), - OsdkSubcommand::Clippy(args) => execute_forwarded_command("clippy", &args.args, true), + OsdkSubcommand::Check(args) => { + execute_forwarded_command_on_each_crate("check", &args.args, true) + } + OsdkSubcommand::Clippy(args) => { + execute_forwarded_command_on_each_crate("clippy", &args.args, true) + } OsdkSubcommand::Doc(args) => execute_forwarded_command("doc", &args.args, false), } } diff --git a/osdk/src/commands/mod.rs b/osdk/src/commands/mod.rs index 875bf4a65..a4893306b 100644 --- a/osdk/src/commands/mod.rs +++ b/osdk/src/commands/mod.rs @@ -10,17 +10,24 @@ mod run; mod test; mod util; +use util::DEFAULT_TARGET_RELPATH; + pub use self::{ build::execute_build_command, debug::execute_debug_command, new::execute_new_command, profile::execute_profile_command, run::execute_run_command, test::execute_test_command, }; -use crate::arch::get_default_arch; +use crate::{ + arch::get_default_arch, + base_crate::{new_base_crate, BaseCrateType}, + error_msg, + util::{get_current_crates, get_target_directory, DirGuard}, +}; /// Execute the forwarded cargo command with arguments. /// /// The `cfg_ktest` parameter controls whether `cfg(ktest)` is enabled. -pub fn execute_forwarded_command(subcommand: &str, args: &Vec, cfg_ktest: bool) -> ! { +pub fn execute_forwarded_command(subcommand: &str, args: &Vec, cfg_ktest: bool) { let mut cargo = util::cargo(); cargo.arg(subcommand).args(util::COMMON_CARGO_ARGS); if !args.contains(&"--target".to_owned()) { @@ -39,5 +46,43 @@ pub fn execute_forwarded_command(subcommand: &str, args: &Vec, cfg_ktest cargo.env("RUSTFLAGS", rustflags); let status = cargo.status().expect("Failed to execute cargo"); - std::process::exit(status.code().unwrap_or(1)); + if !status.success() { + error_msg!("Command {:?} failed with status: {:?}", cargo, status); + std::process::exit(status.code().unwrap_or(1)); + } +} + +/// Execute the forwarded cargo command on each crate in the workspace. +/// +/// It works like invoking [`execute_forwarded_command`] on each crate in the +/// workspace, but it creates a base crate that depends on the target crate and +/// executes the command on the base crate if a target crate is a kernel crate. +/// +/// It invokes Cargo on the base crate only if the target crate is a kernel +/// crate. Otherwise, it behaves just like [`execute_forwarded_command`]. +pub fn execute_forwarded_command_on_each_crate( + subcommand: &str, + args: &Vec, + cfg_ktest: bool, +) { + let cargo_target_directory = get_target_directory(); + let osdk_output_directory = cargo_target_directory.join(DEFAULT_TARGET_RELPATH); + + let target_crates = get_current_crates(); + for target in target_crates { + if !target.is_kernel_crate { + let _dir_guard = DirGuard::change_dir(target.path); + execute_forwarded_command(subcommand, args, cfg_ktest); + } else { + let base_crate_path = new_base_crate( + BaseCrateType::Other, + osdk_output_directory.join(&target.name), + &target.name, + target.path, + false, + ); + let _dir_guard = DirGuard::change_dir(&base_crate_path); + execute_forwarded_command(subcommand, args, cfg_ktest); + } + } }