Skip to content

Commit

Permalink
Propagate supbrocess' exit codes to the ninja exit code
Browse files Browse the repository at this point in the history
  • Loading branch information
Felixoid committed Dec 3, 2024
1 parent d79d8f7 commit 2eb867f
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 13 deletions.
9 changes: 8 additions & 1 deletion src/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,9 @@ bool Builder::Build(string* err) {
}

--pending_commands;
if (!FinishCommand(&result, err)) {
bool command_finished = FinishCommand(&result, err);
SetExitCode(result.status);
if (!command_finished) {
Cleanup();
status_->BuildFinished();
return false;
Expand Down Expand Up @@ -1036,3 +1038,8 @@ bool Builder::LoadDyndeps(Node* node, string* err) {

return true;
}

void Builder::SetExitCode(int code) {
// Set code to the most recent error
if (code != 0) exit_code_ = code;
}
7 changes: 6 additions & 1 deletion src/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ struct CommandRunner {
struct Result {
Result() : edge(NULL) {}
Edge* edge;
ExitStatus status;
int status;
std::string output;
bool success() const { return status == ExitSuccess; }
};
Expand Down Expand Up @@ -233,6 +233,11 @@ struct Builder {
std::unique_ptr<CommandRunner> command_runner_;
Status* status_;

/// Keep the global exit code for the run
int exit_code_;
void SetExitCode(int code);
int ExitCode() { return exit_code_; }

private:
bool ExtractDeps(CommandRunner::Result* result, const std::string& deps_type,
const std::string& deps_prefix,
Expand Down
2 changes: 1 addition & 1 deletion src/ninja.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,7 @@ int NinjaMain::RunBuild(int argc, char** argv, Status* status) {
if (err.find("interrupted by user") != string::npos) {
return 130;
}
return 1;
return builder.ExitCode();
}

return 0;
Expand Down
7 changes: 4 additions & 3 deletions src/subprocess-posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void Subprocess::OnPipeReady() {
}
}

ExitStatus Subprocess::Finish() {
int Subprocess::Finish() {
assert(pid_ != -1);
int status;
if (waitpid(pid_, &status, 0) < 0)
Expand All @@ -164,16 +164,17 @@ ExitStatus Subprocess::Finish() {
}
#endif

int exit = 0;
if (WIFEXITED(status)) {
int exit = WEXITSTATUS(status);
exit = WEXITSTATUS(status);
if (exit == 0)
return ExitSuccess;
} else if (WIFSIGNALED(status)) {
if (WTERMSIG(status) == SIGINT || WTERMSIG(status) == SIGTERM
|| WTERMSIG(status) == SIGHUP)
return ExitInterrupted;
}
return ExitFailure;
return exit;
}

bool Subprocess::Done() const {
Expand Down
7 changes: 3 additions & 4 deletions src/subprocess-win32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void Subprocess::OnPipeReady() {
// function again later and get them at that point.
}

ExitStatus Subprocess::Finish() {
int Subprocess::Finish() {
if (!child_)
return ExitFailure;

Expand All @@ -198,9 +198,8 @@ ExitStatus Subprocess::Finish() {
CloseHandle(child_);
child_ = NULL;

return exit_code == 0 ? ExitSuccess :
exit_code == CONTROL_C_EXIT ? ExitInterrupted :
ExitFailure;
return exit_code == CONTROL_C_EXIT ? ExitInterrupted :
(int)exit_code;
}

bool Subprocess::Done() const {
Expand Down
2 changes: 1 addition & 1 deletion src/subprocess.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct Subprocess {

/// Returns ExitSuccess on successful process exit, ExitInterrupted if
/// the process was interrupted, ExitFailure if it otherwise failed.
ExitStatus Finish();
int Finish();

bool Done() const;

Expand Down
11 changes: 9 additions & 2 deletions src/subprocess_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "subprocess.h"

#include "exit_status.h"
#include "test.h"

#ifndef _WIN32
Expand Down Expand Up @@ -50,7 +51,10 @@ TEST_F(SubprocessTest, BadCommandStderr) {
subprocs_.DoWork();
}

EXPECT_EQ(ExitFailure, subproc->Finish());
int exit = subproc->Finish();
EXPECT_NE(ExitSuccess, exit);
EXPECT_NE(ExitFailure, exit);
EXPECT_NE(ExitInterrupted, exit);
EXPECT_NE("", subproc->GetOutput());
}

Expand All @@ -64,7 +68,10 @@ TEST_F(SubprocessTest, NoSuchCommand) {
subprocs_.DoWork();
}

EXPECT_EQ(ExitFailure, subproc->Finish());
int exit = subproc->Finish();
EXPECT_NE(ExitSuccess, exit);
EXPECT_NE(ExitFailure, exit);
EXPECT_NE(ExitInterrupted, exit);
EXPECT_NE("", subproc->GetOutput());
#ifdef _WIN32
ASSERT_EQ("CreateProcess failed: The system cannot find the file "
Expand Down

0 comments on commit 2eb867f

Please sign in to comment.