Skip to content

Commit

Permalink
Fix assets planning should be compile-kind agnostic (#380)
Browse files Browse the repository at this point in the history
fix assets-build-planner, plan compile-kind agnostic, improve cache
  • Loading branch information
boozook authored Jun 11, 2024
1 parent 43f5439 commit 7eb4e5a
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 40 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ system = { version = "0.3", path = "api/system", package = "playdate-system", de
sys = { version = "0.4", path = "api/sys", package = "playdate-sys", default-features = false }

tool = { version = "0.1", path = "support/tool", package = "playdate-tool" }
build = { version = "=0.4.0-pre2", path = "support/build", package = "playdate-build", default-features = false }
build = { version = "=0.4.0-pre3", path = "support/build", package = "playdate-build", default-features = false }
utils = { version = "0.3", path = "support/utils", package = "playdate-build-utils", default-features = false }
device = { version = "0.2", path = "support/device", package = "playdate-device" }
simulator = { version = "0.1", path = "support/sim-ctrl", package = "playdate-simulator-utils", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion cargo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-playdate"
version = "0.5.0-beta.3"
version = "0.5.0-beta.4"
readme = "README.md"
description = "Build tool for neat yellow console."
keywords = ["playdate", "build", "cargo", "plugin", "cargo-subcommand"]
Expand Down
23 changes: 19 additions & 4 deletions cargo/src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub type AssetsArtifacts<'cfg> = HashMap<&'cfg Package, AssetsArtifact>;
pub mod proto {
use super::*;

use plan::proto::MultiKey;
use plan::proto::RootKey;
use plan::Difference;
use playdate::assets::plan::BuildPlan;
use playdate::assets::BuildReport;
Expand All @@ -59,7 +59,7 @@ pub mod proto {

pub struct AssetsArtifactsNew<'t, 'cfg> {
artifacts: Vec<AssetsArtifact>,
index: BTreeMap<MultiKey, Vec<usize>>,
index: BTreeMap<RootKey, Vec<usize>>,
tree: &'t MetaDeps<'cfg>,
}

Expand Down Expand Up @@ -316,11 +316,17 @@ pub mod proto {

cfg.log_extra_verbose(|mut logger| {
artifacts.iter().for_each(|(root, arts)| {
use cargo::core::compiler::CompileKind;
let ct: Cow<_> = match root.node().unit().platform {
CompileKind::Host => "host".into(),
CompileKind::Target(kind) => kind.short_name().to_owned().into(),
};

let root = format!(
"({}) {}::{}",
"{} {} of {} for {ct}",
root.node().target().kind().description(),
root.node().package_id().name(),
root.node().target().name,
root.node().package_id().name(),
);
logger.status("Assets", format!("artifacts for {root}:"));
arts.for_each(|art| {
Expand Down Expand Up @@ -354,6 +360,15 @@ pub mod proto {
fn plan_cache(path: PathBuf, plan: &BuildPlan<'_, '_>) -> CargoResult<PlanCache> {
let mut serializable = plan.iter_flatten_meta().collect::<Vec<_>>();
serializable.sort();

#[derive(serde::Serialize)]
struct SerializablePlan<'t> {
items: &'t [(playdate::assets::plan::MappingKind, PathBuf, (PathBuf, Option<std::time::SystemTime>))],
env: &'t BTreeMap<String, String>,
}

let serializable = SerializablePlan { items: &serializable,
env: plan.used_env_vars() };
let json = serde_json::to_string(&serializable)?;

let difference = if path.try_exists()? {
Expand Down
29 changes: 18 additions & 11 deletions cargo/src/assets/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ pub mod proto {
/// per-dep ?dev plan
pub index: BTreeMap<Key, usize>,
/// per-root plans to merge
pub targets: BTreeMap<MultiKey, Vec<usize>>,
pub targets: BTreeMap<RootKey, Vec<usize>>,
}

/// Target-agnostic package key.
#[derive(Debug, Clone, Copy, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct Key {
pub id: PackageId,
Expand All @@ -116,20 +117,21 @@ pub mod proto {
}


/// Target-agnostic root-package key with all dependencies.
#[derive(Debug, Clone, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct MultiKey {
pub struct RootKey {
/// Dependencies
id: Vec<PackageId>,
/// Primary target is dev
dev: bool,
}
impl From<&'_ RootNode<'_>> for MultiKey {
impl From<&'_ RootNode<'_>> for RootKey {
fn from(root: &'_ RootNode<'_>) -> Self {
Self { dev: root.node().target().is_dev(),
id: root.deps().iter().map(|d| d.package_id().to_owned()).collect() }
}
}
impl MultiKey {
impl RootKey {
pub fn dev(&self) -> bool { self.dev }

pub fn is_for(&self, root: &'_ RootNode<'_>) -> bool {
Expand All @@ -138,7 +140,6 @@ pub mod proto {
.iter()
.enumerate()
.all(|(i, d)| self.id.get(i).filter(|k| *k == d.package_id()).is_some())
// root.deps().into_iter().enumerate().all(|(i, d)| d.package_id() == &self.id[i])
}
}

Expand Down Expand Up @@ -176,7 +177,8 @@ pub mod proto {
};


for root in tree.roots() {
// use target-agnostic selection of roots:
for root in tree.roots_compile_target_agnostic() {
let meta_source = root.as_source();

let options = meta_source.assets_options();
Expand All @@ -191,7 +193,7 @@ pub mod proto {
log::debug!(" dependencies are allowed: {}", options.dependencies());


let plan_key = MultiKey::from(root);
let plan_key = RootKey::from(root);
if plans.targets.contains_key(&plan_key) {
log::debug!(" skip: already done");
continue;
Expand Down Expand Up @@ -222,20 +224,25 @@ pub mod proto {
.map(|m| if dev { m.dev_assets() } else { m.assets() }) &&
!assets.is_empty()
{
// let plan =
match assets_build_plan(&env, assets, &options, Some(crate_root.into())) {
Ok(plan) => {
let pid = key.id;
let is_dev = key.dev;
let dev_index = plans.plans.len();
let compile_target_agnostic = plan.compile_target_agnostic();
plans.index.insert(key, dev_index);
plans.plans.push(plan);
indices.push(dev_index);

log::debug!(" done: +#{dev_index} (dev:{is_dev})");
cfg.log().verbose(|mut log| {
log.status("Plan", format_args!("{name_log}assets for {pid} planned",))
})
});

if !compile_target_agnostic {
cfg.log()
.error("Assets is not compile-target-agnostic, this is not supported");
}
},
Err(err) => {
cfg.log()
Expand Down Expand Up @@ -307,7 +314,7 @@ pub mod proto {

// check:
for root in tree.roots() {
let key = MultiKey::from(root);
let key = RootKey::from(root);
debug_assert!(plans.targets.contains_key(&key));
}

Expand All @@ -322,7 +329,7 @@ pub mod proto {
plans: &AssetsPlans<'cfg>)
-> CargoResult<()> {
// prepare context:
let mut root_package: HashMap<&MultiKey, HashSet<&PackageId>> = HashMap::with_capacity(tree.roots().len());
let mut root_package: HashMap<&RootKey, HashSet<&PackageId>> = HashMap::with_capacity(tree.roots().len());
let mut root_options: HashMap<&PackageId, AssetsOptions> = HashMap::with_capacity(tree.roots().len());

plans.targets
Expand Down
3 changes: 2 additions & 1 deletion cargo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ fn main() -> CargoResult<()> {
fn execute(config: &Config) -> CargoResult<()> {
match config.cmd {
cli::cmd::Cmd::Assets => {
let _result = assets::build(config)?;
let deps_tree = crate::utils::cargo::meta_deps::meta_deps(config)?;
assets::proto::build_all(config, &deps_tree)?;
},

cli::cmd::Cmd::Build => {
Expand Down
56 changes: 55 additions & 1 deletion cargo/src/utils/cargo/meta_deps.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::path::Path;

use cargo::core::compiler::CompileMode;
Expand Down Expand Up @@ -251,6 +251,38 @@ impl<'t> MetaDeps<'t> {

pub fn roots(&self) -> &[RootNode<'t>] { self.roots.as_slice() }


/// Groups of root-units by compile-target.
///
/// Possible groups:
/// 1. Contains just one root-unit;
/// 2. Contains two units with __same cargo-target__ and different rustc-targets __including__ playdate.
pub fn root_groups(&self) { unimplemented!() }


/// Returns first root for each group by [`roots_by_compile_target`][Self::roots_by_compile_target].
pub fn roots_compile_target_agnostic(&self) -> impl Iterator<Item = &RootNode<'t>> {
self.roots_by_compile_target().into_iter().flat_map(|(key, _)| {
self.roots().iter().find(|n| {
n.node().unit().package_id ==
*key.package_id &&
&n.node().unit().target == key.target
})
})
}

/// Groups of root-units by target.
///
/// Grouping: (package_id + cargo-target) => [rustc-target].
pub fn roots_by_compile_target(&self) -> BTreeMap<TargetKey, BTreeSet<cargo::core::compiler::CompileKind>> {
self.roots.iter().fold(BTreeMap::new(), |mut acc, root| {
let key = TargetKey::from(root);
acc.entry(key).or_default().insert(root.node().unit().platform);
acc
})
}


pub fn root_for(&self, id: &PackageId, tk: &TargetKindWild, tname: &str) -> CargoResult<&RootNode<'t>> {
self.roots
.iter()
Expand Down Expand Up @@ -280,6 +312,28 @@ impl<'t> MetaDeps<'t> {
}


#[derive(Debug, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct TargetKey<'t> {
package_id: &'t PackageId,
target: &'t UnitTarget,
}

impl<'t> From<&'t RootNode<'t>> for TargetKey<'t> {
fn from(node: &'t RootNode<'t>) -> Self { TargetKey::from(node.node()) }
}

impl<'t> From<&'t Node<'t>> for TargetKey<'t> {
fn from(node: &'t Node<'t>) -> Self { TargetKey::from(node.unit()) }
}

impl<'t> From<&'t Unit> for TargetKey<'t> {
fn from(unit: &'t Unit) -> Self {
TargetKey { package_id: &unit.package_id,
target: &unit.target }
}
}


pub trait DependenciesAllowed {
fn deps_allowed(&self) -> bool;
}
Expand Down
2 changes: 1 addition & 1 deletion support/build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "playdate-build"
version = "0.4.0-pre2"
version = "0.4.0-pre3"
readme = "README.md"
description = "Utils that help to build package for Playdate"
keywords = ["playdate", "package", "encoding", "manifest", "assets"]
Expand Down
Loading

0 comments on commit 7eb4e5a

Please sign in to comment.