From 4d19d997f98dfca40e112fd53216fccbda673ec8 Mon Sep 17 00:00:00 2001 From: Evan Martin Date: Tue, 17 May 2011 10:38:23 -0700 Subject: simplify CommandRunner, simplifying users too After much staring at this I think I found the more clear way to express what it's doing. --- src/build.cc | 35 +++++++++-------------------------- src/build.h | 6 ++---- src/build_test.cc | 9 ++------- 3 files changed, 13 insertions(+), 37 deletions(-) diff --git a/src/build.cc b/src/build.cc index 4e5aa1c..f90dcf2 100644 --- a/src/build.cc +++ b/src/build.cc @@ -309,8 +309,7 @@ struct RealCommandRunner : public CommandRunner { virtual ~RealCommandRunner() {} virtual bool CanRunMore(); virtual bool StartCommand(Edge* edge); - virtual bool WaitForCommands(); - virtual Edge* NextFinishedCommand(bool* success, string* output); + virtual Edge* WaitForCommand(bool* success, string* output); const BuildConfig& config_; SubprocessSet subprocs_; @@ -332,20 +331,11 @@ bool RealCommandRunner::StartCommand(Edge* edge) { return true; } -bool RealCommandRunner::WaitForCommands() { - if (subprocs_.running_.empty()) - return false; - - while (subprocs_.finished_.empty()) { +Edge* RealCommandRunner::WaitForCommand(bool* success, string* output) { + Subprocess* subproc; + while ((subproc = subprocs_.NextFinished()) == NULL) { subprocs_.DoWork(); } - return true; -} - -Edge* RealCommandRunner::NextFinishedCommand(bool* success, string* output) { - Subprocess* subproc = subprocs_.NextFinished(); - if (!subproc) - return NULL; *success = subproc->Finish(); *output = subproc->GetOutput(); @@ -368,10 +358,7 @@ struct DryRunCommandRunner : public CommandRunner { finished_.push(edge); return true; } - virtual bool WaitForCommands() { - return true; - } - virtual Edge* NextFinishedCommand(bool* success, string* output) { + virtual Edge* WaitForCommand(bool* success, string* output) { if (finished_.empty()) return NULL; *success = true; @@ -459,7 +446,7 @@ bool Builder::Build(string* err) { bool success; string output; Edge* edge; - if ((edge = command_runner_->NextFinishedCommand(&success, &output))) { + if ((edge = command_runner_->WaitForCommand(&success, &output))) { --pending_commands; FinishEdge(edge, success, output); if (!success) { @@ -477,14 +464,10 @@ bool Builder::Build(string* err) { } } - // If we get here, we can neither enqueue new commands nor are any done. + // If we get here, we can neither enqueue new commands nor are any running. if (pending_commands) { - if (!command_runner_->WaitForCommands()) { - // TODO: change the API such that this doesn't have a return value. - *err = "stuck: pending commands but none to wait for? [this is a bug]"; - return false; - } - continue; + *err = "stuck: pending commands but none to wait for? [this is a bug]"; + return false; } // If we get here, we cannot make any more progress. diff --git a/src/build.h b/src/build.h index 2129ede..5969441 100644 --- a/src/build.h +++ b/src/build.h @@ -72,10 +72,8 @@ struct CommandRunner { virtual ~CommandRunner() {} virtual bool CanRunMore() = 0; virtual bool StartCommand(Edge* edge) = 0; - /// Wait for commands to make progress; return false if there is no - /// progress to be made. - virtual bool WaitForCommands() = 0; - virtual Edge* NextFinishedCommand(bool* success, string* output) = 0; + /// Wait for a command to complete. + virtual Edge* WaitForCommand(bool* success, string* output) = 0; }; /// Options (e.g. verbosity, parallelism) passed to a build. diff --git a/src/build_test.cc b/src/build_test.cc index 9c4bd03..3d75180 100644 --- a/src/build_test.cc +++ b/src/build_test.cc @@ -210,8 +210,7 @@ struct BuildTest : public StateTestWithBuiltinRules, // CommandRunner impl virtual bool CanRunMore(); virtual bool StartCommand(Edge* edge); - virtual bool WaitForCommands(); - virtual Edge* NextFinishedCommand(bool* success, string* output); + virtual Edge* WaitForCommand(bool* success, string* output); BuildConfig MakeConfig() { BuildConfig config; @@ -264,11 +263,7 @@ bool BuildTest::StartCommand(Edge* edge) { return true; } -bool BuildTest::WaitForCommands() { - return last_command_ != NULL; -} - -Edge* BuildTest::NextFinishedCommand(bool* success, string* output) { +Edge* BuildTest::WaitForCommand(bool* success, string* output) { if (Edge* edge = last_command_) { if (edge->rule_->name_ == "fail") *success = false; -- cgit v0.12