Skip to content

Commit

Permalink
fixup! Propagate jobs' exit codes to the ninja's exit code
Browse files Browse the repository at this point in the history
  • Loading branch information
Felixoid committed Dec 10, 2024
1 parent cc694a7 commit 8fed3fc
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 21 deletions.
10 changes: 5 additions & 5 deletions src/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <climits>
#include <functional>
#include <unordered_set>
#include "exit_status.h"

#if defined(__SVR4) && defined(__sun)
#include <sys/termios.h>
Expand All @@ -35,6 +34,7 @@
#include "depfile_parser.h"
#include "deps_log.h"
#include "disk_interface.h"
#include "exit_status.h"
#include "explanations.h"
#include "graph.h"
#include "metrics.h"
Expand Down Expand Up @@ -765,7 +765,7 @@ ExitStatus Builder::Build(string* err) {

--pending_commands;
bool command_finished = FinishCommand(&result, err);
SetExitCode(result.status);
SetFailureCode(result.status);
if (!command_finished) {
Cleanup();
status_->BuildFinished();
Expand Down Expand Up @@ -1039,7 +1039,7 @@ 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;
void Builder::SetFailureCode(ExitStatus code) {
// The ExitSuccess should not overwrite any error
if (code != ExitSuccess) exit_code_ = code;
}
11 changes: 6 additions & 5 deletions src/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ struct Builder {
/// Returns true if the build targets are already up to date.
bool AlreadyUpToDate() const;

/// Run the build. Returns false on error.
/// Run the build. Returns ExitStatus or the exit code of the last failed job.
/// It is an error to call this function when AlreadyUpToDate() is true.
ExitStatus Build(std::string* err);

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

// Get the global ExitStatus for the build
ExitStatus GetExitCode() { return exit_code_; }
/// Returns ExitStatus or the exit code of the last failed job
/// (doesn't need to be an enum value of ExitStatus)
ExitStatus GetExitCode() const { return exit_code_; }

private:
bool ExtractDeps(CommandRunner::Result* result, const std::string& deps_type,
Expand All @@ -257,8 +258,8 @@ struct Builder {
DependencyScan scan_;

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

// Unimplemented copy ctor and operator= ensure we don't copy the auto_ptr.
Builder(const Builder &other); // DO NOT IMPLEMENT
Expand Down
1 change: 0 additions & 1 deletion src/build_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include <assert.h>
#include <climits>
#include <gtest/gtest.h>
#include <stdint.h>

#include "build_log.h"
Expand Down
5 changes: 2 additions & 3 deletions src/status_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

#include "status_printer.h"
#include "exit_status.h"

#ifdef _WIN32
#include "win32port.h"
Expand All @@ -26,7 +25,6 @@

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

#ifdef _WIN32
#include <fcntl.h>
Expand All @@ -35,6 +33,7 @@

#include "build.h"
#include "debug_flags.h"
#include "exit_status.h"

using namespace std;

Expand Down Expand Up @@ -204,7 +203,7 @@ void StatusPrinter::BuildEdgeFinished(Edge* edge, int64_t start_time_millis,
--running_edges_;

// Print the command that is spewing before printing its output.
if (exit_code != 0) {
if (exit_code != ExitSuccess) {
string outputs;
for (vector<Node*>::const_iterator o = edge->outputs_.begin();
o != edge->outputs_.end(); ++o)
Expand Down
4 changes: 1 addition & 3 deletions src/subprocess-posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <string.h>
#include <sys/wait.h>
#include <spawn.h>
#include <cmath>

#if defined(USE_PPOLL)
#include <poll.h>
Expand Down Expand Up @@ -171,13 +170,12 @@ ExitStatus Subprocess::Finish() {
return static_cast<ExitStatus>(WEXITSTATUS(status));
}
if (WIFSIGNALED(status)) {
// Overwrite interrupts to exit code 2
if (WTERMSIG(status) == SIGINT || WTERMSIG(status) == SIGTERM
|| WTERMSIG(status) == SIGHUP)
return ExitInterrupted;
}
// At this point, we exit with any other signal+128
return static_cast<ExitStatus>(status | 0x80);
return static_cast<ExitStatus>(status + 128);
}

bool Subprocess::Done() const {
Expand Down
6 changes: 2 additions & 4 deletions src/subprocess_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ TEST_F(SubprocessTest, BadCommandStderr) {
ExitStatus exit = subproc->Finish();
#ifdef _POSIX_VERSION
EXPECT_EQ(127, exit);
#endif
#ifdef _WIN32
#else
EXPECT_EQ(ExitFailure, exit);
#endif
EXPECT_NE("", subproc->GetOutput());
Expand All @@ -74,8 +73,7 @@ TEST_F(SubprocessTest, NoSuchCommand) {
ExitStatus exit = subproc->Finish();
#ifdef _POSIX_VERSION
EXPECT_EQ(127, exit);
#endif
#ifdef _WIN32
#else
EXPECT_EQ(ExitFailure, exit);
#endif
EXPECT_NE("", subproc->GetOutput());
Expand Down

0 comments on commit 8fed3fc

Please sign in to comment.