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 64c6798
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 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
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
9 changes: 4 additions & 5 deletions src/subprocess-win32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,22 +185,21 @@ void Subprocess::OnPipeReady() {
// function again later and get them at that point.
}

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

// TODO: add error handling for all of these.
WaitForSingleObject(child_, INFINITE);

DWORD exit_code = 0;
int exit_code = 0;
GetExitCodeProcess(child_, &exit_code);

CloseHandle(child_);
child_ = NULL;

return exit_code == 0 ? ExitSuccess :
exit_code == CONTROL_C_EXIT ? ExitInterrupted :
ExitFailure;
return exit_code == CONTROL_C_EXIT ? ExitInterrupted :
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 64c6798

Please sign in to comment.