181 lines
5.7 KiB
Rust
181 lines
5.7 KiB
Rust
use std::collections::HashMap;
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::Command;
|
|
use std::sync::atomic::AtomicBool;
|
|
|
|
use diffy::apply;
|
|
use diffy::patch_set::{FileOperation, ParseOptions, PatchSet};
|
|
use gix::Repository;
|
|
use gix::progress::Discard;
|
|
|
|
const URL: &str = "https://github.com/ipxe/ipxe";
|
|
const REVISION: &str = "2fa08affe9a1a1422766f54b2fb045234b571f06";
|
|
const FILES: &[&str] = &["bin/ipxe.pxe", "bin-x86_64-efi/ipxe.efi"];
|
|
|
|
const SRC_DIR: &str = "src";
|
|
|
|
fn main() {
|
|
let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
|
|
.canonicalize()
|
|
.unwrap();
|
|
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap())
|
|
.canonicalize()
|
|
.unwrap();
|
|
let repo_dir = out_dir.join("repo");
|
|
|
|
let repo = checkout(&out_dir, &repo_dir);
|
|
|
|
apply_patches(&repo, &manifest_dir, &repo_dir);
|
|
println!("cargo::rerun-if-changed=patches");
|
|
|
|
build(&manifest_dir, &repo_dir);
|
|
println!("cargo::rerun-if-changed=src/embed.ipxe");
|
|
|
|
copy_files(&repo_dir, &out_dir);
|
|
}
|
|
|
|
fn checkout(out_dir: &Path, repo_dir: &Path) -> Repository {
|
|
// Stamp file is used instead of comparing against HEAD to guard against incomplete clones
|
|
let stamp = out_dir.join("checkout.stamp");
|
|
if std::fs::read_to_string(&stamp).is_ok_and(|stamp| stamp == REVISION)
|
|
&& let Ok(repo) = gix::open(repo_dir)
|
|
{
|
|
return repo;
|
|
}
|
|
|
|
// Cleanup any left-overs
|
|
std::fs::remove_file(&stamp).ok();
|
|
std::fs::remove_dir_all(repo_dir).ok();
|
|
|
|
std::fs::create_dir_all(repo_dir).unwrap();
|
|
|
|
let mut prepare = gix::prepare_clone(URL, repo_dir)
|
|
.unwrap()
|
|
.with_revision(Some(REVISION))
|
|
.unwrap();
|
|
|
|
let interrupt = AtomicBool::new(false);
|
|
let (mut checkout, _) = prepare.fetch_then_checkout(Discard, &interrupt).unwrap();
|
|
let (repo, _) = checkout.main_worktree(Discard, &interrupt).unwrap();
|
|
|
|
std::fs::write(&stamp, REVISION).unwrap();
|
|
|
|
repo
|
|
}
|
|
|
|
fn apply_patches(repo: &Repository, manifest_dir: &Path, repo_dir: &Path) {
|
|
let commit_id = gix::ObjectId::from_hex(REVISION.as_bytes()).unwrap();
|
|
let tree = repo.find_commit(commit_id).unwrap().tree().unwrap();
|
|
|
|
// Get all the patches and sort them based on name
|
|
let mut patch_files: Vec<PathBuf> = std::fs::read_dir(manifest_dir.join("patches"))
|
|
.unwrap()
|
|
.map(|e| e.unwrap().path())
|
|
.filter(|e| e.extension().is_some_and(|e| e == "patch"))
|
|
.collect();
|
|
patch_files.sort();
|
|
|
|
let mut patched = HashMap::new();
|
|
for path in patch_files {
|
|
let text = std::fs::read_to_string(&path).unwrap();
|
|
let patch_set = PatchSet::parse(&text, ParseOptions::gitdiff());
|
|
|
|
for patch in patch_set {
|
|
let patch = patch.unwrap();
|
|
|
|
match patch.operation().strip_prefix(1) {
|
|
FileOperation::Modify { original, modified } => {
|
|
assert_eq!(original, modified);
|
|
let path = original.to_string();
|
|
|
|
// Either get the current in-memory patched file, otherwise get the prisine file
|
|
let current = patched
|
|
.remove(&path)
|
|
.unwrap_or_else(|| pristine(&tree, &path));
|
|
|
|
let new = apply(¤t, patch.patch().as_text().unwrap()).unwrap();
|
|
patched.insert(path, new);
|
|
}
|
|
_ => todo!(),
|
|
}
|
|
}
|
|
}
|
|
|
|
// If the file was previously patched, but now isn't restore the pristine file
|
|
for path in modified_files(repo) {
|
|
if !patched.contains_key(&path) {
|
|
std::fs::write(repo_dir.join(&path), pristine(&tree, &path)).unwrap();
|
|
}
|
|
}
|
|
|
|
// Write all the patched files
|
|
for (path, content) in &patched {
|
|
let path = repo_dir.join(path);
|
|
let content = content.as_bytes();
|
|
|
|
// But only if the file is actually different from the one on disk
|
|
// This prevents changing the mtime unnecessarily
|
|
if std::fs::read(&path).ok().as_deref() != Some(content) {
|
|
std::fs::write(path, content).unwrap();
|
|
}
|
|
}
|
|
}
|
|
|
|
fn pristine(tree: &gix::Tree<'_>, path: &str) -> String {
|
|
let entry = tree.lookup_entry_by_path(path).unwrap().unwrap();
|
|
|
|
String::from_utf8(entry.object().unwrap().data.clone()).unwrap()
|
|
}
|
|
|
|
fn modified_files(repo: &gix::Repository) -> Vec<String> {
|
|
repo.status(Discard)
|
|
.unwrap()
|
|
.untracked_files(gix::status::UntrackedFiles::None)
|
|
.into_index_worktree_iter(Vec::<gix::bstr::BString>::new())
|
|
.unwrap()
|
|
.map(|item| item.unwrap().rela_path().to_string())
|
|
.collect()
|
|
}
|
|
|
|
fn build(manifest_dir: &Path, repo_dir: &Path) {
|
|
let src_dir = repo_dir.join(SRC_DIR);
|
|
|
|
let mut command = Command::new("make");
|
|
command
|
|
.current_dir(&src_dir)
|
|
.env_remove("DEBUG")
|
|
.env_remove("PROFILE")
|
|
.arg(format!(
|
|
"EMBED={}",
|
|
manifest_dir
|
|
.join("src")
|
|
.join("embed.ipxe")
|
|
.to_string_lossy()
|
|
))
|
|
.args(FILES);
|
|
|
|
if let Ok(flags) = std::env::var("CARGO_MAKEFLAGS") {
|
|
command.env("MAKEFLAGS", flags);
|
|
}
|
|
|
|
let output = command.output().unwrap();
|
|
|
|
if !output.status.success() {
|
|
panic!("make failed:\n{}", String::from_utf8_lossy(&output.stderr));
|
|
}
|
|
}
|
|
|
|
fn copy_files(repo_dir: &Path, out_dir: &Path) {
|
|
let src_dir = repo_dir.join(SRC_DIR);
|
|
let bin_dir = out_dir.join("bin");
|
|
|
|
std::fs::create_dir_all(&bin_dir).unwrap();
|
|
for file in FILES {
|
|
let file = Path::new(file);
|
|
let input = src_dir.join(file);
|
|
let output = bin_dir.join(file.file_name().unwrap());
|
|
|
|
std::fs::copy(input, output).unwrap();
|
|
}
|
|
}
|