Merge pull request #628 from rnk/wstr
[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 "build_log.h"
18 #include "deps_log.h"
19 #include "graph.h"
20 #include "test.h"
21
22 /// Fixture for tests involving Plan.
23 // Though Plan doesn't use State, it's useful to have one around
24 // to create Nodes and Edges.
25 struct PlanTest : public StateTestWithBuiltinRules {
26   Plan plan_;
27
28   /// Because FindWork does not return Edges in any sort of predictable order,
29   // provide a means to get available Edges in order and in a format which is
30   // easy to write tests around.
31   void FindWorkSorted(deque<Edge*>* ret, int count) {
32     struct CompareEdgesByOutput {
33       static bool cmp(const Edge* a, const Edge* b) {
34         return a->outputs_[0]->path() < b->outputs_[0]->path();
35       }
36     };
37
38     for (int i = 0; i < count; ++i) {
39       ASSERT_TRUE(plan_.more_to_do());
40       Edge* edge = plan_.FindWork();
41       ASSERT_TRUE(edge);
42       ret->push_back(edge);
43     }
44     ASSERT_FALSE(plan_.FindWork());
45     sort(ret->begin(), ret->end(), CompareEdgesByOutput::cmp);
46   }
47 };
48
49 TEST_F(PlanTest, Basic) {
50   AssertParse(&state_,
51 "build out: cat mid\n"
52 "build mid: cat in\n");
53   GetNode("mid")->MarkDirty();
54   GetNode("out")->MarkDirty();
55   string err;
56   EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
57   ASSERT_EQ("", err);
58   ASSERT_TRUE(plan_.more_to_do());
59
60   Edge* edge = plan_.FindWork();
61   ASSERT_TRUE(edge);
62   ASSERT_EQ("in",  edge->inputs_[0]->path());
63   ASSERT_EQ("mid", edge->outputs_[0]->path());
64
65   ASSERT_FALSE(plan_.FindWork());
66
67   plan_.EdgeFinished(edge);
68
69   edge = plan_.FindWork();
70   ASSERT_TRUE(edge);
71   ASSERT_EQ("mid", edge->inputs_[0]->path());
72   ASSERT_EQ("out", edge->outputs_[0]->path());
73
74   plan_.EdgeFinished(edge);
75
76   ASSERT_FALSE(plan_.more_to_do());
77   edge = plan_.FindWork();
78   ASSERT_EQ(0, edge);
79 }
80
81 // Test that two outputs from one rule can be handled as inputs to the next.
82 TEST_F(PlanTest, DoubleOutputDirect) {
83   AssertParse(&state_,
84 "build out: cat mid1 mid2\n"
85 "build mid1 mid2: cat in\n");
86   GetNode("mid1")->MarkDirty();
87   GetNode("mid2")->MarkDirty();
88   GetNode("out")->MarkDirty();
89
90   string err;
91   EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
92   ASSERT_EQ("", err);
93   ASSERT_TRUE(plan_.more_to_do());
94
95   Edge* edge;
96   edge = plan_.FindWork();
97   ASSERT_TRUE(edge);  // cat in
98   plan_.EdgeFinished(edge);
99
100   edge = plan_.FindWork();
101   ASSERT_TRUE(edge);  // cat mid1 mid2
102   plan_.EdgeFinished(edge);
103
104   edge = plan_.FindWork();
105   ASSERT_FALSE(edge);  // done
106 }
107
108 // Test that two outputs from one rule can eventually be routed to another.
109 TEST_F(PlanTest, DoubleOutputIndirect) {
110   AssertParse(&state_,
111 "build out: cat b1 b2\n"
112 "build b1: cat a1\n"
113 "build b2: cat a2\n"
114 "build a1 a2: cat in\n");
115   GetNode("a1")->MarkDirty();
116   GetNode("a2")->MarkDirty();
117   GetNode("b1")->MarkDirty();
118   GetNode("b2")->MarkDirty();
119   GetNode("out")->MarkDirty();
120   string err;
121   EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
122   ASSERT_EQ("", err);
123   ASSERT_TRUE(plan_.more_to_do());
124
125   Edge* edge;
126   edge = plan_.FindWork();
127   ASSERT_TRUE(edge);  // cat in
128   plan_.EdgeFinished(edge);
129
130   edge = plan_.FindWork();
131   ASSERT_TRUE(edge);  // cat a1
132   plan_.EdgeFinished(edge);
133
134   edge = plan_.FindWork();
135   ASSERT_TRUE(edge);  // cat a2
136   plan_.EdgeFinished(edge);
137
138   edge = plan_.FindWork();
139   ASSERT_TRUE(edge);  // cat b1 b2
140   plan_.EdgeFinished(edge);
141
142   edge = plan_.FindWork();
143   ASSERT_FALSE(edge);  // done
144 }
145
146 // Test that two edges from one output can both execute.
147 TEST_F(PlanTest, DoubleDependent) {
148   AssertParse(&state_,
149 "build out: cat a1 a2\n"
150 "build a1: cat mid\n"
151 "build a2: cat mid\n"
152 "build mid: cat in\n");
153   GetNode("mid")->MarkDirty();
154   GetNode("a1")->MarkDirty();
155   GetNode("a2")->MarkDirty();
156   GetNode("out")->MarkDirty();
157
158   string err;
159   EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
160   ASSERT_EQ("", err);
161   ASSERT_TRUE(plan_.more_to_do());
162
163   Edge* edge;
164   edge = plan_.FindWork();
165   ASSERT_TRUE(edge);  // cat in
166   plan_.EdgeFinished(edge);
167
168   edge = plan_.FindWork();
169   ASSERT_TRUE(edge);  // cat mid
170   plan_.EdgeFinished(edge);
171
172   edge = plan_.FindWork();
173   ASSERT_TRUE(edge);  // cat mid
174   plan_.EdgeFinished(edge);
175
176   edge = plan_.FindWork();
177   ASSERT_TRUE(edge);  // cat a1 a2
178   plan_.EdgeFinished(edge);
179
180   edge = plan_.FindWork();
181   ASSERT_FALSE(edge);  // done
182 }
183
184 TEST_F(PlanTest, DependencyCycle) {
185   AssertParse(&state_,
186 "build out: cat mid\n"
187 "build mid: cat in\n"
188 "build in: cat pre\n"
189 "build pre: cat out\n");
190   GetNode("out")->MarkDirty();
191   GetNode("mid")->MarkDirty();
192   GetNode("in")->MarkDirty();
193   GetNode("pre")->MarkDirty();
194
195   string err;
196   EXPECT_FALSE(plan_.AddTarget(GetNode("out"), &err));
197   ASSERT_EQ("dependency cycle: out -> mid -> in -> pre -> out", err);
198 }
199
200 TEST_F(PlanTest, PoolWithDepthOne) {
201   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
202 "pool foobar\n"
203 "  depth = 1\n"
204 "rule poolcat\n"
205 "  command = cat $in > $out\n"
206 "  pool = foobar\n"
207 "build out1: poolcat in\n"
208 "build out2: poolcat in\n"));
209   GetNode("out1")->MarkDirty();
210   GetNode("out2")->MarkDirty();
211   string err;
212   EXPECT_TRUE(plan_.AddTarget(GetNode("out1"), &err));
213   ASSERT_EQ("", err);
214   EXPECT_TRUE(plan_.AddTarget(GetNode("out2"), &err));
215   ASSERT_EQ("", err);
216   ASSERT_TRUE(plan_.more_to_do());
217
218   Edge* edge = plan_.FindWork();
219   ASSERT_TRUE(edge);
220   ASSERT_EQ("in",  edge->inputs_[0]->path());
221   ASSERT_EQ("out1", edge->outputs_[0]->path());
222
223   // This will be false since poolcat is serialized
224   ASSERT_FALSE(plan_.FindWork());
225
226   plan_.EdgeFinished(edge);
227
228   edge = plan_.FindWork();
229   ASSERT_TRUE(edge);
230   ASSERT_EQ("in", edge->inputs_[0]->path());
231   ASSERT_EQ("out2", edge->outputs_[0]->path());
232
233   ASSERT_FALSE(plan_.FindWork());
234
235   plan_.EdgeFinished(edge);
236
237   ASSERT_FALSE(plan_.more_to_do());
238   edge = plan_.FindWork();
239   ASSERT_EQ(0, edge);
240 }
241
242 TEST_F(PlanTest, PoolsWithDepthTwo) {
243   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
244 "pool foobar\n"
245 "  depth = 2\n"
246 "pool bazbin\n"
247 "  depth = 2\n"
248 "rule foocat\n"
249 "  command = cat $in > $out\n"
250 "  pool = foobar\n"
251 "rule bazcat\n"
252 "  command = cat $in > $out\n"
253 "  pool = bazbin\n"
254 "build out1: foocat in\n"
255 "build out2: foocat in\n"
256 "build out3: foocat in\n"
257 "build outb1: bazcat in\n"
258 "build outb2: bazcat in\n"
259 "build outb3: bazcat in\n"
260 "  pool =\n"
261 "build allTheThings: cat out1 out2 out3 outb1 outb2 outb3\n"
262 ));
263   // Mark all the out* nodes dirty
264   for (int i = 0; i < 3; ++i) {
265     GetNode("out" + string(1, '1' + static_cast<char>(i)))->MarkDirty();
266     GetNode("outb" + string(1, '1' + static_cast<char>(i)))->MarkDirty();
267   }
268   GetNode("allTheThings")->MarkDirty();
269
270   string err;
271   EXPECT_TRUE(plan_.AddTarget(GetNode("allTheThings"), &err));
272   ASSERT_EQ("", err);
273
274   deque<Edge*> edges;
275   FindWorkSorted(&edges, 5);
276
277   for (int i = 0; i < 4; ++i) {
278     Edge *edge = edges[i];
279     ASSERT_EQ("in",  edge->inputs_[0]->path());
280     string base_name(i < 2 ? "out" : "outb");
281     ASSERT_EQ(base_name + string(1, '1' + (i % 2)), edge->outputs_[0]->path());
282   }
283
284   // outb3 is exempt because it has an empty pool
285   Edge* edge = edges[4];
286   ASSERT_TRUE(edge);
287   ASSERT_EQ("in",  edge->inputs_[0]->path());
288   ASSERT_EQ("outb3", edge->outputs_[0]->path());
289
290   // finish out1
291   plan_.EdgeFinished(edges.front());
292   edges.pop_front();
293
294   // out3 should be available
295   Edge* out3 = plan_.FindWork();
296   ASSERT_TRUE(out3);
297   ASSERT_EQ("in",  out3->inputs_[0]->path());
298   ASSERT_EQ("out3", out3->outputs_[0]->path());
299
300   ASSERT_FALSE(plan_.FindWork());
301
302   plan_.EdgeFinished(out3);
303
304   ASSERT_FALSE(plan_.FindWork());
305
306   for (deque<Edge*>::iterator it = edges.begin(); it != edges.end(); ++it) {
307     plan_.EdgeFinished(*it);
308   }
309
310   Edge* last = plan_.FindWork();
311   ASSERT_TRUE(last);
312   ASSERT_EQ("allTheThings", last->outputs_[0]->path());
313
314   plan_.EdgeFinished(last);
315
316   ASSERT_FALSE(plan_.more_to_do());
317   ASSERT_FALSE(plan_.FindWork());
318 }
319
320 TEST_F(PlanTest, PoolWithRedundantEdges) {
321   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
322     "pool compile\n"
323     "  depth = 1\n"
324     "rule gen_foo\n"
325     "  command = touch foo.cpp\n"
326     "rule gen_bar\n"
327     "  command = touch bar.cpp\n"
328     "rule echo\n"
329     "  command = echo $out > $out\n"
330     "build foo.cpp.obj: echo foo.cpp || foo.cpp\n"
331     "  pool = compile\n"
332     "build bar.cpp.obj: echo bar.cpp || bar.cpp\n"
333     "  pool = compile\n"
334     "build libfoo.a: echo foo.cpp.obj bar.cpp.obj\n"
335     "build foo.cpp: gen_foo\n"
336     "build bar.cpp: gen_bar\n"
337     "build all: phony libfoo.a\n"));
338   GetNode("foo.cpp")->MarkDirty();
339   GetNode("foo.cpp.obj")->MarkDirty();
340   GetNode("bar.cpp")->MarkDirty();
341   GetNode("bar.cpp.obj")->MarkDirty();
342   GetNode("libfoo.a")->MarkDirty();
343   GetNode("all")->MarkDirty();
344   string err;
345   EXPECT_TRUE(plan_.AddTarget(GetNode("all"), &err));
346   ASSERT_EQ("", err);
347   ASSERT_TRUE(plan_.more_to_do());
348
349   Edge* edge = NULL;
350
351   deque<Edge*> initial_edges;
352   FindWorkSorted(&initial_edges, 2);
353
354   edge = initial_edges[1];  // Foo first
355   ASSERT_EQ("foo.cpp", edge->outputs_[0]->path());
356   plan_.EdgeFinished(edge);
357
358   edge = plan_.FindWork();
359   ASSERT_TRUE(edge);
360   ASSERT_FALSE(plan_.FindWork());
361   ASSERT_EQ("foo.cpp", edge->inputs_[0]->path());
362   ASSERT_EQ("foo.cpp", edge->inputs_[1]->path());
363   ASSERT_EQ("foo.cpp.obj", edge->outputs_[0]->path());
364   plan_.EdgeFinished(edge);
365
366   edge = initial_edges[0];  // Now for bar
367   ASSERT_EQ("bar.cpp", edge->outputs_[0]->path());
368   plan_.EdgeFinished(edge);
369
370   edge = plan_.FindWork();
371   ASSERT_TRUE(edge);
372   ASSERT_FALSE(plan_.FindWork());
373   ASSERT_EQ("bar.cpp", edge->inputs_[0]->path());
374   ASSERT_EQ("bar.cpp", edge->inputs_[1]->path());
375   ASSERT_EQ("bar.cpp.obj", edge->outputs_[0]->path());
376   plan_.EdgeFinished(edge);
377
378   edge = plan_.FindWork();
379   ASSERT_TRUE(edge);
380   ASSERT_FALSE(plan_.FindWork());
381   ASSERT_EQ("foo.cpp.obj", edge->inputs_[0]->path());
382   ASSERT_EQ("bar.cpp.obj", edge->inputs_[1]->path());
383   ASSERT_EQ("libfoo.a", edge->outputs_[0]->path());
384   plan_.EdgeFinished(edge);
385
386   edge = plan_.FindWork();
387   ASSERT_TRUE(edge);
388   ASSERT_FALSE(plan_.FindWork());
389   ASSERT_EQ("libfoo.a", edge->inputs_[0]->path());
390   ASSERT_EQ("all", edge->outputs_[0]->path());
391   plan_.EdgeFinished(edge);
392
393   edge = plan_.FindWork();
394   ASSERT_FALSE(edge);
395   ASSERT_FALSE(plan_.more_to_do());
396 }
397
398 /// Fake implementation of CommandRunner, useful for tests.
399 struct FakeCommandRunner : public CommandRunner {
400   explicit FakeCommandRunner(VirtualFileSystem* fs) :
401       last_command_(NULL), fs_(fs) {}
402
403   // CommandRunner impl
404   virtual bool CanRunMore();
405   virtual bool StartCommand(Edge* edge);
406   virtual bool WaitForCommand(Result* result);
407   virtual vector<Edge*> GetActiveEdges();
408   virtual void Abort();
409
410   vector<string> commands_ran_;
411   Edge* last_command_;
412   VirtualFileSystem* fs_;
413 };
414
415 struct BuildTest : public StateTestWithBuiltinRules {
416   BuildTest() : config_(MakeConfig()), command_runner_(&fs_),
417                 builder_(&state_, config_, NULL, NULL, &fs_),
418                 status_(config_) {
419   }
420
421   virtual void SetUp() {
422     StateTestWithBuiltinRules::SetUp();
423
424     builder_.command_runner_.reset(&command_runner_);
425     AssertParse(&state_,
426 "build cat1: cat in1\n"
427 "build cat2: cat in1 in2\n"
428 "build cat12: cat cat1 cat2\n");
429
430     fs_.Create("in1", "");
431     fs_.Create("in2", "");
432   }
433
434   ~BuildTest() {
435     builder_.command_runner_.release();
436   }
437
438   // Mark a path dirty.
439   void Dirty(const string& path);
440
441   BuildConfig MakeConfig() {
442     BuildConfig config;
443     config.verbosity = BuildConfig::QUIET;
444     return config;
445   }
446
447   BuildConfig config_;
448   FakeCommandRunner command_runner_;
449   VirtualFileSystem fs_;
450   Builder builder_;
451
452   BuildStatus status_;
453 };
454
455 bool FakeCommandRunner::CanRunMore() {
456   // Only run one at a time.
457   return last_command_ == NULL;
458 }
459
460 bool FakeCommandRunner::StartCommand(Edge* edge) {
461   assert(!last_command_);
462   commands_ran_.push_back(edge->EvaluateCommand());
463   if (edge->rule().name() == "cat"  ||
464       edge->rule().name() == "cat_rsp" ||
465       edge->rule().name() == "cc" ||
466       edge->rule().name() == "touch" ||
467       edge->rule().name() == "touch-interrupt") {
468     for (vector<Node*>::iterator out = edge->outputs_.begin();
469          out != edge->outputs_.end(); ++out) {
470       fs_->Create((*out)->path(), "");
471     }
472   } else if (edge->rule().name() == "true" ||
473              edge->rule().name() == "fail" ||
474              edge->rule().name() == "interrupt") {
475     // Don't do anything.
476   } else {
477     printf("unknown command\n");
478     return false;
479   }
480
481   last_command_ = edge;
482   return true;
483 }
484
485 bool FakeCommandRunner::WaitForCommand(Result* result) {
486   if (!last_command_)
487     return false;
488
489   Edge* edge = last_command_;
490   result->edge = edge;
491
492   if (edge->rule().name() == "interrupt" ||
493       edge->rule().name() == "touch-interrupt") {
494     result->status = ExitInterrupted;
495     return true;
496   }
497
498   if (edge->rule().name() == "fail")
499     result->status = ExitFailure;
500   else
501     result->status = ExitSuccess;
502   last_command_ = NULL;
503   return true;
504 }
505
506 vector<Edge*> FakeCommandRunner::GetActiveEdges() {
507   vector<Edge*> edges;
508   if (last_command_)
509     edges.push_back(last_command_);
510   return edges;
511 }
512
513 void FakeCommandRunner::Abort() {
514   last_command_ = NULL;
515 }
516
517 void BuildTest::Dirty(const string& path) {
518   Node* node = GetNode(path);
519   node->MarkDirty();
520
521   // If it's an input file, mark that we've already stat()ed it and
522   // it's missing.
523   if (!node->in_edge())
524     node->MarkMissing();
525 }
526
527 TEST_F(BuildTest, NoWork) {
528   string err;
529   EXPECT_TRUE(builder_.AlreadyUpToDate());
530 }
531
532 TEST_F(BuildTest, OneStep) {
533   // Given a dirty target with one ready input,
534   // we should rebuild the target.
535   Dirty("cat1");
536   string err;
537   EXPECT_TRUE(builder_.AddTarget("cat1", &err));
538   ASSERT_EQ("", err);
539   EXPECT_TRUE(builder_.Build(&err));
540   ASSERT_EQ("", err);
541
542   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
543   EXPECT_EQ("cat in1 > cat1", command_runner_.commands_ran_[0]);
544 }
545
546 TEST_F(BuildTest, OneStep2) {
547   // Given a target with one dirty input,
548   // we should rebuild the target.
549   Dirty("cat1");
550   string err;
551   EXPECT_TRUE(builder_.AddTarget("cat1", &err));
552   ASSERT_EQ("", err);
553   EXPECT_TRUE(builder_.Build(&err));
554   EXPECT_EQ("", err);
555
556   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
557   EXPECT_EQ("cat in1 > cat1", command_runner_.commands_ran_[0]);
558 }
559
560 TEST_F(BuildTest, TwoStep) {
561   string err;
562   EXPECT_TRUE(builder_.AddTarget("cat12", &err));
563   ASSERT_EQ("", err);
564   EXPECT_TRUE(builder_.Build(&err));
565   EXPECT_EQ("", err);
566   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
567   // Depending on how the pointers work out, we could've ran
568   // the first two commands in either order.
569   EXPECT_TRUE((command_runner_.commands_ran_[0] == "cat in1 > cat1" &&
570                command_runner_.commands_ran_[1] == "cat in1 in2 > cat2") ||
571               (command_runner_.commands_ran_[1] == "cat in1 > cat1" &&
572                command_runner_.commands_ran_[0] == "cat in1 in2 > cat2"));
573
574   EXPECT_EQ("cat cat1 cat2 > cat12", command_runner_.commands_ran_[2]);
575
576   fs_.Tick();
577
578   // Modifying in2 requires rebuilding one intermediate file
579   // and the final file.
580   fs_.Create("in2", "");
581   state_.Reset();
582   EXPECT_TRUE(builder_.AddTarget("cat12", &err));
583   ASSERT_EQ("", err);
584   EXPECT_TRUE(builder_.Build(&err));
585   ASSERT_EQ("", err);
586   ASSERT_EQ(5u, command_runner_.commands_ran_.size());
587   EXPECT_EQ("cat in1 in2 > cat2", command_runner_.commands_ran_[3]);
588   EXPECT_EQ("cat cat1 cat2 > cat12", command_runner_.commands_ran_[4]);
589 }
590
591 TEST_F(BuildTest, TwoOutputs) {
592   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
593 "rule touch\n"
594 "  command = touch $out\n"
595 "build out1 out2: touch in.txt\n"));
596
597   fs_.Create("in.txt", "");
598
599   string err;
600   EXPECT_TRUE(builder_.AddTarget("out1", &err));
601   ASSERT_EQ("", err);
602   EXPECT_TRUE(builder_.Build(&err));
603   EXPECT_EQ("", err);
604   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
605   EXPECT_EQ("touch out1 out2", command_runner_.commands_ran_[0]);
606 }
607
608 // Test case from
609 //   https://github.com/martine/ninja/issues/148
610 TEST_F(BuildTest, MultiOutIn) {
611   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
612 "rule touch\n"
613 "  command = touch $out\n"
614 "build in1 otherfile: touch in\n"
615 "build out: touch in | in1\n"));
616
617   fs_.Create("in", "");
618   fs_.Tick();
619   fs_.Create("in1", "");
620
621   string err;
622   EXPECT_TRUE(builder_.AddTarget("out", &err));
623   ASSERT_EQ("", err);
624   EXPECT_TRUE(builder_.Build(&err));
625   EXPECT_EQ("", err);
626 }
627
628 TEST_F(BuildTest, Chain) {
629   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
630 "build c2: cat c1\n"
631 "build c3: cat c2\n"
632 "build c4: cat c3\n"
633 "build c5: cat c4\n"));
634
635   fs_.Create("c1", "");
636
637   string err;
638   EXPECT_TRUE(builder_.AddTarget("c5", &err));
639   ASSERT_EQ("", err);
640   EXPECT_TRUE(builder_.Build(&err));
641   EXPECT_EQ("", err);
642   ASSERT_EQ(4u, command_runner_.commands_ran_.size());
643
644   err.clear();
645   command_runner_.commands_ran_.clear();
646   state_.Reset();
647   EXPECT_TRUE(builder_.AddTarget("c5", &err));
648   ASSERT_EQ("", err);
649   EXPECT_TRUE(builder_.AlreadyUpToDate());
650
651   fs_.Tick();
652
653   fs_.Create("c3", "");
654   err.clear();
655   command_runner_.commands_ran_.clear();
656   state_.Reset();
657   EXPECT_TRUE(builder_.AddTarget("c5", &err));
658   ASSERT_EQ("", err);
659   EXPECT_FALSE(builder_.AlreadyUpToDate());
660   EXPECT_TRUE(builder_.Build(&err));
661   ASSERT_EQ(2u, command_runner_.commands_ran_.size());  // 3->4, 4->5
662 }
663
664 TEST_F(BuildTest, MissingInput) {
665   // Input is referenced by build file, but no rule for it.
666   string err;
667   Dirty("in1");
668   EXPECT_FALSE(builder_.AddTarget("cat1", &err));
669   EXPECT_EQ("'in1', needed by 'cat1', missing and no known rule to make it",
670             err);
671 }
672
673 TEST_F(BuildTest, MissingTarget) {
674   // Target is not referenced by build file.
675   string err;
676   EXPECT_FALSE(builder_.AddTarget("meow", &err));
677   EXPECT_EQ("unknown target: 'meow'", err);
678 }
679
680 TEST_F(BuildTest, MakeDirs) {
681   string err;
682
683 #ifdef _WIN32
684   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
685                                       "build subdir\\dir2\\file: cat in1\n"));
686   EXPECT_TRUE(builder_.AddTarget("subdir\\dir2\\file", &err));
687 #else
688   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
689                                       "build subdir/dir2/file: cat in1\n"));
690   EXPECT_TRUE(builder_.AddTarget("subdir/dir2/file", &err));
691 #endif
692
693   EXPECT_EQ("", err);
694   EXPECT_TRUE(builder_.Build(&err));
695   ASSERT_EQ("", err);
696   ASSERT_EQ(2u, fs_.directories_made_.size());
697   EXPECT_EQ("subdir", fs_.directories_made_[0]);
698 #ifdef _WIN32
699   EXPECT_EQ("subdir\\dir2", fs_.directories_made_[1]);
700 #else
701   EXPECT_EQ("subdir/dir2", fs_.directories_made_[1]);
702 #endif
703 }
704
705 TEST_F(BuildTest, DepFileMissing) {
706   string err;
707   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
708 "rule cc\n  command = cc $in\n  depfile = $out.d\n"
709 "build foo.o: cc foo.c\n"));
710   fs_.Create("foo.c", "");
711
712   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
713   ASSERT_EQ("", err);
714   ASSERT_EQ(1u, fs_.files_read_.size());
715   EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
716 }
717
718 TEST_F(BuildTest, DepFileOK) {
719   string err;
720   int orig_edges = state_.edges_.size();
721   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
722 "rule cc\n  command = cc $in\n  depfile = $out.d\n"
723 "build foo.o: cc foo.c\n"));
724   Edge* edge = state_.edges_.back();
725
726   fs_.Create("foo.c", "");
727   GetNode("bar.h")->MarkDirty();  // Mark bar.h as missing.
728   fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
729   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
730   ASSERT_EQ("", err);
731   ASSERT_EQ(1u, fs_.files_read_.size());
732   EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
733
734   // Expect three new edges: one generating foo.o, and two more from
735   // loading the depfile.
736   ASSERT_EQ(orig_edges + 3, (int)state_.edges_.size());
737   // Expect our edge to now have three inputs: foo.c and two headers.
738   ASSERT_EQ(3u, edge->inputs_.size());
739
740   // Expect the command line we generate to only use the original input.
741   ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
742 }
743
744 TEST_F(BuildTest, DepFileParseError) {
745   string err;
746   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
747 "rule cc\n  command = cc $in\n  depfile = $out.d\n"
748 "build foo.o: cc foo.c\n"));
749   fs_.Create("foo.c", "");
750   fs_.Create("foo.o.d", "randomtext\n");
751   EXPECT_FALSE(builder_.AddTarget("foo.o", &err));
752   EXPECT_EQ("expected depfile 'foo.o.d' to mention 'foo.o', got 'randomtext'",
753             err);
754 }
755
756 TEST_F(BuildTest, OrderOnlyDeps) {
757   string err;
758   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
759 "rule cc\n  command = cc $in\n  depfile = $out.d\n"
760 "build foo.o: cc foo.c || otherfile\n"));
761   Edge* edge = state_.edges_.back();
762
763   fs_.Create("foo.c", "");
764   fs_.Create("otherfile", "");
765   fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
766   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
767   ASSERT_EQ("", err);
768
769   // One explicit, two implicit, one order only.
770   ASSERT_EQ(4u, edge->inputs_.size());
771   EXPECT_EQ(2, edge->implicit_deps_);
772   EXPECT_EQ(1, edge->order_only_deps_);
773   // Verify the inputs are in the order we expect
774   // (explicit then implicit then orderonly).
775   EXPECT_EQ("foo.c", edge->inputs_[0]->path());
776   EXPECT_EQ("blah.h", edge->inputs_[1]->path());
777   EXPECT_EQ("bar.h", edge->inputs_[2]->path());
778   EXPECT_EQ("otherfile", edge->inputs_[3]->path());
779
780   // Expect the command line we generate to only use the original input.
781   ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
782
783   // explicit dep dirty, expect a rebuild.
784   EXPECT_TRUE(builder_.Build(&err));
785   ASSERT_EQ("", err);
786   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
787
788   fs_.Tick();
789
790   // Recreate the depfile, as it should have been deleted by the build.
791   fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
792
793   // implicit dep dirty, expect a rebuild.
794   fs_.Create("blah.h", "");
795   fs_.Create("bar.h", "");
796   command_runner_.commands_ran_.clear();
797   state_.Reset();
798   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
799   EXPECT_TRUE(builder_.Build(&err));
800   ASSERT_EQ("", err);
801   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
802
803   fs_.Tick();
804
805   // Recreate the depfile, as it should have been deleted by the build.
806   fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
807
808   // order only dep dirty, no rebuild.
809   fs_.Create("otherfile", "");
810   command_runner_.commands_ran_.clear();
811   state_.Reset();
812   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
813   EXPECT_EQ("", err);
814   EXPECT_TRUE(builder_.AlreadyUpToDate());
815
816   // implicit dep missing, expect rebuild.
817   fs_.RemoveFile("bar.h");
818   command_runner_.commands_ran_.clear();
819   state_.Reset();
820   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
821   EXPECT_TRUE(builder_.Build(&err));
822   ASSERT_EQ("", err);
823   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
824 }
825
826 TEST_F(BuildTest, RebuildOrderOnlyDeps) {
827   string err;
828   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
829 "rule cc\n  command = cc $in\n"
830 "rule true\n  command = true\n"
831 "build oo.h: cc oo.h.in\n"
832 "build foo.o: cc foo.c || oo.h\n"));
833
834   fs_.Create("foo.c", "");
835   fs_.Create("oo.h.in", "");
836
837   // foo.o and order-only dep dirty, build both.
838   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
839   EXPECT_TRUE(builder_.Build(&err));
840   ASSERT_EQ("", err);
841   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
842
843   // all clean, no rebuild.
844   command_runner_.commands_ran_.clear();
845   state_.Reset();
846   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
847   EXPECT_EQ("", err);
848   EXPECT_TRUE(builder_.AlreadyUpToDate());
849
850   // order-only dep missing, build it only.
851   fs_.RemoveFile("oo.h");
852   command_runner_.commands_ran_.clear();
853   state_.Reset();
854   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
855   EXPECT_TRUE(builder_.Build(&err));
856   ASSERT_EQ("", err);
857   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
858   ASSERT_EQ("cc oo.h.in", command_runner_.commands_ran_[0]);
859
860   fs_.Tick();
861
862   // order-only dep dirty, build it only.
863   fs_.Create("oo.h.in", "");
864   command_runner_.commands_ran_.clear();
865   state_.Reset();
866   EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
867   EXPECT_TRUE(builder_.Build(&err));
868   ASSERT_EQ("", err);
869   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
870   ASSERT_EQ("cc oo.h.in", command_runner_.commands_ran_[0]);
871 }
872
873 TEST_F(BuildTest, Phony) {
874   string err;
875   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
876 "build out: cat bar.cc\n"
877 "build all: phony out\n"));
878   fs_.Create("bar.cc", "");
879
880   EXPECT_TRUE(builder_.AddTarget("all", &err));
881   ASSERT_EQ("", err);
882
883   // Only one command to run, because phony runs no command.
884   EXPECT_FALSE(builder_.AlreadyUpToDate());
885   EXPECT_TRUE(builder_.Build(&err));
886   ASSERT_EQ("", err);
887   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
888 }
889
890 TEST_F(BuildTest, PhonyNoWork) {
891   string err;
892   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
893 "build out: cat bar.cc\n"
894 "build all: phony out\n"));
895   fs_.Create("bar.cc", "");
896   fs_.Create("out", "");
897
898   EXPECT_TRUE(builder_.AddTarget("all", &err));
899   ASSERT_EQ("", err);
900   EXPECT_TRUE(builder_.AlreadyUpToDate());
901 }
902
903 TEST_F(BuildTest, Fail) {
904   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
905 "rule fail\n"
906 "  command = fail\n"
907 "build out1: fail\n"));
908
909   string err;
910   EXPECT_TRUE(builder_.AddTarget("out1", &err));
911   ASSERT_EQ("", err);
912
913   EXPECT_FALSE(builder_.Build(&err));
914   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
915   ASSERT_EQ("subcommand failed", err);
916 }
917
918 TEST_F(BuildTest, SwallowFailures) {
919   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
920 "rule fail\n"
921 "  command = fail\n"
922 "build out1: fail\n"
923 "build out2: fail\n"
924 "build out3: fail\n"
925 "build all: phony out1 out2 out3\n"));
926
927   // Swallow two failures, die on the third.
928   config_.failures_allowed = 3;
929
930   string err;
931   EXPECT_TRUE(builder_.AddTarget("all", &err));
932   ASSERT_EQ("", err);
933
934   EXPECT_FALSE(builder_.Build(&err));
935   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
936   ASSERT_EQ("subcommands failed", err);
937 }
938
939 TEST_F(BuildTest, SwallowFailuresLimit) {
940   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
941 "rule fail\n"
942 "  command = fail\n"
943 "build out1: fail\n"
944 "build out2: fail\n"
945 "build out3: fail\n"
946 "build final: cat out1 out2 out3\n"));
947
948   // Swallow ten failures; we should stop before building final.
949   config_.failures_allowed = 11;
950
951   string err;
952   EXPECT_TRUE(builder_.AddTarget("final", &err));
953   ASSERT_EQ("", err);
954
955   EXPECT_FALSE(builder_.Build(&err));
956   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
957   ASSERT_EQ("cannot make progress due to previous errors", err);
958 }
959
960 struct BuildWithLogTest : public BuildTest {
961   BuildWithLogTest() {
962     builder_.SetBuildLog(&build_log_);
963   }
964
965   BuildLog build_log_;
966 };
967
968 TEST_F(BuildWithLogTest, NotInLogButOnDisk) {
969   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
970 "rule cc\n"
971 "  command = cc\n"
972 "build out1: cc in\n"));
973
974   // Create input/output that would be considered up to date when
975   // not considering the command line hash.
976   fs_.Create("in", "");
977   fs_.Create("out1", "");
978   string err;
979
980   // Because it's not in the log, it should not be up-to-date until
981   // we build again.
982   EXPECT_TRUE(builder_.AddTarget("out1", &err));
983   EXPECT_FALSE(builder_.AlreadyUpToDate());
984
985   command_runner_.commands_ran_.clear();
986   state_.Reset();
987
988   EXPECT_TRUE(builder_.AddTarget("out1", &err));
989   EXPECT_TRUE(builder_.Build(&err));
990   EXPECT_TRUE(builder_.AlreadyUpToDate());
991 }
992
993 TEST_F(BuildWithLogTest, RestatTest) {
994   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
995 "rule true\n"
996 "  command = true\n"
997 "  restat = 1\n"
998 "rule cc\n"
999 "  command = cc\n"
1000 "  restat = 1\n"
1001 "build out1: cc in\n"
1002 "build out2: true out1\n"
1003 "build out3: cat out2\n"));
1004
1005   fs_.Create("out1", "");
1006   fs_.Create("out2", "");
1007   fs_.Create("out3", "");
1008
1009   fs_.Tick();
1010
1011   fs_.Create("in", "");
1012
1013   // Do a pre-build so that there's commands in the log for the outputs,
1014   // otherwise, the lack of an entry in the build log will cause out3 to rebuild
1015   // regardless of restat.
1016   string err;
1017   EXPECT_TRUE(builder_.AddTarget("out3", &err));
1018   ASSERT_EQ("", err);
1019   EXPECT_TRUE(builder_.Build(&err));
1020   ASSERT_EQ("", err);
1021   EXPECT_EQ("[3/3]", builder_.status_->FormatProgressStatus("[%s/%t]"));
1022   command_runner_.commands_ran_.clear();
1023   state_.Reset();
1024
1025   fs_.Tick();
1026
1027   fs_.Create("in", "");
1028   // "cc" touches out1, so we should build out2.  But because "true" does not
1029   // touch out2, we should cancel the build of out3.
1030   EXPECT_TRUE(builder_.AddTarget("out3", &err));
1031   ASSERT_EQ("", err);
1032   EXPECT_TRUE(builder_.Build(&err));
1033   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1034
1035   // If we run again, it should be a no-op, because the build log has recorded
1036   // that we've already built out2 with an input timestamp of 2 (from out1).
1037   command_runner_.commands_ran_.clear();
1038   state_.Reset();
1039   EXPECT_TRUE(builder_.AddTarget("out3", &err));
1040   ASSERT_EQ("", err);
1041   EXPECT_TRUE(builder_.AlreadyUpToDate());
1042
1043   fs_.Tick();
1044
1045   fs_.Create("in", "");
1046
1047   // The build log entry should not, however, prevent us from rebuilding out2
1048   // if out1 changes.
1049   command_runner_.commands_ran_.clear();
1050   state_.Reset();
1051   EXPECT_TRUE(builder_.AddTarget("out3", &err));
1052   ASSERT_EQ("", err);
1053   EXPECT_TRUE(builder_.Build(&err));
1054   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1055 }
1056
1057 TEST_F(BuildWithLogTest, RestatMissingFile) {
1058   // If a restat rule doesn't create its output, and the output didn't
1059   // exist before the rule was run, consider that behavior equivalent
1060   // to a rule that doesn't modify its existent output file.
1061
1062   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1063 "rule true\n"
1064 "  command = true\n"
1065 "  restat = 1\n"
1066 "rule cc\n"
1067 "  command = cc\n"
1068 "build out1: true in\n"
1069 "build out2: cc out1\n"));
1070
1071   fs_.Create("in", "");
1072   fs_.Create("out2", "");
1073
1074   // Do a pre-build so that there's commands in the log for the outputs,
1075   // otherwise, the lack of an entry in the build log will cause out2 to rebuild
1076   // regardless of restat.
1077   string err;
1078   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1079   ASSERT_EQ("", err);
1080   EXPECT_TRUE(builder_.Build(&err));
1081   ASSERT_EQ("", err);
1082   command_runner_.commands_ran_.clear();
1083   state_.Reset();
1084
1085   fs_.Tick();
1086   fs_.Create("in", "");
1087   fs_.Create("out2", "");
1088
1089   // Run a build, expect only the first command to run.
1090   // It doesn't touch its output (due to being the "true" command), so
1091   // we shouldn't run the dependent build.
1092   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1093   ASSERT_EQ("", err);
1094   EXPECT_TRUE(builder_.Build(&err));
1095   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1096 }
1097
1098 TEST_F(BuildWithLogTest, RestatSingleDependentOutputDirty) {
1099   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1100     "rule true\n"
1101     "  command = true\n"
1102     "  restat = 1\n"
1103     "rule touch\n"
1104     "  command = touch\n"
1105     "build out1: true in\n"
1106     "build out2 out3: touch out1\n"
1107     "build out4: touch out2\n"
1108     ));
1109
1110   // Create the necessary files
1111   fs_.Create("in", "");
1112
1113   string err;
1114   EXPECT_TRUE(builder_.AddTarget("out4", &err));
1115   ASSERT_EQ("", err);
1116   EXPECT_TRUE(builder_.Build(&err));
1117   ASSERT_EQ("", err);
1118   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1119
1120   fs_.Tick();
1121   fs_.Create("in", "");
1122   fs_.RemoveFile("out3");
1123
1124   // Since "in" is missing, out1 will be built. Since "out3" is missing,
1125   // out2 and out3 will be built even though "in" is not touched when built.
1126   // Then, since out2 is rebuilt, out4 should be rebuilt -- the restat on the
1127   // "true" rule should not lead to the "touch" edge writing out2 and out3 being
1128   // cleard.
1129   command_runner_.commands_ran_.clear();
1130   state_.Reset();
1131   EXPECT_TRUE(builder_.AddTarget("out4", &err));
1132   ASSERT_EQ("", err);
1133   EXPECT_TRUE(builder_.Build(&err));
1134   ASSERT_EQ("", err);
1135   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1136 }
1137
1138 // Test scenario, in which an input file is removed, but output isn't changed
1139 // https://github.com/martine/ninja/issues/295
1140 TEST_F(BuildWithLogTest, RestatMissingInput) {
1141   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1142     "rule true\n"
1143     "  command = true\n"
1144     "  depfile = $out.d\n"
1145     "  restat = 1\n"
1146     "rule cc\n"
1147     "  command = cc\n"
1148     "build out1: true in\n"
1149     "build out2: cc out1\n"));
1150
1151   // Create all necessary files
1152   fs_.Create("in", "");
1153
1154   // The implicit dependencies and the depfile itself
1155   // are newer than the output
1156   TimeStamp restat_mtime = fs_.Tick();
1157   fs_.Create("out1.d", "out1: will.be.deleted restat.file\n");
1158   fs_.Create("will.be.deleted", "");
1159   fs_.Create("restat.file", "");
1160
1161   // Run the build, out1 and out2 get built
1162   string err;
1163   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1164   ASSERT_EQ("", err);
1165   EXPECT_TRUE(builder_.Build(&err));
1166   ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1167
1168   // See that an entry in the logfile is created, capturing
1169   // the right mtime
1170   BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out1");
1171   ASSERT_TRUE(NULL != log_entry);
1172   ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
1173
1174   // Now remove a file, referenced from depfile, so that target becomes
1175   // dirty, but the output does not change
1176   fs_.RemoveFile("will.be.deleted");
1177
1178   // Trigger the build again - only out1 gets built
1179   command_runner_.commands_ran_.clear();
1180   state_.Reset();
1181   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1182   ASSERT_EQ("", err);
1183   EXPECT_TRUE(builder_.Build(&err));
1184   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1185
1186   // Check that the logfile entry remains correctly set
1187   log_entry = build_log_.LookupByOutput("out1");
1188   ASSERT_TRUE(NULL != log_entry);
1189   ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
1190 }
1191
1192 struct BuildDryRun : public BuildWithLogTest {
1193   BuildDryRun() {
1194     config_.dry_run = true;
1195   }
1196 };
1197
1198 TEST_F(BuildDryRun, AllCommandsShown) {
1199   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1200 "rule true\n"
1201 "  command = true\n"
1202 "  restat = 1\n"
1203 "rule cc\n"
1204 "  command = cc\n"
1205 "  restat = 1\n"
1206 "build out1: cc in\n"
1207 "build out2: true out1\n"
1208 "build out3: cat out2\n"));
1209
1210   fs_.Create("out1", "");
1211   fs_.Create("out2", "");
1212   fs_.Create("out3", "");
1213
1214   fs_.Tick();
1215
1216   fs_.Create("in", "");
1217
1218   // "cc" touches out1, so we should build out2.  But because "true" does not
1219   // touch out2, we should cancel the build of out3.
1220   string err;
1221   EXPECT_TRUE(builder_.AddTarget("out3", &err));
1222   ASSERT_EQ("", err);
1223   EXPECT_TRUE(builder_.Build(&err));
1224   ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1225 }
1226
1227 // Test that RSP files are created when & where appropriate and deleted after
1228 // successful execution.
1229 TEST_F(BuildTest, RspFileSuccess)
1230 {
1231   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1232     "rule cat_rsp\n"
1233     "  command = cat $rspfile > $out\n"
1234     "  rspfile = $rspfile\n"
1235     "  rspfile_content = $long_command\n"
1236     "build out1: cat in\n"
1237     "build out2: cat_rsp in\n"
1238     "  rspfile = out2.rsp\n"
1239     "  long_command = Some very long command\n"));
1240
1241   fs_.Create("out1", "");
1242   fs_.Create("out2", "");
1243   fs_.Create("out3", "");
1244
1245   fs_.Tick();
1246
1247   fs_.Create("in", "");
1248
1249   string err;
1250   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1251   ASSERT_EQ("", err);
1252   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1253   ASSERT_EQ("", err);
1254
1255   size_t files_created = fs_.files_created_.size();
1256   size_t files_removed = fs_.files_removed_.size();
1257
1258   EXPECT_TRUE(builder_.Build(&err));
1259   ASSERT_EQ(2u, command_runner_.commands_ran_.size()); // cat + cat_rsp
1260
1261   // The RSP file was created
1262   ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1263   ASSERT_EQ(1u, fs_.files_created_.count("out2.rsp"));
1264
1265   // The RSP file was removed
1266   ASSERT_EQ(files_removed + 1, fs_.files_removed_.size());
1267   ASSERT_EQ(1u, fs_.files_removed_.count("out2.rsp"));
1268 }
1269
1270 // Test that RSP file is created but not removed for commands, which fail
1271 TEST_F(BuildTest, RspFileFailure) {
1272   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1273     "rule fail\n"
1274     "  command = fail\n"
1275     "  rspfile = $rspfile\n"
1276     "  rspfile_content = $long_command\n"
1277     "build out: fail in\n"
1278     "  rspfile = out.rsp\n"
1279     "  long_command = Another very long command\n"));
1280
1281   fs_.Create("out", "");
1282   fs_.Tick();
1283   fs_.Create("in", "");
1284
1285   string err;
1286   EXPECT_TRUE(builder_.AddTarget("out", &err));
1287   ASSERT_EQ("", err);
1288
1289   size_t files_created = fs_.files_created_.size();
1290   size_t files_removed = fs_.files_removed_.size();
1291
1292   EXPECT_FALSE(builder_.Build(&err));
1293   ASSERT_EQ("subcommand failed", err);
1294   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1295
1296   // The RSP file was created
1297   ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1298   ASSERT_EQ(1u, fs_.files_created_.count("out.rsp"));
1299
1300   // The RSP file was NOT removed
1301   ASSERT_EQ(files_removed, fs_.files_removed_.size());
1302   ASSERT_EQ(0u, fs_.files_removed_.count("out.rsp"));
1303
1304   // The RSP file contains what it should
1305   ASSERT_EQ("Another very long command", fs_.files_["out.rsp"].contents);
1306 }
1307
1308 // Test that contens of the RSP file behaves like a regular part of
1309 // command line, i.e. triggers a rebuild if changed
1310 TEST_F(BuildWithLogTest, RspFileCmdLineChange) {
1311   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1312     "rule cat_rsp\n"
1313     "  command = cat $rspfile > $out\n"
1314     "  rspfile = $rspfile\n"
1315     "  rspfile_content = $long_command\n"
1316     "build out: cat_rsp in\n"
1317     "  rspfile = out.rsp\n"
1318     "  long_command = Original very long command\n"));
1319
1320   fs_.Create("out", "");
1321   fs_.Tick();
1322   fs_.Create("in", "");
1323
1324   string err;
1325   EXPECT_TRUE(builder_.AddTarget("out", &err));
1326   ASSERT_EQ("", err);
1327
1328   // 1. Build for the 1st time (-> populate log)
1329   EXPECT_TRUE(builder_.Build(&err));
1330   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1331
1332   // 2. Build again (no change)
1333   command_runner_.commands_ran_.clear();
1334   state_.Reset();
1335   EXPECT_TRUE(builder_.AddTarget("out", &err));
1336   EXPECT_EQ("", err);
1337   ASSERT_TRUE(builder_.AlreadyUpToDate());
1338
1339   // 3. Alter the entry in the logfile
1340   // (to simulate a change in the command line between 2 builds)
1341   BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out");
1342   ASSERT_TRUE(NULL != log_entry);
1343   ASSERT_NO_FATAL_FAILURE(AssertHash(
1344         "cat out.rsp > out;rspfile=Original very long command",
1345         log_entry->command_hash));
1346   log_entry->command_hash++;  // Change the command hash to something else.
1347   // Now expect the target to be rebuilt
1348   command_runner_.commands_ran_.clear();
1349   state_.Reset();
1350   EXPECT_TRUE(builder_.AddTarget("out", &err));
1351   EXPECT_EQ("", err);
1352   EXPECT_TRUE(builder_.Build(&err));
1353   EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1354 }
1355
1356 TEST_F(BuildTest, InterruptCleanup) {
1357   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1358 "rule interrupt\n"
1359 "  command = interrupt\n"
1360 "rule touch-interrupt\n"
1361 "  command = touch-interrupt\n"
1362 "build out1: interrupt in1\n"
1363 "build out2: touch-interrupt in2\n"));
1364
1365   fs_.Create("out1", "");
1366   fs_.Create("out2", "");
1367   fs_.Tick();
1368   fs_.Create("in1", "");
1369   fs_.Create("in2", "");
1370
1371   // An untouched output of an interrupted command should be retained.
1372   string err;
1373   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1374   EXPECT_EQ("", err);
1375   EXPECT_FALSE(builder_.Build(&err));
1376   EXPECT_EQ("interrupted by user", err);
1377   builder_.Cleanup();
1378   EXPECT_GT(fs_.Stat("out1"), 0);
1379   err = "";
1380
1381   // A touched output of an interrupted command should be deleted.
1382   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1383   EXPECT_EQ("", err);
1384   EXPECT_FALSE(builder_.Build(&err));
1385   EXPECT_EQ("interrupted by user", err);
1386   builder_.Cleanup();
1387   EXPECT_EQ(0, fs_.Stat("out2"));
1388 }
1389
1390 TEST_F(BuildTest, PhonyWithNoInputs) {
1391   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1392 "build nonexistent: phony\n"
1393 "build out1: cat || nonexistent\n"
1394 "build out2: cat nonexistent\n"));
1395   fs_.Create("out1", "");
1396   fs_.Create("out2", "");
1397
1398   // out1 should be up to date even though its input is dirty, because its
1399   // order-only dependency has nothing to do.
1400   string err;
1401   EXPECT_TRUE(builder_.AddTarget("out1", &err));
1402   ASSERT_EQ("", err);
1403   EXPECT_TRUE(builder_.AlreadyUpToDate());
1404
1405   // out2 should still be out of date though, because its input is dirty.
1406   err.clear();
1407   command_runner_.commands_ran_.clear();
1408   state_.Reset();
1409   EXPECT_TRUE(builder_.AddTarget("out2", &err));
1410   ASSERT_EQ("", err);
1411   EXPECT_TRUE(builder_.Build(&err));
1412   EXPECT_EQ("", err);
1413   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1414 }
1415
1416 TEST_F(BuildTest, DepsGccWithEmptyDepfileErrorsOut) {
1417   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1418 "rule cc\n"
1419 "  command = cc\n"
1420 "  deps = gcc\n"
1421 "build out: cc\n"));
1422   Dirty("out");
1423
1424   string err;
1425   EXPECT_TRUE(builder_.AddTarget("out", &err));
1426   ASSERT_EQ("", err);
1427   EXPECT_FALSE(builder_.AlreadyUpToDate());
1428
1429   EXPECT_FALSE(builder_.Build(&err));
1430   ASSERT_EQ("subcommand failed", err);
1431   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1432 }
1433
1434 TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
1435   EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
1436             status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]"));
1437 }
1438
1439 TEST_F(BuildTest, FailedDepsParse) {
1440   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1441 "build bad_deps.o: cat in1\n"
1442 "  deps = gcc\n"
1443 "  depfile = in1.d\n"));
1444
1445   string err;
1446   EXPECT_TRUE(builder_.AddTarget("bad_deps.o", &err));
1447   ASSERT_EQ("", err);
1448
1449   // These deps will fail to parse, as they should only have one
1450   // path to the left of the colon.
1451   fs_.Create("in1.d", "AAA BBB");
1452
1453   EXPECT_FALSE(builder_.Build(&err));
1454   EXPECT_EQ("subcommand failed", err);
1455 }
1456
1457 /// Tests of builds involving deps logs necessarily must span
1458 /// multiple builds.  We reuse methods on BuildTest but not the
1459 /// builder_ it sets up, because we want pristine objects for
1460 /// each build.
1461 struct BuildWithDepsLogTest : public BuildTest {
1462   BuildWithDepsLogTest() {}
1463
1464   virtual void SetUp() {
1465     BuildTest::SetUp();
1466
1467     temp_dir_.CreateAndEnter("BuildWithDepsLogTest");
1468   }
1469
1470   virtual void TearDown() {
1471     temp_dir_.Cleanup();
1472   }
1473
1474   ScopedTempDir temp_dir_;
1475
1476   /// Shadow parent class builder_ so we don't accidentally use it.
1477   void* builder_;
1478 };
1479
1480 /// Run a straightforwad build where the deps log is used.
1481 TEST_F(BuildWithDepsLogTest, Straightforward) {
1482   string err;
1483   // Note: in1 was created by the superclass SetUp().
1484   const char* manifest =
1485       "build out: cat in1\n"
1486       "  deps = gcc\n"
1487       "  depfile = in1.d\n";
1488   {
1489     State state;
1490     ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1491     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1492
1493     // Run the build once, everything should be ok.
1494     DepsLog deps_log;
1495     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1496     ASSERT_EQ("", err);
1497
1498     Builder builder(&state, config_, NULL, &deps_log, &fs_);
1499     builder.command_runner_.reset(&command_runner_);
1500     EXPECT_TRUE(builder.AddTarget("out", &err));
1501     ASSERT_EQ("", err);
1502     fs_.Create("in1.d", "out: in2");
1503     EXPECT_TRUE(builder.Build(&err));
1504     EXPECT_EQ("", err);
1505
1506     // The deps file should have been removed.
1507     EXPECT_EQ(0, fs_.Stat("in1.d"));
1508     // Recreate it for the next step.
1509     fs_.Create("in1.d", "out: in2");
1510     deps_log.Close();
1511     builder.command_runner_.release();
1512   }
1513
1514   {
1515     State state;
1516     ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1517     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1518
1519     // Touch the file only mentioned in the deps.
1520     fs_.Tick();
1521     fs_.Create("in2", "");
1522
1523     // Run the build again.
1524     DepsLog deps_log;
1525     ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1526     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1527
1528     Builder builder(&state, config_, NULL, &deps_log, &fs_);
1529     builder.command_runner_.reset(&command_runner_);
1530     command_runner_.commands_ran_.clear();
1531     EXPECT_TRUE(builder.AddTarget("out", &err));
1532     ASSERT_EQ("", err);
1533     EXPECT_TRUE(builder.Build(&err));
1534     EXPECT_EQ("", err);
1535
1536     // We should have rebuilt the output due to in2 being
1537     // out of date.
1538     EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1539
1540     builder.command_runner_.release();
1541   }
1542 }
1543
1544 /// Verify that obsolete dependency info causes a rebuild.
1545 /// 1) Run a successful build where everything has time t, record deps.
1546 /// 2) Move input/output to time t+1 -- despite files in alignment,
1547 ///    should still need to rebuild due to deps at older time.
1548 TEST_F(BuildWithDepsLogTest, ObsoleteDeps) {
1549   string err;
1550   // Note: in1 was created by the superclass SetUp().
1551   const char* manifest =
1552       "build out: cat in1\n"
1553       "  deps = gcc\n"
1554       "  depfile = in1.d\n";
1555   {
1556     // Run an ordinary build that gathers dependencies.
1557     fs_.Create("in1", "");
1558     fs_.Create("in1.d", "out: ");
1559
1560     State state;
1561     ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1562     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1563
1564     // Run the build once, everything should be ok.
1565     DepsLog deps_log;
1566     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1567     ASSERT_EQ("", err);
1568
1569     Builder builder(&state, config_, NULL, &deps_log, &fs_);
1570     builder.command_runner_.reset(&command_runner_);
1571     EXPECT_TRUE(builder.AddTarget("out", &err));
1572     ASSERT_EQ("", err);
1573     EXPECT_TRUE(builder.Build(&err));
1574     EXPECT_EQ("", err);
1575
1576     deps_log.Close();
1577     builder.command_runner_.release();
1578   }
1579
1580   // Push all files one tick forward so that only the deps are out
1581   // of date.
1582   fs_.Tick();
1583   fs_.Create("in1", "");
1584   fs_.Create("out", "");
1585
1586   // The deps file should have been removed, so no need to timestamp it.
1587   EXPECT_EQ(0, fs_.Stat("in1.d"));
1588
1589   {
1590     State state;
1591     ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1592     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1593
1594     DepsLog deps_log;
1595     ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1596     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1597
1598     Builder builder(&state, config_, NULL, &deps_log, &fs_);
1599     builder.command_runner_.reset(&command_runner_);
1600     command_runner_.commands_ran_.clear();
1601     EXPECT_TRUE(builder.AddTarget("out", &err));
1602     ASSERT_EQ("", err);
1603
1604     // Recreate the deps file here because the build expects them to exist.
1605     fs_.Create("in1.d", "out: ");
1606
1607     EXPECT_TRUE(builder.Build(&err));
1608     EXPECT_EQ("", err);
1609
1610     // We should have rebuilt the output due to the deps being
1611     // out of date.
1612     EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1613
1614     builder.command_runner_.release();
1615   }
1616 }
1617
1618 TEST_F(BuildWithDepsLogTest, DepsIgnoredInDryRun) {
1619   const char* manifest =
1620       "build out: cat in1\n"
1621       "  deps = gcc\n"
1622       "  depfile = in1.d\n";
1623
1624   fs_.Create("out", "");
1625   fs_.Tick();
1626   fs_.Create("in1", "");
1627
1628   State state;
1629   ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1630   ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1631
1632   // The deps log is NULL in dry runs.
1633   config_.dry_run = true;
1634   Builder builder(&state, config_, NULL, NULL, &fs_);
1635   builder.command_runner_.reset(&command_runner_);
1636   command_runner_.commands_ran_.clear();
1637
1638   string err;
1639   EXPECT_TRUE(builder.AddTarget("out", &err));
1640   ASSERT_EQ("", err);
1641   EXPECT_TRUE(builder.Build(&err));
1642   ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1643
1644   builder.command_runner_.release();
1645 }
1646
1647 /// Check that a restat rule generating a header cancels compilations correctly.
1648 TEST_F(BuildTest, RestatDepfileDependency) {
1649   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1650 "rule true\n"
1651 "  command = true\n"  // Would be "write if out-of-date" in reality.
1652 "  restat = 1\n"
1653 "build header.h: true header.in\n"
1654 "build out: cat in1\n"
1655 "  depfile = in1.d\n"));
1656
1657   fs_.Create("header.h", "");
1658   fs_.Create("in1.d", "out: header.h");
1659   fs_.Tick();
1660   fs_.Create("header.in", "");
1661
1662   string err;
1663   EXPECT_TRUE(builder_.AddTarget("out", &err));
1664   ASSERT_EQ("", err);
1665   EXPECT_TRUE(builder_.Build(&err));
1666   EXPECT_EQ("", err);
1667 }
1668
1669 /// Check that a restat rule generating a header cancels compilations correctly,
1670 /// depslog case.
1671 TEST_F(BuildWithDepsLogTest, RestatDepfileDependencyDepsLog) {
1672   string err;
1673   // Note: in1 was created by the superclass SetUp().
1674   const char* manifest =
1675       "rule true\n"
1676       "  command = true\n"  // Would be "write if out-of-date" in reality.
1677       "  restat = 1\n"
1678       "build header.h: true header.in\n"
1679       "build out: cat in1\n"
1680       "  deps = gcc\n"
1681       "  depfile = in1.d\n";
1682   {
1683     State state;
1684     ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1685     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1686
1687     // Run the build once, everything should be ok.
1688     DepsLog deps_log;
1689     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1690     ASSERT_EQ("", err);
1691
1692     Builder builder(&state, config_, NULL, &deps_log, &fs_);
1693     builder.command_runner_.reset(&command_runner_);
1694     EXPECT_TRUE(builder.AddTarget("out", &err));
1695     ASSERT_EQ("", err);
1696     fs_.Create("in1.d", "out: header.h");
1697     EXPECT_TRUE(builder.Build(&err));
1698     EXPECT_EQ("", err);
1699
1700     deps_log.Close();
1701     builder.command_runner_.release();
1702   }
1703
1704   {
1705     State state;
1706     ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1707     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1708
1709     // Touch the input of the restat rule.
1710     fs_.Tick();
1711     fs_.Create("header.in", "");
1712
1713     // Run the build again.
1714     DepsLog deps_log;
1715     ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1716     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1717
1718     Builder builder(&state, config_, NULL, &deps_log, &fs_);
1719     builder.command_runner_.reset(&command_runner_);
1720     command_runner_.commands_ran_.clear();
1721     EXPECT_TRUE(builder.AddTarget("out", &err));
1722     ASSERT_EQ("", err);
1723     EXPECT_TRUE(builder.Build(&err));
1724     EXPECT_EQ("", err);
1725
1726     // Rule "true" should have run again, but the build of "out" should have
1727     // been cancelled due to restat propagating through the depfile header.
1728     EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1729
1730     builder.command_runner_.release();
1731   }
1732 }
1733
1734 TEST_F(BuildWithDepsLogTest, DepFileOKDepsLog) {
1735   string err;
1736   const char* manifest =
1737       "rule cc\n  command = cc $in\n  depfile = $out.d\n  deps = gcc\n"
1738       "build foo.o: cc foo.c\n";
1739
1740   fs_.Create("foo.c", "");
1741
1742   {
1743     State state;
1744     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1745
1746     // Run the build once, everything should be ok.
1747     DepsLog deps_log;
1748     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1749     ASSERT_EQ("", err);
1750
1751     Builder builder(&state, config_, NULL, &deps_log, &fs_);
1752     builder.command_runner_.reset(&command_runner_);
1753     EXPECT_TRUE(builder.AddTarget("foo.o", &err));
1754     ASSERT_EQ("", err);
1755     fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
1756     EXPECT_TRUE(builder.Build(&err));
1757     EXPECT_EQ("", err);
1758
1759     deps_log.Close();
1760     builder.command_runner_.release();
1761   }
1762
1763   {
1764     State state;
1765     ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1766
1767     DepsLog deps_log;
1768     ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1769     ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1770     ASSERT_EQ("", err);
1771
1772     Builder builder(&state, config_, NULL, &deps_log, &fs_);
1773     builder.command_runner_.reset(&command_runner_);
1774
1775     Edge* edge = state.edges_.back();
1776
1777     state.GetNode("bar.h")->MarkDirty();  // Mark bar.h as missing.
1778     EXPECT_TRUE(builder.AddTarget("foo.o", &err));
1779     ASSERT_EQ("", err);
1780
1781     // Expect three new edges: one generating foo.o, and two more from
1782     // loading the depfile.
1783     ASSERT_EQ(3, (int)state.edges_.size());
1784     // Expect our edge to now have three inputs: foo.c and two headers.
1785     ASSERT_EQ(3u, edge->inputs_.size());
1786
1787     // Expect the command line we generate to only use the original input.
1788     ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
1789
1790     deps_log.Close();
1791     builder.command_runner_.release();
1792   }
1793 }