Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions include/pup/graph/dag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,21 +156,39 @@ template<typename T>
[[nodiscard]]
auto get(Graph const& graph, NodeId id) -> T;

/// Get the parent directory node of a file node
[[nodiscard]]
auto get_parent_dir(Graph const& graph, NodeId id) -> NodeId;

/// Get the input operand NodeIds of a command node
[[nodiscard]]
auto get_command_inputs(Graph const& graph, NodeId id) -> Vec<NodeId> const&;
/// Role tags for composite-property access via view<Tag>.
struct Inputs { };
struct Outputs { };
struct ExportedVars { };

/// Storage type each role tag maps to.
template<typename Tag>
struct view_storage;

template<>
struct view_storage<Inputs> {
using type = Vec<NodeId>;
};
template<>
struct view_storage<Outputs> {
using type = Vec<NodeId>;
};
template<>
struct view_storage<ExportedVars> {
using type = SortedIdVec;
};

/// Get the output operand NodeIds of a command node
/// Composite-property accessor returning a range-like const reference.
/// Specialized per role tag out-of-line in dag.cpp. Use for collection
/// properties (inputs, outputs, exported vars) where iteration is the
/// natural access pattern.
template<typename Tag>
[[nodiscard]]
auto get_command_outputs(Graph const& graph, NodeId id) -> Vec<NodeId> const&;
auto view(Graph const& graph, NodeId id) -> typename view_storage<Tag>::type const&;

/// Get the exported environment variables of a command node
/// Get the parent directory node of a file node
[[nodiscard]]
auto get_exported_vars(Graph const& graph, NodeId id) -> SortedIdVec const&;
auto get_parent_dir(Graph const& graph, NodeId id) -> NodeId;

/// Get the parent command for InjectImplicitDeps commands
[[nodiscard]]
Expand Down
4 changes: 2 additions & 2 deletions src/cli/cmd_build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,8 @@ auto serialize_command_nodes(
continue;
}

auto inputs = pup::graph::get_command_inputs(g, id);
auto outputs = pup::graph::get_command_outputs(g, id);
auto inputs = pup::graph::view<pup::graph::Inputs>(g, id);
auto outputs = pup::graph::view<pup::graph::Outputs>(g, id);
auto& pool = pup::global_pool();

auto source_dir_sv = pup::graph::get_source_dir(g, id);
Expand Down
2 changes: 1 addition & 1 deletion src/exec/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ auto Scheduler::build_job_list(
auto cmd_id = graph::expand_instruction(g, id, cache, source_root_sv, config_root_sv);
auto display_id = graph::get_display_id(g, id);

auto const& exported_raw = graph::get_exported_vars(g, id);
auto const& exported_raw = graph::view<graph::ExportedVars>(g, id);
auto exported_ids = Vec<StringId> {};
exported_ids.reserve(exported_raw.size());
for (auto raw_id : exported_raw) {
Expand Down
9 changes: 6 additions & 3 deletions src/graph/dag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,21 +289,24 @@ auto get_parent_dir(Graph const& graph, NodeId id) -> NodeId
return node ? node->parent_dir : NodeId { 0 };
}

auto get_command_inputs(Graph const& graph, NodeId id) -> Vec<NodeId> const&
template<>
auto view<Inputs>(Graph const& graph, NodeId id) -> Vec<NodeId> const&
{
static auto const empty = Vec<NodeId> {};
auto const* node = get_command_node(graph, id);
return node ? node->inputs : empty;
}

auto get_command_outputs(Graph const& graph, NodeId id) -> Vec<NodeId> const&
template<>
auto view<Outputs>(Graph const& graph, NodeId id) -> Vec<NodeId> const&
{
static auto const empty = Vec<NodeId> {};
auto const* node = get_command_node(graph, id);
return node ? node->outputs : empty;
}

auto get_exported_vars(Graph const& graph, NodeId id) -> SortedIdVec const&
template<>
auto view<ExportedVars>(Graph const& graph, NodeId id) -> SortedIdVec const&
{
static auto const empty = SortedIdVec {};
auto const* node = get_command_node(graph, id);
Expand Down
10 changes: 5 additions & 5 deletions test/unit/test_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ TEST_CASE("BuildGraph node types", "[graph]")
CHECK(get_parent_dir(g, *cmd) == 0);
}

SECTION("get_command_inputs and get_command_outputs accessors")
SECTION("view<Inputs> and view<Outputs>")
{
auto in1 = add_file_node(g, FileNode { .name = intern("a.c") });
auto in2 = add_file_node(g, FileNode { .name = intern("b.c") });
Expand All @@ -373,17 +373,17 @@ TEST_CASE("BuildGraph node types", "[graph]")
});
REQUIRE(cmd.has_value());

auto const& inputs = get_command_inputs(g, *cmd);
auto const& inputs = view<Inputs>(g, *cmd);
REQUIRE(inputs.size() == 2);
CHECK(inputs[0] == *in1);
CHECK(inputs[1] == *in2);

auto const& outputs = get_command_outputs(g, *cmd);
auto const& outputs = view<Outputs>(g, *cmd);
REQUIRE(outputs.size() == 1);
CHECK(outputs[0] == *out);

CHECK(get_command_inputs(g, *in1).empty());
CHECK(get_command_outputs(g, *in1).empty());
CHECK(view<Inputs>(g, *in1).empty());
CHECK(view<Outputs>(g, *in1).empty());
}

SECTION("all node types")
Expand Down
Loading