Skip to content

Commit

Permalink
feat: add rebuild-if-changed mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
koehlma committed Jan 19, 2024
1 parent 8d5b0b1 commit 042c8c2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
24 changes: 20 additions & 4 deletions crates/rugpi-bakery/src/bake/customize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use tempfile::tempdir;
use xscript::{cmd, run, vars, ParentEnv, Run};

use crate::{
caching::mtime,
caching::{mtime, mtime_recursive},
project::{
config::Architecture,
layers::{Layer, LayerConfig},
Expand All @@ -41,26 +41,39 @@ pub fn customize(
layer: &Layer,
src: &Path,
target: &Path,
layer_path: &Path,
) -> Anyhow<()> {
let library = project.load_library()?;
// Collect the recipes to apply.
let config = layer.config(arch).unwrap();
let jobs = recipe_schedule(layer.repo, config, &library)?;
let last_modified = jobs
let mut last_modified = jobs
.iter()
.map(|job| job.recipe.modified)
.max()
.unwrap()
.max(layer.modified);
if target.exists() && last_modified < mtime(target)? {
let mut force_run = false;
let used_files = project.dir.join(layer_path.join("rebuild-if-changed.txt"));
if used_files.exists() {
for line in std::fs::read_to_string(used_files)?.lines() {
if let Ok(modified) = mtime_recursive(&project.dir.join(line)) {
last_modified = last_modified.max(modified)
} else {
eprintln!("Error determining modification time for {line}.");
force_run = true;
}
}
}
if target.exists() && last_modified < mtime(target)? && !force_run {
return Ok(());
}
// Prepare system chroot.
let root_dir = tempdir()?;
let root_dir_path = root_dir.path();
println!("Extracting system files...");
run!(["tar", "-x", "-f", &src, "-C", root_dir_path])?;
apply_recipes(project, arch, &jobs, root_dir_path)?;
apply_recipes(project, arch, &jobs, root_dir_path, layer_path)?;
println!("Packing system files...");
run!(["tar", "-c", "-f", &target, "-C", root_dir_path, "."])?;
Ok(())
Expand Down Expand Up @@ -158,6 +171,7 @@ fn apply_recipes(
arch: Architecture,
jobs: &Vec<RecipeJob>,
root_dir_path: &Path,
layer_path: &Path,
) -> Anyhow<()> {
let _mounted_dev = Mounted::bind("/dev", root_dir_path.join("dev"))?;
let _mounted_dev_pts = Mounted::bind("/dev/pts", root_dir_path.join("dev/pts"))?;
Expand Down Expand Up @@ -206,6 +220,7 @@ fn apply_recipes(
RUGPI_ROOT_DIR = "/",
RUGPI_PROJECT_DIR = "/run/rugpi/bakery/project/",
RUGPI_ARCH = arch.as_str(),
LAYER_REBUILD_IF_CHANGED = Path::new("/run/rugpi/bakery/project").join(layer_path).join("rebuild-if-changed.txt"),
RECIPE_DIR = "/run/rugpi/bakery/recipe/",
RECIPE_STEP_PATH = &script,
};
Expand All @@ -221,6 +236,7 @@ fn apply_recipes(
RUGPI_ROOT_DIR = root_dir_path,
RUGPI_PROJECT_DIR = &project_dir,
RUGPI_ARCH = arch.as_str(),
LAYER_REBUILD_IF_CHANGED = project_dir.join(layer_path).join("rebuild-if-changed.txt"),
RECIPE_DIR = &recipe.path,
RECIPE_STEP_PATH = &script,
};
Expand Down
7 changes: 3 additions & 4 deletions crates/rugpi-bakery/src/bake/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ pub fn bake_layer(project: &Project, arch: Architecture, layer_name: &str) -> An
layer_string.push('.');
layer_string.push_str(library.repositories[layer.repo].source.id.as_str());
let layer_id = sha1(&layer_string);
let target = project
.dir
.join(format!(".rugpi/layers/{layer_id}/system.tar"));
let layer_path = PathBuf::from(format!(".rugpi/layers/{layer_id}"));
let target = project.dir.join(&layer_path).join("system.tar");
fs::create_dir_all(target.parent().unwrap()).ok();
customize::customize(project, arch, layer, &src, &target)?;
customize::customize(project, arch, layer, &src, &target, &layer_path)?;
Ok(target)
} else {
bail!("invalid layer configuration")
Expand Down

0 comments on commit 042c8c2

Please sign in to comment.