Skip to content

Commit

Permalink
Package with topological sorting (#1097)
Browse files Browse the repository at this point in the history
  • Loading branch information
smklein authored Jan 15, 2024
1 parent ba8746d commit 4e4d18a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ tokio-rustls = { version = "0.24.1" }
tokio-test = "*"
tokio-util = { version = "0.7", features = ["codec"]}
toml = "0.8"
topological-sort = "0.2.2"
tracing = "0.1"
tracing-opentelemetry = "0.18.0"
tracing-subscriber = "0.3.18"
Expand Down
1 change: 1 addition & 0 deletions package/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ edition = "2021"
anyhow.workspace = true
omicron-zone-package.workspace = true
tokio.workspace = true
topological-sort.workspace = true
crucible-workspace-hack.workspace = true
66 changes: 61 additions & 5 deletions package/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Copyright 2022 Oxide Computer Company

use anyhow::Result;
use anyhow::{bail, Result};
use omicron_zone_package::config;
use omicron_zone_package::package::{PackageOutput, PackageSource};
use omicron_zone_package::target::Target;
use std::collections::BTreeMap;
use std::fs::create_dir_all;
use std::path::Path;
use topological_sort::TopologicalSort;

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -13,10 +16,63 @@ async fn main() -> Result<()> {
let output_dir = Path::new("out");
create_dir_all(output_dir)?;

for (name, package) in cfg.packages {
package
.create_for_target(&Target::default(), &name, output_dir)
.await?;
// Reverse lookup of "output" -> "package creating this output".
let all_packages = cfg
.packages
.iter()
.map(|(name, package)| (package.get_output_file(name), (name, package)))
.collect::<BTreeMap<_, _>>();

// Collect all packages, and sort them in dependency order,
// so we know which ones to build first.
let mut outputs = TopologicalSort::<String>::new();
for (package_output, (_, package)) in &all_packages {
match &package.source {
PackageSource::Local { .. }
| PackageSource::Prebuilt { .. }
| PackageSource::Manual => {
// Skip intermediate leaf packages; if necessary they'll be
// added to the dependency graph by whatever composite package
// actually depends on them.
if !matches!(
package.output,
PackageOutput::Zone {
intermediate_only: true
}
) {
outputs.insert(package_output);
}
}
PackageSource::Composite { packages: deps } => {
for dep in deps {
outputs.add_dependency(dep, package_output);
}
}
}
}

while !outputs.is_empty() {
let batch = outputs.pop_all();
assert!(
!batch.is_empty() || outputs.is_empty(),
"cyclic dependency in package manifest!"
);

for output in &batch {
println!("Creating '{output}'");

let Some((name, package)) = all_packages.get(output) else {
bail!(
"Cannot find a package to create output: '{output}' \n\
\tThis can happen when building a composite package, where one of \n\
\tthe 'source.packages' has not been found."
);
};

package
.create_for_target(&Target::default(), &name, output_dir)
.await?;
}
}

Ok(())
Expand Down

0 comments on commit 4e4d18a

Please sign in to comment.