From 17c8da745976cca149d656af31cee182e11d44de Mon Sep 17 00:00:00 2001 From: Zhang Junyang Date: Wed, 19 Feb 2025 21:21:33 +0800 Subject: [PATCH] Fix the problem that OSDK picks the test kernel by default --- Makefile | 12 ++++++------ osdk/src/cli.rs | 2 +- osdk/src/commands/build/mod.rs | 21 ++++++--------------- osdk/src/commands/debug.rs | 4 ++-- osdk/src/commands/profile.rs | 4 ++-- osdk/src/commands/run.rs | 20 ++++---------------- osdk/src/commands/test.rs | 6 ++++++ osdk/src/commands/util.rs | 4 ++-- osdk/src/error.rs | 1 + osdk/src/util.rs | 19 +++++++++++++++++++ 10 files changed, 49 insertions(+), 44 deletions(-) diff --git a/Makefile b/Makefile index fd8d9b5e..1cb9cc62 100644 --- a/Makefile +++ b/Makefile @@ -197,7 +197,7 @@ initramfs: .PHONY: build build: initramfs $(CARGO_OSDK) - @cargo osdk build $(CARGO_OSDK_ARGS) + @cd kernel && cargo osdk build $(CARGO_OSDK_ARGS) .PHONY: tools tools: @@ -205,7 +205,7 @@ tools: .PHONY: run run: initramfs $(CARGO_OSDK) - @cargo osdk run $(CARGO_OSDK_ARGS) + @cd kernel && cargo osdk run $(CARGO_OSDK_ARGS) # Check the running status of auto tests from the QEMU log ifeq ($(AUTO_TEST), syscall) @tail --lines 100 qemu.log | grep -q "^.* of .* test cases passed." \ @@ -223,19 +223,19 @@ endif .PHONY: gdb_server gdb_server: initramfs $(CARGO_OSDK) - @cargo osdk run $(CARGO_OSDK_ARGS) --gdb-server wait-client,vscode,addr=:$(GDB_TCP_PORT) + @cd kernel && cargo osdk run $(CARGO_OSDK_ARGS) --gdb-server wait-client,vscode,addr=:$(GDB_TCP_PORT) .PHONY: gdb_client gdb_client: initramfs $(CARGO_OSDK) - @cargo osdk debug $(CARGO_OSDK_ARGS) --remote :$(GDB_TCP_PORT) + @cd kernel && cargo osdk debug $(CARGO_OSDK_ARGS) --remote :$(GDB_TCP_PORT) .PHONY: profile_server profile_server: initramfs $(CARGO_OSDK) - @cargo osdk run $(CARGO_OSDK_ARGS) --gdb-server addr=:$(GDB_TCP_PORT) + @cd kernel && cargo osdk run $(CARGO_OSDK_ARGS) --gdb-server addr=:$(GDB_TCP_PORT) .PHONY: profile_client profile_client: initramfs $(CARGO_OSDK) - @cargo osdk profile $(CARGO_OSDK_ARGS) --remote :$(GDB_TCP_PORT) \ + @cd kernel && cargo osdk profile $(CARGO_OSDK_ARGS) --remote :$(GDB_TCP_PORT) \ --samples $(GDB_PROFILE_COUNT) --interval $(GDB_PROFILE_INTERVAL) --format $(GDB_PROFILE_FORMAT) .PHONY: test diff --git a/osdk/src/cli.rs b/osdk/src/cli.rs index 9b937c7e..7482e30a 100644 --- a/osdk/src/cli.rs +++ b/osdk/src/cli.rs @@ -308,7 +308,7 @@ impl DebugProfileOutArgs { .display() ) } else { - let crate_name = crate::util::get_current_crates().remove(0).name; + let crate_name = crate::util::get_kernel_crate().name; let time_stamp = std::time::SystemTime::now(); let time_stamp: DateTime = time_stamp.into(); let time_stamp = time_stamp.format("%H%M%S"); diff --git a/osdk/src/commands/build/mod.rs b/osdk/src/commands/build/mod.rs index 22ecf014..c862ccbd 100644 --- a/osdk/src/commands/build/mod.rs +++ b/osdk/src/commands/build/mod.rs @@ -29,7 +29,10 @@ use crate::{ }, error::Errno, error_msg, - util::{get_cargo_metadata, get_current_crates, get_target_directory, CrateInfo, DirGuard}, + util::{ + get_cargo_metadata, get_current_crates, get_kernel_crate, get_target_directory, CrateInfo, + DirGuard, + }, }; pub fn execute_build_command(config: &Config, build_args: &BuildArgs) { @@ -42,19 +45,7 @@ pub fn execute_build_command(config: &Config, build_args: &BuildArgs) { std::fs::create_dir_all(&osdk_output_directory).unwrap(); } - let targets = get_current_crates(); - let mut target_info = None; - for target in targets { - if target.is_kernel_crate { - target_info = Some(target); - break; - } - } - - let target_info = target_info.unwrap_or_else(|| { - error_msg!("No kernel crate found in the current workspace"); - process::exit(Errno::NoKernelCrate as _); - }); + let target_info = get_kernel_crate(); let bundle_path = osdk_output_directory.join(target_info.name.clone()); @@ -91,7 +82,7 @@ pub fn create_base_and_cached_build( }, osdk_output_directory.as_ref().join(&target_crate.name), &target_crate.name, - target_crate.path, + &target_crate.path, false, ); let _dir_guard = DirGuard::change_dir(&base_crate_path); diff --git a/osdk/src/commands/debug.rs b/osdk/src/commands/debug.rs index 261e2ab7..30961ae5 100644 --- a/osdk/src/commands/debug.rs +++ b/osdk/src/commands/debug.rs @@ -3,7 +3,7 @@ use crate::{ cli::DebugArgs, commands::util::bin_file_name, - util::{get_current_crates, get_target_directory}, + util::{get_kernel_crate, get_target_directory}, }; use std::process::Command; @@ -12,7 +12,7 @@ pub fn execute_debug_command(_profile: &str, args: &DebugArgs) { let file_path = get_target_directory() .join("osdk") - .join(get_current_crates().remove(0).name) + .join(get_kernel_crate().name) .join(bin_file_name()); println!("Debugging {}", file_path.display()); diff --git a/osdk/src/commands/profile.rs b/osdk/src/commands/profile.rs index eda1b186..a11529af 100644 --- a/osdk/src/commands/profile.rs +++ b/osdk/src/commands/profile.rs @@ -13,7 +13,7 @@ use inferno::flamegraph; use crate::{ cli::{ProfileArgs, ProfileFormat}, commands::util::bin_file_name, - util::{get_current_crates, get_target_directory}, + util::{get_kernel_crate, get_target_directory}, }; use regex::Regex; use std::{ @@ -58,7 +58,7 @@ macro_rules! profile_round_delimiter { fn do_collect_stack_traces(args: &ProfileArgs) { let file_path = get_target_directory() .join("osdk") - .join(get_current_crates().remove(0).name) + .join(get_kernel_crate().name) .join(bin_file_name()); let remote = &args.remote; diff --git a/osdk/src/commands/run.rs b/osdk/src/commands/run.rs index 5a2435aa..42a96ca2 100644 --- a/osdk/src/commands/run.rs +++ b/osdk/src/commands/run.rs @@ -12,7 +12,7 @@ use crate::{ config::{scheme::ActionChoice, Config}, error::Errno, error_msg, - util::{get_current_crates, get_target_directory}, + util::{get_kernel_crate, get_target_directory}, warn_msg, }; @@ -20,19 +20,7 @@ pub fn execute_run_command(config: &Config, gdb_server_args: Option<&str>) { let cargo_target_directory = get_target_directory(); let osdk_output_directory = cargo_target_directory.join(DEFAULT_TARGET_RELPATH); - let targets = get_current_crates(); - let mut target_info = None; - for target in targets { - if target.is_kernel_crate { - target_info = Some(target); - break; - } - } - - let target_info = target_info.unwrap_or_else(|| { - error_msg!("No kernel crate found in the current workspace"); - exit(Errno::NoKernelCrate as _); - }); + let target_info = get_kernel_crate(); let mut config = config.clone(); @@ -187,7 +175,7 @@ mod gdb { mod vsc { use crate::{ commands::util::bin_file_name, - util::{get_cargo_metadata, get_current_crates}, + util::{get_cargo_metadata, get_kernel_crate}, }; use serde_json::{from_str, Value}; use std::{ @@ -301,7 +289,7 @@ mod vsc { ) -> Result<(), std::io::Error> { let contents = include_str!("launch.json.template") .replace("#PROFILE#", profile) - .replace("#CRATE_NAME#", &get_current_crates().remove(0).name) + .replace("#CRATE_NAME#", &get_kernel_crate().name) .replace("#BIN_NAME#", &bin_file_name()) .replace( "#ADDR_PORT#", diff --git a/osdk/src/commands/test.rs b/osdk/src/commands/test.rs index 3f1bd280..701351ac 100644 --- a/osdk/src/commands/test.rs +++ b/osdk/src/commands/test.rs @@ -21,7 +21,13 @@ pub fn execute_test_command(config: &Config, args: &TestArgs) { } pub fn test_current_crate(config: &Config, args: &TestArgs) { + let current_crates = get_current_crates(); + if current_crates.len() != 1 { + error_msg!("The current directory contains more than one crate"); + std::process::exit(Errno::TooManyCrates as _); + } let current_crate = get_current_crates().remove(0); + let cargo_target_directory = get_target_directory(); let osdk_output_directory = cargo_target_directory.join(DEFAULT_TARGET_RELPATH); diff --git a/osdk/src/commands/util.rs b/osdk/src/commands/util.rs index 3bd6bd04..e21dd53a 100644 --- a/osdk/src/commands/util.rs +++ b/osdk/src/commands/util.rs @@ -2,7 +2,7 @@ use std::process::Command; -use crate::util::get_current_crates; +use crate::util::get_kernel_crate; pub const COMMON_CARGO_ARGS: &[&str] = &[ "-Zbuild-std=core,alloc,compiler_builtins", @@ -23,7 +23,7 @@ pub fn profile_name_adapter(profile: &str) -> &str { } pub fn bin_file_name() -> String { - get_current_crates().remove(0).name + "-osdk-bin" + get_kernel_crate().name + "-osdk-bin" } pub(crate) fn is_tdx_enabled() -> bool { diff --git a/osdk/src/error.rs b/osdk/src/error.rs index b3376a0b..67953522 100644 --- a/osdk/src/error.rs +++ b/osdk/src/error.rs @@ -13,6 +13,7 @@ pub enum Errno { RunBundle = 8, BadCrateName = 9, NoKernelCrate = 10, + TooManyCrates = 11, } /// Print error message to console diff --git a/osdk/src/util.rs b/osdk/src/util.rs index 63732601..ad8a645d 100644 --- a/osdk/src/util.rs +++ b/osdk/src/util.rs @@ -144,6 +144,25 @@ pub fn get_current_crates() -> Vec { result } +/// Get the kernel crate in the current directory. +/// +/// If the current directory is a virtual workspace and no/multiple kernel +/// crate is found, This function will print an error message and exit the +/// process. +pub fn get_kernel_crate() -> CrateInfo { + let crates = get_current_crates(); + let kernel_crates: Vec<_> = crates.iter().filter(|c| c.is_kernel_crate).collect(); + if kernel_crates.len() == 1 { + kernel_crates[0].clone() + } else if kernel_crates.is_empty() { + error_msg!("No kernel crate found in the current workspace"); + std::process::exit(Errno::NoKernelCrate as _); + } else { + error_msg!("Multiple kernel crates found in the current workspace"); + std::process::exit(Errno::TooManyCrates as _); + } +} + fn package_contains_ostd_main(package: &serde_json::Value) -> bool { let src_path = { let targets = package.get("targets").unwrap().as_array().unwrap();