Skip to content

Commit

Permalink
feat: multiple architecture layers
Browse files Browse the repository at this point in the history
  • Loading branch information
koehlma committed Jan 8, 2024
1 parent d8459af commit c6f65e4
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 11 deletions.
4 changes: 3 additions & 1 deletion crates/rugpi-bakery/src/bake/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ pub fn bake_layer(project: &Project, arch: Architecture, layer: &str) -> Anyhow<
let library = project.load_library()?;
let layer_config = &library.layers[library
.lookup_layer(library.repositories.root_repository, layer)
.unwrap()];
.unwrap()]
.config(arch)
.unwrap();
if let Some(url) = &layer_config.url {
let layer_id = sha1(url);
let system_tar = project
Expand Down
21 changes: 19 additions & 2 deletions crates/rugpi-bakery/src/project/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Project configuration.
use std::{collections::HashMap, fs, path::Path};
use std::{collections::HashMap, fs, path::Path, str::FromStr};

use anyhow::Context;
use clap::ValueEnum;
use rugpi_common::Anyhow;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use super::{images::ImageConfig, repositories::Source};

Expand Down Expand Up @@ -40,7 +41,7 @@ pub enum IncludeFirmware {
Pi5,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, ValueEnum)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq, Hash, ValueEnum)]
#[clap(rename_all = "lowercase")]
#[serde(rename_all = "lowercase")]
pub enum Architecture {
Expand All @@ -49,6 +50,22 @@ pub enum Architecture {
Armhf,
}

#[derive(Debug, Error)]
#[error("invalid architecture")]
pub struct InvalidArchitectureError;

impl FromStr for Architecture {
type Err = InvalidArchitectureError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"arm64" => Ok(Self::Arm64),
"armhf" => Ok(Self::Armhf),
_ => Err(InvalidArchitectureError),
}
}
}

impl Architecture {
pub fn as_str(self) -> &'static str {
match self {
Expand Down
22 changes: 21 additions & 1 deletion crates/rugpi-bakery/src/project/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ use anyhow::Context;
use rugpi_common::Anyhow;
use serde::{Deserialize, Serialize};

use super::recipes::{ParameterValue, RecipeName};
use super::{
config::Architecture,
recipes::{ParameterValue, RecipeName},
};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LayerConfig {
/// Optional human-readable name of the layer.
pub name: Option<String>,
/// An URL to fetch the layer from.
pub url: Option<String>,
pub parent: Option<String>,
Expand All @@ -36,3 +41,18 @@ impl LayerConfig {
.with_context(|| format!("error parsing layer config from path `{path:?}`"))
}
}

#[derive(Debug, Default)]
pub struct Layer {
pub default_config: Option<LayerConfig>,
pub arch_configs: HashMap<Architecture, LayerConfig>,
}

impl Layer {
/// The layer configuration for the given architecture.
pub fn config(&self, arch: Architecture) -> Option<&LayerConfig> {
self.arch_configs
.get(&arch)
.or(self.default_config.as_ref())
}
}
27 changes: 21 additions & 6 deletions crates/rugpi-bakery/src/project/library.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::{collections::HashMap, fs, ops::Deref, sync::Arc};
use std::{collections::HashMap, fs, ops::Deref, str::FromStr, sync::Arc};

use rugpi_common::Anyhow;

use super::{
layers::LayerConfig,
config::Architecture,
layers::{Layer, LayerConfig},
recipes::{Recipe, RecipeLoader},
repositories::{ProjectRepositories, RepositoryIdx},
};
Expand All @@ -12,7 +13,7 @@ use crate::idx_vec::{new_idx_type, IdxVec};
pub struct Library {
pub repositories: ProjectRepositories,
pub recipes: IdxVec<RecipeIdx, Arc<Recipe>>,
pub layers: IdxVec<LayerIdx, LayerConfig>,
pub layers: IdxVec<LayerIdx, Layer>,
pub recipe_tables: IdxVec<RepositoryIdx, HashMap<String, RecipeIdx>>,
pub layer_tables: IdxVec<RepositoryIdx, HashMap<String, LayerIdx>>,
}
Expand Down Expand Up @@ -52,10 +53,24 @@ impl Library {
for entry in fs::read_dir(layers_dir)? {
let entry = entry?;
let path = entry.path();
let layer_name = path.file_stem().unwrap();
let mut name = path.file_stem().unwrap().to_string_lossy().into_owned();
let mut arch = None;
if let Some((layer_name, arch_str)) = name.split_once('.') {
arch = Some(Architecture::from_str(arch_str)?);
name = layer_name.to_owned();
}
let layer_config = LayerConfig::load(&path)?;
let layer_idx = layers.push(layer_config);
table.insert(layer_name.to_string_lossy().into_owned(), layer_idx);
let layer_idx = *table
.entry(name)
.or_insert_with(|| layers.push(Layer::default()));
match arch {
Some(arch) => {
layers[layer_idx].arch_configs.insert(arch, layer_config);
}
None => {
layers[layer_idx].default_config = Some(layer_config);
}
}
}
Ok(table)
})
Expand Down
2 changes: 2 additions & 0 deletions repositories/core/layers/raspios-bookworm.arm64.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "Raspberry Pi OS (Bookworm)"
url = "https://downloads.raspberrypi.com/raspios_lite_arm64/images/raspios_lite_arm64-2023-12-11/2023-12-11-raspios-bookworm-arm64-lite.img.xz"
2 changes: 2 additions & 0 deletions repositories/core/layers/raspios-bookworm.armhf.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "Raspberry Pi OS (Bookworm)"
url = "https://downloads.raspberrypi.com/raspios_lite_armhf/images/raspios_lite_armhf-2023-12-11/2023-12-11-raspios-bookworm-armhf-lite.img.xz"
2 changes: 2 additions & 0 deletions repositories/core/layers/raspios-bullseye.arm64.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "Raspberry Pi OS (Bullseye)"
url = "https://downloads.raspberrypi.com/raspios_oldstable_lite_arm64/images/raspios_oldstable_lite_arm64-2023-12-06/2023-12-05-raspios-bullseye-arm64-lite.img.xz"
2 changes: 2 additions & 0 deletions repositories/core/layers/raspios-bullseye.armhf.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "Raspberry Pi OS (Bullseye)"
url = "https://downloads.raspberrypi.com/raspios_oldstable_lite_armhf/images/raspios_oldstable_lite_armhf-2023-12-06/2023-12-05-raspios-bullseye-armhf-lite.img.xz"
2 changes: 1 addition & 1 deletion template

0 comments on commit c6f65e4

Please sign in to comment.