Skip to content

Commit

Permalink
Merge pull request #1431 from zmrow/supported_arch
Browse files Browse the repository at this point in the history
Add support for specifying a variant's supported architectures
  • Loading branch information
zmrow authored Apr 2, 2021
2 parents e04cdf5 + 0b70807 commit 2db2265
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
52 changes: 49 additions & 3 deletions tools/buildsys/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ mod spec;

use builder::{PackageBuilder, VariantBuilder};
use cache::LookasideCache;
use manifest::ManifestInfo;
use manifest::{ManifestInfo, SupportedArch};
use project::ProjectInfo;
use serde::Deserialize;
use snafu::ResultExt;
use snafu::{ensure, ResultExt};
use spec::SpecInfo;
use std::env;
use std::path::PathBuf;
Expand Down Expand Up @@ -56,6 +56,22 @@ mod error {
var: String,
source: std::env::VarError,
},

#[snafu(display("Unknown architecture: '{}'", arch))]
UnknownArch {
arch: String,
source: serde_plain::Error,
},

#[snafu(display(
"Unsupported architecture {}, this variant supports {}",
arch,
supported_arches.join(", ")
))]
UnsupportedArch {
arch: String,
supported_arches: Vec<String>,
},
}
}

Expand Down Expand Up @@ -102,10 +118,17 @@ fn run() -> Result<()> {
}

fn build_package() -> Result<()> {
let manifest_dir: PathBuf = getenv("CARGO_MANIFEST_DIR")?.into();
let manifest_file = "Cargo.toml";
println!("cargo:rerun-if-changed={}", manifest_file);

let root_dir: PathBuf = getenv("BUILDSYS_ROOT_DIR")?.into();
let variant = getenv("BUILDSYS_VARIANT")?;
let variant_manifest_path = root_dir.join("variants").join(variant).join(manifest_file);
let variant_manifest =
ManifestInfo::new(variant_manifest_path).context(error::ManifestParse)?;
supported_arch(&variant_manifest)?;

let manifest_dir: PathBuf = getenv("CARGO_MANIFEST_DIR")?.into();
let manifest =
ManifestInfo::new(manifest_dir.join(manifest_file)).context(error::ManifestParse)?;

Expand Down Expand Up @@ -165,6 +188,8 @@ fn build_variant() -> Result<()> {
let manifest =
ManifestInfo::new(manifest_dir.join(manifest_file)).context(error::ManifestParse)?;

supported_arch(&manifest)?;

if let Some(packages) = manifest.included_packages() {
let image_format = manifest.image_format();
VariantBuilder::build(&packages, image_format).context(error::BuildAttempt)?;
Expand All @@ -175,6 +200,27 @@ fn build_variant() -> Result<()> {
Ok(())
}

/// Ensure that the current arch is supported by the current variant
fn supported_arch(manifest: &ManifestInfo) -> Result<()> {
if let Some(supported_arches) = manifest.supported_arches() {
let arch = getenv("BUILDSYS_ARCH")?;
let current_arch: SupportedArch =
serde_plain::from_str(&arch).context(error::UnknownArch { arch: &arch })?;

ensure!(
supported_arches.contains(&current_arch),
error::UnsupportedArch {
arch: &arch,
supported_arches: supported_arches
.into_iter()
.map(|a| a.to_string())
.collect::<Vec<String>>()
}
)
}
Ok(())
}

/// Retrieve a variable that we expect to be set in the environment.
fn getenv(var: &str) -> Result<String> {
env::var(var).context(error::Environment { var })
Expand Down
25 changes: 25 additions & 0 deletions tools/buildsys/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ use error::Result;

use serde::Deserialize;
use snafu::ResultExt;
use std::collections::HashSet;
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -115,6 +117,12 @@ impl ManifestInfo {
self.build_variant().and_then(|b| b.image_format.as_ref())
}

/// Convenience method to return the supported architectures for this variant.
pub(crate) fn supported_arches(&self) -> Option<&HashSet<SupportedArch>> {
self.build_variant()
.and_then(|b| b.supported_arches.as_ref())
}

/// Helper methods to navigate the series of optional struct fields.
fn build_package(&self) -> Option<&BuildPackage> {
self.package
Expand Down Expand Up @@ -158,6 +166,7 @@ pub(crate) struct BuildPackage {
pub(crate) struct BuildVariant {
pub(crate) included_packages: Option<Vec<String>>,
pub(crate) image_format: Option<ImageFormat>,
pub(crate) supported_arches: Option<HashSet<SupportedArch>>,
}

#[derive(Deserialize, Debug)]
Expand All @@ -168,10 +177,26 @@ pub(crate) enum ImageFormat {
Vmdk,
}

#[derive(Deserialize, Debug, PartialEq, Eq, Hash)]
#[serde(rename_all = "lowercase")]
pub(crate) enum SupportedArch {
X86_64,
Aarch64,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "kebab-case")]
pub(crate) struct ExternalFile {
pub(crate) path: Option<PathBuf>,
pub(crate) sha512: String,
pub(crate) url: String,
}

impl fmt::Display for SupportedArch {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
SupportedArch::X86_64 => write!(f, "x86_64"),
SupportedArch::Aarch64 => write!(f, "aarch64"),
}
}
}
1 change: 1 addition & 0 deletions variants/vmware-dev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exclude = ["README.md"]

[package.metadata.build-variant]
image-format = "vmdk"
supported-arches = ["x86_64"]
included-packages = [
# core
"release",
Expand Down

0 comments on commit 2db2265

Please sign in to comment.