Skip to content

Commit

Permalink
Put builder output through status interface
Browse files Browse the repository at this point in the history
Send all output after manifest parsing is finished to the Status
interface, so that when status frontends are added they can handle
build messages.
  • Loading branch information
colincross authored and Eli Ribble committed Aug 12, 2019
1 parent 2b64e46 commit d89f457
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ void Builder::Cleanup() {
string err;
TimeStamp new_mtime = disk_interface_->Stat((*o)->path(), &err);
if (new_mtime == -1) // Log and ignore Stat() errors.
Error("%s", err.c_str());
status_->Error("%s", err.c_str());
if (!depfile.empty() || (*o)->mtime() != new_mtime)
disk_interface_->RemoveFile((*o)->path());
}
Expand Down
25 changes: 13 additions & 12 deletions src/ninja.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ int GuessParallelism() {
/// Rebuild the build manifest, if necessary.
/// Returns true if the manifest was rebuilt.
bool NinjaMain::RebuildManifest(const char* input_file, string* err,
Status *status) {
Status* status) {
string path = input_file;
uint64_t slash_bits; // Unused because this path is only used for lookup.
if (!CanonicalizePath(&path, &slash_bits, err))
Expand Down Expand Up @@ -1124,7 +1124,7 @@ int NinjaMain::RunBuild(int argc, char** argv, Status* status) {
string err;
vector<Node*> targets;
if (!CollectTargetsFromArgs(argc, argv, &targets, &err)) {
Error("%s", err.c_str());
status->Error("%s", err.c_str());
return 1;
}

Expand All @@ -1135,7 +1135,7 @@ int NinjaMain::RunBuild(int argc, char** argv, Status* status) {
for (size_t i = 0; i < targets.size(); ++i) {
if (!builder.AddTarget(targets[i], &err)) {
if (!err.empty()) {
Error("%s", err.c_str());
status->Error("%s", err.c_str());
return 1;
} else {
// Added a target that is already up-to-date; not really
Expand All @@ -1148,12 +1148,12 @@ int NinjaMain::RunBuild(int argc, char** argv, Status* status) {
disk_interface_.AllowStatCache(false);

if (builder.AlreadyUpToDate()) {
printf("ninja: no work to do.\n");
status->Info("no work to do.");
return 0;
}

if (!builder.Build(&err)) {
printf("ninja: build stopped: %s.\n", err.c_str());
status->Info("build stopped: %s.", err.c_str());
if (err.find("interrupted by user") != string::npos) {
return 2;
}
Expand Down Expand Up @@ -1296,18 +1296,17 @@ NORETURN void real_main(int argc, char** argv) {
kDepfileDistinctTargetLinesActionError;
}

Status* status = new StatusPrinter(config);

if (options.working_dir) {
// The formatting of this string, complete with funny quotes, is
// so Emacs can properly identify that the cwd has changed for
// subsequent commands.
// Don't print this if a tool is being used, so that tool output
// can be piped into a file without this string showing up.
if (!options.tool)
printf("ninja: Entering directory `%s'\n", options.working_dir);
Info("Entering directory `%s'", options.working_dir);
if (chdir(options.working_dir) < 0) {
Fatal("chdir to '%s' - %s", options.working_dir, strerror(errno));
Error("chdir to '%s' - %s", options.working_dir, strerror(errno));
exit(1);
}
}

Expand All @@ -1318,6 +1317,8 @@ NORETURN void real_main(int argc, char** argv) {
exit((ninja.*options.tool->func)(&options, argc, argv));
}

Status* status = new StatusPrinter(config);

// Limit number of rebuilds, to prevent infinite loops.
const int kCycleLimit = 100;
for (int cycle = 1; cycle <= kCycleLimit; ++cycle) {
Expand All @@ -1333,7 +1334,7 @@ NORETURN void real_main(int argc, char** argv) {
ManifestParser parser(&ninja.state_, &ninja.disk_interface_, parser_opts);
string err;
if (!parser.Load(options.input_file, &err)) {
Error("%s", err.c_str());
status->Error("%s", err.c_str());
exit(1);
}

Expand All @@ -1358,7 +1359,7 @@ NORETURN void real_main(int argc, char** argv) {
// Start the build over with the new manifest.
continue;
} else if (!err.empty()) {
Error("rebuilding '%s': %s", options.input_file, err.c_str());
status->Error("rebuilding '%s': %s", options.input_file, err.c_str());
exit(1);
}

Expand All @@ -1368,7 +1369,7 @@ NORETURN void real_main(int argc, char** argv) {
exit(result);
}

Error("manifest '%s' still dirty after %d tries\n",
status->Error("manifest '%s' still dirty after %d tries",
options.input_file, kCycleLimit);
exit(1);
}
Expand Down
22 changes: 22 additions & 0 deletions src/status.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "status.h"

#include <stdarg.h>
#include <stdlib.h>

#ifdef _WIN32
Expand Down Expand Up @@ -242,3 +243,24 @@ void StatusPrinter::PrintStatus(Edge* edge, int64_t time_millis) {
printer_.Print(to_print,
force_full_command ? LinePrinter::FULL : LinePrinter::ELIDE);
}

void StatusPrinter::Warning(const char* msg, ...) {
va_list ap;
va_start(ap, msg);
::Warning(msg, ap);
va_end(ap);
}

void StatusPrinter::Error(const char* msg, ...) {
va_list ap;
va_start(ap, msg);
::Error(msg, ap);
va_end(ap);
}

void StatusPrinter::Info(const char* msg, ...) {
va_list ap;
va_start(ap, msg);
::Info(msg, ap);
va_end(ap);
}
10 changes: 10 additions & 0 deletions src/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ struct Status {
virtual void BuildLoadDyndeps() = 0;
virtual void BuildStarted() = 0;
virtual void BuildFinished() = 0;

virtual void Info(const char* msg, ...) = 0;
virtual void Warning(const char* msg, ...) = 0;
virtual void Error(const char* msg, ...) = 0;

virtual ~Status() { }
};

Expand All @@ -46,6 +51,11 @@ struct StatusPrinter : Status {
virtual void BuildLoadDyndeps();
virtual void BuildStarted();
virtual void BuildFinished();

virtual void Info(const char* msg, ...);
virtual void Warning(const char* msg, ...);
virtual void Error(const char* msg, ...);

virtual ~StatusPrinter() { }

/// Format the progress status string by replacing the placeholders.
Expand Down
30 changes: 25 additions & 5 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@

void Fatal(const char* msg, ...) {
va_list ap;
fprintf(stderr, "ninja: fatal: ");
va_start(ap, msg);
vfprintf(stderr, msg, ap);
va_end(ap);
Expand All @@ -72,24 +71,45 @@ void Fatal(const char* msg, ...) {
#endif
}

void Warning(const char* msg, va_list ap) {
fprintf(stderr, "ninja: warning: ");
vfprintf(stderr, msg, ap);
fprintf(stderr, "\n");
}

void Warning(const char* msg, ...) {
va_list ap;
fprintf(stderr, "ninja: warning: ");
va_start(ap, msg);
vfprintf(stderr, msg, ap);
Warning(msg, ap);
va_end(ap);
}

void Error(const char* msg, va_list ap) {
fprintf(stderr, "ninja: error: ");
vfprintf(stderr, msg, ap);
fprintf(stderr, "\n");
}

void Error(const char* msg, ...) {
va_list ap;
fprintf(stderr, "ninja: error: ");
va_start(ap, msg);
vfprintf(stderr, msg, ap);
Error(msg, ap);
va_end(ap);
}

void Info(const char* msg, va_list ap) {
fprintf(stderr, "ninja: ");
vfprintf(stderr, msg, ap);
fprintf(stderr, "\n");
}

void Info(const char* msg, ...) {
va_list ap;
va_start(ap, msg);
Info(msg, ap);
va_end(ap);
}

bool CanonicalizePath(string* path, uint64_t* slash_bits, string* err) {
METRIC_RECORD("canonicalize str");
size_t len = path->size();
Expand Down
8 changes: 8 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <stdint.h>
#endif

#include <stdarg.h>

#include <string>
#include <vector>
using namespace std;
Expand Down Expand Up @@ -50,9 +52,15 @@ NORETURN void Fatal(const char* msg, ...);

/// Log a warning message.
void Warning(const char* msg, ...);
void Warning(const char* msg, va_list ap);

/// Log an error message.
void Error(const char* msg, ...);
void Error(const char* msg, va_list ap);

/// Log an informational message.
void Info(const char* msg, ...);
void Info(const char* msg, va_list ap);

/// Canonicalize a path like "foo/../bar.h" into just "bar.h".
/// |slash_bits| has bits set starting from lowest for a backslash that was
Expand Down

0 comments on commit d89f457

Please sign in to comment.