Skip to content

Commit

Permalink
Propagate jobs' exit codes to the ninja's exit code
Browse files Browse the repository at this point in the history
Make all the methods return ExitStatus:
  Subprocess::Finish->
  CommandRunner::Result.status->
  Builder::Build->
  NinjaMain::RunBuild

Ninja now return different exit codes for Win32 and Posix systems:
  - Win32 -> 2
  - Posix -> 130 (128+2)
  • Loading branch information
Felixoid committed Dec 10, 2024
1 parent d344479 commit d515f55
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 147 deletions.
25 changes: 16 additions & 9 deletions src/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <climits>
#include <functional>
#include <unordered_set>
#include "exit_status.h"

#if defined(__SVR4) && defined(__sun)
#include <sys/termios.h>
Expand All @@ -39,7 +40,6 @@
#include "metrics.h"
#include "state.h"
#include "status.h"
#include "subprocess.h"
#include "util.h"

using namespace std;
Expand Down Expand Up @@ -687,7 +687,7 @@ bool Builder::AlreadyUpToDate() const {
return !plan_.more_to_do();
}

bool Builder::Build(string* err) {
ExitStatus Builder::Build(string* err) {
assert(!AlreadyUpToDate());
plan_.PrepareQueue();

Expand Down Expand Up @@ -726,14 +726,14 @@ bool Builder::Build(string* err) {
if (!StartEdge(edge, err)) {
Cleanup();
status_->BuildFinished();
return false;
return ExitFailure;
}

if (edge->is_phony()) {
if (!plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, err)) {
Cleanup();
status_->BuildFinished();
return false;
return ExitFailure;
}
} else {
++pending_commands;
Expand All @@ -760,14 +760,16 @@ bool Builder::Build(string* err) {
Cleanup();
status_->BuildFinished();
*err = "interrupted by user";
return false;
return result.status;
}

--pending_commands;
if (!FinishCommand(&result, err)) {
bool command_finished = FinishCommand(&result, err);
SetExitCode(result.status);
if (!command_finished) {
Cleanup();
status_->BuildFinished();
return false;
return result.status;
}

if (!result.success()) {
Expand All @@ -791,11 +793,11 @@ bool Builder::Build(string* err) {
else
*err = "stuck [this is a bug]";

return false;
return GetExitCode();
}

status_->BuildFinished();
return true;
return ExitSuccess;
}

bool Builder::StartEdge(Edge* edge, string* err) {
Expand Down Expand Up @@ -1036,3 +1038,8 @@ bool Builder::LoadDyndeps(Node* node, string* err) {

return true;
}

void Builder::SetExitCode(ExitStatus code) {
// Set code to the most recent error
if (code != 0) exit_code_ = code;
}
9 changes: 8 additions & 1 deletion src/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ struct Builder {

/// Run the build. Returns false on error.
/// It is an error to call this function when AlreadyUpToDate() is true.
bool Build(std::string* err);
ExitStatus Build(std::string* err);

bool StartEdge(Edge* edge, std::string* err);

Expand All @@ -233,6 +233,9 @@ struct Builder {
std::unique_ptr<CommandRunner> command_runner_;
Status* status_;

// Get the global ExitStatus for the build
ExitStatus GetExitCode() { return exit_code_; }

private:
bool ExtractDeps(CommandRunner::Result* result, const std::string& deps_type,
const std::string& deps_prefix,
Expand All @@ -253,6 +256,10 @@ struct Builder {

DependencyScan scan_;

/// Keep the global exit code for the build
ExitStatus exit_code_;
void SetExitCode(ExitStatus code);

// Unimplemented copy ctor and operator= ensure we don't copy the auto_ptr.
Builder(const Builder &other); // DO NOT IMPLEMENT
void operator=(const Builder &other); // DO NOT IMPLEMENT
Expand Down
Loading

0 comments on commit d515f55

Please sign in to comment.