From bde00ef0c7819ff363d42ce9892c13517282d04e Mon Sep 17 00:00:00 2001 From: LLeny <125205-LLeny@users.noreply.gitlab.redox-os.org> Date: Tue, 18 Feb 2025 21:14:57 +0800 Subject: [PATCH] Adds patches and script to SourceRecipe::Git --- src/bin/cook.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/recipe.rs | 7 +++++++ 2 files changed, 52 insertions(+) diff --git a/src/bin/cook.rs b/src/bin/cook.rs index 9bdbbf5ec..174d56e37 100644 --- a/src/bin/cook.rs +++ b/src/bin/cook.rs @@ -249,6 +249,8 @@ fn fetch(recipe_dir: &Path, source: &Option) -> Result { //TODO: use libgit? if !source_dir.is_dir() { @@ -326,6 +328,14 @@ fi"#, run_command(command)?; } + if !patches.is_empty() || script.is_some() { + // Hard reset + let mut command = Command::new("git"); + command.arg("-C").arg(&source_dir); + command.arg("reset").arg("--hard"); + run_command(command)?; + } + // Sync submodules URL let mut command = Command::new("git"); command.arg("-C").arg(&source_dir); @@ -341,6 +351,41 @@ fi"#, .arg("--init") .arg("--recursive"); run_command(command)?; + + // Apply patches + for patch_name in patches { + let patch_file = recipe_dir.join(patch_name); + if !patch_file.is_file() { + return Err(format!( + "failed to find patch file '{}'", + patch_file.display() + )); + } + + let patch = fs::read_to_string(&patch_file).map_err(|err| { + format!( + "failed to read patch file '{}': {}\n{:#?}", + patch_file.display(), + err, + err + ) + })?; + + let mut command = Command::new("patch"); + command.arg("--forward"); + command.arg("--batch"); + command.arg("--directory").arg(&source_dir); + command.arg("--strip=1"); + run_command_stdin(command, patch.as_bytes())?; + } + + // Run source script + if let Some(script) = script { + let mut command = Command::new("bash"); + command.arg("-ex"); + command.current_dir(&source_dir); + run_command_stdin(command, format!("{SHARED_PRESCRIPT}\n{script}").as_bytes())?; + } } Some(SourceRecipe::Tar { tar, diff --git a/src/recipe.rs b/src/recipe.rs index eb73239c0..d897355cf 100644 --- a/src/recipe.rs +++ b/src/recipe.rs @@ -36,6 +36,11 @@ pub enum SourceRecipe { /// The optional revision of the git repository to use for builds. Please specify for /// reproducible builds rev: Option, + /// A list of patch files to apply to the source + #[serde(default)] + patches: Vec, + /// Optional script to run to prepare the source + script: Option, }, /// A tar file source Tar { @@ -236,6 +241,8 @@ mod tests { upstream: None, branch: Some("master".to_string()), rev: Some("06344744d3d55a5ac9a62a6059cb363d40699bbc".to_string()), + patches: Vec::new(), + script: None, }), build: BuildRecipe { kind: BuildKind::Cargo {