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