Refactor the parsing of OSDK crate types

This commit is contained in:
Zhang Junyang
2025-02-09 17:53:02 +08:00
committed by Tate, Hongliang Tian
parent fecf766771
commit 32a6025819
11 changed files with 131 additions and 101 deletions

View File

@ -17,7 +17,7 @@ use crate::{
bin::{AsterBin, AsterBinType, AsterBzImageMeta, AsterElfMeta},
file::BundleFile,
},
util::{get_current_crate_info, hard_link_or_copy},
util::{get_current_crates, hard_link_or_copy},
};
pub fn make_install_bzimage(
@ -27,7 +27,7 @@ pub fn make_install_bzimage(
linux_x86_legacy_boot: bool,
encoding: PayloadEncoding,
) -> AsterBin {
let target_name = get_current_crate_info().name;
let target_name = get_current_crates().remove(0).name;
let image_type = if linux_x86_legacy_boot {
BzImageType::Legacy32
} else {

View File

@ -16,7 +16,7 @@ use crate::{
scheme::{ActionChoice, BootProtocol},
Config,
},
util::{get_current_crate_info, hard_link_or_copy},
util::{get_current_crates, hard_link_or_copy},
};
pub fn create_bootdev_image(
@ -26,7 +26,7 @@ pub fn create_bootdev_image(
config: &Config,
action: ActionChoice,
) -> AsterVmImage {
let target_name = get_current_crate_info().name;
let target_name = get_current_crates().remove(0).name;
let iso_root = &target_dir.as_ref().join("iso_root");
let action = match &action {
ActionChoice::Run => &config.run,
@ -108,7 +108,7 @@ fn generate_grub_cfg(
initramfs_path: Option<String>,
protocol: &BootProtocol,
) -> String {
let target_name = get_current_crate_info().name;
let target_name = get_current_crates().remove(0).name;
let grub_cfg = include_str!("grub.cfg.template").to_string();
// Delete the first two lines that notes the file a template file.

View File

@ -29,7 +29,7 @@ use crate::{
},
error::Errno,
error_msg,
util::{get_cargo_metadata, get_current_crate_info, get_target_directory},
util::{get_cargo_metadata, get_current_crates, get_target_directory},
};
pub fn execute_build_command(config: &Config, build_args: &BuildArgs) {
@ -41,8 +41,22 @@ pub fn execute_build_command(config: &Config, build_args: &BuildArgs) {
if !osdk_output_directory.exists() {
std::fs::create_dir_all(&osdk_output_directory).unwrap();
}
let target_info = get_current_crate_info();
let bundle_path = osdk_output_directory.join(target_info.name);
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 bundle_path = osdk_output_directory.join(target_info.name.clone());
let action = if build_args.for_test {
ActionChoice::Test
@ -71,8 +85,8 @@ pub fn create_base_and_cached_build(
let base_crate_path = osdk_output_directory.as_ref().join("base");
new_base_crate(
&base_crate_path,
&get_current_crate_info().name,
get_current_crate_info().path,
&get_current_crates().remove(0).name,
get_current_crates().remove(0).path,
false,
);
let original_dir = std::env::current_dir().unwrap();
@ -251,7 +265,7 @@ fn build_kernel_elf(
.as_ref()
.join(&target_os_string)
.join(profile_name_adapter(profile))
.join(get_current_crate_info().name);
.join(get_current_crates().remove(0).name);
AsterBin::new(
aster_bin_path,
@ -262,7 +276,7 @@ fn build_kernel_elf(
has_multiboot_header: true,
has_multiboot2_header: true,
}),
get_current_crate_info().version,
get_current_crates().remove(0).version,
false,
)
}

View File

@ -3,7 +3,7 @@
use crate::{
cli::DebugArgs,
commands::util::bin_file_name,
util::{get_current_crate_info, get_target_directory},
util::{get_current_crates, 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_crate_info().name)
.join(get_current_crates().remove(0).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_crate_info, get_target_directory},
util::{get_current_crates, get_target_directory},
};
use regex::Regex;
use std::{collections::HashMap, fs::File, io::Write, path::PathBuf, process::Command};
@ -45,7 +45,7 @@ fn do_parse_stack_traces(target_file: &PathBuf, args: &ProfileArgs) {
fn do_collect_stack_traces(args: &ProfileArgs) {
let file_path = get_target_directory()
.join("osdk")
.join(get_current_crate_info().name)
.join(get_current_crates().remove(0).name)
.join(bin_file_name());
let remote = &args.remote;

View File

@ -12,14 +12,27 @@ use crate::{
config::{scheme::ActionChoice, Config},
error::Errno,
error_msg,
util::{get_current_crate_info, get_target_directory},
util::{get_current_crates, get_target_directory},
warn_msg,
};
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 target_name = get_current_crate_info().name;
let targets = get_current_crates();
let mut target_name = None;
for target in targets {
if target.is_kernel_crate {
target_name = Some(target.name);
break;
}
}
let target_name = target_name.unwrap_or_else(|| {
error_msg!("No kernel crate found in the current workspace");
exit(Errno::NoKernelCrate as _);
});
let mut config = config.clone();
@ -173,7 +186,7 @@ mod gdb {
mod vsc {
use crate::{
commands::util::bin_file_name,
util::{get_cargo_metadata, get_current_crate_info},
util::{get_cargo_metadata, get_current_crates},
};
use serde_json::{from_str, Value};
use std::{
@ -287,7 +300,7 @@ mod vsc {
) -> Result<(), std::io::Error> {
let contents = include_str!("launch.json.template")
.replace("#PROFILE#", profile)
.replace("#CRATE_NAME#", &get_current_crate_info().name)
.replace("#CRATE_NAME#", &get_current_crates().remove(0).name)
.replace("#BIN_NAME#", &bin_file_name())
.replace(
"#ADDR_PORT#",

View File

@ -9,21 +9,19 @@ use crate::{
config::{scheme::ActionChoice, Config},
error::Errno,
error_msg,
util::{
get_cargo_metadata, get_current_crate_info, get_target_directory, parse_package_id_string,
},
util::{get_current_crates, get_target_directory},
};
pub fn execute_test_command(config: &Config, args: &TestArgs) {
let crates = get_workspace_default_members();
for crate_path in crates {
std::env::set_current_dir(crate_path).unwrap();
let crates = get_current_crates();
for crate_info in crates {
std::env::set_current_dir(crate_info.path).unwrap();
test_current_crate(config, args);
}
}
pub fn test_current_crate(config: &Config, args: &TestArgs) {
let current_crate = get_current_crate_info();
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);
// Use a different name for better separation and reusability of `run` and `test`
@ -87,7 +85,7 @@ pub static KTEST_CRATE_WHITELIST: Option<&[&str]> = Some(&{:#?});
fs::write(&main_rs_path, main_rs_content).unwrap();
// Build the kernel with the given base crate
let target_name = get_current_crate_info().name;
let target_name = get_current_crates().remove(0).name;
let default_bundle_directory = osdk_output_directory.join(target_name);
let original_dir = std::env::current_dir().unwrap();
std::env::set_current_dir(&target_crate_dir).unwrap();
@ -104,20 +102,3 @@ pub static KTEST_CRATE_WHITELIST: Option<&[&str]> = Some(&{:#?});
bundle.run(config, ActionChoice::Test);
}
fn get_workspace_default_members() -> Vec<String> {
let metadata = get_cargo_metadata(None::<&str>, None::<&[&str]>).unwrap();
let default_members = metadata
.get("workspace_default_members")
.unwrap()
.as_array()
.unwrap();
default_members
.iter()
.map(|value| {
let default_member = value.as_str().unwrap();
let crate_info = parse_package_id_string(default_member);
crate_info.path
})
.collect()
}

View File

@ -2,7 +2,7 @@
use std::process::Command;
use crate::util::get_current_crate_info;
use crate::util::get_current_crates;
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_crate_info().name + "-osdk-bin"
get_current_crates().remove(0).name + "-osdk-bin"
}
pub(crate) fn is_tdx_enabled() -> bool {