Fix the problem that OSDK picks the test kernel by default

This commit is contained in:
Zhang Junyang 2025-02-19 21:21:33 +08:00 committed by Tate, Hongliang Tian
parent 29791ba77e
commit 17c8da7459
10 changed files with 49 additions and 44 deletions

View File

@ -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

View File

@ -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<Local> = time_stamp.into();
let time_stamp = time_stamp.format("%H%M%S");

View File

@ -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);

View File

@ -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());

View File

@ -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;

View File

@ -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#",

View File

@ -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);

View File

@ -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 {

View File

@ -13,6 +13,7 @@ pub enum Errno {
RunBundle = 8,
BadCrateName = 9,
NoKernelCrate = 10,
TooManyCrates = 11,
}
/// Print error message to console

View File

@ -144,6 +144,25 @@ pub fn get_current_crates() -> Vec<CrateInfo> {
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();