os_build/build.rs

55 lines
1.8 KiB
Rust
Raw Normal View History

2024-08-06 19:46:57 -05:00
use std::{
os::unix::ffi::OsStrExt,
path::{Path, PathBuf},
process::Command,
};
2024-06-06 22:03:02 -05:00
extern crate bootloader;
2024-08-06 19:46:57 -05:00
use bootloader::{BiosBoot, UefiBoot};
2024-06-06 22:03:02 -05:00
fn main() {
println!("cargo:rerun-if-changed=sysroot");
println!("cargo:rerun-if-changed=build.rs");
// set by cargo, build scripts should use this directory for output files
let out_dir = std::env::var_os("OUT_DIR").unwrap();
let mut initrd_path = out_dir.clone();
2024-08-06 19:46:06 -05:00
initrd_path.push("/initrd.tar");
2024-06-06 22:03:02 -05:00
Command::new("sh")
.arg("-c")
.arg(format!("cd sysroot; tar cvf {:?} *", initrd_path))
.spawn()
.unwrap()
.wait()
.unwrap();
// set by cargo's artifact dependency feature, see
// https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#artifact-dependencies
let kernel = PathBuf::from(std::env::var_os("CARGO_BIN_FILE_KERNEL_kernel").unwrap());
// create an UEFI disk image (optional)
let mut uefi_path = out_dir.clone();
2024-08-06 19:46:06 -05:00
uefi_path.push("/uefi.img");
2024-06-06 22:03:02 -05:00
UefiBoot::new(&kernel)
.set_ramdisk(Path::new(&initrd_path))
.create_disk_image(Path::new(&uefi_path))
.unwrap();
// create a BIOS disk image (optional)
let mut bios_path = out_dir.clone();
2024-08-06 19:46:06 -05:00
bios_path.push("/bios.img");
2024-06-06 22:03:02 -05:00
BiosBoot::new(&kernel)
.set_ramdisk(Path::new(&initrd_path))
.create_disk_image(Path::new(&bios_path))
.unwrap();
println!("cargo:rustc-env=UEFI_PATH={}", uefi_path.to_string_lossy());
println!("cargo:rustc-env=BIOS_PATH={}", bios_path.to_string_lossy());
std::fs::write("kernel_path", kernel.as_os_str().as_bytes()).unwrap();
std::fs::write("initrd_path", initrd_path.as_os_str().as_bytes()).unwrap();
2024-08-06 19:46:06 -05:00
std::fs::write("out_dir_path", out_dir.as_bytes()).unwrap();
2024-06-06 22:03:02 -05:00
}