move BuildLog to DependencyScan
authorEvan Martin <martine@danga.com>
Tue, 4 Sep 2012 22:39:04 +0000 (18:39 -0400)
committerEvan Martin <martine@danga.com>
Tue, 4 Sep 2012 22:39:04 +0000 (18:39 -0400)
The build log is needed in computing whether an edge is
dirty, so I think it belongs here.  (It's a bit weird
that Builder needs to reach into it to record completed
commands, maybe it will become cleaner with more thought.)

src/build.cc
src/build.h
src/build_test.cc
src/disk_interface_test.cc
src/graph.h
src/graph_test.cc
src/ninja.cc
src/state.cc
src/state.h

index 4102209..efc05b0 100644 (file)
@@ -554,11 +554,10 @@ struct DryRunCommandRunner : public CommandRunner {
 };
 
 Builder::Builder(State* state, const BuildConfig& config,
-                 DiskInterface* disk_interface)
+                 BuildLog* log, DiskInterface* disk_interface)
     : state_(state), config_(config), disk_interface_(disk_interface),
-      scan_(state, disk_interface) {
+      scan_(state, log, disk_interface) {
   status_ = new BuildStatus(config);
-  log_ = state->build_log_;
 }
 
 Builder::~Builder() {
@@ -797,7 +796,7 @@ void Builder::FinishEdge(Edge* edge, bool success, const string& output) {
 
   int start_time, end_time;
   status_->BuildEdgeFinished(edge, success, output, &start_time, &end_time);
-  if (success && log_)
-    log_->RecordCommand(edge, start_time, end_time, restat_mtime);
+  if (success && scan_.build_log())
+    scan_.build_log()->RecordCommand(edge, start_time, end_time, restat_mtime);
 }
 
index 9152eac..3e7a144 100644 (file)
@@ -121,7 +121,7 @@ struct BuildConfig {
 /// Builder wraps the build process: starting commands, updating status.
 struct Builder {
   Builder(State* state, const BuildConfig& config,
-          DiskInterface* disk_interface);
+          BuildLog* log, DiskInterface* disk_interface);
   ~Builder();
 
   /// Clean up after interrupted commands by deleting output files.
@@ -143,12 +143,16 @@ struct Builder {
   bool StartEdge(Edge* edge, string* err);
   void FinishEdge(Edge* edge, bool success, const string& output);
 
+  /// Used for tests.
+  void SetBuildLog(BuildLog* log) {
+    scan_.set_build_log(log);
+  }
+
   State* state_;
   const BuildConfig& config_;
   Plan plan_;
   auto_ptr<CommandRunner> command_runner_;
   BuildStatus* status_;
-  BuildLog* log_;
 
  private:
   DiskInterface* disk_interface_;
index d4673ae..859e758 100644 (file)
@@ -178,7 +178,8 @@ TEST_F(PlanTest, DependencyCycle) {
 
 struct BuildTest : public StateTestWithBuiltinRules,
                    public CommandRunner {
-  BuildTest() : config_(MakeConfig()), builder_(&state_, config_, &fs_),
+  BuildTest() : config_(MakeConfig()),
+                builder_(&state_, config_, NULL, &fs_),
                 now_(1), last_command_(NULL), status_(config_) {
     builder_.command_runner_.reset(this);
     AssertParse(&state_,
@@ -717,7 +718,7 @@ TEST_F(BuildTest, SwallowFailuresLimit) {
 
 struct BuildWithLogTest : public BuildTest {
   BuildWithLogTest() {
-    state_.build_log_ = builder_.log_ = &build_log_;
+    builder_.SetBuildLog(&build_log_);
   }
 
   BuildLog build_log_;
index 68a6ca9..32fe9cb 100644 (file)
@@ -105,7 +105,7 @@ TEST_F(DiskInterfaceTest, RemoveFile) {
 
 struct StatTest : public StateTestWithBuiltinRules,
                   public DiskInterface {
-  StatTest() : scan_(&state_, this) {}
+  StatTest() : scan_(&state_, NULL, this) {}
 
   // DiskInterface implementation.
   virtual TimeStamp Stat(const string& path);
index ced1f4f..893ec09 100644 (file)
@@ -201,8 +201,10 @@ struct Edge {
 /// DependencyScan manages the process of scanning the files in a graph
 /// and updating the dirty/outputs_ready state of all the nodes and edges.
 struct DependencyScan {
-  DependencyScan(State* state, DiskInterface* disk_interface)
-      : state_(state), disk_interface_(disk_interface) {}
+  DependencyScan(State* state, BuildLog* build_log,
+                 DiskInterface* disk_interface)
+      : state_(state), build_log_(build_log),
+        disk_interface_(disk_interface) {}
 
   /// Examine inputs, outputs, and command lines to judge whether an edge
   /// needs to be re-run, and update outputs_ready_ and each outputs' |dirty_|
@@ -217,11 +219,17 @@ struct DependencyScan {
 
   bool LoadDepFile(Edge* edge, string* err);
 
-  State* state_;
-  DiskInterface* disk_interface_;
   BuildLog* build_log() const {
-    return state_->build_log_;
+    return build_log_;
   }
+  void set_build_log(BuildLog* log) {
+    build_log_ = log;
+  }
+
+ private:
+  State* state_;
+  BuildLog* build_log_;
+  DiskInterface* disk_interface_;
 };
 
 #endif  // NINJA_GRAPH_H_
index 2bc0c17..5b25c2f 100644 (file)
@@ -17,7 +17,7 @@
 #include "test.h"
 
 struct GraphTest : public StateTestWithBuiltinRules {
-  GraphTest() : scan_(&state_, &fs_) {}
+  GraphTest() : scan_(&state_, NULL, &fs_) {}
 
   VirtualFileSystem fs_;
   DependencyScan scan_;
index 799af02..fb2bb3c 100644 (file)
@@ -737,7 +737,6 @@ reload:
     return RunTool(tool, &globals, argc, argv);
 
   BuildLog build_log;
-  globals.state->build_log_ = &build_log;
 
   const string build_dir = globals.state->bindings_.LookupVariable("builddir");
   const char* kLogPath = ".ninja_log";
@@ -770,7 +769,7 @@ reload:
 
   if (!rebuilt_manifest) { // Don't get caught in an infinite loop by a rebuild
                            // target that is never up to date.
-    Builder manifest_builder(globals.state, globals.config,
+    Builder manifest_builder(globals.state, globals.config, &build_log,
                              &globals.disk_interface);
     if (RebuildManifest(&manifest_builder, input_file, &err)) {
       rebuilt_manifest = true;
@@ -782,7 +781,8 @@ reload:
     }
   }
 
-  Builder builder(globals.state, globals.config, &globals.disk_interface);
+  Builder builder(globals.state, globals.config, &build_log,
+                  &globals.disk_interface);
   int result = RunBuild(&builder, argc, argv);
   if (g_metrics) {
     g_metrics->Report();
index 9f38fe9..4c7168b 100644 (file)
@@ -24,7 +24,7 @@
 
 const Rule State::kPhonyRule("phony");
 
-State::State() : build_log_(NULL) {
+State::State() {
   AddRule(&kPhonyRule);
 }
 
index 9197ef8..026acf3 100644 (file)
@@ -23,7 +23,6 @@ using namespace std;
 #include "eval_env.h"
 #include "hash_map.h"
 
-struct BuildLog;
 struct Edge;
 struct Node;
 struct Rule;
@@ -71,7 +70,6 @@ struct State {
 
   BindingEnv bindings_;
   vector<Node*> defaults_;
-  BuildLog* build_log_;
 };
 
 #endif  // NINJA_STATE_H_