mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-17 06:47:49 +00:00
Replace bootimage with local builder executable
This commit is contained in:
13
disk_image/Cargo.toml
Normal file
13
disk_image/Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "disk_image"
|
||||
version = "0.1.0"
|
||||
authors = ["Philipp Oppermann <dev@phil-opp.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.32"
|
||||
bootloader-locator = {path = "../../../bootloader-locator"}
|
||||
runner-utils = { path = "../../../runner-utils/" }
|
||||
locate-cargo-manifest = "0.2.0"
|
||||
42
disk_image/src/bin/runner.rs
Normal file
42
disk_image/src/bin/runner.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use std::{process::{ExitStatus, Command}, path::PathBuf, time::Duration};
|
||||
use anyhow::anyhow;
|
||||
|
||||
const TEST_ARGS: &[&str] = &["-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio",
|
||||
"-display", "none"
|
||||
];
|
||||
const TEST_TIMEOUT_SECS: u64 = 10;
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let kernel_binary_path = {
|
||||
let path = PathBuf::from(std::env::args().nth(1).unwrap());
|
||||
path.canonicalize()?
|
||||
};
|
||||
|
||||
let disk_image = disk_image::create_disk_image(&kernel_binary_path, true)?;
|
||||
|
||||
let mut run_cmd = Command::new("qemu-system-x86_64");
|
||||
run_cmd.arg("-drive").arg(format!("format=raw,file={}", disk_image.display()));
|
||||
|
||||
let binary_kind = runner_utils::binary_kind(&kernel_binary_path);
|
||||
if binary_kind.is_test() {
|
||||
run_cmd.args(TEST_ARGS);
|
||||
|
||||
let exit_status = run_test_command(run_cmd)?;
|
||||
match exit_status.code() {
|
||||
Some(33) => {}, // success
|
||||
other => return Err(anyhow!("Test failed (exit code: {:?})", other)),
|
||||
}
|
||||
} else {
|
||||
let exit_status = run_cmd.status()?;
|
||||
if !exit_status.success() {
|
||||
std::process::exit(exit_status.code().unwrap_or(1));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_test_command(mut cmd: Command) -> anyhow::Result<ExitStatus> {
|
||||
let status = runner_utils::run_with_timeout(&mut cmd, Duration::from_secs(TEST_TIMEOUT_SECS))?;
|
||||
Ok(status)
|
||||
}
|
||||
31
disk_image/src/lib.rs
Normal file
31
disk_image/src/lib.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use std::{process::Command, path::{Path, PathBuf}};
|
||||
use anyhow::anyhow;
|
||||
|
||||
pub fn create_disk_image(kernel_binary_path: &Path, bios_only: bool) -> anyhow::Result<PathBuf> {
|
||||
let bootloader_info= bootloader_locator::locate_bootloader()?;
|
||||
let kernel_manifest_path = &bootloader_info.kernel_manifest_path;
|
||||
|
||||
let mut build_cmd = Command::new(env!("CARGO"));
|
||||
build_cmd.current_dir(bootloader_info.package.manifest_path.parent().unwrap());
|
||||
build_cmd.arg("builder");
|
||||
build_cmd.arg("--kernel-manifest").arg(&kernel_manifest_path);
|
||||
build_cmd.arg("--kernel-binary").arg(&kernel_binary_path);
|
||||
build_cmd.arg("--features").args(bootloader_info.features);
|
||||
build_cmd.arg("--target-dir").arg(kernel_manifest_path.parent().unwrap().join("target"));
|
||||
build_cmd.arg("--out-dir").arg(kernel_binary_path.parent().unwrap());
|
||||
build_cmd.arg("--quiet");
|
||||
if bios_only {
|
||||
build_cmd.arg("--firmware").arg("bios");
|
||||
}
|
||||
|
||||
if !build_cmd.status()?.success() {
|
||||
return Err(anyhow!("build failed"));
|
||||
}
|
||||
|
||||
let kernel_binary_name = kernel_binary_path.file_name().unwrap().to_str().unwrap();
|
||||
let disk_image = kernel_binary_path.parent().unwrap().join(format!("bootimage-bios-{}.bin", kernel_binary_name));
|
||||
if !disk_image.exists() {
|
||||
return Err(anyhow!("Disk image does not exist at {} after bootloader build", disk_image.display()));
|
||||
}
|
||||
Ok(disk_image)
|
||||
}
|
||||
34
disk_image/src/main.rs
Normal file
34
disk_image/src/main.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use std::process::Command;
|
||||
use anyhow::anyhow;
|
||||
|
||||
const TARGET_NAME: &str = "x86_64-blog_os";
|
||||
const KERNEL_BINARIES: &[&str] = &["blog_os"];
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
// build all binaries
|
||||
let mut build_cmd = Command::new(env!("CARGO"));
|
||||
build_cmd.arg("build");
|
||||
build_cmd.arg("--release");
|
||||
build_cmd.arg("-Zbuild-std=core");
|
||||
build_cmd.arg("--target").arg(format!("{}.json", TARGET_NAME));
|
||||
if !build_cmd.status()?.success() {
|
||||
return Err(anyhow!("build failed"));
|
||||
};
|
||||
|
||||
let kernel_manifest = locate_cargo_manifest::locate_manifest()?;
|
||||
let target_dir_root = kernel_manifest.parent().unwrap().join("target");
|
||||
let target_dir = target_dir_root.join(TARGET_NAME).join("release");
|
||||
|
||||
for binary_name in KERNEL_BINARIES {
|
||||
let binary_path = {
|
||||
let path = target_dir.join(binary_name);
|
||||
path.canonicalize()?
|
||||
};
|
||||
|
||||
let disk_image = disk_image::create_disk_image(&binary_path, false)?;
|
||||
|
||||
println!("Created disk image for binary {} at {}", binary_name, disk_image.display());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user