use std::collections::{BTreeMap, BTreeSet};
use crate::grants::built::built;
use crate::placement::expand::{Lists, expand};
use crate::placement::layout::Layout;
use crate::types::{
Argument, EnvMark, EnvName, NetworkAccess, Node, NodeId, Program, SettingName, SettingValue,
SourcePath, TargetGraph, WantedSet, Workspace,
};
#[derive(Debug)]
pub(crate) struct Sites<'g> {
pub(crate) programs: BTreeMap<Program, &'g Node>,
pub(crate) env: BTreeMap<&'g EnvName, &'g Node>,
pub(crate) unkeyed: BTreeMap<&'g EnvName, &'g Node>,
pub(crate) inputs: BTreeMap<&'g SourcePath, &'g Node>,
}
pub(crate) fn sites<'g>(
graph: &'g TargetGraph,
settings: &BTreeMap<SettingName, SettingValue>,
layout: &Layout,
) -> Sites<'g> {
let mut programs = BTreeMap::new();
let mut env = BTreeMap::new();
let mut unkeyed = BTreeMap::new();
let mut inputs = BTreeMap::new();
for (node, id) in graph.nodes().iter().zip((0_u32..).map(NodeId::new)) {
if let Some(program) = program_of(graph, node, id, settings, layout) {
programs.entry(program).or_insert(node);
}
for (name, mark) in &node.env {
let sites = match mark {
EnvMark::Key => &mut env,
EnvMark::Unkeyed => &mut unkeyed,
};
sites.entry(name).or_insert(node);
}
for path in &node.inputs {
inputs.entry(path).or_insert(node);
}
}
Sites {
programs,
env,
unkeyed,
inputs,
}
}
pub(crate) fn program_of(
graph: &TargetGraph,
node: &Node,
id: NodeId,
settings: &BTreeMap<SettingName, SettingValue>,
layout: &Layout,
) -> Option<Program> {
let dep_paths = layout.deps(graph, id);
let lists = Lists {
inputs: &node.inputs,
outputs: layout.outputs(id),
deps: &dep_paths,
};
expand(&node.run, settings, &lists)
.into_iter()
.next()
.and_then(|first| Argument::parse(first).ok())
.as_ref()
.and_then(Program::of)
.filter(|program| !built(program, &dep_paths))
}
pub(crate) fn wanted(graph: &TargetGraph, sites: &Sites<'_>, workspace: &Workspace) -> WantedSet {
WantedSet {
programs: sites.programs.keys().cloned().collect(),
env: sites.env.keys().map(|name| (*name).clone()).collect(),
unkeyed: sites.unkeyed.keys().map(|name| (*name).clone()).collect(),
write: BTreeSet::from([workspace.out.clone(), workspace.state.clone()]),
network: graph
.nodes()
.iter()
.filter(|node| node.network == NetworkAccess::Declared)
.map(|node| node.label.clone())
.collect(),
}
}
#[cfg(test)]
mod tests {
#![expect(
clippy::unwrap_used,
reason = "tests unwrap known-valid fixtures; a panic is the intended failure signal"
)]
use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;
use super::{sites, wanted as wanted_set};
use crate::grants::fixtures::{
command, dir, env_name, graph, keyed, marked, node, path, settings,
};
use crate::placement::layout::Layout;
use crate::types::{EnvMark, NetworkAccess, SettingName, SettingValue, TargetGraph, Workspace};
fn wanted(
graph: &TargetGraph,
settings: &BTreeMap<SettingName, SettingValue>,
) -> Vec<(String, String)> {
let layout = Layout::of(graph, &dir("out")).unwrap();
sites(graph, settings, &layout)
.programs
.iter()
.map(|(program, node)| (program.to_string(), node.label.to_string()))
.collect()
}
fn pair(program: &str, label: &str) -> (String, String) {
(program.to_owned(), label.to_owned())
}
#[test]
fn sites_name_the_lowest_labelled_target_for_each_want() {
let app = node("//:app", |node| {
node.run = command(&["ld", "$in"]);
node.env = keyed(&["PATH"]);
node.inputs = vec![path("src/main.c")];
});
let text = node("//lib:text", |node| {
node.env = keyed(&["PATH", "HOME"]);
node.inputs = vec![path("lib/text.c")];
node.network = NetworkAccess::Declared;
});
let graph = graph(vec![app, text], &[&[], &[]]);
let layout = Layout::of(&graph, &dir("out")).unwrap();
let sites = sites(&graph, &BTreeMap::new(), &layout);
let programs: Vec<(&str, String)> = sites
.programs
.iter()
.map(|(program, node)| (program.as_str(), node.label.to_string()))
.collect();
let env: Vec<(&str, String)> = sites
.env
.iter()
.map(|(name, node)| (name.as_str(), node.label.to_string()))
.collect();
let inputs: Vec<(&str, &Path)> = sites
.inputs
.iter()
.map(|(path, node)| (path.as_str(), node.provenance.file()))
.collect();
assert_eq!(
programs,
[("cc", "//lib:text".to_owned()), ("ld", "//:app".to_owned())]
);
assert_eq!(
env,
[
("HOME", "//lib:text".to_owned()),
("PATH", "//:app".to_owned())
]
);
assert_eq!(
inputs,
[
("lib/text.c", Path::new("lib/build.lua")),
("src/main.c", Path::new("build.lua")),
]
);
}
fn wanted_of(graph: &TargetGraph) -> crate::types::WantedSet {
let layout = Layout::of(graph, &dir("out")).unwrap();
let sites = sites(graph, &BTreeMap::new(), &layout);
wanted_set(graph, &sites, &workspace())
}
fn workspace() -> Workspace {
Workspace {
out: dir("out"),
state: dir(".buildl"),
ceiling: crate::types::Ceiling {
run: BTreeSet::new(),
env: BTreeSet::new(),
unkeyed: BTreeSet::new(),
read: BTreeSet::new(),
write: BTreeSet::new(),
},
execution: crate::types::Execution::default(),
approver: crate::types::ApproverMode::Manifest,
plugins: BTreeMap::new(),
remote: None,
}
}
#[test]
fn unkeyed_names_are_wanted_apart() {
let graph = graph(
vec![node("//:app", |node| {
node.env = marked(&[("RUSTFLAGS", EnvMark::Key), ("HOME", EnvMark::Unkeyed)]);
})],
&[&[]],
);
let wanted = wanted_of(&graph);
assert_eq!(wanted.env, BTreeSet::from([env_name("RUSTFLAGS")]));
assert_eq!(wanted.unkeyed, BTreeSet::from([env_name("HOME")]));
}
#[test]
fn a_name_marked_both_ways_is_in_both_sets() {
let graph = graph(
vec![
node("//:a", |node| {
node.env = marked(&[("PATH", EnvMark::Key)]);
}),
node("//:b", |node| {
node.env = marked(&[("PATH", EnvMark::Unkeyed)]);
}),
],
&[&[], &[]],
);
let wanted = wanted_of(&graph);
assert_eq!(wanted.env, BTreeSet::from([env_name("PATH")]));
assert_eq!(wanted.unkeyed, BTreeSet::from([env_name("PATH")]));
}
#[test]
fn a_program_named_through_in_is_wanted() {
let graph = graph(
vec![node("//:run", |node| {
node.run = command(&["$in", "--fast"]);
node.inputs = vec![path("tools/run.sh")];
})],
&[&[]],
);
assert_eq!(
wanted(&graph, &BTreeMap::new()),
[pair("tools/run.sh", "//:run")]
);
}
#[test]
fn a_program_named_through_deps_is_not_wanted() {
let graph = graph(
vec![
node("//:gen", |_| {}),
node("//:use", |node| node.run = command(&["$deps", "$out"])),
],
&[&[], &[0]],
);
assert_eq!(wanted(&graph, &BTreeMap::new()), [pair("cc", "//:gen")]);
}
#[test]
fn a_written_out_dependency_output_is_not_wanted() {
let graph = graph(
vec![
node("//:gen", |_| {}),
node("//:use", |node| node.run = command(&["out/gen", "$out"])),
],
&[&[], &[0]],
);
assert_eq!(wanted(&graph, &BTreeMap::new()), [pair("cc", "//:gen")]);
}
#[test]
fn a_setting_naming_a_dependency_output_is_not_wanted() {
let graph = graph(
vec![
node("//:gen", |_| {}),
node("//:use", |node| {
node.run = command(&["$opt:tool", "$out"]);
}),
],
&[&[], &[0]],
);
assert_eq!(
wanted(&graph, &settings(&[("tool", "out/gen")])),
[pair("cc", "//:gen")]
);
}
#[test]
fn a_written_out_output_of_a_target_not_depended_on_is_wanted() {
let graph = graph(
vec![
node("//:gen", |_| {}),
node("//:use", |node| node.run = command(&["out/gen", "$out"])),
],
&[&[], &[]],
);
assert_eq!(
wanted(&graph, &BTreeMap::new()),
[pair("cc", "//:gen"), pair("out/gen", "//:use")]
);
}
#[test]
fn a_program_named_through_out_is_wanted() {
let graph = graph(
vec![node("//:gen", |node| {
node.run = command(&["$out", "--check"]);
})],
&[&[]],
);
assert_eq!(
wanted(&graph, &BTreeMap::new()),
[pair("out/gen", "//:gen")]
);
}
#[test]
fn a_command_naming_no_program_is_left_out() {
let graph = graph(
vec![
node("//:a", |node| node.run = command(&["$opt:missing", "x"])),
node("//:b", |node| node.run = command(&["", "x"])),
node("//:c", |node| node.run = command(&["$in"])),
],
&[&[], &[], &[]],
);
assert_eq!(wanted(&graph, &BTreeMap::new()), []);
}
#[test]
fn each_program_names_its_lowest_labelled_target() {
let graph = graph(
vec![
node("//:a", |node| node.run = command(&["ld", "$in"])),
node("//:b", |node| node.run = command(&["cc", "-c"])),
node("//:c", |node| node.run = command(&["cc", "-O2"])),
node("//lib:d", |node| node.run = command(&["ar"])),
],
&[&[], &[], &[], &[]],
);
assert_eq!(
wanted(&graph, &BTreeMap::new()),
[
pair("ar", "//lib:d"),
pair("cc", "//:b"),
pair("ld", "//:a")
]
);
}
}