Gone with x86_64-custom

This commit is contained in:
Zhang Junyang
2024-02-27 20:28:43 +08:00
committed by Tate, Hongliang Tian
parent e3c227ae06
commit 7eac2772d0
18 changed files with 64 additions and 147 deletions

View File

@ -3,11 +3,7 @@
mod bin;
mod grub;
use std::{
path::{Path, PathBuf},
process,
str::FromStr,
};
use std::{path::Path, process};
use bin::strip_elf_for_qemu;
@ -40,6 +36,7 @@ pub fn execute_build_command(config: &BuildConfig) {
&osdk_target_directory,
&ws_target_directory,
config,
&[],
);
}
@ -48,6 +45,7 @@ pub fn create_base_and_build(
osdk_target_directory: impl AsRef<Path>,
cargo_target_directory: impl AsRef<Path>,
config: &BuildConfig,
rustflags: &[&str],
) -> Bundle {
let base_crate_path = osdk_target_directory.as_ref().join("base");
new_base_crate(
@ -62,6 +60,7 @@ pub fn create_base_and_build(
&osdk_target_directory,
&cargo_target_directory,
config,
rustflags,
);
std::env::set_current_dir(original_dir).unwrap();
bundle
@ -72,6 +71,7 @@ pub fn do_build(
osdk_target_directory: impl AsRef<Path>,
cargo_target_directory: impl AsRef<Path>,
config: &BuildConfig,
rustflags: &[&str],
) -> Bundle {
if bundle_path.as_ref().exists() {
std::fs::remove_dir_all(&bundle_path).unwrap();
@ -93,7 +93,7 @@ pub fn do_build(
};
info!("Building kernel ELF");
let aster_elf = build_kernel_elf(&config.cargo_args, &cargo_target_directory);
let aster_elf = build_kernel_elf(&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);
@ -117,13 +117,25 @@ pub fn do_build(
bundle
}
fn build_kernel_elf(args: &CargoArgs, cargo_target_directory: impl AsRef<Path>) -> AsterBin {
let target_json_path = PathBuf::from_str("x86_64-custom.json").unwrap();
fn build_kernel_elf(args: &CargoArgs, cargo_target_directory: impl AsRef<Path>, rustflags: &[&str]) -> AsterBin {
let target = "x86_64-unknown-none";
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",
"-C relocation-model=static",
"-Z relro-level=off",
]);
let mut command = cargo();
command.env_remove("RUSTUP_TOOLCHAIN");
command.env("RUSTFLAGS", rustflags.join(" "));
command.arg("build");
command.arg("--target").arg(&target_json_path);
command.arg("--target").arg(target);
command
.arg("--target-dir")
.arg(cargo_target_directory.as_ref());
@ -135,9 +147,7 @@ fn build_kernel_elf(args: &CargoArgs, cargo_target_directory: impl AsRef<Path>)
process::exit(Errno::ExecuteCommand as _);
}
let aster_bin_path = cargo_target_directory
.as_ref()
.join(target_json_path.file_stem().unwrap().to_str().unwrap());
let aster_bin_path = cargo_target_directory.as_ref().join(target);
let aster_bin_path = if args.profile == "dev" {
aster_bin_path.join("debug")
} else {

View File

@ -3,13 +3,14 @@
use std::process;
use super::util::{cargo, COMMON_CARGO_ARGS};
use crate::{commands::util::create_target_json, error::Errno, error_msg};
use crate::{error::Errno, error_msg};
pub fn execute_check_command() {
let target_json_path = create_target_json();
let mut command = cargo();
command.arg("check").arg("--target").arg(target_json_path);
command
.arg("check")
.arg("--target")
.arg("x86_64-unkown-none");
command.args(COMMON_CARGO_ARGS);
let status = command.status().unwrap();
if !status.success() {

View File

@ -3,13 +3,16 @@
use std::process;
use super::util::{cargo, COMMON_CARGO_ARGS};
use crate::{commands::util::create_target_json, error::Errno, error_msg};
use crate::{error::Errno, error_msg};
pub fn execute_clippy_command() {
let target_json_path = create_target_json();
let mut command = cargo();
command.arg("clippy").arg("-h");
command
.arg("clippy")
.arg("-h")
.arg("--target")
.arg("x86_64-unknown-none")
.args(COMMON_CARGO_ARGS);
info!("Running `cargo clippy -h`");
let output = command.output().unwrap();
if !output.status.success() {
@ -20,8 +23,11 @@ pub fn execute_clippy_command() {
}
let mut command = cargo();
command.arg("clippy").arg("--target").arg(target_json_path);
command.args(COMMON_CARGO_ARGS);
command
.arg("clippy")
.arg("--target")
.arg("x86_64-unknown-none")
.args(COMMON_CARGO_ARGS);
// TODO: Add support for custom clippy args using OSDK commandline rather than hardcode it.
command.args(["--", "-D", "warnings"]);
let status = command.status().unwrap();

View File

@ -50,6 +50,7 @@ pub fn execute_run_command(config: &RunConfig) {
&osdk_target_directory,
&ws_target_directory,
&required_build_config,
&[],
);
bundle.run(config);

View File

@ -52,13 +52,12 @@ pub static KTEST_CRATE_WHITELIST: Option<&[&str]> = Some(&{:#?});
};
let original_dir = std::env::current_dir().unwrap();
std::env::set_current_dir(&target_crate_dir).unwrap();
// Add `--cfg ktest` to RUSTFLAGS
std::env::set_var("RUSTFLAGS", "--cfg ktest");
let bundle = do_build(
default_bundle_directory,
&osdk_target_directory,
&ws_target_directory,
&required_build_config,
&["--cfg ktest", "-C panic=unwind"],
);
std::env::remove_var("RUSTFLAGS");
std::env::set_current_dir(original_dir).unwrap();

View File

@ -1,8 +1,6 @@
// SPDX-License-Identifier: MPL-2.0
use std::{fs, path::PathBuf, process::Command};
use crate::util::get_target_directory;
use std::process::Command;
pub const COMMON_CARGO_ARGS: &[&str] = &[
"-Zbuild-std=core,alloc,compiler_builtins",
@ -14,18 +12,3 @@ pub const DEFAULT_TARGET_RELPATH: &str = "osdk";
pub fn cargo() -> Command {
Command::new("cargo")
}
pub fn create_target_json() -> PathBuf {
let target_osdk_dir = get_target_directory().join(DEFAULT_TARGET_RELPATH);
fs::create_dir_all(&target_osdk_dir).unwrap();
let target_json_path = target_osdk_dir.join("x86_64-custom.json");
if target_json_path.is_file() {
return target_json_path;
}
let contents = include_str!("../base_crate/x86_64-custom.json.template");
fs::write(&target_json_path, contents).unwrap();
target_json_path
}