use std::{ os::unix::ffi::OsStrExt, path::{Path, PathBuf}, process::Command, }; extern crate bootloader; use bootloader::{BiosBoot, UefiBoot}; 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(); initrd_path.push("/initrd.tar"); 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(); uefi_path.push("/uefi.img"); 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(); bios_path.push("/bios.img"); 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(); std::fs::write("out_dir_path", out_dir.as_bytes()).unwrap(); }