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,