From d53358c350898091d9cf2ba76ccdfa9e590275ee Mon Sep 17 00:00:00 2001 From: Klaus Aehlig Date: Wed, 26 Feb 2025 10:16:30 +0100 Subject: Make graph-dumping options cummulative If --dump-graph or --dump-plain-graph is given several times, the action graph wil also be written several times. In this way, regular use of those options will not be affected by adding them implicitly through invocation-logging options in the rc file. --- .../build_engine/target_map/result_map.hpp | 20 +++++++++++------ src/buildtool/common/cli.hpp | 26 +++++++++++++--------- src/buildtool/main/main.cpp | 12 ++++------ 3 files changed, 33 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/buildtool/build_engine/target_map/result_map.hpp b/src/buildtool/build_engine/target_map/result_map.hpp index 14fd8f11..9753b9e0 100644 --- a/src/buildtool/build_engine/target_map/result_map.hpp +++ b/src/buildtool/build_engine/target_map/result_map.hpp @@ -18,9 +18,9 @@ #include #include #include +#include #include #include -#include #include #include #include @@ -381,15 +381,21 @@ class ResultTargetMap { } template - auto ToFile(std::string const& graph_file, + auto ToFile(std::vector const& destinations, gsl::not_null const& stats, gsl::not_null const& progress, int indent = 2) const -> void { - Logger::Log( - LogLevel::Info, "Dumping action graph to file {}.", graph_file); - std::ofstream os(graph_file); - os << std::setw(indent) << ToJson(stats, progress) - << std::endl; + if (not destinations.empty()) { + // As serialization is expensive, compute the string only once + auto data = ToJson(stats, progress).dump(indent); + for (auto const& graph_file : destinations) { + Logger::Log(LogLevel::Info, + "Dumping action graph to file {}.", + graph_file.string()); + std::ofstream os(graph_file); + os << data << std::endl; + } + } } void Clear(gsl::not_null const& ts) { diff --git a/src/buildtool/common/cli.hpp b/src/buildtool/common/cli.hpp index ef6c043c..a8acea87 100644 --- a/src/buildtool/common/cli.hpp +++ b/src/buildtool/common/cli.hpp @@ -73,8 +73,8 @@ struct AnalysisArguments { std::optional target_root; std::optional rule_root; std::optional expression_root; - std::optional graph_file; - std::optional graph_file_plain; + std::vector graph_file; + std::vector graph_file_plain; std::optional artifacts_to_build_file; std::optional serve_errors_file; }; @@ -346,16 +346,22 @@ static inline auto SetupAnalysisArguments( "errors as json.") ->type_name("PATH"); if (with_graph) { - app->add_option( + app->add_option_function( "--dump-graph", - clargs->graph_file, + [clargs](auto const& file_) { + clargs->graph_file.emplace_back(file_); + }, "File path for writing the action graph description to.") - ->type_name("PATH"); - app->add_option("--dump-plain-graph", - clargs->graph_file_plain, - "File path for writing the action graph description " - "(without origins) to.") - ->type_name("PATH"); + ->type_name("PATH") + ->trigger_on_parse(); + app->add_option_function( + "--dump-plain-graph", + [clargs](auto const& file_) { + clargs->graph_file_plain.emplace_back(file_); + }, + "File path for writing the action graph description to.") + ->type_name("PATH") + ->trigger_on_parse(); app->add_option("--dump-artifacts-to-build", clargs->artifacts_to_build_file, "File path for writing the artifacts to build to.") diff --git a/src/buildtool/main/main.cpp b/src/buildtool/main/main.cpp index e4ac6a02..b8579b55 100644 --- a/src/buildtool/main/main.cpp +++ b/src/buildtool/main/main.cpp @@ -1150,14 +1150,10 @@ auto main(int argc, char* argv[]) -> int { not_eligible); } - if (arguments.analysis.graph_file) { - analyse_result->result_map.ToFile( - *arguments.analysis.graph_file, &stats, &progress); - } - if (arguments.analysis.graph_file_plain) { - analyse_result->result_map.ToFile( - *arguments.analysis.graph_file_plain, &stats, &progress); - } + analyse_result->result_map.ToFile( + arguments.analysis.graph_file, &stats, &progress); + analyse_result->result_map.ToFile( + arguments.analysis.graph_file_plain, &stats, &progress); auto const [artifacts, runfiles] = ReadOutputArtifacts(analyse_result->target); if (arguments.analysis.artifacts_to_build_file) { -- cgit v1.2.3