Prepare OSDK for multi arch support and the upcoming refactor

This commit is contained in:
Zhang Junyang
2024-04-22 22:23:24 +08:00
committed by Tate, Hongliang Tian
parent 0ecb919e73
commit 735d7b7b11
25 changed files with 437 additions and 155 deletions

View File

@ -3,12 +3,13 @@
mod bin;
mod grub;
use std::{path::Path, process};
use std::{ffi::OsString, path::Path, process};
use bin::strip_elf_for_qemu;
use super::util::{cargo, profile_name_adapter, COMMON_CARGO_ARGS, DEFAULT_TARGET_RELPATH};
use super::util::{cargo, COMMON_CARGO_ARGS, DEFAULT_TARGET_RELPATH};
use crate::{
arch::Arch,
base_crate::new_base_crate,
bundle::{
bin::{AsterBin, AsterBinType, AsterElfMeta},
@ -93,7 +94,12 @@ pub fn do_build(
};
info!("Building kernel ELF");
let aster_elf = build_kernel_elf(&config.cargo_args, &cargo_target_directory, rustflags);
let aster_elf = build_kernel_elf(
&config.arch,
&config.cargo_args,
&cargo_target_directory,
rustflags,
);
if matches!(config.manifest.qemu.machine, QemuMachine::Microvm) {
let stripped_elf = strip_elf_for_qemu(&osdk_target_directory, &aster_elf);
@ -118,19 +124,20 @@ pub fn do_build(
}
fn build_kernel_elf(
args: &CargoArgs,
arch: &Arch,
cargo_args: &CargoArgs,
cargo_target_directory: impl AsRef<Path>,
rustflags: &[&str],
) -> AsterBin {
let target = "x86_64-unknown-none";
let target_os_string = OsString::from(&arch.triple());
let rustc_linker_script_arg = format!("-C link-arg=-T{}.ld", arch);
let env_rustflags = std::env::var("RUSTFLAGS").unwrap_or_default();
let mut rustflags = Vec::from(rustflags);
// We disable RELRO and PIC here because they cause link failures
rustflags.extend(vec![
&env_rustflags,
"-C link-arg=-Tx86_64.ld",
"-C code-model=kernel",
&rustc_linker_script_arg,
"-C relocation-model=static",
"-Z relro-level=off",
// We do not really allow unwinding except for kernel testing. However, we need to specify
@ -142,26 +149,25 @@ fn build_kernel_elf(
command.env_remove("RUSTUP_TOOLCHAIN");
command.env("RUSTFLAGS", rustflags.join(" "));
command.arg("build");
command.arg("--target").arg(target);
command.arg("--target").arg(&target_os_string);
command
.arg("--target-dir")
.arg(cargo_target_directory.as_ref());
command.args(COMMON_CARGO_ARGS);
command.arg("--profile=".to_string() + &args.profile);
for override_config in &args.override_configs {
command.arg("--config").arg(override_config);
}
command.arg("--profile=".to_string() + &cargo_args.profile);
let status = command.status().unwrap();
if !status.success() {
error_msg!("Cargo build failed");
process::exit(Errno::ExecuteCommand as _);
}
let aster_bin_path = cargo_target_directory.as_ref().join(target);
let aster_bin_path = aster_bin_path
.join(profile_name_adapter(&args.profile))
.join(get_current_crate_info().name);
let aster_bin_path = cargo_target_directory.as_ref().join(&target_os_string);
let aster_bin_path = if cargo_args.profile == "dev" {
aster_bin_path.join("debug")
} else {
aster_bin_path.join(&cargo_args.profile)
}
.join(get_current_crate_info().name);
AsterBin::new(
aster_bin_path,

View File

@ -1,6 +1,6 @@
// SPDX-License-Identifier: MPL-2.0
use crate::commands::util::{bin_file_name, profile_name_adapter};
use crate::commands::util::{bin_file_name, profile_adapter};
use crate::config_manager::DebugConfig;
use crate::util::get_target_directory;
@ -9,7 +9,7 @@ use std::process::Command;
pub fn execute_debug_command(config: &DebugConfig) {
let DebugConfig { cargo_args, remote } = config;
let profile = profile_name_adapter(&cargo_args.profile);
let profile = profile_adapter(&cargo_args.profile);
let file_path = get_target_directory()
.join("x86_64-unknown-none")
.join(profile)

View File

@ -14,6 +14,8 @@ pub use self::{
run::execute_run_command, test::execute_test_command,
};
use crate::arch::get_default_arch;
/// Execute the forwarded cargo command with args containing the subcommand and its arguments.
pub fn execute_forwarded_command(subcommand: &str, args: &Vec<String>) -> ! {
let mut cargo = util::cargo();
@ -21,7 +23,7 @@ pub fn execute_forwarded_command(subcommand: &str, args: &Vec<String>) -> ! {
.arg(subcommand)
.args(util::COMMON_CARGO_ARGS)
.arg("--target")
.arg("x86_64-unknown-none")
.arg(get_default_arch().triple())
.args(args);
let status = cargo.status().expect("Failed to execute cargo");
std::process::exit(status.code().unwrap_or(1));

View File

@ -60,42 +60,16 @@ pub fn execute_run_command(config: &RunConfig) {
.collect();
let mut manifest = config.manifest.clone();
manifest.qemu.args.extend(qemu_gdb_args);
// FIXME: Disable KVM from QEMU args in debug mode.
// Currently, the QEMU GDB server does not work properly with KVM enabled.
let args_num = manifest.qemu.args.len();
manifest.qemu.args.retain(|x| !x.contains("kvm"));
if manifest.qemu.args.len() != args_num {
println!(
"[WARNING] KVM is forced to be disabled in GDB server currently. \
Options related with KVM are ignored."
);
}
manifest
} else {
config.manifest.clone()
}
},
cargo_args: {
fn is_release_profile(cfg: &RunConfig) -> bool {
cfg.cargo_args.profile == "release" || cfg.cargo_args.release
}
if config.gdb_server_args.is_gdb_enabled && is_release_profile(config) {
let mut cargo_args = config.cargo_args.clone();
cargo_args
.override_configs
.push("profile.release.debug=true".to_owned());
cargo_args
} else {
config.cargo_args.clone()
}
},
..config.clone()
};
let _vsc_launch_file = config.gdb_server_args.vsc_launch_file.then(|| {
vsc::check_gdb_config(&config.gdb_server_args);
let profile = super::util::profile_name_adapter(&config.cargo_args.profile);
let profile = super::util::profile_adapter(&config.cargo_args.profile);
vsc::VscLaunchConfig::new(profile, &config.gdb_server_args.gdb_server_addr)
});
@ -121,9 +95,11 @@ pub fn execute_run_command(config: &RunConfig) {
}
let required_build_config = BuildConfig {
arch: config.arch.clone(),
manifest: config.manifest.clone(),
cargo_args: config.cargo_args.clone(),
};
let bundle = create_base_and_build(
default_bundle_directory,
&osdk_target_directory,

View File

@ -56,6 +56,7 @@ pub static KTEST_CRATE_WHITELIST: Option<&[&str]> = Some(&{:#?});
let target_name = get_current_crate_info().name;
let default_bundle_directory = osdk_target_directory.join(target_name);
let required_build_config = BuildConfig {
arch: config.arch.clone(),
manifest: config.manifest.clone(),
cargo_args: config.cargo_args.clone(),
};
@ -72,6 +73,7 @@ pub static KTEST_CRATE_WHITELIST: Option<&[&str]> = Some(&{:#?});
std::env::set_current_dir(original_dir).unwrap();
let required_run_config = RunConfig {
arch: config.arch.clone(),
manifest: required_build_config.manifest.clone(),
cargo_args: required_build_config.cargo_args.clone(),
gdb_server_args: GdbServerArgs::default(),

View File

@ -15,7 +15,7 @@ pub fn cargo() -> Command {
Command::new("cargo")
}
pub fn profile_name_adapter(profile: &str) -> &str {
pub fn profile_adapter(profile: &str) -> &str {
match profile {
"dev" => "debug",
_ => profile,