mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-26 10:53:25 +00:00
Refactor the generation of base crates
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
32a6025819
commit
cb2d8412aa
@ -16,7 +16,7 @@ use bin::make_elf_for_qemu;
|
||||
use super::util::{cargo, profile_name_adapter, COMMON_CARGO_ARGS, DEFAULT_TARGET_RELPATH};
|
||||
use crate::{
|
||||
arch::Arch,
|
||||
base_crate::new_base_crate,
|
||||
base_crate::{new_base_crate, BaseCrateType},
|
||||
bundle::{
|
||||
bin::{AsterBin, AsterBinType, AsterElfMeta},
|
||||
file::BundleFile,
|
||||
@ -29,7 +29,7 @@ use crate::{
|
||||
},
|
||||
error::Errno,
|
||||
error_msg,
|
||||
util::{get_cargo_metadata, get_current_crates, get_target_directory},
|
||||
util::{get_cargo_metadata, get_current_crates, get_target_directory, CrateInfo, DirGuard},
|
||||
};
|
||||
|
||||
pub fn execute_build_command(config: &Config, build_args: &BuildArgs) {
|
||||
@ -65,6 +65,7 @@ pub fn execute_build_command(config: &Config, build_args: &BuildArgs) {
|
||||
};
|
||||
|
||||
let _bundle = create_base_and_cached_build(
|
||||
target_info,
|
||||
bundle_path,
|
||||
&osdk_output_directory,
|
||||
&cargo_target_directory,
|
||||
@ -75,6 +76,7 @@ pub fn execute_build_command(config: &Config, build_args: &BuildArgs) {
|
||||
}
|
||||
|
||||
pub fn create_base_and_cached_build(
|
||||
target_crate: CrateInfo,
|
||||
bundle_path: impl AsRef<Path>,
|
||||
osdk_output_directory: impl AsRef<Path>,
|
||||
cargo_target_directory: impl AsRef<Path>,
|
||||
@ -82,25 +84,25 @@ pub fn create_base_and_cached_build(
|
||||
action: ActionChoice,
|
||||
rustflags: &[&str],
|
||||
) -> Bundle {
|
||||
let base_crate_path = osdk_output_directory.as_ref().join("base");
|
||||
new_base_crate(
|
||||
&base_crate_path,
|
||||
&get_current_crates().remove(0).name,
|
||||
get_current_crates().remove(0).path,
|
||||
let base_crate_path = new_base_crate(
|
||||
match action {
|
||||
ActionChoice::Run => BaseCrateType::Run,
|
||||
ActionChoice::Test => BaseCrateType::Test,
|
||||
},
|
||||
osdk_output_directory.as_ref().join(&target_crate.name),
|
||||
&target_crate.name,
|
||||
target_crate.path,
|
||||
false,
|
||||
);
|
||||
let original_dir = std::env::current_dir().unwrap();
|
||||
std::env::set_current_dir(&base_crate_path).unwrap();
|
||||
let bundle = do_cached_build(
|
||||
let _dir_guard = DirGuard::change_dir(&base_crate_path);
|
||||
do_cached_build(
|
||||
&bundle_path,
|
||||
&osdk_output_directory,
|
||||
&cargo_target_directory,
|
||||
config,
|
||||
action,
|
||||
rustflags,
|
||||
);
|
||||
std::env::set_current_dir(original_dir).unwrap();
|
||||
bundle
|
||||
)
|
||||
}
|
||||
|
||||
fn get_reusable_existing_bundle(
|
||||
|
@ -21,15 +21,15 @@ pub fn execute_run_command(config: &Config, gdb_server_args: Option<&str>) {
|
||||
let osdk_output_directory = cargo_target_directory.join(DEFAULT_TARGET_RELPATH);
|
||||
|
||||
let targets = get_current_crates();
|
||||
let mut target_name = None;
|
||||
let mut target_info = None;
|
||||
for target in targets {
|
||||
if target.is_kernel_crate {
|
||||
target_name = Some(target.name);
|
||||
target_info = Some(target);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let target_name = target_name.unwrap_or_else(|| {
|
||||
let target_info = target_info.unwrap_or_else(|| {
|
||||
error_msg!("No kernel crate found in the current workspace");
|
||||
exit(Errno::NoKernelCrate as _);
|
||||
});
|
||||
@ -42,8 +42,9 @@ pub fn execute_run_command(config: &Config, gdb_server_args: Option<&str>) {
|
||||
None
|
||||
};
|
||||
|
||||
let default_bundle_directory = osdk_output_directory.join(target_name);
|
||||
let default_bundle_directory = osdk_output_directory.join(&target_info.name);
|
||||
let bundle = create_base_and_cached_build(
|
||||
target_info,
|
||||
default_bundle_directory,
|
||||
&osdk_output_directory,
|
||||
&cargo_target_directory,
|
||||
|
@ -4,12 +4,12 @@ use std::fs;
|
||||
|
||||
use super::{build::do_cached_build, util::DEFAULT_TARGET_RELPATH};
|
||||
use crate::{
|
||||
base_crate::new_base_crate,
|
||||
base_crate::{new_base_crate, BaseCrateType},
|
||||
cli::TestArgs,
|
||||
config::{scheme::ActionChoice, Config},
|
||||
error::Errno,
|
||||
error_msg,
|
||||
util::{get_current_crates, get_target_directory},
|
||||
util::{get_current_crates, get_target_directory, DirGuard},
|
||||
};
|
||||
|
||||
pub fn execute_test_command(config: &Config, args: &TestArgs) {
|
||||
@ -24,8 +24,6 @@ pub fn test_current_crate(config: &Config, args: &TestArgs) {
|
||||
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`
|
||||
let target_crate_dir = osdk_output_directory.join("test-base");
|
||||
|
||||
// A special case is that we use OSDK to test the OSDK test runner crate
|
||||
// itself. We check it by name.
|
||||
@ -40,8 +38,9 @@ pub fn test_current_crate(config: &Config, args: &TestArgs) {
|
||||
false
|
||||
};
|
||||
|
||||
new_base_crate(
|
||||
&target_crate_dir,
|
||||
let target_crate_dir = new_base_crate(
|
||||
BaseCrateType::Test,
|
||||
osdk_output_directory.join(¤t_crate.name),
|
||||
¤t_crate.name,
|
||||
¤t_crate.path,
|
||||
!runner_self_test,
|
||||
@ -54,7 +53,7 @@ pub fn test_current_crate(config: &Config, args: &TestArgs) {
|
||||
None => r#"None"#.to_string(),
|
||||
};
|
||||
|
||||
let mut ktest_crate_whitelist = vec![current_crate.name];
|
||||
let mut ktest_crate_whitelist = vec![current_crate.name.clone()];
|
||||
if let Some(name) = &args.test_name {
|
||||
ktest_crate_whitelist.push(name.clone());
|
||||
}
|
||||
@ -85,10 +84,8 @@ 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_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();
|
||||
let default_bundle_directory = osdk_output_directory.join(¤t_crate.name);
|
||||
let dir_guard = DirGuard::change_dir(&target_crate_dir);
|
||||
let bundle = do_cached_build(
|
||||
default_bundle_directory,
|
||||
&osdk_output_directory,
|
||||
@ -98,7 +95,7 @@ pub static KTEST_CRATE_WHITELIST: Option<&[&str]> = Some(&{:#?});
|
||||
&["--cfg ktest"],
|
||||
);
|
||||
std::env::remove_var("RUSTFLAGS");
|
||||
std::env::set_current_dir(original_dir).unwrap();
|
||||
drop(dir_guard);
|
||||
|
||||
bundle.run(config, ActionChoice::Test);
|
||||
}
|
||||
|
Reference in New Issue
Block a user