Imported Upstream version 1.10.1
[platform/upstream/ninja.git] / src / build_test.cc
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "build.h"
16
17 #include <assert.h>
18
19 #include "build_log.h"
20 #include "deps_log.h"
21 #include "graph.h"
22 #include "test.h"
23
24 struct CompareEdgesByOutput {
25   static bool cmp(const Edge* a, const Edge* b) {
26     return a->outputs_[0]->path() < b->outputs_[0]->path();
27   }
28 };
29
30 /// Fixture for tests involving Plan.
31 // Though Plan doesn't use State, it's useful to have one around
32 // to create Nodes and Edges.
33 struct PlanTest : public StateTestWithBuiltinRules {
34   Plan plan_;
35
36   /// Because FindWork does not return Edges in any sort of predictable order,
37   // provide a means to get available Edges in order and in a format which is
38   // easy to write tests around.
39   void FindWorkSorted(deque<Edge*>* ret, int count) {
40     for (int i = 0; i < count; ++i) {
41       ASSERT_TRUE(plan_.more_to_do());
42       Edge* edge = plan_.FindWork();
43       ASSERT_TRUE(edge);
44       ret->push_back(edge);
45     }
46     ASSERT_FALSE(plan_.FindWork());
47     sort(ret->begin(), ret->end(), CompareEdgesByOutput::cmp);
48   }
49
50   void TestPoolWithDepthOne(const char *test_case);
51 };
52
53 TEST_F(PlanTest, Basic) {
54   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
55 "build out: cat mid\n"
56 "build mid: cat in\n"));
57   GetNode("mid")->MarkDirty();
58   GetNode("out")->MarkDirty();
59   string err;
60   EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
61   ASSERT_EQ("", err);
62   ASSERT_TRUE(plan_.more_to_do());
63
64   Edge* edge = plan_.FindWork();
65   ASSERT_TRUE(edge);
66   ASSERT_EQ("in",  edge->inputs_[0]->path());
67   ASSERT_EQ("mid", edge->outputs_[0]->path());
68
69   ASSERT_FALSE(plan_.FindWork());
70
71   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
72   ASSERT_EQ("", err);
73
74   edge = plan_.FindWork();
75   ASSERT_TRUE(edge);
76   ASSERT_EQ("mid", edge->inputs_[0]->path());
77   ASSERT_EQ("out", edge->outputs_[0]->path());
78
79   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
80   ASSERT_EQ("", err);
81
82   ASSERT_FALSE(plan_.more_to_do());
83   edge = plan_.FindWork();
84   ASSERT_EQ(0, edge);
85 }
86
87 // Test that two outputs from one rule can be handled as inputs to the next.
88 TEST_F(PlanTest, DoubleOutputDirect) {
89   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
90 "build out: cat mid1 mid2\n"
91 "build mid1 mid2: cat in\n"));
92   GetNode("mid1")->MarkDirty();
93   GetNode("mid2")->MarkDirty();
94   GetNode("out")->MarkDirty();
95
96   string err;
97   EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
98   ASSERT_EQ("", err);
99   ASSERT_TRUE(plan_.more_to_do());
100
101   Edge* edge;
102   edge = plan_.FindWork();
103   ASSERT_TRUE(edge);  // cat in
104   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
105   ASSERT_EQ("", err);
106
107   edge = plan_.FindWork();
108   ASSERT_TRUE(edge);  // cat mid1 mid2
109   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
110   ASSERT_EQ("", err);
111
112   edge = plan_.FindWork();
113   ASSERT_FALSE(edge);  // done
114 }
115
116 // Test that two outputs from one rule can eventually be routed to another.
117 TEST_F(PlanTest, DoubleOutputIndirect) {
118   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
119 "build out: cat b1 b2\n"
120 "build b1: cat a1\n"
121 "build b2: cat a2\n"
122 "build a1 a2: cat in\n"));
123   GetNode("a1")->MarkDirty();
124   GetNode("a2")->MarkDirty();
125   GetNode("b1")->MarkDirty();
126   GetNode("b2")->MarkDirty();
127   GetNode("out")->MarkDirty();
128   string err;
129   EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
130   ASSERT_EQ("", err);
131   ASSERT_TRUE(plan_.more_to_do());
132
133   Edge* edge;
134   edge = plan_.FindWork();
135   ASSERT_TRUE(edge);  // cat in
136   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
137   ASSERT_EQ("", err);
138
139   edge = plan_.FindWork();
140   ASSERT_TRUE(edge);  // cat a1
141   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
142   ASSERT_EQ("", err);
143
144   edge = plan_.FindWork();
145   ASSERT_TRUE(edge);  // cat a2
146   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
147   ASSERT_EQ("", err);
148
149   edge = plan_.FindWork();
150   ASSERT_TRUE(edge);  // cat b1 b2
151   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
152   ASSERT_EQ("", err);
153
154   edge = plan_.FindWork();
155   ASSERT_FALSE(edge);  // done
156 }
157
158 // Test that two edges from one output can both execute.
159 TEST_F(PlanTest, DoubleDependent) {
160   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
161 "build out: cat a1 a2\n"
162 "build a1: cat mid\n"
163 "build a2: cat mid\n"
164 "build mid: cat in\n"));
165   GetNode("mid")->MarkDirty();
166   GetNode("a1")->MarkDirty();
167   GetNode("a2")->MarkDirty();
168   GetNode("out")->MarkDirty();
169
170   string err;
171   EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
172   ASSERT_EQ("", err);
173   ASSERT_TRUE(plan_.more_to_do());
174
175   Edge* edge;
176   edge = plan_.FindWork();
177   ASSERT_TRUE(edge);  // cat in
178   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
179   ASSERT_EQ("", err);
180
181   edge = plan_.FindWork();
182   ASSERT_TRUE(edge);  // cat mid
183   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
184   ASSERT_EQ("", err);
185
186   edge = plan_.FindWork();
187   ASSERT_TRUE(edge);  // cat mid
188   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
189   ASSERT_EQ("", err);
190
191   edge = plan_.FindWork();
192   ASSERT_TRUE(edge);  // cat a1 a2
193   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
194   ASSERT_EQ("", err);
195
196   edge = plan_.FindWork();
197   ASSERT_FALSE(edge);  // done
198 }
199
200 void PlanTest::TestPoolWithDepthOne(const char* test_case) {
201   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, test_case));
202   GetNode("out1")->MarkDirty();
203   GetNode("out2")->MarkDirty();
204   string err;
205   EXPECT_TRUE(plan_.AddTarget(GetNode("out1"), &err));
206   ASSERT_EQ("", err);
207   EXPECT_TRUE(plan_.AddTarget(GetNode("out2"), &err));
208   ASSERT_EQ("", err);
209   ASSERT_TRUE(plan_.more_to_do());
210
211   Edge* edge = plan_.FindWork();
212   ASSERT_TRUE(edge);
213   ASSERT_EQ("in",  edge->inputs_[0]->path());
214   ASSERT_EQ("out1", edge->outputs_[0]->path());
215
216   // This will be false since poolcat is serialized
217   ASSERT_FALSE(plan_.FindWork());
218
219   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
220   ASSERT_EQ("", err);
221
222   edge = plan_.FindWork();
223   ASSERT_TRUE(edge);
224   ASSERT_EQ("in", edge->inputs_[0]->path());
225   ASSERT_EQ("out2", edge->outputs_[0]->path());
226
227   ASSERT_FALSE(plan_.FindWork());
228
229   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
230   ASSERT_EQ("", err);
231
232   ASSERT_FALSE(plan_.more_to_do());
233   edge = plan_.FindWork();
234   ASSERT_EQ(0, edge);
235 }
236
237 TEST_F(PlanTest, PoolWithDepthOne) {
238   TestPoolWithDepthOne(
239 "pool foobar\n"
240 "  depth = 1\n"
241 "rule poolcat\n"
242 "  command = cat $in > $out\n"
243 "  pool = foobar\n"
244 "build out1: poolcat in\n"
245 "build out2: poolcat in\n");
246 }
247
248 TEST_F(PlanTest, ConsolePool) {
249   TestPoolWithDepthOne(
250 "rule poolcat\n"
251 "  command = cat $in > $out\n"
252 "  pool = console\n"
253 "build out1: poolcat in\n"
254 "build out2: poolcat in\n");
255 }
256
257 TEST_F(PlanTest, PoolsWithDepthTwo) {
258   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
259 "pool foobar\n"
260 "  depth = 2\n"
261 "pool bazbin\n"
262 "  depth = 2\n"
263 "rule foocat\n"
264 "  command = cat $in > $out\n"
265 "  pool = foobar\n"
266 "rule bazcat\n"
267 "  command = cat $in > $out\n"
268 "  pool = bazbin\n"
269 "build out1: foocat in\n"
270 "build out2: foocat in\n"
271 "build out3: foocat in\n"
272 "build outb1: bazcat in\n"
273 "build outb2: bazcat in\n"
274 "build outb3: bazcat in\n"
275 "  pool =\n"
276 "build allTheThings: cat out1 out2 out3 outb1 outb2 outb3\n"
277 ));
278   // Mark all the out* nodes dirty
279   for (int i = 0; i < 3; ++i) {
280     GetNode("out" + string(1, '1' + static_cast<char>(i)))->MarkDirty();
281     GetNode("outb" + string(1, '1' + static_cast<char>(i)))->MarkDirty();
282   }
283   GetNode("allTheThings")->MarkDirty();
284
285   string err;
286   EXPECT_TRUE(plan_.AddTarget(GetNode("allTheThings"), &err));
287   ASSERT_EQ("", err);
288
289   deque<Edge*> edges;
290   FindWorkSorted(&edges, 5);
291
292   for (int i = 0; i < 4; ++i) {
293     Edge *edge = edges[i];
294     ASSERT_EQ("in",  edge->inputs_[0]->path());
295     string base_name(i < 2 ? "out" : "outb");
296     ASSERT_EQ(base_name + string(1, '1' + (i % 2)), edge->outputs_[0]->path());
297   }
298
299   // outb3 is exempt because it has an empty pool
300   Edge* edge = edges[4];
301   ASSERT_TRUE(edge);
302   ASSERT_EQ("in",  edge->inputs_[0]->path());
303   ASSERT_EQ("outb3", edge->outputs_[0]->path());
304
305   // finish out1
306   plan_.EdgeFinished(edges.front(), Plan::kEdgeSucceeded, &err);
307   ASSERT_EQ("", err);
308   edges.pop_front();
309
310   // out3 should be available
311   Edge* out3 = plan_.FindWork();
312   ASSERT_TRUE(out3);
313   ASSERT_EQ("in",  out3->inputs_[0]->path());
314   ASSERT_EQ("out3", out3->outputs_[0]->path());
315
316   ASSERT_FALSE(plan_.FindWork());
317
318   plan_.EdgeFinished(out3, Plan::kEdgeSucceeded, &err);
319   ASSERT_EQ("", err);
320
321   ASSERT_FALSE(plan_.FindWork());
322
323   for (deque<Edge*>::iterator it = edges.begin(); it != edges.end(); ++it) {
324     plan_.EdgeFinished(*it, Plan::kEdgeSucceeded, &err);
325     ASSERT_EQ("", err);
326   }
327
328   Edge* last = plan_.FindWork();
329   ASSERT_TRUE(last);
330   ASSERT_EQ("allTheThings", last->outputs_[0]->path());
331
332   plan_.EdgeFinished(last, Plan::kEdgeSucceeded, &err);
333   ASSERT_EQ("", err);
334
335   ASSERT_FALSE(plan_.more_to_do());
336   ASSERT_FALSE(plan_.FindWork());
337 }
338
339 TEST_F(PlanTest, PoolWithRedundantEdges) {
340   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
341     "pool compile\n"
342     "  depth = 1\n"
343     "rule gen_foo\n"
344     "  command = touch foo.cpp\n"
345     "rule gen_bar\n"
346     "  command = touch bar.cpp\n"
347     "rule echo\n"
348     "  command = echo $out > $out\n"
349     "build foo.cpp.obj: echo foo.cpp || foo.cpp\n"
350     "  pool = compile\n"
351     "build bar.cpp.obj: echo bar.cpp || bar.cpp\n"
352     "  pool = compile\n"
353     "build libfoo.a: echo foo.cpp.obj bar.cpp.obj\n"
354     "build foo.cpp: gen_foo\n"
355     "build bar.cpp: gen_bar\n"
356     "build all: phony libfoo.a\n"));
357   GetNode("foo.cpp")->MarkDirty();
358   GetNode("foo.cpp.obj")->MarkDirty();
359   GetNode("bar.cpp")->MarkDirty();
360   GetNode("bar.cpp.obj")->MarkDirty();
361   GetNode("libfoo.a")->MarkDirty();
362   GetNode("all")->MarkDirty();
363   string err;
364   EXPECT_TRUE(plan_.AddTarget(GetNode("all"), &err));
365   ASSERT_EQ("", err);
366   ASSERT_TRUE(plan_.more_to_do());
367
368   Edge* edge = NULL;
369
370   deque<Edge*> initial_edges;
371   FindWorkSorted(&initial_edges, 2);
372
373   edge = initial_edges[1];  // Foo first
374   ASSERT_EQ("foo.cpp", edge->outputs_[0]->path());
375   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
376   ASSERT_EQ("", err);
377
378   edge = plan_.FindWork();
379   ASSERT_TRUE(edge);
380   ASSERT_FALSE(plan_.FindWork());
381   ASSERT_EQ("foo.cpp", edge->inputs_[0]->path());
382   ASSERT_EQ("foo.cpp", edge->inputs_[1]->path());
383   ASSERT_EQ("foo.cpp.obj", edge->outputs_[0]->path());
384   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
385   ASSERT_EQ("", err);
386
387   edge = initial_edges[0];  // Now for bar
388   ASSERT_EQ("bar.cpp", edge->outputs_[0]->path());
389   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
390   ASSERT_EQ("", err);
391
392   edge = plan_.FindWork();
393   ASSERT_TRUE(edge);
394   ASSERT_FALSE(plan_.FindWork());
395   ASSERT_EQ("bar.cpp", edge->inputs_[0]->path());
396   ASSERT_EQ("bar.cpp", edge->inputs_[1]->path());
397   ASSERT_EQ("bar.cpp.obj", edge->outputs_[0]->path());
398   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
399   ASSERT_EQ("", err);
400
401   edge = plan_.FindWork();
402   ASSERT_TRUE(edge);
403   ASSERT_FALSE(plan_.FindWork());
404   ASSERT_EQ("foo.cpp.obj", edge->inputs_[0]->path());
405   ASSERT_EQ("bar.cpp.obj", edge->inputs_[1]->path());
406   ASSERT_EQ("libfoo.a", edge->outputs_[0]->path());
407   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
408   ASSERT_EQ("", err);
409
410   edge = plan_.FindWork();
411   ASSERT_TRUE(edge);
412   ASSERT_FALSE(plan_.FindWork());
413   ASSERT_EQ("libfoo.a", edge->inputs_[0]->path());
414   ASSERT_EQ("all", edge->outputs_[0]->path());
415   plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
416   ASSERT_EQ("", err);
417
418   edge = plan_.FindWork();
419   ASSERT_FALSE(edge);
420   ASSERT_FALSE(plan_.more_to_do());
421 }
422
423 TEST_F(PlanTest, PoolWithFailingEdge) {
424   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
425     "pool foobar\n"
426     "  depth = 1\n"
427     "rule poolcat\n"
428     "  command = cat $in > $out\n"
429     "  pool = foobar\n"
430     "build out1: poolcat in\n"
431     "build out2: poolcat in\n"));
432   GetNode("out1")->MarkDirty();
433   GetNode("out2")->MarkDirty();
434   string err;
435   EXPECT_TRUE(plan_.AddTarget(GetNode("out1"), &err));
436   ASSERT_EQ("", err);
437   EXPECT_TRUE(plan_.AddTarget(GetNode("out2"), &err));
438   ASSERT_EQ("", err);
439   ASSERT_TRUE(plan_.more_to_do());
440
441   Edge* edge = plan_.FindWork();
442   ASSERT_TRUE(edge);
443   ASSERT_EQ("in",  edge->inputs_[0]->path());
444   ASSERT_EQ("out1", edge->outputs_[0]->path());
445
446   // This will be false since poolcat is serialized
447   ASSERT_FALSE(plan_.FindWork());
448
449   plan_.EdgeFinished(edge, Plan::kEdgeFailed, &err);
450   ASSERT_EQ("", err);
451
452   edge = plan_.FindWork();
453   ASSERT_TRUE(edge);
454   ASSERT_EQ("in", edge->inputs_[0]->path());
455   ASSERT_EQ("out2", edge->outputs_[0]->path());
456
457   ASSERT_FALSE(plan_.FindWork());
458
459   plan_.EdgeFinished(edge, Plan::kEdgeFailed, &err);
460   ASSERT_EQ("", err);
461
462   ASSERT_TRUE(plan_.more_to_do()); // Jobs have failed
463   edge = plan_.FindWork();
464   ASSERT_EQ(0, edge);
465 }
466
467 /// Fake implementation of CommandRunner, useful for tests.
468 struct FakeCommandRunner : public CommandRunner {
469   explicit FakeCommandRunner(VirtualFileSystem* fs) :
470       max_active_edges_(1), fs_(fs) {}
471
472   // CommandRunner impl
473   virtual bool CanRunMore() const;
474   virtual bool StartCommand(Edge* edge);
475   virtual bool WaitForCommand(Result* result);
476   virtual vector<Edge*> GetActiveEdges();
477   virtual void Abort();
478
479   vector<string> commands_ran_;
480   vector<Edge*> active_edges_;
481   size_t max_active_edges_;
482   VirtualFileSystem* fs_;
483 };
484
485 struct BuildTest : public StateTestWithBuiltinRules, public BuildLogUser {
486   BuildTest() : config_(MakeConfig()), command_runner_(&fs_),
487                 builder_(&state_, config_, NULL, NULL, &fs_),
488                 status_(config_) {
489   }
490
491   BuildTest(DepsLog* log) : config_(MakeConfig()), command_runner_(&fs_),
492                             builder_(&state_, config_, NULL, log, &fs_),
493                             status_(config_) {
494   }
495
496   virtual void SetUp() {
497     StateTestWithBuiltinRules::SetUp();
498
499     builder_.command_runner_.reset(&command_runner_);
500     AssertParse(&state_,
501 "build cat1: cat in1\n"
502 "build cat2: cat in1 in2\n"
503 "build cat12: cat cat1 cat2\n");
504
505     fs_.Create("in1", "");
506     fs_.Create("in2", "");
507   }
508
509   ~BuildTest() {
510     builder_.command_runner_.release();
511   }
512
513   virtual bool IsPathDead(StringPiece s) const { return false; }
514
515   /// Rebuild target in the 'working tree' (fs_).
516   /// State of command_runner_ and logs contents (if specified) ARE MODIFIED.
517   /// Handy to check for NOOP builds, and higher-level rebuild tests.
518   void RebuildTarget(const string& target, const char* manifest,
519                      const char* log_path = NULL, const char* deps_path = NULL,
520                      State* state = NULL);
521
522   // Mark a path dirty.
523   void Dirty(const string& path);
524
525   BuildConfig MakeConfig() {
526     BuildConfig config;
527     config.verbosity = BuildConfig::QUIET;
528     return config;
529   }
530
531   BuildConfig config_;
532   FakeCommandRunner command_runner_;
533   VirtualFileSystem fs_;
534   Builder builder_;
535
536   BuildStatus status_;
537 };
538
539 void BuildTest::RebuildTarget(const string& target, const char* manifest,
540                               const char* log_path, const char* deps_path,
541                               State* state) {
542   State local_state, *pstate = &local_state;
543   if (state)
544     pstate = state;
545   ASSERT_NO_FATAL_FAILURE(AddCatRule(pstate));
546   AssertParse(pstate, manifest);
547
548   string err;
549   BuildLog build_log, *pbuild_log = NULL;
550   if (log_path) {
551     ASSERT_TRUE(build_log.Load(log_path, &err));
552     ASSERT_TRUE(build_log.OpenForWrite(log_path, *this, &err));
553     ASSERT_EQ("", err);
554     pbuild_log = &build_log;
555   }
556
557   DepsLog deps_log, *pdeps_log = NULL;
558   if (deps_path) {
559     ASSERT_TRUE(deps_log.Load(deps_path, pstate, &err));
560     ASSERT_TRUE(deps_log.OpenForWrite(deps_path, &err));
561     ASSERT_EQ("", err);
562     pdeps_log = &deps_log;
563   }
564
565   Builder builder(pstate, config_, pbuild_log, pdeps_log, &fs_);
566   EXPECT_TRUE(builder.AddTarget(target, &err));
567
568   command_runner_.commands_ran_.clear();
569   builder.command_runner_.reset(&command_runner_);
570   if (!builder.AlreadyUpToDate()) {
571     bool build_res = builder.Build(&err);
572     EXPECT_TRUE(build_res);
573   }
574   builder.command_runner_.release();
575 }
576
577 bool FakeCommandRunner::CanRunMore() const {
578   return active_edges_.size() < max_active_edges_;
579 }
580
581 bool FakeCommandRunner::StartCommand(Edge* edge) {
582   assert(active_edges_.size() < max_active_edges_);
583   assert(find(active_edges_.begin(), active_edges_.end(), edge)
584          == active_edges_.end());
585   commands_ran_.push_back(edge->EvaluateCommand());
586   if (edge->rule().name() == "cat"  ||
587       edge->rule().name() == "cat_rsp" ||
588       edge->rule().name() == "cat_rsp_out" ||
589       edge->rule().name() == "cc" ||
590       edge->rule().name() == "cp_multi_msvc" ||
591       edge->rule().name() == "cp_multi_gcc" ||
592       edge->rule().name() == "touch" ||
593       edge->rule().name() == "touch-interrupt" ||
594       edge->rule().name() == "touch-fail-tick2") {
595     for (vector<Node*>::iterator out = edge->outputs_.begin();
596          out != edge->outputs_.end(); ++out) {
597       fs_->Create((*out)->path(), "");
598     }
599   } else if (edge->rule().name() == "true" ||
600              edge->rule().name() == "fail" ||
601              edge->rule().name() == "interrupt" ||
602              edge->rule().name() == "console") {
603     // Don't do anything.
604   } else if (edge->rule().name() == "cp") {
605     assert(!edge->inputs_.empty());
606     assert(edge->outputs_.size() == 1);
607     string content;
608     string err;
609     if (fs_->ReadFile(edge->inputs_[0]->path(), &content, &err) ==
610         DiskInterface::Okay)
611       fs_->WriteFile(edge->outputs_[0]->path(), content);
612   } else {
613     printf("unknown command\n");
614     return false;
615   }
616
617   active_edges_.push_back(edge);
618
619   // Allow tests to control the order by the name of the first output.
620   sort(active_edges_.begin(), active_edges_.end(),
621        CompareEdgesByOutput::cmp);
622
623   return true;
624 }
625
626 bool FakeCommandRunner::WaitForCommand(Result* result) {
627   if (active_edges_.empty())
628     return false;
629
630   // All active edges were already completed immediately when started,
631   // so we can pick any edge here.  Pick the last edge.  Tests can
632   // control the order of edges by the name of the first output.
633   vector<Edge*>::iterator edge_iter = active_edges_.end() - 1;
634
635   Edge* edge = *edge_iter;
636   result->edge = edge;
637
638   if (edge->rule().name() == "interrupt" ||
639       edge->rule().name() == "touch-interrupt") {
640     result->status = ExitInterrupted;
641     return true;
642   }
643
644   if (edge->rule().name() == "console") {
645     if (edge->use_console())
646       result->status = ExitSuccess;
647     else
648       result->status = ExitFailure;
649     active_edges_.erase(edge_iter);
650     return true;
651   }
652
653   if (edge->rule().name() == "cp_multi_msvc") {
654     const std::string prefix = edge->GetBinding("msvc_deps_prefix");
655     for (std::vector<Node*>::iterator in = edge->inputs_.begin();
656          in != edge->inputs_.end(); ++in) {
657       result->output += prefix + (*in)->path() + '\n';
658     }
659   }
660
661   if (edge->rule().name() == "fail" ||
662       (edge->rule().name() == "touch-fail-tick2" && fs_->now_ == 2))
663     result->status = ExitFailure;
664   else
665     result->status = ExitSuccess;
666
667   // Provide a way for test cases to verify when an edge finishes that
668   // some other edge is still active.  This is useful for test cases
669   // covering behavior involving multiple active edges.
670   const string& verify_active_edge = edge->GetBinding("verify_active_edge");
671   if (!verify_active_edge.empty()) {
672     bool verify_active_edge_found = false;
673     for (vector<Edge*>::iterator i = active_edges_.begin();
674          i != active_edges_.end(); ++i) {
675       if (!(*i)->outputs_.empty() &&
676           (*i)->outputs_[0]->path() == verify_active_edge) {
677         verify_active_edge_found = true;
678       }
679     }
680     EXPECT_TRUE(verify_active_edge_found);
681   }
682
683   active_edges_.erase(edge_iter);
684   return true;
685 }
686
687 vector<Edge*> FakeCommandRunner::GetActiveEdges() {
688   return active_edges_;
689 }
690
691 void FakeCommandRunner::Abort() {
692   active_edges_.clear();
693 }
694
695 void BuildTest::Dirty(const string& path) {
696   Node* node = GetNode(path);
697   node->MarkDirty();
698
699   // If it's an input file, mark that we've already stat()ed it and
700   // it's missing.
701   if (!node->in_edge())
702     node->MarkMissing();
703 }
704
705 TEST_F(BuildTest, NoWork) {
706   string err;
707   EXPECT_TRUE(builder_.AlreadyUpToDate());
708 }
709
710 TEST_F(BuildTest, OneStep) {
711   // Given a dirty target with one ready input,
712   // we should rebuild the target.
713   Dirty("cat1");
714   string err;
715   EXPECT_TRUE(builder_.AddTarget("cat1", &err));
716   ASSERT_EQ("", err);
717   EXPECT_TRUE(builder_.Build(&err));
718   ASSERT_EQ("", err);
719
720   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
721   EXPECT_EQ("cat in1 > cat1", command_runner_.commands_ran_[0]);
722 }
723
724 TEST_F(BuildTest, OneStep2) {
725   // Given a target with one dirty input,
726   // we should rebuild the target.
727   Dirty("cat1");
728   string err;
729   EXPECT_TRUE(builder_.AddTarget("cat1", &err));
730   ASSERT_EQ("", err);
731   EXPECT_TRUE(builder_.Build(&err));
732   EXPECT_EQ("", err);
733
734   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
735   EXPECT_EQ("cat in1 > cat1", command_runner_.commands_ran_[0]);
736 }
737
738 TEST_F(BuildTest, TwoStep) {
739   string err;
740   EXPECT_TRUE(builder_.AddTarget("cat12", &err));
741   ASSERT_EQ("", err);
742   EXPECT_TRUE(builder_.Build(&err));
743   EXPECT_EQ("", err);
744   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
745   // Depending on how the pointers work out, we could've ran
746   // the first two commands in either order.
747   EXPECT_TRUE((command_runner_.commands_ran_[0] == "cat in1 > cat1" &&
748                command_runner_.commands_ran_[1] == "cat in1 in2 > cat2") ||
749               (command_runner_.commands_ran_[1] == "cat in1 > cat1" &&
750                command_runner_.commands_ran_[0] == "cat in1 in2 > cat2"));
751
752   EXPECT_EQ("cat cat1 cat2 > cat12", command_runner_.commands_ran_[2]);
753
754   fs_.Tick();
755
756   // Modifying in2 requires rebuilding one intermediate file
757   // and the final file.
758   fs_.Create("in2", "");
759   state_.Reset();
760   EXPECT_TRUE(builder_.AddTarget("cat12", &err));
761   ASSERT_EQ("", err);
762   EXPECT_TRUE(builder_.Build(&err));
763   ASSERT_EQ("", err);
764   ASSERT_EQ(5u, command_runner_.commands_ran_.size());
765   EXPECT_EQ("cat in1 in2 > cat2", command_runner_.commands_ran_[3]);
766   EXPECT_EQ("cat cat1 cat2 > cat12", command_runner_.commands_ran_[4]);
767 }
768
769 TEST_F(BuildTest, TwoOutputs) {
770   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
771 "rule touch\n"
772 "  command = touch $out\n"
773 "build out1 out2: touch in.txt\n"));
774
775   fs_.Create("in.txt", "");
776
777   string err;
778   EXPECT_TRUE(builder_.AddTarget("out1", &err));
779   ASSERT_EQ("", err);
780   EXPECT_TRUE(builder_.Build(&err));
781   EXPECT_EQ("", err);
782   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
783   EXPECT_EQ("touch out1 out2", command_runner_.commands_ran_[0]);
784 }
785
786 TEST_F(BuildTest, ImplicitOutput) {
787   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
788 "rule touch\n"
789 "  command = touch $out $out.imp\n"
790 "build out | out.imp: touch in.txt\n"));
791   fs_.Create("in.txt", "");
792
793   string err;
794   EXPECT_TRUE(builder_.AddTarget("out.imp", &err));
795   ASSERT_EQ("", err);
796   EXPECT_TRUE(builder_.Build(&err));
797   EXPECT_EQ("", err);
798   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
799   EXPECT_EQ("touch out out.imp", command_runner_.commands_ran_[0]);
800 }
801
802 // Test case from
803 //   https://github.com/ninja-build/ninja/issues/148
804 TEST_F(BuildTest, MultiOutIn) {
805   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
806 "rule touch\n"
807 "  command = touch $out\n"
808 "build in1 otherfile: touch in\n"
809 "build out: touch in | in1\n"));
810
811   fs_.Create("in", "");
812   fs_.Tick();
813   fs_.Create("in1", "");
814
815   string err;
816   EXPECT_TRUE(builder_.AddTarget("out", &err));
817   ASSERT_EQ("", err);
818   EXPECT_TRUE(builder_.Build(&err));
819   EXPECT_EQ("", err);
820 }
821
822 TEST_F(BuildTest, Chain) {
823   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
824 "build c2: cat c1\n"
825 "build c3: cat c2\n"
826 "build c4: cat c3\n"
827 "build c5: cat c4\n"));
828
829   fs_.Create("c1", "");
830
831   string err;
832   EXPECT_TRUE(builder_.AddTarget("c5", &err));
833   ASSERT_EQ("", err);
834   EXPECT_TRUE(builder_.Build(&err));
835   EXPECT_EQ("", err);
836   ASSERT_EQ(4u, command_runner_.commands_ran_.size());
837
838   err.clear();
839   command_runner_.commands_ran_.clear();
840   state_.Reset();
841   EXPECT_TRUE(builder_.AddTarget("c5", &err));
842   ASSERT_EQ("", err);
843   EXPECT_TRUE(builder_.AlreadyUpToDate());
844
845   fs_.Tick();
846
847   fs_.Create("c3", "");
848   err.clear();
849   command_runner_.commands_ran_.clear();
850   state_.Reset();
851   EXPECT_TRUE(builder_.AddTarget("c5", &err));
852   ASSERT_EQ("", err);
853   EXPECT_FALSE(builder_.AlreadyUpToDate());
854   EXPECT_TRUE(builder_.Build(&err));
855   ASSERT_EQ(2u, command_runner_.commands_ran_.size());  // 3->4, 4->5
856 }
857
858 TEST_F(BuildTest, MissingInput) {
859   // Input is referenced by build file, but no rule for it.
860   string err;
861   Dirty("in1");
862   EXPECT_FALSE(builder_.AddTarget("cat1", &err));
863   EXPECT_EQ("'in1', needed by 'cat1', missing and no known rule to make it",
864             err);
865 }
866
867 TEST_F(BuildTest, MissingTarget) {
868   // Target is not referenced by build file.
869   string err;
870   EXPECT_FALSE(builder_.AddTarget("meow", &err));
871   EXPECT_EQ("unknown target: 'meow'", err);
872 }
873
874 TEST_F(BuildTest, MakeDirs) {
875   string err;
876
877 #ifdef _WIN32
878   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
879                                       "build subdir\\dir2\\file: cat in1\n"));
880 #else
881   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
882                                       "build subdir/dir2/file: cat in1\n"));
883 #endif
884   EXPECT_TRUE(builder_.AddTarget("subdir/dir2/file", &err));
885
886   EXPECT_EQ("", err);
887   EXPECT_TRUE(builder_.Build(&err));
888   ASSERT_EQ("", err);
889   ASSERT_EQ(2u, fs_.directories_made_.size());
890   EXPECT_EQ("subdir", fs_.directories_made_[0]);
891   EXPECT_EQ("subdir/dir2", fs_.directories_made_[1]);
892 }
893
894 TEST_F(BuildTest, DepFileMissing) {
895   string err;
896   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
897 "rule cc\n  command = cc $in\n  depfile = $out.d\n"
898 "build fo$ o.o: cc foo.c\n"));
899   fs_.Create("foo.c", "");
900
901   EXPECT_TRUE(builder_.AddTarget("fo o.o", &err));
902   ASSERT_EQ("", err);
903   ASSERT_EQ(1u, fs_.files_read_.size());
904   EXPECT_EQ("fo o.o.d", fs_.files_read_[0]);
905 }
906
907 TEST_F(BuildTest, DepFileOK) {
908   string err;
909   int orig_edges = state_.edges_.size();
910   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
911 "rule cc\n  command = cc $in\n  depfile = $out.d\n"
912 "build foo.o: cc foo.c\n"));
913   Edge* edge = state_.edges_.back();
914
915   fs_.Create("foo.c", "");
916   GetNode("bar.h")->MarkDirty();  // Mark bar.h as missing.
917   fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
918   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
919   ASSERT_EQ("", err);
920   ASSERT_EQ(1u, fs_.files_read_.size());
921   EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
922
923   // Expect three new edges: one generating foo.o, and two more from
924   // loading the depfile.
925   ASSERT_EQ(orig_edges + 3, (int)state_.edges_.size());
926   // Expect our edge to now have three inputs: foo.c and two headers.
927   ASSERT_EQ(3u, edge->inputs_.size());
928
929   // Expect the command line we generate to only use the original input.
930   ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
931 }
932
933 TEST_F(BuildTest, DepFileParseError) {
934   string err;
935   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
936 "rule cc\n  command = cc $in\n  depfile = $out.d\n"
937 "build foo.o: cc foo.c\n"));
938   fs_.Create("foo.c", "");
939   fs_.Create("foo.o.d", "randomtext\n");
940   EXPECT_FALSE(builder_.AddTarget("foo.o", &err));
941   EXPECT_EQ("foo.o.d: expected ':' in depfile", err);
942 }
943
944 TEST_F(BuildTest, EncounterReadyTwice) {
945   string err;
946   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
947 "rule touch\n"
948 "  command = touch $out\n"
949 "build c: touch\n"
950 "build b: touch || c\n"
951 "build a: touch | b || c\n"));
952
953   vector<Edge*> c_out = GetNode("c")->out_edges();
954   ASSERT_EQ(2u, c_out.size());
955   EXPECT_EQ("b", c_out[0]->outputs_[0]->path());
956   EXPECT_EQ("a", c_out[1]->outputs_[0]->path());
957
958   fs_.Create("b", "");
959   EXPECT_TRUE(builder_.AddTarget("a", &err));
960   ASSERT_EQ("", err);
961
962   EXPECT_TRUE(builder_.Build(&err));
963   ASSERT_EQ("", err);
964   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
965 }
966
967 TEST_F(BuildTest, OrderOnlyDeps) {
968   string err;
969   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
970 "rule cc\n  command = cc $in\n  depfile = $out.d\n"
971 "build foo.o: cc foo.c || otherfile\n"));
972   Edge* edge = state_.edges_.back();
973
974   fs_.Create("foo.c", "");
975   fs_.Create("otherfile", "");
976   fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
977   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
978   ASSERT_EQ("", err);
979
980   // One explicit, two implicit, one order only.
981   ASSERT_EQ(4u, edge->inputs_.size());
982   EXPECT_EQ(2, edge->implicit_deps_);
983   EXPECT_EQ(1, edge->order_only_deps_);
984   // Verify the inputs are in the order we expect
985   // (explicit then implicit then orderonly).
986   EXPECT_EQ("foo.c", edge->inputs_[0]->path());
987   EXPECT_EQ("blah.h", edge->inputs_[1]->path());
988   EXPECT_EQ("bar.h", edge->inputs_[2]->path());
989   EXPECT_EQ("otherfile", edge->inputs_[3]->path());
990
991   // Expect the command line we generate to only use the original input.
992   ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
993
994   // explicit dep dirty, expect a rebuild.
995   EXPECT_TRUE(builder_.Build(&err));
996   ASSERT_EQ("", err);
997   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
998
999   fs_.Tick();
1000
1001   // Recreate the depfile, as it should have been deleted by the build.
1002   fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
1003
1004   // implicit dep dirty, expect a rebuild.
1005   fs_.Create("blah.h", "");
1006   fs_.Create("bar.h", "");
1007   command_runner_.commands_ran_.clear();
1008   state_.Reset();
1009   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
1010   EXPECT_TRUE(builder_.Build(&err));
1011   ASSERT_EQ("", err);
1012   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1013
1014   fs_.Tick();
1015
1016   // Recreate the depfile, as it should have been deleted by the build.
1017   fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
1018
1019   // order only dep dirty, no rebuild.
1020   fs_.Create("otherfile", "");
1021   command_runner_.commands_ran_.clear();
1022   state_.Reset();
1023   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
1024   EXPECT_EQ("", err);
1025   EXPECT_TRUE(builder_.AlreadyUpToDate());
1026
1027   // implicit dep missing, expect rebuild.
1028   fs_.RemoveFile("bar.h");
1029   command_runner_.commands_ran_.clear();
1030   state_.Reset();
1031   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
1032   EXPECT_TRUE(builder_.Build(&err));
1033   ASSERT_EQ("", err);
1034   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1035 }
1036
1037 TEST_F(BuildTest, RebuildOrderOnlyDeps) {
1038   string err;
1039   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1040 "rule cc\n  command = cc $in\n"
1041 "rule true\n  command = true\n"
1042 "build oo.h: cc oo.h.in\n"
1043 "build foo.o: cc foo.c || oo.h\n"));
1044
1045   fs_.Create("foo.c", "");
1046   fs_.Create("oo.h.in", "");
1047
1048   // foo.o and order-only dep dirty, build both.
1049   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
1050   EXPECT_TRUE(builder_.Build(&err));
1051   ASSERT_EQ("", err);
1052   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1053
1054   // all clean, no rebuild.
1055   command_runner_.commands_ran_.clear();
1056   state_.Reset();
1057   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
1058   EXPECT_EQ("", err);
1059   EXPECT_TRUE(builder_.AlreadyUpToDate());
1060
1061   // order-only dep missing, build it only.
1062   fs_.RemoveFile("oo.h");
1063   command_runner_.commands_ran_.clear();
1064   state_.Reset();
1065   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
1066   EXPECT_TRUE(builder_.Build(&err));
1067   ASSERT_EQ("", err);
1068   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1069   ASSERT_EQ("cc oo.h.in", command_runner_.commands_ran_[0]);
1070
1071   fs_.Tick();
1072
1073   // order-only dep dirty, build it only.
1074   fs_.Create("oo.h.in", "");
1075   command_runner_.commands_ran_.clear();
1076   state_.Reset();
1077   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
1078   EXPECT_TRUE(builder_.Build(&err));
1079   ASSERT_EQ("", err);
1080   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1081   ASSERT_EQ("cc oo.h.in", command_runner_.commands_ran_[0]);
1082 }
1083
1084 #ifdef _WIN32
1085 TEST_F(BuildTest, DepFileCanonicalize) {
1086   string err;
1087   int orig_edges = state_.edges_.size();
1088   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1089 "rule cc\n  command = cc $in\n  depfile = $out.d\n"
1090 "build gen/stuff\\things/foo.o: cc x\\y/z\\foo.c\n"));
1091   Edge* edge = state_.edges_.back();
1092
1093   fs_.Create("x/y/z/foo.c", "");
1094   GetNode("bar.h")->MarkDirty();  // Mark bar.h as missing.
1095   // Note, different slashes from manifest.
1096   fs_.Create("gen/stuff\\things/foo.o.d",
1097              "gen\\stuff\\things\\foo.o: blah.h bar.h\n");
1098   EXPECT_TRUE(builder_.AddTarget("gen/stuff/things/foo.o", &err));
1099   ASSERT_EQ("", err);
1100   ASSERT_EQ(1u, fs_.files_read_.size());
1101   // The depfile path does not get Canonicalize as it seems unnecessary.
1102   EXPECT_EQ("gen/stuff\\things/foo.o.d", fs_.files_read_[0]);
1103
1104   // Expect three new edges: one generating foo.o, and two more from
1105   // loading the depfile.
1106   ASSERT_EQ(orig_edges + 3, (int)state_.edges_.size());
1107   // Expect our edge to now have three inputs: foo.c and two headers.
1108   ASSERT_EQ(3u, edge->inputs_.size());
1109
1110   // Expect the command line we generate to only use the original input, and
1111   // using the slashes from the manifest.
1112   ASSERT_EQ("cc x\\y/z\\foo.c", edge->EvaluateCommand());
1113 }
1114 #endif
1115
1116 TEST_F(BuildTest, Phony) {
1117   string err;
1118   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1119 "build out: cat bar.cc\n"
1120 "build all: phony out\n"));
1121   fs_.Create("bar.cc", "");
1122
1123   EXPECT_TRUE(builder_.AddTarget("all", &err));
1124   ASSERT_EQ("", err);
1125
1126   // Only one command to run, because phony runs no command.
1127   EXPECT_FALSE(builder_.AlreadyUpToDate());
1128   EXPECT_TRUE(builder_.Build(&err));
1129   ASSERT_EQ("", err);
1130   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1131 }
1132
1133 TEST_F(BuildTest, PhonyNoWork) {
1134   string err;
1135   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1136 "build out: cat bar.cc\n"
1137 "build all: phony out\n"));
1138   fs_.Create("bar.cc", "");
1139   fs_.Create("out", "");
1140
1141   EXPECT_TRUE(builder_.AddTarget("all", &err));
1142   ASSERT_EQ("", err);
1143   EXPECT_TRUE(builder_.AlreadyUpToDate());
1144 }
1145
1146 // Test a self-referencing phony.  Ideally this should not work, but
1147 // ninja 1.7 and below tolerated and CMake 2.8.12.x and 3.0.x both
1148 // incorrectly produce it.  We tolerate it for compatibility.
1149 TEST_F(BuildTest, PhonySelfReference) {
1150   string err;
1151   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1152 "build a: phony a\n"));
1153
1154   EXPECT_TRUE(builder_.AddTarget("a", &err));
1155   ASSERT_EQ("", err);
1156   EXPECT_TRUE(builder_.AlreadyUpToDate());
1157 }
1158
1159 TEST_F(BuildTest, Fail) {
1160   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1161 "rule fail\n"
1162 "  command = fail\n"
1163 "build out1: fail\n"));
1164
1165   string err;
1166   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1167   ASSERT_EQ("", err);
1168
1169   EXPECT_FALSE(builder_.Build(&err));
1170   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1171   ASSERT_EQ("subcommand failed", err);
1172 }
1173
1174 TEST_F(BuildTest, SwallowFailures) {
1175   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1176 "rule fail\n"
1177 "  command = fail\n"
1178 "build out1: fail\n"
1179 "build out2: fail\n"
1180 "build out3: fail\n"
1181 "build all: phony out1 out2 out3\n"));
1182
1183   // Swallow two failures, die on the third.
1184   config_.failures_allowed = 3;
1185
1186   string err;
1187   EXPECT_TRUE(builder_.AddTarget("all", &err));
1188   ASSERT_EQ("", err);
1189
1190   EXPECT_FALSE(builder_.Build(&err));
1191   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1192   ASSERT_EQ("subcommands failed", err);
1193 }
1194
1195 TEST_F(BuildTest, SwallowFailuresLimit) {
1196   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1197 "rule fail\n"
1198 "  command = fail\n"
1199 "build out1: fail\n"
1200 "build out2: fail\n"
1201 "build out3: fail\n"
1202 "build final: cat out1 out2 out3\n"));
1203
1204   // Swallow ten failures; we should stop before building final.
1205   config_.failures_allowed = 11;
1206
1207   string err;
1208   EXPECT_TRUE(builder_.AddTarget("final", &err));
1209   ASSERT_EQ("", err);
1210
1211   EXPECT_FALSE(builder_.Build(&err));
1212   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1213   ASSERT_EQ("cannot make progress due to previous errors", err);
1214 }
1215
1216 TEST_F(BuildTest, SwallowFailuresPool) {
1217   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1218 "pool failpool\n"
1219 "  depth = 1\n"
1220 "rule fail\n"
1221 "  command = fail\n"
1222 "  pool = failpool\n"
1223 "build out1: fail\n"
1224 "build out2: fail\n"
1225 "build out3: fail\n"
1226 "build final: cat out1 out2 out3\n"));
1227
1228   // Swallow ten failures; we should stop before building final.
1229   config_.failures_allowed = 11;
1230
1231   string err;
1232   EXPECT_TRUE(builder_.AddTarget("final", &err));
1233   ASSERT_EQ("", err);
1234
1235   EXPECT_FALSE(builder_.Build(&err));
1236   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1237   ASSERT_EQ("cannot make progress due to previous errors", err);
1238 }
1239
1240 TEST_F(BuildTest, PoolEdgesReadyButNotWanted) {
1241   fs_.Create("x", "");
1242
1243   const char* manifest =
1244     "pool some_pool\n"
1245     "  depth = 4\n"
1246     "rule touch\n"
1247     "  command = touch $out\n"
1248     "  pool = some_pool\n"
1249     "rule cc\n"
1250     "  command = touch grit\n"
1251     "\n"
1252     "build B.d.stamp: cc | x\n"
1253     "build C.stamp: touch B.d.stamp\n"
1254     "build final.stamp: touch || C.stamp\n";
1255
1256   RebuildTarget("final.stamp", manifest);
1257
1258   fs_.RemoveFile("B.d.stamp");
1259
1260   State save_state;
1261   RebuildTarget("final.stamp", manifest, NULL, NULL, &save_state);
1262   EXPECT_GE(save_state.LookupPool("some_pool")->current_use(), 0);
1263 }
1264
1265 struct BuildWithLogTest : public BuildTest {
1266   BuildWithLogTest() {
1267     builder_.SetBuildLog(&build_log_);
1268   }
1269
1270   BuildLog build_log_;
1271 };
1272
1273 TEST_F(BuildWithLogTest, NotInLogButOnDisk) {
1274   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1275 "rule cc\n"
1276 "  command = cc\n"
1277 "build out1: cc in\n"));
1278
1279   // Create input/output that would be considered up to date when
1280   // not considering the command line hash.
1281   fs_.Create("in", "");
1282   fs_.Create("out1", "");
1283   string err;
1284
1285   // Because it's not in the log, it should not be up-to-date until
1286   // we build again.
1287   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1288   EXPECT_FALSE(builder_.AlreadyUpToDate());
1289
1290   command_runner_.commands_ran_.clear();
1291   state_.Reset();
1292
1293   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1294   EXPECT_TRUE(builder_.Build(&err));
1295   EXPECT_TRUE(builder_.AlreadyUpToDate());
1296 }
1297
1298 TEST_F(BuildWithLogTest, RebuildAfterFailure) {
1299   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1300 "rule touch-fail-tick2\n"
1301 "  command = touch-fail-tick2\n"
1302 "build out1: touch-fail-tick2 in\n"));
1303
1304   string err;
1305
1306   fs_.Create("in", "");
1307
1308   // Run once successfully to get out1 in the log
1309   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1310   EXPECT_TRUE(builder_.Build(&err));
1311   EXPECT_EQ("", err);
1312   EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1313
1314   command_runner_.commands_ran_.clear();
1315   state_.Reset();
1316   builder_.Cleanup();
1317   builder_.plan_.Reset();
1318
1319   fs_.Tick();
1320   fs_.Create("in", "");
1321
1322   // Run again with a failure that updates the output file timestamp
1323   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1324   EXPECT_FALSE(builder_.Build(&err));
1325   EXPECT_EQ("subcommand failed", err);
1326   EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1327
1328   command_runner_.commands_ran_.clear();
1329   state_.Reset();
1330   builder_.Cleanup();
1331   builder_.plan_.Reset();
1332
1333   fs_.Tick();
1334
1335   // Run again, should rerun even though the output file is up to date on disk
1336   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1337   EXPECT_FALSE(builder_.AlreadyUpToDate());
1338   EXPECT_TRUE(builder_.Build(&err));
1339   EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1340   EXPECT_EQ("", err);
1341 }
1342
1343 TEST_F(BuildWithLogTest, RebuildWithNoInputs) {
1344   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1345 "rule touch\n"
1346 "  command = touch\n"
1347 "build out1: touch\n"
1348 "build out2: touch in\n"));
1349
1350   string err;
1351
1352   fs_.Create("in", "");
1353
1354   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1355   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1356   EXPECT_TRUE(builder_.Build(&err));
1357   EXPECT_EQ("", err);
1358   EXPECT_EQ(2u, command_runner_.commands_ran_.size());
1359
1360   command_runner_.commands_ran_.clear();
1361   state_.Reset();
1362
1363   fs_.Tick();
1364
1365   fs_.Create("in", "");
1366
1367   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1368   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1369   EXPECT_TRUE(builder_.Build(&err));
1370   EXPECT_EQ("", err);
1371   EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1372 }
1373
1374 TEST_F(BuildWithLogTest, RestatTest) {
1375   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1376 "rule true\n"
1377 "  command = true\n"
1378 "  restat = 1\n"
1379 "rule cc\n"
1380 "  command = cc\n"
1381 "  restat = 1\n"
1382 "build out1: cc in\n"
1383 "build out2: true out1\n"
1384 "build out3: cat out2\n"));
1385
1386   fs_.Create("out1", "");
1387   fs_.Create("out2", "");
1388   fs_.Create("out3", "");
1389
1390   fs_.Tick();
1391
1392   fs_.Create("in", "");
1393
1394   // Do a pre-build so that there's commands in the log for the outputs,
1395   // otherwise, the lack of an entry in the build log will cause out3 to rebuild
1396   // regardless of restat.
1397   string err;
1398   EXPECT_TRUE(builder_.AddTarget("out3", &err));
1399   ASSERT_EQ("", err);
1400   EXPECT_TRUE(builder_.Build(&err));
1401   ASSERT_EQ("", err);
1402   EXPECT_EQ("[3/3]", builder_.status_->FormatProgressStatus("[%s/%t]",
1403       BuildStatus::kEdgeStarted));
1404   command_runner_.commands_ran_.clear();
1405   state_.Reset();
1406
1407   fs_.Tick();
1408
1409   fs_.Create("in", "");
1410   // "cc" touches out1, so we should build out2.  But because "true" does not
1411   // touch out2, we should cancel the build of out3.
1412   EXPECT_TRUE(builder_.AddTarget("out3", &err));
1413   ASSERT_EQ("", err);
1414   EXPECT_TRUE(builder_.Build(&err));
1415   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1416
1417   // If we run again, it should be a no-op, because the build log has recorded
1418   // that we've already built out2 with an input timestamp of 2 (from out1).
1419   command_runner_.commands_ran_.clear();
1420   state_.Reset();
1421   EXPECT_TRUE(builder_.AddTarget("out3", &err));
1422   ASSERT_EQ("", err);
1423   EXPECT_TRUE(builder_.AlreadyUpToDate());
1424
1425   fs_.Tick();
1426
1427   fs_.Create("in", "");
1428
1429   // The build log entry should not, however, prevent us from rebuilding out2
1430   // if out1 changes.
1431   command_runner_.commands_ran_.clear();
1432   state_.Reset();
1433   EXPECT_TRUE(builder_.AddTarget("out3", &err));
1434   ASSERT_EQ("", err);
1435   EXPECT_TRUE(builder_.Build(&err));
1436   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1437 }
1438
1439 TEST_F(BuildWithLogTest, RestatMissingFile) {
1440   // If a restat rule doesn't create its output, and the output didn't
1441   // exist before the rule was run, consider that behavior equivalent
1442   // to a rule that doesn't modify its existent output file.
1443
1444   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1445 "rule true\n"
1446 "  command = true\n"
1447 "  restat = 1\n"
1448 "rule cc\n"
1449 "  command = cc\n"
1450 "build out1: true in\n"
1451 "build out2: cc out1\n"));
1452
1453   fs_.Create("in", "");
1454   fs_.Create("out2", "");
1455
1456   // Do a pre-build so that there's commands in the log for the outputs,
1457   // otherwise, the lack of an entry in the build log will cause out2 to rebuild
1458   // regardless of restat.
1459   string err;
1460   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1461   ASSERT_EQ("", err);
1462   EXPECT_TRUE(builder_.Build(&err));
1463   ASSERT_EQ("", err);
1464   command_runner_.commands_ran_.clear();
1465   state_.Reset();
1466
1467   fs_.Tick();
1468   fs_.Create("in", "");
1469   fs_.Create("out2", "");
1470
1471   // Run a build, expect only the first command to run.
1472   // It doesn't touch its output (due to being the "true" command), so
1473   // we shouldn't run the dependent build.
1474   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1475   ASSERT_EQ("", err);
1476   EXPECT_TRUE(builder_.Build(&err));
1477   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1478 }
1479
1480 TEST_F(BuildWithLogTest, RestatSingleDependentOutputDirty) {
1481   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1482     "rule true\n"
1483     "  command = true\n"
1484     "  restat = 1\n"
1485     "rule touch\n"
1486     "  command = touch\n"
1487     "build out1: true in\n"
1488     "build out2 out3: touch out1\n"
1489     "build out4: touch out2\n"
1490     ));
1491
1492   // Create the necessary files
1493   fs_.Create("in", "");
1494
1495   string err;
1496   EXPECT_TRUE(builder_.AddTarget("out4", &err));
1497   ASSERT_EQ("", err);
1498   EXPECT_TRUE(builder_.Build(&err));
1499   ASSERT_EQ("", err);
1500   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1501
1502   fs_.Tick();
1503   fs_.Create("in", "");
1504   fs_.RemoveFile("out3");
1505
1506   // Since "in" is missing, out1 will be built. Since "out3" is missing,
1507   // out2 and out3 will be built even though "in" is not touched when built.
1508   // Then, since out2 is rebuilt, out4 should be rebuilt -- the restat on the
1509   // "true" rule should not lead to the "touch" edge writing out2 and out3 being
1510   // cleard.
1511   command_runner_.commands_ran_.clear();
1512   state_.Reset();
1513   EXPECT_TRUE(builder_.AddTarget("out4", &err));
1514   ASSERT_EQ("", err);
1515   EXPECT_TRUE(builder_.Build(&err));
1516   ASSERT_EQ("", err);
1517   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1518 }
1519
1520 // Test scenario, in which an input file is removed, but output isn't changed
1521 // https://github.com/ninja-build/ninja/issues/295
1522 TEST_F(BuildWithLogTest, RestatMissingInput) {
1523   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1524     "rule true\n"
1525     "  command = true\n"
1526     "  depfile = $out.d\n"
1527     "  restat = 1\n"
1528     "rule cc\n"
1529     "  command = cc\n"
1530     "build out1: true in\n"
1531     "build out2: cc out1\n"));
1532
1533   // Create all necessary files
1534   fs_.Create("in", "");
1535
1536   // The implicit dependencies and the depfile itself
1537   // are newer than the output
1538   TimeStamp restat_mtime = fs_.Tick();
1539   fs_.Create("out1.d", "out1: will.be.deleted restat.file\n");
1540   fs_.Create("will.be.deleted", "");
1541   fs_.Create("restat.file", "");
1542
1543   // Run the build, out1 and out2 get built
1544   string err;
1545   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1546   ASSERT_EQ("", err);
1547   EXPECT_TRUE(builder_.Build(&err));
1548   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1549
1550   // See that an entry in the logfile is created, capturing
1551   // the right mtime
1552   BuildLog::LogEntry* log_entry = build_log_.LookupByOutput("out1");
1553   ASSERT_TRUE(NULL != log_entry);
1554   ASSERT_EQ(restat_mtime, log_entry->mtime);
1555
1556   // Now remove a file, referenced from depfile, so that target becomes
1557   // dirty, but the output does not change
1558   fs_.RemoveFile("will.be.deleted");
1559
1560   // Trigger the build again - only out1 gets built
1561   command_runner_.commands_ran_.clear();
1562   state_.Reset();
1563   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1564   ASSERT_EQ("", err);
1565   EXPECT_TRUE(builder_.Build(&err));
1566   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1567
1568   // Check that the logfile entry remains correctly set
1569   log_entry = build_log_.LookupByOutput("out1");
1570   ASSERT_TRUE(NULL != log_entry);
1571   ASSERT_EQ(restat_mtime, log_entry->mtime);
1572 }
1573
1574 struct BuildDryRun : public BuildWithLogTest {
1575   BuildDryRun() {
1576     config_.dry_run = true;
1577   }
1578 };
1579
1580 TEST_F(BuildDryRun, AllCommandsShown) {
1581   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1582 "rule true\n"
1583 "  command = true\n"
1584 "  restat = 1\n"
1585 "rule cc\n"
1586 "  command = cc\n"
1587 "  restat = 1\n"
1588 "build out1: cc in\n"
1589 "build out2: true out1\n"
1590 "build out3: cat out2\n"));
1591
1592   fs_.Create("out1", "");
1593   fs_.Create("out2", "");
1594   fs_.Create("out3", "");
1595
1596   fs_.Tick();
1597
1598   fs_.Create("in", "");
1599
1600   // "cc" touches out1, so we should build out2.  But because "true" does not
1601   // touch out2, we should cancel the build of out3.
1602   string err;
1603   EXPECT_TRUE(builder_.AddTarget("out3", &err));
1604   ASSERT_EQ("", err);
1605   EXPECT_TRUE(builder_.Build(&err));
1606   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1607 }
1608
1609 // Test that RSP files are created when & where appropriate and deleted after
1610 // successful execution.
1611 TEST_F(BuildTest, RspFileSuccess)
1612 {
1613   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1614     "rule cat_rsp\n"
1615     "  command = cat $rspfile > $out\n"
1616     "  rspfile = $rspfile\n"
1617     "  rspfile_content = $long_command\n"
1618     "rule cat_rsp_out\n"
1619     "  command = cat $rspfile > $out\n"
1620     "  rspfile = $out.rsp\n"
1621     "  rspfile_content = $long_command\n"
1622     "build out1: cat in\n"
1623     "build out2: cat_rsp in\n"
1624     "  rspfile = out 2.rsp\n"
1625     "  long_command = Some very long command\n"
1626     "build out$ 3: cat_rsp_out in\n"
1627     "  long_command = Some very long command\n"));
1628
1629   fs_.Create("out1", "");
1630   fs_.Create("out2", "");
1631   fs_.Create("out 3", "");
1632
1633   fs_.Tick();
1634
1635   fs_.Create("in", "");
1636
1637   string err;
1638   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1639   ASSERT_EQ("", err);
1640   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1641   ASSERT_EQ("", err);
1642   EXPECT_TRUE(builder_.AddTarget("out 3", &err));
1643   ASSERT_EQ("", err);
1644
1645   size_t files_created = fs_.files_created_.size();
1646   size_t files_removed = fs_.files_removed_.size();
1647
1648   EXPECT_TRUE(builder_.Build(&err));
1649   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1650
1651   // The RSP files were created
1652   ASSERT_EQ(files_created + 2, fs_.files_created_.size());
1653   ASSERT_EQ(1u, fs_.files_created_.count("out 2.rsp"));
1654   ASSERT_EQ(1u, fs_.files_created_.count("out 3.rsp"));
1655
1656   // The RSP files were removed
1657   ASSERT_EQ(files_removed + 2, fs_.files_removed_.size());
1658   ASSERT_EQ(1u, fs_.files_removed_.count("out 2.rsp"));
1659   ASSERT_EQ(1u, fs_.files_removed_.count("out 3.rsp"));
1660 }
1661
1662 // Test that RSP file is created but not removed for commands, which fail
1663 TEST_F(BuildTest, RspFileFailure) {
1664   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1665     "rule fail\n"
1666     "  command = fail\n"
1667     "  rspfile = $rspfile\n"
1668     "  rspfile_content = $long_command\n"
1669     "build out: fail in\n"
1670     "  rspfile = out.rsp\n"
1671     "  long_command = Another very long command\n"));
1672
1673   fs_.Create("out", "");
1674   fs_.Tick();
1675   fs_.Create("in", "");
1676
1677   string err;
1678   EXPECT_TRUE(builder_.AddTarget("out", &err));
1679   ASSERT_EQ("", err);
1680
1681   size_t files_created = fs_.files_created_.size();
1682   size_t files_removed = fs_.files_removed_.size();
1683
1684   EXPECT_FALSE(builder_.Build(&err));
1685   ASSERT_EQ("subcommand failed", err);
1686   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1687
1688   // The RSP file was created
1689   ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1690   ASSERT_EQ(1u, fs_.files_created_.count("out.rsp"));
1691
1692   // The RSP file was NOT removed
1693   ASSERT_EQ(files_removed, fs_.files_removed_.size());
1694   ASSERT_EQ(0u, fs_.files_removed_.count("out.rsp"));
1695
1696   // The RSP file contains what it should
1697   ASSERT_EQ("Another very long command", fs_.files_["out.rsp"].contents);
1698 }
1699
1700 // Test that contents of the RSP file behaves like a regular part of
1701 // command line, i.e. triggers a rebuild if changed
1702 TEST_F(BuildWithLogTest, RspFileCmdLineChange) {
1703   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1704     "rule cat_rsp\n"
1705     "  command = cat $rspfile > $out\n"
1706     "  rspfile = $rspfile\n"
1707     "  rspfile_content = $long_command\n"
1708     "build out: cat_rsp in\n"
1709     "  rspfile = out.rsp\n"
1710     "  long_command = Original very long command\n"));
1711
1712   fs_.Create("out", "");
1713   fs_.Tick();
1714   fs_.Create("in", "");
1715
1716   string err;
1717   EXPECT_TRUE(builder_.AddTarget("out", &err));
1718   ASSERT_EQ("", err);
1719
1720   // 1. Build for the 1st time (-> populate log)
1721   EXPECT_TRUE(builder_.Build(&err));
1722   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1723
1724   // 2. Build again (no change)
1725   command_runner_.commands_ran_.clear();
1726   state_.Reset();
1727   EXPECT_TRUE(builder_.AddTarget("out", &err));
1728   EXPECT_EQ("", err);
1729   ASSERT_TRUE(builder_.AlreadyUpToDate());
1730
1731   // 3. Alter the entry in the logfile
1732   // (to simulate a change in the command line between 2 builds)
1733   BuildLog::LogEntry* log_entry = build_log_.LookupByOutput("out");
1734   ASSERT_TRUE(NULL != log_entry);
1735   ASSERT_NO_FATAL_FAILURE(AssertHash(
1736         "cat out.rsp > out;rspfile=Original very long command",
1737         log_entry->command_hash));
1738   log_entry->command_hash++;  // Change the command hash to something else.
1739   // Now expect the target to be rebuilt
1740   command_runner_.commands_ran_.clear();
1741   state_.Reset();
1742   EXPECT_TRUE(builder_.AddTarget("out", &err));
1743   EXPECT_EQ("", err);
1744   EXPECT_TRUE(builder_.Build(&err));
1745   EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1746 }
1747
1748 TEST_F(BuildTest, InterruptCleanup) {
1749   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1750 "rule interrupt\n"
1751 "  command = interrupt\n"
1752 "rule touch-interrupt\n"
1753 "  command = touch-interrupt\n"
1754 "build out1: interrupt in1\n"
1755 "build out2: touch-interrupt in2\n"));
1756
1757   fs_.Create("out1", "");
1758   fs_.Create("out2", "");
1759   fs_.Tick();
1760   fs_.Create("in1", "");
1761   fs_.Create("in2", "");
1762
1763   // An untouched output of an interrupted command should be retained.
1764   string err;
1765   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1766   EXPECT_EQ("", err);
1767   EXPECT_FALSE(builder_.Build(&err));
1768   EXPECT_EQ("interrupted by user", err);
1769   builder_.Cleanup();
1770   EXPECT_GT(fs_.Stat("out1", &err), 0);
1771   err = "";
1772
1773   // A touched output of an interrupted command should be deleted.
1774   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1775   EXPECT_EQ("", err);
1776   EXPECT_FALSE(builder_.Build(&err));
1777   EXPECT_EQ("interrupted by user", err);
1778   builder_.Cleanup();
1779   EXPECT_EQ(0, fs_.Stat("out2", &err));
1780 }
1781
1782 TEST_F(BuildTest, StatFailureAbortsBuild) {
1783   const string kTooLongToStat(400, 'i');
1784   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1785 ("build " + kTooLongToStat + ": cat in\n").c_str()));
1786   fs_.Create("in", "");
1787
1788   // This simulates a stat failure:
1789   fs_.files_[kTooLongToStat].mtime = -1;
1790   fs_.files_[kTooLongToStat].stat_error = "stat failed";
1791
1792   string err;
1793   EXPECT_FALSE(builder_.AddTarget(kTooLongToStat, &err));
1794   EXPECT_EQ("stat failed", err);
1795 }
1796
1797 TEST_F(BuildTest, PhonyWithNoInputs) {
1798   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1799 "build nonexistent: phony\n"
1800 "build out1: cat || nonexistent\n"
1801 "build out2: cat nonexistent\n"));
1802   fs_.Create("out1", "");
1803   fs_.Create("out2", "");
1804
1805   // out1 should be up to date even though its input is dirty, because its
1806   // order-only dependency has nothing to do.
1807   string err;
1808   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1809   ASSERT_EQ("", err);
1810   EXPECT_TRUE(builder_.AlreadyUpToDate());
1811
1812   // out2 should still be out of date though, because its input is dirty.
1813   err.clear();
1814   command_runner_.commands_ran_.clear();
1815   state_.Reset();
1816   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1817   ASSERT_EQ("", err);
1818   EXPECT_TRUE(builder_.Build(&err));
1819   EXPECT_EQ("", err);
1820   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1821 }
1822
1823 TEST_F(BuildTest, DepsGccWithEmptyDepfileErrorsOut) {
1824   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1825 "rule cc\n"
1826 "  command = cc\n"
1827 "  deps = gcc\n"
1828 "build out: cc\n"));
1829   Dirty("out");
1830
1831   string err;
1832   EXPECT_TRUE(builder_.AddTarget("out", &err));
1833   ASSERT_EQ("", err);
1834   EXPECT_FALSE(builder_.AlreadyUpToDate());
1835
1836   EXPECT_FALSE(builder_.Build(&err));
1837   ASSERT_EQ("subcommand failed", err);
1838   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1839 }
1840
1841 TEST_F(BuildTest, StatusFormatElapsed) {
1842   status_.BuildStarted();
1843   // Before any task is done, the elapsed time must be zero.
1844   EXPECT_EQ("[%/e0.000]",
1845             status_.FormatProgressStatus("[%%/e%e]",
1846                 BuildStatus::kEdgeStarted));
1847 }
1848
1849 TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
1850   EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
1851             status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]",
1852                 BuildStatus::kEdgeStarted));
1853 }
1854
1855 TEST_F(BuildTest, FailedDepsParse) {
1856   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1857 "build bad_deps.o: cat in1\n"
1858 "  deps = gcc\n"
1859 "  depfile = in1.d\n"));
1860
1861   string err;
1862   EXPECT_TRUE(builder_.AddTarget("bad_deps.o", &err));
1863   ASSERT_EQ("", err);
1864
1865   // These deps will fail to parse, as they should only have one
1866   // path to the left of the colon.
1867   fs_.Create("in1.d", "AAA BBB");
1868
1869   EXPECT_FALSE(builder_.Build(&err));
1870   EXPECT_EQ("subcommand failed", err);
1871 }
1872
1873 struct BuildWithQueryDepsLogTest : public BuildTest {
1874   BuildWithQueryDepsLogTest() : BuildTest(&log_) {
1875   }
1876
1877   ~BuildWithQueryDepsLogTest() {
1878     log_.Close();
1879   }
1880
1881   virtual void SetUp() {
1882     BuildTest::SetUp();
1883
1884     temp_dir_.CreateAndEnter("BuildWithQueryDepsLogTest");
1885
1886     std::string err;
1887     ASSERT_TRUE(log_.OpenForWrite("ninja_deps", &err));
1888     ASSERT_EQ("", err);
1889   }
1890
1891   ScopedTempDir temp_dir_;
1892
1893   DepsLog log_;
1894 };
1895
1896 /// Test a MSVC-style deps log with multiple outputs.
1897 TEST_F(BuildWithQueryDepsLogTest, TwoOutputsDepFileMSVC) {
1898   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1899 "rule cp_multi_msvc\n"
1900 "    command = echo 'using $in' && for file in $out; do cp $in $$file; done\n"
1901 "    deps = msvc\n"
1902 "    msvc_deps_prefix = using \n"
1903 "build out1 out2: cp_multi_msvc in1\n"));
1904
1905   std::string err;
1906   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1907   ASSERT_EQ("", err);
1908   EXPECT_TRUE(builder_.Build(&err));
1909   EXPECT_EQ("", err);
1910   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1911   EXPECT_EQ("echo 'using in1' && for file in out1 out2; do cp in1 $file; done", command_runner_.commands_ran_[0]);
1912
1913   Node* out1_node = state_.LookupNode("out1");
1914   DepsLog::Deps* out1_deps = log_.GetDeps(out1_node);
1915   EXPECT_EQ(1, out1_deps->node_count);
1916   EXPECT_EQ("in1", out1_deps->nodes[0]->path());
1917
1918   Node* out2_node = state_.LookupNode("out2");
1919   DepsLog::Deps* out2_deps = log_.GetDeps(out2_node);
1920   EXPECT_EQ(1, out2_deps->node_count);
1921   EXPECT_EQ("in1", out2_deps->nodes[0]->path());
1922 }
1923
1924 /// Test a GCC-style deps log with multiple outputs.
1925 TEST_F(BuildWithQueryDepsLogTest, TwoOutputsDepFileGCCOneLine) {
1926   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1927 "rule cp_multi_gcc\n"
1928 "    command = echo '$out: $in' > in.d && for file in $out; do cp in1 $$file; done\n"
1929 "    deps = gcc\n"
1930 "    depfile = in.d\n"
1931 "build out1 out2: cp_multi_gcc in1 in2\n"));
1932
1933   std::string err;
1934   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1935   ASSERT_EQ("", err);
1936   fs_.Create("in.d", "out1 out2: in1 in2");
1937   EXPECT_TRUE(builder_.Build(&err));
1938   EXPECT_EQ("", err);
1939   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1940   EXPECT_EQ("echo 'out1 out2: in1 in2' > in.d && for file in out1 out2; do cp in1 $file; done", command_runner_.commands_ran_[0]);
1941
1942   Node* out1_node = state_.LookupNode("out1");
1943   DepsLog::Deps* out1_deps = log_.GetDeps(out1_node);
1944   EXPECT_EQ(2, out1_deps->node_count);
1945   EXPECT_EQ("in1", out1_deps->nodes[0]->path());
1946   EXPECT_EQ("in2", out1_deps->nodes[1]->path());
1947
1948   Node* out2_node = state_.LookupNode("out2");
1949   DepsLog::Deps* out2_deps = log_.GetDeps(out2_node);
1950   EXPECT_EQ(2, out2_deps->node_count);
1951   EXPECT_EQ("in1", out2_deps->nodes[0]->path());
1952   EXPECT_EQ("in2", out2_deps->nodes[1]->path());
1953 }
1954
1955 /// Test a GCC-style deps log with multiple outputs using a line per input.
1956 TEST_F(BuildWithQueryDepsLogTest, TwoOutputsDepFileGCCMultiLineInput) {
1957   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1958 "rule cp_multi_gcc\n"
1959 "    command = echo '$out: in1\\n$out: in2' > in.d && for file in $out; do cp in1 $$file; done\n"
1960 "    deps = gcc\n"
1961 "    depfile = in.d\n"
1962 "build out1 out2: cp_multi_gcc in1 in2\n"));
1963
1964   std::string err;
1965   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1966   ASSERT_EQ("", err);
1967   fs_.Create("in.d", "out1 out2: in1\nout1 out2: in2");
1968   EXPECT_TRUE(builder_.Build(&err));
1969   EXPECT_EQ("", err);
1970   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1971   EXPECT_EQ("echo 'out1 out2: in1\\nout1 out2: in2' > in.d && for file in out1 out2; do cp in1 $file; done", command_runner_.commands_ran_[0]);
1972
1973   Node* out1_node = state_.LookupNode("out1");
1974   DepsLog::Deps* out1_deps = log_.GetDeps(out1_node);
1975   EXPECT_EQ(2, out1_deps->node_count);
1976   EXPECT_EQ("in1", out1_deps->nodes[0]->path());
1977   EXPECT_EQ("in2", out1_deps->nodes[1]->path());
1978
1979   Node* out2_node = state_.LookupNode("out2");
1980   DepsLog::Deps* out2_deps = log_.GetDeps(out2_node);
1981   EXPECT_EQ(2, out2_deps->node_count);
1982   EXPECT_EQ("in1", out2_deps->nodes[0]->path());
1983   EXPECT_EQ("in2", out2_deps->nodes[1]->path());
1984 }
1985
1986 /// Test a GCC-style deps log with multiple outputs using a line per output.
1987 TEST_F(BuildWithQueryDepsLogTest, TwoOutputsDepFileGCCMultiLineOutput) {
1988   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1989 "rule cp_multi_gcc\n"
1990 "    command = echo 'out1: $in\\nout2: $in' > in.d && for file in $out; do cp in1 $$file; done\n"
1991 "    deps = gcc\n"
1992 "    depfile = in.d\n"
1993 "build out1 out2: cp_multi_gcc in1 in2\n"));
1994
1995   std::string err;
1996   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1997   ASSERT_EQ("", err);
1998   fs_.Create("in.d", "out1: in1 in2\nout2: in1 in2");
1999   EXPECT_TRUE(builder_.Build(&err));
2000   EXPECT_EQ("", err);
2001   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
2002   EXPECT_EQ("echo 'out1: in1 in2\\nout2: in1 in2' > in.d && for file in out1 out2; do cp in1 $file; done", command_runner_.commands_ran_[0]);
2003
2004   Node* out1_node = state_.LookupNode("out1");
2005   DepsLog::Deps* out1_deps = log_.GetDeps(out1_node);
2006   EXPECT_EQ(2, out1_deps->node_count);
2007   EXPECT_EQ("in1", out1_deps->nodes[0]->path());
2008   EXPECT_EQ("in2", out1_deps->nodes[1]->path());
2009
2010   Node* out2_node = state_.LookupNode("out2");
2011   DepsLog::Deps* out2_deps = log_.GetDeps(out2_node);
2012   EXPECT_EQ(2, out2_deps->node_count);
2013   EXPECT_EQ("in1", out2_deps->nodes[0]->path());
2014   EXPECT_EQ("in2", out2_deps->nodes[1]->path());
2015 }
2016
2017 /// Test a GCC-style deps log with multiple outputs mentioning only the main output.
2018 TEST_F(BuildWithQueryDepsLogTest, TwoOutputsDepFileGCCOnlyMainOutput) {
2019   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2020 "rule cp_multi_gcc\n"
2021 "    command = echo 'out1: $in' > in.d && for file in $out; do cp in1 $$file; done\n"
2022 "    deps = gcc\n"
2023 "    depfile = in.d\n"
2024 "build out1 out2: cp_multi_gcc in1 in2\n"));
2025
2026   std::string err;
2027   EXPECT_TRUE(builder_.AddTarget("out1", &err));
2028   ASSERT_EQ("", err);
2029   fs_.Create("in.d", "out1: in1 in2");
2030   EXPECT_TRUE(builder_.Build(&err));
2031   EXPECT_EQ("", err);
2032   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
2033   EXPECT_EQ("echo 'out1: in1 in2' > in.d && for file in out1 out2; do cp in1 $file; done", command_runner_.commands_ran_[0]);
2034
2035   Node* out1_node = state_.LookupNode("out1");
2036   DepsLog::Deps* out1_deps = log_.GetDeps(out1_node);
2037   EXPECT_EQ(2, out1_deps->node_count);
2038   EXPECT_EQ("in1", out1_deps->nodes[0]->path());
2039   EXPECT_EQ("in2", out1_deps->nodes[1]->path());
2040
2041   Node* out2_node = state_.LookupNode("out2");
2042   DepsLog::Deps* out2_deps = log_.GetDeps(out2_node);
2043   EXPECT_EQ(2, out2_deps->node_count);
2044   EXPECT_EQ("in1", out2_deps->nodes[0]->path());
2045   EXPECT_EQ("in2", out2_deps->nodes[1]->path());
2046 }
2047
2048 /// Test a GCC-style deps log with multiple outputs mentioning only the secondary output.
2049 TEST_F(BuildWithQueryDepsLogTest, TwoOutputsDepFileGCCOnlySecondaryOutput) {
2050   // Note: This ends up short-circuiting the node creation due to the primary
2051   // output not being present, but it should still work.
2052   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2053 "rule cp_multi_gcc\n"
2054 "    command = echo 'out2: $in' > in.d && for file in $out; do cp in1 $$file; done\n"
2055 "    deps = gcc\n"
2056 "    depfile = in.d\n"
2057 "build out1 out2: cp_multi_gcc in1 in2\n"));
2058
2059   std::string err;
2060   EXPECT_TRUE(builder_.AddTarget("out1", &err));
2061   ASSERT_EQ("", err);
2062   fs_.Create("in.d", "out2: in1 in2");
2063   EXPECT_TRUE(builder_.Build(&err));
2064   EXPECT_EQ("", err);
2065   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
2066   EXPECT_EQ("echo 'out2: in1 in2' > in.d && for file in out1 out2; do cp in1 $file; done", command_runner_.commands_ran_[0]);
2067
2068   Node* out1_node = state_.LookupNode("out1");
2069   DepsLog::Deps* out1_deps = log_.GetDeps(out1_node);
2070   EXPECT_EQ(2, out1_deps->node_count);
2071   EXPECT_EQ("in1", out1_deps->nodes[0]->path());
2072   EXPECT_EQ("in2", out1_deps->nodes[1]->path());
2073
2074   Node* out2_node = state_.LookupNode("out2");
2075   DepsLog::Deps* out2_deps = log_.GetDeps(out2_node);
2076   EXPECT_EQ(2, out2_deps->node_count);
2077   EXPECT_EQ("in1", out2_deps->nodes[0]->path());
2078   EXPECT_EQ("in2", out2_deps->nodes[1]->path());
2079 }
2080
2081 /// Tests of builds involving deps logs necessarily must span
2082 /// multiple builds.  We reuse methods on BuildTest but not the
2083 /// builder_ it sets up, because we want pristine objects for
2084 /// each build.
2085 struct BuildWithDepsLogTest : public BuildTest {
2086   BuildWithDepsLogTest() {}
2087
2088   virtual void SetUp() {
2089     BuildTest::SetUp();
2090
2091     temp_dir_.CreateAndEnter("BuildWithDepsLogTest");
2092   }
2093
2094   virtual void TearDown() {
2095     temp_dir_.Cleanup();
2096   }
2097
2098   ScopedTempDir temp_dir_;
2099
2100   /// Shadow parent class builder_ so we don't accidentally use it.
2101   void* builder_;
2102 };
2103
2104 /// Run a straightforwad build where the deps log is used.
2105 TEST_F(BuildWithDepsLogTest, Straightforward) {
2106   string err;
2107   // Note: in1 was created by the superclass SetUp().
2108   const char* manifest =
2109       "build out: cat in1\n"
2110       "  deps = gcc\n"
2111       "  depfile = in1.d\n";
2112   {
2113     State state;
2114     ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
2115     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2116
2117     // Run the build once, everything should be ok.
2118     DepsLog deps_log;
2119     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2120     ASSERT_EQ("", err);
2121
2122     Builder builder(&state, config_, NULL, &deps_log, &fs_);
2123     builder.command_runner_.reset(&command_runner_);
2124     EXPECT_TRUE(builder.AddTarget("out", &err));
2125     ASSERT_EQ("", err);
2126     fs_.Create("in1.d", "out: in2");
2127     EXPECT_TRUE(builder.Build(&err));
2128     EXPECT_EQ("", err);
2129
2130     // The deps file should have been removed.
2131     EXPECT_EQ(0, fs_.Stat("in1.d", &err));
2132     // Recreate it for the next step.
2133     fs_.Create("in1.d", "out: in2");
2134     deps_log.Close();
2135     builder.command_runner_.release();
2136   }
2137
2138   {
2139     State state;
2140     ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
2141     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2142
2143     // Touch the file only mentioned in the deps.
2144     fs_.Tick();
2145     fs_.Create("in2", "");
2146
2147     // Run the build again.
2148     DepsLog deps_log;
2149     ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
2150     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2151
2152     Builder builder(&state, config_, NULL, &deps_log, &fs_);
2153     builder.command_runner_.reset(&command_runner_);
2154     command_runner_.commands_ran_.clear();
2155     EXPECT_TRUE(builder.AddTarget("out", &err));
2156     ASSERT_EQ("", err);
2157     EXPECT_TRUE(builder.Build(&err));
2158     EXPECT_EQ("", err);
2159
2160     // We should have rebuilt the output due to in2 being
2161     // out of date.
2162     EXPECT_EQ(1u, command_runner_.commands_ran_.size());
2163
2164     builder.command_runner_.release();
2165   }
2166 }
2167
2168 /// Verify that obsolete dependency info causes a rebuild.
2169 /// 1) Run a successful build where everything has time t, record deps.
2170 /// 2) Move input/output to time t+1 -- despite files in alignment,
2171 ///    should still need to rebuild due to deps at older time.
2172 TEST_F(BuildWithDepsLogTest, ObsoleteDeps) {
2173   string err;
2174   // Note: in1 was created by the superclass SetUp().
2175   const char* manifest =
2176       "build out: cat in1\n"
2177       "  deps = gcc\n"
2178       "  depfile = in1.d\n";
2179   {
2180     // Run an ordinary build that gathers dependencies.
2181     fs_.Create("in1", "");
2182     fs_.Create("in1.d", "out: ");
2183
2184     State state;
2185     ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
2186     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2187
2188     // Run the build once, everything should be ok.
2189     DepsLog deps_log;
2190     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2191     ASSERT_EQ("", err);
2192
2193     Builder builder(&state, config_, NULL, &deps_log, &fs_);
2194     builder.command_runner_.reset(&command_runner_);
2195     EXPECT_TRUE(builder.AddTarget("out", &err));
2196     ASSERT_EQ("", err);
2197     EXPECT_TRUE(builder.Build(&err));
2198     EXPECT_EQ("", err);
2199
2200     deps_log.Close();
2201     builder.command_runner_.release();
2202   }
2203
2204   // Push all files one tick forward so that only the deps are out
2205   // of date.
2206   fs_.Tick();
2207   fs_.Create("in1", "");
2208   fs_.Create("out", "");
2209
2210   // The deps file should have been removed, so no need to timestamp it.
2211   EXPECT_EQ(0, fs_.Stat("in1.d", &err));
2212
2213   {
2214     State state;
2215     ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
2216     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2217
2218     DepsLog deps_log;
2219     ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
2220     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2221
2222     Builder builder(&state, config_, NULL, &deps_log, &fs_);
2223     builder.command_runner_.reset(&command_runner_);
2224     command_runner_.commands_ran_.clear();
2225     EXPECT_TRUE(builder.AddTarget("out", &err));
2226     ASSERT_EQ("", err);
2227
2228     // Recreate the deps file here because the build expects them to exist.
2229     fs_.Create("in1.d", "out: ");
2230
2231     EXPECT_TRUE(builder.Build(&err));
2232     EXPECT_EQ("", err);
2233
2234     // We should have rebuilt the output due to the deps being
2235     // out of date.
2236     EXPECT_EQ(1u, command_runner_.commands_ran_.size());
2237
2238     builder.command_runner_.release();
2239   }
2240 }
2241
2242 TEST_F(BuildWithDepsLogTest, DepsIgnoredInDryRun) {
2243   const char* manifest =
2244       "build out: cat in1\n"
2245       "  deps = gcc\n"
2246       "  depfile = in1.d\n";
2247
2248   fs_.Create("out", "");
2249   fs_.Tick();
2250   fs_.Create("in1", "");
2251
2252   State state;
2253   ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
2254   ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2255
2256   // The deps log is NULL in dry runs.
2257   config_.dry_run = true;
2258   Builder builder(&state, config_, NULL, NULL, &fs_);
2259   builder.command_runner_.reset(&command_runner_);
2260   command_runner_.commands_ran_.clear();
2261
2262   string err;
2263   EXPECT_TRUE(builder.AddTarget("out", &err));
2264   ASSERT_EQ("", err);
2265   EXPECT_TRUE(builder.Build(&err));
2266   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
2267
2268   builder.command_runner_.release();
2269 }
2270
2271 /// Check that a restat rule generating a header cancels compilations correctly.
2272 TEST_F(BuildTest, RestatDepfileDependency) {
2273   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2274 "rule true\n"
2275 "  command = true\n"  // Would be "write if out-of-date" in reality.
2276 "  restat = 1\n"
2277 "build header.h: true header.in\n"
2278 "build out: cat in1\n"
2279 "  depfile = in1.d\n"));
2280
2281   fs_.Create("header.h", "");
2282   fs_.Create("in1.d", "out: header.h");
2283   fs_.Tick();
2284   fs_.Create("header.in", "");
2285
2286   string err;
2287   EXPECT_TRUE(builder_.AddTarget("out", &err));
2288   ASSERT_EQ("", err);
2289   EXPECT_TRUE(builder_.Build(&err));
2290   EXPECT_EQ("", err);
2291 }
2292
2293 /// Check that a restat rule generating a header cancels compilations correctly,
2294 /// depslog case.
2295 TEST_F(BuildWithDepsLogTest, RestatDepfileDependencyDepsLog) {
2296   string err;
2297   // Note: in1 was created by the superclass SetUp().
2298   const char* manifest =
2299       "rule true\n"
2300       "  command = true\n"  // Would be "write if out-of-date" in reality.
2301       "  restat = 1\n"
2302       "build header.h: true header.in\n"
2303       "build out: cat in1\n"
2304       "  deps = gcc\n"
2305       "  depfile = in1.d\n";
2306   {
2307     State state;
2308     ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
2309     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2310
2311     // Run the build once, everything should be ok.
2312     DepsLog deps_log;
2313     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2314     ASSERT_EQ("", err);
2315
2316     Builder builder(&state, config_, NULL, &deps_log, &fs_);
2317     builder.command_runner_.reset(&command_runner_);
2318     EXPECT_TRUE(builder.AddTarget("out", &err));
2319     ASSERT_EQ("", err);
2320     fs_.Create("in1.d", "out: header.h");
2321     EXPECT_TRUE(builder.Build(&err));
2322     EXPECT_EQ("", err);
2323
2324     deps_log.Close();
2325     builder.command_runner_.release();
2326   }
2327
2328   {
2329     State state;
2330     ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
2331     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2332
2333     // Touch the input of the restat rule.
2334     fs_.Tick();
2335     fs_.Create("header.in", "");
2336
2337     // Run the build again.
2338     DepsLog deps_log;
2339     ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
2340     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2341
2342     Builder builder(&state, config_, NULL, &deps_log, &fs_);
2343     builder.command_runner_.reset(&command_runner_);
2344     command_runner_.commands_ran_.clear();
2345     EXPECT_TRUE(builder.AddTarget("out", &err));
2346     ASSERT_EQ("", err);
2347     EXPECT_TRUE(builder.Build(&err));
2348     EXPECT_EQ("", err);
2349
2350     // Rule "true" should have run again, but the build of "out" should have
2351     // been cancelled due to restat propagating through the depfile header.
2352     EXPECT_EQ(1u, command_runner_.commands_ran_.size());
2353
2354     builder.command_runner_.release();
2355   }
2356 }
2357
2358 TEST_F(BuildWithDepsLogTest, DepFileOKDepsLog) {
2359   string err;
2360   const char* manifest =
2361       "rule cc\n  command = cc $in\n  depfile = $out.d\n  deps = gcc\n"
2362       "build fo$ o.o: cc foo.c\n";
2363
2364   fs_.Create("foo.c", "");
2365
2366   {
2367     State state;
2368     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2369
2370     // Run the build once, everything should be ok.
2371     DepsLog deps_log;
2372     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2373     ASSERT_EQ("", err);
2374
2375     Builder builder(&state, config_, NULL, &deps_log, &fs_);
2376     builder.command_runner_.reset(&command_runner_);
2377     EXPECT_TRUE(builder.AddTarget("fo o.o", &err));
2378     ASSERT_EQ("", err);
2379     fs_.Create("fo o.o.d", "fo\\ o.o: blah.h bar.h\n");
2380     EXPECT_TRUE(builder.Build(&err));
2381     EXPECT_EQ("", err);
2382
2383     deps_log.Close();
2384     builder.command_runner_.release();
2385   }
2386
2387   {
2388     State state;
2389     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2390
2391     DepsLog deps_log;
2392     ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
2393     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2394     ASSERT_EQ("", err);
2395
2396     Builder builder(&state, config_, NULL, &deps_log, &fs_);
2397     builder.command_runner_.reset(&command_runner_);
2398
2399     Edge* edge = state.edges_.back();
2400
2401     state.GetNode("bar.h", 0)->MarkDirty();  // Mark bar.h as missing.
2402     EXPECT_TRUE(builder.AddTarget("fo o.o", &err));
2403     ASSERT_EQ("", err);
2404
2405     // Expect three new edges: one generating fo o.o, and two more from
2406     // loading the depfile.
2407     ASSERT_EQ(3u, state.edges_.size());
2408     // Expect our edge to now have three inputs: foo.c and two headers.
2409     ASSERT_EQ(3u, edge->inputs_.size());
2410
2411     // Expect the command line we generate to only use the original input.
2412     ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
2413
2414     deps_log.Close();
2415     builder.command_runner_.release();
2416   }
2417 }
2418
2419 #ifdef _WIN32
2420 TEST_F(BuildWithDepsLogTest, DepFileDepsLogCanonicalize) {
2421   string err;
2422   const char* manifest =
2423       "rule cc\n  command = cc $in\n  depfile = $out.d\n  deps = gcc\n"
2424       "build a/b\\c\\d/e/fo$ o.o: cc x\\y/z\\foo.c\n";
2425
2426   fs_.Create("x/y/z/foo.c", "");
2427
2428   {
2429     State state;
2430     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2431
2432     // Run the build once, everything should be ok.
2433     DepsLog deps_log;
2434     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2435     ASSERT_EQ("", err);
2436
2437     Builder builder(&state, config_, NULL, &deps_log, &fs_);
2438     builder.command_runner_.reset(&command_runner_);
2439     EXPECT_TRUE(builder.AddTarget("a/b/c/d/e/fo o.o", &err));
2440     ASSERT_EQ("", err);
2441     // Note, different slashes from manifest.
2442     fs_.Create("a/b\\c\\d/e/fo o.o.d",
2443                "a\\b\\c\\d\\e\\fo\\ o.o: blah.h bar.h\n");
2444     EXPECT_TRUE(builder.Build(&err));
2445     EXPECT_EQ("", err);
2446
2447     deps_log.Close();
2448     builder.command_runner_.release();
2449   }
2450
2451   {
2452     State state;
2453     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2454
2455     DepsLog deps_log;
2456     ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
2457     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2458     ASSERT_EQ("", err);
2459
2460     Builder builder(&state, config_, NULL, &deps_log, &fs_);
2461     builder.command_runner_.reset(&command_runner_);
2462
2463     Edge* edge = state.edges_.back();
2464
2465     state.GetNode("bar.h", 0)->MarkDirty();  // Mark bar.h as missing.
2466     EXPECT_TRUE(builder.AddTarget("a/b/c/d/e/fo o.o", &err));
2467     ASSERT_EQ("", err);
2468
2469     // Expect three new edges: one generating fo o.o, and two more from
2470     // loading the depfile.
2471     ASSERT_EQ(3u, state.edges_.size());
2472     // Expect our edge to now have three inputs: foo.c and two headers.
2473     ASSERT_EQ(3u, edge->inputs_.size());
2474
2475     // Expect the command line we generate to only use the original input.
2476     // Note, slashes from manifest, not .d.
2477     ASSERT_EQ("cc x\\y/z\\foo.c", edge->EvaluateCommand());
2478
2479     deps_log.Close();
2480     builder.command_runner_.release();
2481   }
2482 }
2483 #endif
2484
2485 /// Check that a restat rule doesn't clear an edge if the depfile is missing.
2486 /// Follows from: https://github.com/ninja-build/ninja/issues/603
2487 TEST_F(BuildTest, RestatMissingDepfile) {
2488 const char* manifest =
2489 "rule true\n"
2490 "  command = true\n"  // Would be "write if out-of-date" in reality.
2491 "  restat = 1\n"
2492 "build header.h: true header.in\n"
2493 "build out: cat header.h\n"
2494 "  depfile = out.d\n";
2495
2496   fs_.Create("header.h", "");
2497   fs_.Tick();
2498   fs_.Create("out", "");
2499   fs_.Create("header.in", "");
2500
2501   // Normally, only 'header.h' would be rebuilt, as
2502   // its rule doesn't touch the output and has 'restat=1' set.
2503   // But we are also missing the depfile for 'out',
2504   // which should force its command to run anyway!
2505   RebuildTarget("out", manifest);
2506   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
2507 }
2508
2509 /// Check that a restat rule doesn't clear an edge if the deps are missing.
2510 /// https://github.com/ninja-build/ninja/issues/603
2511 TEST_F(BuildWithDepsLogTest, RestatMissingDepfileDepslog) {
2512   string err;
2513   const char* manifest =
2514 "rule true\n"
2515 "  command = true\n"  // Would be "write if out-of-date" in reality.
2516 "  restat = 1\n"
2517 "build header.h: true header.in\n"
2518 "build out: cat header.h\n"
2519 "  deps = gcc\n"
2520 "  depfile = out.d\n";
2521
2522   // Build once to populate ninja deps logs from out.d
2523   fs_.Create("header.in", "");
2524   fs_.Create("out.d", "out: header.h");
2525   fs_.Create("header.h", "");
2526
2527   RebuildTarget("out", manifest, "build_log", "ninja_deps");
2528   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
2529
2530   // Sanity: this rebuild should be NOOP
2531   RebuildTarget("out", manifest, "build_log", "ninja_deps");
2532   ASSERT_EQ(0u, command_runner_.commands_ran_.size());
2533
2534   // Touch 'header.in', blank dependencies log (create a different one).
2535   // Building header.h triggers 'restat' outputs cleanup.
2536   // Validate that out is rebuilt netherless, as deps are missing.
2537   fs_.Tick();
2538   fs_.Create("header.in", "");
2539
2540   // (switch to a new blank deps_log "ninja_deps2")
2541   RebuildTarget("out", manifest, "build_log", "ninja_deps2");
2542   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
2543
2544   // Sanity: this build should be NOOP
2545   RebuildTarget("out", manifest, "build_log", "ninja_deps2");
2546   ASSERT_EQ(0u, command_runner_.commands_ran_.size());
2547
2548   // Check that invalidating deps by target timestamp also works here
2549   // Repeat the test but touch target instead of blanking the log.
2550   fs_.Tick();
2551   fs_.Create("header.in", "");
2552   fs_.Create("out", "");
2553   RebuildTarget("out", manifest, "build_log", "ninja_deps2");
2554   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
2555
2556   // And this build should be NOOP again
2557   RebuildTarget("out", manifest, "build_log", "ninja_deps2");
2558   ASSERT_EQ(0u, command_runner_.commands_ran_.size());
2559 }
2560
2561 TEST_F(BuildTest, WrongOutputInDepfileCausesRebuild) {
2562   string err;
2563   const char* manifest =
2564 "rule cc\n"
2565 "  command = cc $in\n"
2566 "  depfile = $out.d\n"
2567 "build foo.o: cc foo.c\n";
2568
2569   fs_.Create("foo.c", "");
2570   fs_.Create("foo.o", "");
2571   fs_.Create("header.h", "");
2572   fs_.Create("foo.o.d", "bar.o.d: header.h\n");
2573
2574   RebuildTarget("foo.o", manifest, "build_log", "ninja_deps");
2575   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
2576 }
2577
2578 TEST_F(BuildTest, Console) {
2579   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2580 "rule console\n"
2581 "  command = console\n"
2582 "  pool = console\n"
2583 "build cons: console in.txt\n"));
2584
2585   fs_.Create("in.txt", "");
2586
2587   string err;
2588   EXPECT_TRUE(builder_.AddTarget("cons", &err));
2589   ASSERT_EQ("", err);
2590   EXPECT_TRUE(builder_.Build(&err));
2591   EXPECT_EQ("", err);
2592   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
2593 }
2594
2595 TEST_F(BuildTest, DyndepMissingAndNoRule) {
2596   // Verify that we can diagnose when a dyndep file is missing and
2597   // has no rule to build it.
2598   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2599 "rule touch\n"
2600 "  command = touch $out\n"
2601 "build out: touch || dd\n"
2602 "  dyndep = dd\n"
2603 ));
2604
2605   string err;
2606   EXPECT_FALSE(builder_.AddTarget("out", &err));
2607   EXPECT_EQ("loading 'dd': No such file or directory", err);
2608 }
2609
2610 TEST_F(BuildTest, DyndepReadyImplicitConnection) {
2611   // Verify that a dyndep file can be loaded immediately to discover
2612   // that one edge has an implicit output that is also an implicit
2613   // input of another edge.
2614   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2615 "rule touch\n"
2616 "  command = touch $out $out.imp\n"
2617 "build tmp: touch || dd\n"
2618 "  dyndep = dd\n"
2619 "build out: touch || dd\n"
2620 "  dyndep = dd\n"
2621 ));
2622   fs_.Create("dd",
2623 "ninja_dyndep_version = 1\n"
2624 "build out | out.imp: dyndep | tmp.imp\n"
2625 "build tmp | tmp.imp: dyndep\n"
2626 );
2627
2628   string err;
2629   EXPECT_TRUE(builder_.AddTarget("out", &err));
2630   ASSERT_EQ("", err);
2631   EXPECT_TRUE(builder_.Build(&err));
2632   EXPECT_EQ("", err);
2633   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
2634   EXPECT_EQ("touch tmp tmp.imp", command_runner_.commands_ran_[0]);
2635   EXPECT_EQ("touch out out.imp", command_runner_.commands_ran_[1]);
2636 }
2637
2638 TEST_F(BuildTest, DyndepReadySyntaxError) {
2639   // Verify that a dyndep file can be loaded immediately to discover
2640   // and reject a syntax error in it.
2641   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2642 "rule touch\n"
2643 "  command = touch $out\n"
2644 "build out: touch || dd\n"
2645 "  dyndep = dd\n"
2646 ));
2647   fs_.Create("dd",
2648 "build out: dyndep\n"
2649 );
2650
2651   string err;
2652   EXPECT_FALSE(builder_.AddTarget("out", &err));
2653   EXPECT_EQ("dd:1: expected 'ninja_dyndep_version = ...'\n", err);
2654 }
2655
2656 TEST_F(BuildTest, DyndepReadyCircular) {
2657   // Verify that a dyndep file can be loaded immediately to discover
2658   // and reject a circular dependency.
2659   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2660 "rule r\n"
2661 "  command = unused\n"
2662 "build out: r in || dd\n"
2663 "  dyndep = dd\n"
2664 "build in: r circ\n"
2665   ));
2666   fs_.Create("dd",
2667 "ninja_dyndep_version = 1\n"
2668 "build out | circ: dyndep\n"
2669   );
2670   fs_.Create("out", "");
2671
2672   string err;
2673   EXPECT_FALSE(builder_.AddTarget("out", &err));
2674   EXPECT_EQ("dependency cycle: circ -> in -> circ", err);
2675 }
2676
2677 TEST_F(BuildTest, DyndepBuild) {
2678   // Verify that a dyndep file can be built and loaded to discover nothing.
2679   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2680 "rule touch\n"
2681 "  command = touch $out\n"
2682 "rule cp\n"
2683 "  command = cp $in $out\n"
2684 "build dd: cp dd-in\n"
2685 "build out: touch || dd\n"
2686 "  dyndep = dd\n"
2687 ));
2688   fs_.Create("dd-in",
2689 "ninja_dyndep_version = 1\n"
2690 "build out: dyndep\n"
2691 );
2692
2693   string err;
2694   EXPECT_TRUE(builder_.AddTarget("out", &err));
2695   EXPECT_EQ("", err);
2696
2697   size_t files_created = fs_.files_created_.size();
2698   EXPECT_TRUE(builder_.Build(&err));
2699   EXPECT_EQ("", err);
2700
2701   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
2702   EXPECT_EQ("cp dd-in dd", command_runner_.commands_ran_[0]);
2703   EXPECT_EQ("touch out", command_runner_.commands_ran_[1]);
2704   ASSERT_EQ(2u, fs_.files_read_.size());
2705   EXPECT_EQ("dd-in", fs_.files_read_[0]);
2706   EXPECT_EQ("dd", fs_.files_read_[1]);
2707   ASSERT_EQ(2u + files_created, fs_.files_created_.size());
2708   EXPECT_EQ(1u, fs_.files_created_.count("dd"));
2709   EXPECT_EQ(1u, fs_.files_created_.count("out"));
2710 }
2711
2712 TEST_F(BuildTest, DyndepBuildSyntaxError) {
2713   // Verify that a dyndep file can be built and loaded to discover
2714   // and reject a syntax error in it.
2715   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2716 "rule touch\n"
2717 "  command = touch $out\n"
2718 "rule cp\n"
2719 "  command = cp $in $out\n"
2720 "build dd: cp dd-in\n"
2721 "build out: touch || dd\n"
2722 "  dyndep = dd\n"
2723 ));
2724   fs_.Create("dd-in",
2725 "build out: dyndep\n"
2726 );
2727
2728   string err;
2729   EXPECT_TRUE(builder_.AddTarget("out", &err));
2730   EXPECT_EQ("", err);
2731
2732   EXPECT_FALSE(builder_.Build(&err));
2733   EXPECT_EQ("dd:1: expected 'ninja_dyndep_version = ...'\n", err);
2734 }
2735
2736 TEST_F(BuildTest, DyndepBuildUnrelatedOutput) {
2737   // Verify that a dyndep file can have dependents that do not specify
2738   // it as their dyndep binding.
2739   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2740 "rule touch\n"
2741 "  command = touch $out\n"
2742 "rule cp\n"
2743 "  command = cp $in $out\n"
2744 "build dd: cp dd-in\n"
2745 "build unrelated: touch || dd\n"
2746 "build out: touch unrelated || dd\n"
2747 "  dyndep = dd\n"
2748   ));
2749   fs_.Create("dd-in",
2750 "ninja_dyndep_version = 1\n"
2751 "build out: dyndep\n"
2752 );
2753   fs_.Tick();
2754   fs_.Create("out", "");
2755
2756   string err;
2757   EXPECT_TRUE(builder_.AddTarget("out", &err));
2758   EXPECT_EQ("", err);
2759
2760   EXPECT_TRUE(builder_.Build(&err));
2761   EXPECT_EQ("", err);
2762   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
2763   EXPECT_EQ("cp dd-in dd", command_runner_.commands_ran_[0]);
2764   EXPECT_EQ("touch unrelated", command_runner_.commands_ran_[1]);
2765   EXPECT_EQ("touch out", command_runner_.commands_ran_[2]);
2766 }
2767
2768 TEST_F(BuildTest, DyndepBuildDiscoverNewOutput) {
2769   // Verify that a dyndep file can be built and loaded to discover
2770   // a new output of an edge.
2771   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2772 "rule touch\n"
2773 "  command = touch $out $out.imp\n"
2774 "rule cp\n"
2775 "  command = cp $in $out\n"
2776 "build dd: cp dd-in\n"
2777 "build out: touch in || dd\n"
2778 "  dyndep = dd\n"
2779   ));
2780   fs_.Create("in", "");
2781   fs_.Create("dd-in",
2782 "ninja_dyndep_version = 1\n"
2783 "build out | out.imp: dyndep\n"
2784 );
2785   fs_.Tick();
2786   fs_.Create("out", "");
2787
2788   string err;
2789   EXPECT_TRUE(builder_.AddTarget("out", &err));
2790   EXPECT_EQ("", err);
2791
2792   EXPECT_TRUE(builder_.Build(&err));
2793   EXPECT_EQ("", err);
2794   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
2795   EXPECT_EQ("cp dd-in dd", command_runner_.commands_ran_[0]);
2796   EXPECT_EQ("touch out out.imp", command_runner_.commands_ran_[1]);
2797 }
2798
2799 TEST_F(BuildTest, DyndepBuildDiscoverNewOutputWithMultipleRules1) {
2800   // Verify that a dyndep file can be built and loaded to discover
2801   // a new output of an edge that is already the output of another edge.
2802   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2803 "rule touch\n"
2804 "  command = touch $out $out.imp\n"
2805 "rule cp\n"
2806 "  command = cp $in $out\n"
2807 "build dd: cp dd-in\n"
2808 "build out1 | out-twice.imp: touch in\n"
2809 "build out2: touch in || dd\n"
2810 "  dyndep = dd\n"
2811   ));
2812   fs_.Create("in", "");
2813   fs_.Create("dd-in",
2814 "ninja_dyndep_version = 1\n"
2815 "build out2 | out-twice.imp: dyndep\n"
2816 );
2817   fs_.Tick();
2818   fs_.Create("out1", "");
2819   fs_.Create("out2", "");
2820
2821   string err;
2822   EXPECT_TRUE(builder_.AddTarget("out1", &err));
2823   EXPECT_TRUE(builder_.AddTarget("out2", &err));
2824   EXPECT_EQ("", err);
2825
2826   EXPECT_FALSE(builder_.Build(&err));
2827   EXPECT_EQ("multiple rules generate out-twice.imp", err);
2828 }
2829
2830 TEST_F(BuildTest, DyndepBuildDiscoverNewOutputWithMultipleRules2) {
2831   // Verify that a dyndep file can be built and loaded to discover
2832   // a new output of an edge that is already the output of another
2833   // edge also discovered by dyndep.
2834   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2835 "rule touch\n"
2836 "  command = touch $out $out.imp\n"
2837 "rule cp\n"
2838 "  command = cp $in $out\n"
2839 "build dd1: cp dd1-in\n"
2840 "build out1: touch || dd1\n"
2841 "  dyndep = dd1\n"
2842 "build dd2: cp dd2-in || dd1\n" // make order predictable for test
2843 "build out2: touch || dd2\n"
2844 "  dyndep = dd2\n"
2845 ));
2846   fs_.Create("out1", "");
2847   fs_.Create("out2", "");
2848   fs_.Create("dd1-in",
2849 "ninja_dyndep_version = 1\n"
2850 "build out1 | out-twice.imp: dyndep\n"
2851 );
2852   fs_.Create("dd2-in", "");
2853   fs_.Create("dd2",
2854 "ninja_dyndep_version = 1\n"
2855 "build out2 | out-twice.imp: dyndep\n"
2856 );
2857   fs_.Tick();
2858   fs_.Create("out1", "");
2859   fs_.Create("out2", "");
2860
2861   string err;
2862   EXPECT_TRUE(builder_.AddTarget("out1", &err));
2863   EXPECT_TRUE(builder_.AddTarget("out2", &err));
2864   EXPECT_EQ("", err);
2865
2866   EXPECT_FALSE(builder_.Build(&err));
2867   EXPECT_EQ("multiple rules generate out-twice.imp", err);
2868 }
2869
2870 TEST_F(BuildTest, DyndepBuildDiscoverNewInput) {
2871   // Verify that a dyndep file can be built and loaded to discover
2872   // a new input to an edge.
2873   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2874 "rule touch\n"
2875 "  command = touch $out\n"
2876 "rule cp\n"
2877 "  command = cp $in $out\n"
2878 "build dd: cp dd-in\n"
2879 "build in: touch\n"
2880 "build out: touch || dd\n"
2881 "  dyndep = dd\n"
2882   ));
2883   fs_.Create("dd-in",
2884 "ninja_dyndep_version = 1\n"
2885 "build out: dyndep | in\n"
2886 );
2887   fs_.Tick();
2888   fs_.Create("out", "");
2889
2890   string err;
2891   EXPECT_TRUE(builder_.AddTarget("out", &err));
2892   EXPECT_EQ("", err);
2893
2894   EXPECT_TRUE(builder_.Build(&err));
2895   EXPECT_EQ("", err);
2896   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
2897   EXPECT_EQ("cp dd-in dd", command_runner_.commands_ran_[0]);
2898   EXPECT_EQ("touch in", command_runner_.commands_ran_[1]);
2899   EXPECT_EQ("touch out", command_runner_.commands_ran_[2]);
2900 }
2901
2902 TEST_F(BuildTest, DyndepBuildDiscoverImplicitConnection) {
2903   // Verify that a dyndep file can be built and loaded to discover
2904   // that one edge has an implicit output that is also an implicit
2905   // input of another edge.
2906   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2907 "rule touch\n"
2908 "  command = touch $out $out.imp\n"
2909 "rule cp\n"
2910 "  command = cp $in $out\n"
2911 "build dd: cp dd-in\n"
2912 "build tmp: touch || dd\n"
2913 "  dyndep = dd\n"
2914 "build out: touch || dd\n"
2915 "  dyndep = dd\n"
2916 ));
2917   fs_.Create("dd-in",
2918 "ninja_dyndep_version = 1\n"
2919 "build out | out.imp: dyndep | tmp.imp\n"
2920 "build tmp | tmp.imp: dyndep\n"
2921 );
2922
2923   string err;
2924   EXPECT_TRUE(builder_.AddTarget("out", &err));
2925   ASSERT_EQ("", err);
2926   EXPECT_TRUE(builder_.Build(&err));
2927   EXPECT_EQ("", err);
2928   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
2929   EXPECT_EQ("cp dd-in dd", command_runner_.commands_ran_[0]);
2930   EXPECT_EQ("touch tmp tmp.imp", command_runner_.commands_ran_[1]);
2931   EXPECT_EQ("touch out out.imp", command_runner_.commands_ran_[2]);
2932 }
2933
2934 TEST_F(BuildTest, DyndepBuildDiscoverNowWantEdge) {
2935   // Verify that a dyndep file can be built and loaded to discover
2936   // that an edge is actually wanted due to a missing implicit output.
2937   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2938 "rule touch\n"
2939 "  command = touch $out $out.imp\n"
2940 "rule cp\n"
2941 "  command = cp $in $out\n"
2942 "build dd: cp dd-in\n"
2943 "build tmp: touch || dd\n"
2944 "  dyndep = dd\n"
2945 "build out: touch tmp || dd\n"
2946 "  dyndep = dd\n"
2947 ));
2948   fs_.Create("tmp", "");
2949   fs_.Create("out", "");
2950   fs_.Create("dd-in",
2951 "ninja_dyndep_version = 1\n"
2952 "build out: dyndep\n"
2953 "build tmp | tmp.imp: dyndep\n"
2954 );
2955
2956   string err;
2957   EXPECT_TRUE(builder_.AddTarget("out", &err));
2958   ASSERT_EQ("", err);
2959   EXPECT_TRUE(builder_.Build(&err));
2960   EXPECT_EQ("", err);
2961   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
2962   EXPECT_EQ("cp dd-in dd", command_runner_.commands_ran_[0]);
2963   EXPECT_EQ("touch tmp tmp.imp", command_runner_.commands_ran_[1]);
2964   EXPECT_EQ("touch out out.imp", command_runner_.commands_ran_[2]);
2965 }
2966
2967 TEST_F(BuildTest, DyndepBuildDiscoverNowWantEdgeAndDependent) {
2968   // Verify that a dyndep file can be built and loaded to discover
2969   // that an edge and a dependent are actually wanted.
2970   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2971 "rule touch\n"
2972 "  command = touch $out $out.imp\n"
2973 "rule cp\n"
2974 "  command = cp $in $out\n"
2975 "build dd: cp dd-in\n"
2976 "build tmp: touch || dd\n"
2977 "  dyndep = dd\n"
2978 "build out: touch tmp\n"
2979 ));
2980   fs_.Create("tmp", "");
2981   fs_.Create("out", "");
2982   fs_.Create("dd-in",
2983 "ninja_dyndep_version = 1\n"
2984 "build tmp | tmp.imp: dyndep\n"
2985 );
2986
2987   string err;
2988   EXPECT_TRUE(builder_.AddTarget("out", &err));
2989   ASSERT_EQ("", err);
2990   EXPECT_TRUE(builder_.Build(&err));
2991   EXPECT_EQ("", err);
2992   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
2993   EXPECT_EQ("cp dd-in dd", command_runner_.commands_ran_[0]);
2994   EXPECT_EQ("touch tmp tmp.imp", command_runner_.commands_ran_[1]);
2995   EXPECT_EQ("touch out out.imp", command_runner_.commands_ran_[2]);
2996 }
2997
2998 TEST_F(BuildTest, DyndepBuildDiscoverCircular) {
2999   // Verify that a dyndep file can be built and loaded to discover
3000   // and reject a circular dependency.
3001   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
3002 "rule r\n"
3003 "  command = unused\n"
3004 "rule cp\n"
3005 "  command = cp $in $out\n"
3006 "build dd: cp dd-in\n"
3007 "build out: r in || dd\n"
3008 "  depfile = out.d\n"
3009 "  dyndep = dd\n"
3010 "build in: r || dd\n"
3011 "  dyndep = dd\n"
3012   ));
3013   fs_.Create("out.d", "out: inimp\n");
3014   fs_.Create("dd-in",
3015 "ninja_dyndep_version = 1\n"
3016 "build out | circ: dyndep\n"
3017 "build in: dyndep | circ\n"
3018   );
3019   fs_.Create("out", "");
3020
3021   string err;
3022   EXPECT_TRUE(builder_.AddTarget("out", &err));
3023   EXPECT_EQ("", err);
3024
3025   EXPECT_FALSE(builder_.Build(&err));
3026   // Depending on how the pointers in Plan::ready_ work out, we could have
3027   // discovered the cycle from either starting point.
3028   EXPECT_TRUE(err == "dependency cycle: circ -> in -> circ" ||
3029               err == "dependency cycle: in -> circ -> in");
3030 }
3031
3032 TEST_F(BuildWithLogTest, DyndepBuildDiscoverRestat) {
3033   // Verify that a dyndep file can be built and loaded to discover
3034   // that an edge has a restat binding.
3035   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
3036 "rule true\n"
3037 "  command = true\n"
3038 "rule cp\n"
3039 "  command = cp $in $out\n"
3040 "build dd: cp dd-in\n"
3041 "build out1: true in || dd\n"
3042 "  dyndep = dd\n"
3043 "build out2: cat out1\n"));
3044
3045   fs_.Create("out1", "");
3046   fs_.Create("out2", "");
3047   fs_.Create("dd-in",
3048 "ninja_dyndep_version = 1\n"
3049 "build out1: dyndep\n"
3050 "  restat = 1\n"
3051 );
3052   fs_.Tick();
3053   fs_.Create("in", "");
3054
3055   // Do a pre-build so that there's commands in the log for the outputs,
3056   // otherwise, the lack of an entry in the build log will cause "out2" to
3057   // rebuild regardless of restat.
3058   string err;
3059   EXPECT_TRUE(builder_.AddTarget("out2", &err));
3060   ASSERT_EQ("", err);
3061   EXPECT_TRUE(builder_.Build(&err));
3062   ASSERT_EQ("", err);
3063   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
3064   EXPECT_EQ("cp dd-in dd", command_runner_.commands_ran_[0]);
3065   EXPECT_EQ("true", command_runner_.commands_ran_[1]);
3066   EXPECT_EQ("cat out1 > out2", command_runner_.commands_ran_[2]);
3067
3068   command_runner_.commands_ran_.clear();
3069   state_.Reset();
3070   fs_.Tick();
3071   fs_.Create("in", "");
3072
3073   // We touched "in", so we should build "out1".  But because "true" does not
3074   // touch "out1", we should cancel the build of "out2".
3075   EXPECT_TRUE(builder_.AddTarget("out2", &err));
3076   ASSERT_EQ("", err);
3077   EXPECT_TRUE(builder_.Build(&err));
3078   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
3079   EXPECT_EQ("true", command_runner_.commands_ran_[0]);
3080 }
3081
3082 TEST_F(BuildTest, DyndepBuildDiscoverScheduledEdge) {
3083   // Verify that a dyndep file can be built and loaded to discover a
3084   // new input that itself is an output from an edge that has already
3085   // been scheduled but not finished.  We should not re-schedule it.
3086   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
3087 "rule touch\n"
3088 "  command = touch $out $out.imp\n"
3089 "rule cp\n"
3090 "  command = cp $in $out\n"
3091 "build out1 | out1.imp: touch\n"
3092 "build zdd: cp zdd-in\n"
3093 "  verify_active_edge = out1\n" // verify out1 is active when zdd is finished
3094 "build out2: cp out1 || zdd\n"
3095 "  dyndep = zdd\n"
3096 ));
3097   fs_.Create("zdd-in",
3098 "ninja_dyndep_version = 1\n"
3099 "build out2: dyndep | out1.imp\n"
3100 );
3101
3102   // Enable concurrent builds so that we can load the dyndep file
3103   // while another edge is still active.
3104   command_runner_.max_active_edges_ = 2;
3105
3106   // During the build "out1" and "zdd" should be built concurrently.
3107   // The fake command runner will finish these in reverse order
3108   // of the names of the first outputs, so "zdd" will finish first
3109   // and we will load the dyndep file while the edge for "out1" is
3110   // still active.  This will add a new dependency on "out1.imp",
3111   // also produced by the active edge.  The builder should not
3112   // re-schedule the already-active edge.
3113
3114   string err;
3115   EXPECT_TRUE(builder_.AddTarget("out1", &err));
3116   EXPECT_TRUE(builder_.AddTarget("out2", &err));
3117   ASSERT_EQ("", err);
3118   EXPECT_TRUE(builder_.Build(&err));
3119   EXPECT_EQ("", err);
3120   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
3121   // Depending on how the pointers in Plan::ready_ work out, the first
3122   // two commands may have run in either order.
3123   EXPECT_TRUE((command_runner_.commands_ran_[0] == "touch out1 out1.imp" &&
3124                command_runner_.commands_ran_[1] == "cp zdd-in zdd") ||
3125               (command_runner_.commands_ran_[1] == "touch out1 out1.imp" &&
3126                command_runner_.commands_ran_[0] == "cp zdd-in zdd"));
3127   EXPECT_EQ("cp out1 out2", command_runner_.commands_ran_[2]);
3128 }
3129
3130 TEST_F(BuildTest, DyndepTwoLevelDirect) {
3131   // Verify that a clean dyndep file can depend on a dirty dyndep file
3132   // and be loaded properly after the dirty one is built and loaded.
3133   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
3134 "rule touch\n"
3135 "  command = touch $out $out.imp\n"
3136 "rule cp\n"
3137 "  command = cp $in $out\n"
3138 "build dd1: cp dd1-in\n"
3139 "build out1 | out1.imp: touch || dd1\n"
3140 "  dyndep = dd1\n"
3141 "build dd2: cp dd2-in || dd1\n" // direct order-only dep on dd1
3142 "build out2: touch || dd2\n"
3143 "  dyndep = dd2\n"
3144 ));
3145   fs_.Create("out1.imp", "");
3146   fs_.Create("out2", "");
3147   fs_.Create("out2.imp", "");
3148   fs_.Create("dd1-in",
3149 "ninja_dyndep_version = 1\n"
3150 "build out1: dyndep\n"
3151 );
3152   fs_.Create("dd2-in", "");
3153   fs_.Create("dd2",
3154 "ninja_dyndep_version = 1\n"
3155 "build out2 | out2.imp: dyndep | out1.imp\n"
3156 );
3157
3158   // During the build dd1 should be built and loaded.  The RecomputeDirty
3159   // called as a result of loading dd1 should not cause dd2 to be loaded
3160   // because the builder will never get a chance to update the build plan
3161   // to account for dd2.  Instead dd2 should only be later loaded once the
3162   // builder recognizes that it is now ready (as its order-only dependency
3163   // on dd1 has been satisfied).  This test case verifies that each dyndep
3164   // file is loaded to update the build graph independently.
3165
3166   string err;
3167   EXPECT_TRUE(builder_.AddTarget("out2", &err));
3168   ASSERT_EQ("", err);
3169   EXPECT_TRUE(builder_.Build(&err));
3170   EXPECT_EQ("", err);
3171   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
3172   EXPECT_EQ("cp dd1-in dd1", command_runner_.commands_ran_[0]);
3173   EXPECT_EQ("touch out1 out1.imp", command_runner_.commands_ran_[1]);
3174   EXPECT_EQ("touch out2 out2.imp", command_runner_.commands_ran_[2]);
3175 }
3176
3177 TEST_F(BuildTest, DyndepTwoLevelIndirect) {
3178   // Verify that dyndep files can add to an edge new implicit inputs that
3179   // correspond to implicit outputs added to other edges by other dyndep
3180   // files on which they (order-only) depend.
3181   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
3182 "rule touch\n"
3183 "  command = touch $out $out.imp\n"
3184 "rule cp\n"
3185 "  command = cp $in $out\n"
3186 "build dd1: cp dd1-in\n"
3187 "build out1: touch || dd1\n"
3188 "  dyndep = dd1\n"
3189 "build dd2: cp dd2-in || out1\n" // indirect order-only dep on dd1
3190 "build out2: touch || dd2\n"
3191 "  dyndep = dd2\n"
3192 ));
3193   fs_.Create("out1.imp", "");
3194   fs_.Create("out2", "");
3195   fs_.Create("out2.imp", "");
3196   fs_.Create("dd1-in",
3197 "ninja_dyndep_version = 1\n"
3198 "build out1 | out1.imp: dyndep\n"
3199 );
3200   fs_.Create("dd2-in", "");
3201   fs_.Create("dd2",
3202 "ninja_dyndep_version = 1\n"
3203 "build out2 | out2.imp: dyndep | out1.imp\n"
3204 );
3205
3206   // During the build dd1 should be built and loaded.  Then dd2 should
3207   // be built and loaded.  Loading dd2 should cause the builder to
3208   // recognize that out2 needs to be built even though it was originally
3209   // clean without dyndep info.
3210
3211   string err;
3212   EXPECT_TRUE(builder_.AddTarget("out2", &err));
3213   ASSERT_EQ("", err);
3214   EXPECT_TRUE(builder_.Build(&err));
3215   EXPECT_EQ("", err);
3216   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
3217   EXPECT_EQ("cp dd1-in dd1", command_runner_.commands_ran_[0]);
3218   EXPECT_EQ("touch out1 out1.imp", command_runner_.commands_ran_[1]);
3219   EXPECT_EQ("touch out2 out2.imp", command_runner_.commands_ran_[2]);
3220 }
3221
3222 TEST_F(BuildTest, DyndepTwoLevelDiscoveredReady) {
3223   // Verify that a dyndep file can discover a new input whose
3224   // edge also has a dyndep file that is ready to load immediately.
3225   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
3226 "rule touch\n"
3227 "  command = touch $out\n"
3228 "rule cp\n"
3229 "  command = cp $in $out\n"
3230 "build dd0: cp dd0-in\n"
3231 "build dd1: cp dd1-in\n"
3232 "build in: touch\n"
3233 "build tmp: touch || dd0\n"
3234 "  dyndep = dd0\n"
3235 "build out: touch || dd1\n"
3236 "  dyndep = dd1\n"
3237   ));
3238   fs_.Create("dd1-in",
3239 "ninja_dyndep_version = 1\n"
3240 "build out: dyndep | tmp\n"
3241 );
3242   fs_.Create("dd0-in", "");
3243   fs_.Create("dd0",
3244 "ninja_dyndep_version = 1\n"
3245 "build tmp: dyndep | in\n"
3246 );
3247   fs_.Tick();
3248   fs_.Create("out", "");
3249
3250   string err;
3251   EXPECT_TRUE(builder_.AddTarget("out", &err));
3252   EXPECT_EQ("", err);
3253
3254   EXPECT_TRUE(builder_.Build(&err));
3255   EXPECT_EQ("", err);
3256   ASSERT_EQ(4u, command_runner_.commands_ran_.size());
3257   EXPECT_EQ("cp dd1-in dd1", command_runner_.commands_ran_[0]);
3258   EXPECT_EQ("touch in", command_runner_.commands_ran_[1]);
3259   EXPECT_EQ("touch tmp", command_runner_.commands_ran_[2]);
3260   EXPECT_EQ("touch out", command_runner_.commands_ran_[3]);
3261 }
3262
3263 TEST_F(BuildTest, DyndepTwoLevelDiscoveredDirty) {
3264   // Verify that a dyndep file can discover a new input whose
3265   // edge also has a dyndep file that needs to be built.
3266   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
3267 "rule touch\n"
3268 "  command = touch $out\n"
3269 "rule cp\n"
3270 "  command = cp $in $out\n"
3271 "build dd0: cp dd0-in\n"
3272 "build dd1: cp dd1-in\n"
3273 "build in: touch\n"
3274 "build tmp: touch || dd0\n"
3275 "  dyndep = dd0\n"
3276 "build out: touch || dd1\n"
3277 "  dyndep = dd1\n"
3278   ));
3279   fs_.Create("dd1-in",
3280 "ninja_dyndep_version = 1\n"
3281 "build out: dyndep | tmp\n"
3282 );
3283   fs_.Create("dd0-in",
3284 "ninja_dyndep_version = 1\n"
3285 "build tmp: dyndep | in\n"
3286 );
3287   fs_.Tick();
3288   fs_.Create("out", "");
3289
3290   string err;
3291   EXPECT_TRUE(builder_.AddTarget("out", &err));
3292   EXPECT_EQ("", err);
3293
3294   EXPECT_TRUE(builder_.Build(&err));
3295   EXPECT_EQ("", err);
3296   ASSERT_EQ(5u, command_runner_.commands_ran_.size());
3297   EXPECT_EQ("cp dd1-in dd1", command_runner_.commands_ran_[0]);
3298   EXPECT_EQ("cp dd0-in dd0", command_runner_.commands_ran_[1]);
3299   EXPECT_EQ("touch in", command_runner_.commands_ran_[2]);
3300   EXPECT_EQ("touch tmp", command_runner_.commands_ran_[3]);
3301   EXPECT_EQ("touch out", command_runner_.commands_ran_[4]);
3302 }