diff options
author | David 'Digit' Turner <digit+github@google.com> | 2024-05-19 16:59:56 +0200 |
---|---|---|
committer | David 'Digit' Turner <digit+github@google.com> | 2024-05-19 17:31:50 +0200 |
commit | 1cb6ef84921eec0e4f35919f2fc657933ecc7f58 (patch) | |
tree | 0f7f2c0275bbd484e7d851f5f9005c9a58cb9c82 | |
parent | 986d8440cac8bcc673e648db75278ab677b5c4e3 (diff) | |
download | ninja-1cb6ef84921eec0e4f35919f2fc657933ecc7f58.tar.gz |
Status: Add SetExplanations() method.
Allow the StatusPrinter::PrintStatus() method to
use an Explanations instance, instead of calling
the global `print_explanations()` method.
Since the rest of Ninja still uses `record_explanation()`
at the moment, keep the old code path until we can get
rid of it as well.
-rw-r--r-- | src/status.h | 6 | ||||
-rw-r--r-- | src/status_printer.cc | 25 | ||||
-rw-r--r-- | src/status_printer.h | 9 |
3 files changed, 35 insertions, 5 deletions
diff --git a/src/status.h b/src/status.h index e38b4e6..29db7c2 100644 --- a/src/status.h +++ b/src/status.h @@ -19,6 +19,7 @@ struct BuildConfig; struct Edge; +struct Explanations; /// Abstract interface to object that tracks the status of a build: /// completion fraction, printing updates. @@ -33,6 +34,11 @@ struct Status { virtual void BuildStarted() = 0; virtual void BuildFinished() = 0; + /// Set the Explanations instance to use to report explanations, + /// argument can be nullptr if no explanations need to be printed + /// (which is the default). + virtual void SetExplanations(Explanations*) = 0; + virtual void Info(const char* msg, ...) = 0; virtual void Warning(const char* msg, ...) = 0; virtual void Error(const char* msg, ...) = 0; diff --git a/src/status_printer.cc b/src/status_printer.cc index 577aa93..45854d1 100644 --- a/src/status_printer.cc +++ b/src/status_printer.cc @@ -403,11 +403,26 @@ string StatusPrinter::FormatProgressStatus(const char* progress_status_format, } void StatusPrinter::PrintStatus(const Edge* edge, int64_t time_millis) { - if (g_explaining) { - // Start a new line so that the first explanation does not append to the - // status line. - printer_.PrintOnNewLine(""); - print_explanations(stderr, edge); + if (explanations_) { + // Collect all explanations for the current edge's outputs. + std::vector<std::string> explanations; + for (Node* output : edge->outputs_) { + explanations_->LookupAndAppend(output, &explanations); + } + if (!explanations.empty()) { + // Start a new line so that the first explanation does not append to the + // status line. + printer_.PrintOnNewLine(""); + for (const auto& exp : explanations) { + fprintf(stderr, "ninja explain: %s\n", exp.c_str()); + } + } + } else { + // DEPRECATED: Remove this code path once record_explanations() is + // no longer used by Ninja. + if (g_explaining) { + print_explanations(stderr, edge); + } } if (config_.verbosity == BuildConfig::QUIET diff --git a/src/status_printer.h b/src/status_printer.h index 1681fb8..0fa3a90 100644 --- a/src/status_printer.h +++ b/src/status_printer.h @@ -16,6 +16,7 @@ #include <cstdint> #include <queue> +#include "explanations.h" #include "line_printer.h" #include "status.h" @@ -49,6 +50,11 @@ struct StatusPrinter : Status { std::string FormatProgressStatus(const char* progress_status_format, int64_t time_millis) const; + /// Set the |explanations_| pointer. Used to implement `-d explain`. + void SetExplanations(Explanations* explanations) override { + explanations_ = explanations; + } + private: void PrintStatus(const Edge* edge, int64_t time_millis); @@ -83,6 +89,9 @@ struct StatusPrinter : Status { /// Prints progress output. LinePrinter printer_; + /// An optional Explaantions pointer, used to implement `-d explain`. + Explanations* explanations_ = nullptr; + /// The custom progress status format to use. const char* progress_status_format_; |