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