1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "build_log.h"
22 /// Fixture for tests involving Plan.
23 // Though Plan doesn't use State, it's useful to have one around
24 // to create Nodes and Edges.
25 struct PlanTest : public StateTestWithBuiltinRules {
28 /// Because FindWork does not return Edges in any sort of predictable order,
29 // provide a means to get available Edges in order and in a format which is
30 // easy to write tests around.
31 void FindWorkSorted(deque<Edge*>* ret, int count) {
32 struct CompareEdgesByOutput {
33 static bool cmp(const Edge* a, const Edge* b) {
34 return a->outputs_[0]->path() < b->outputs_[0]->path();
38 for (int i = 0; i < count; ++i) {
39 ASSERT_TRUE(plan_.more_to_do());
40 Edge* edge = plan_.FindWork();
44 ASSERT_FALSE(plan_.FindWork());
45 sort(ret->begin(), ret->end(), CompareEdgesByOutput::cmp);
49 TEST_F(PlanTest, Basic) {
51 "build out: cat mid\n"
52 "build mid: cat in\n");
53 GetNode("mid")->MarkDirty();
54 GetNode("out")->MarkDirty();
56 EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
58 ASSERT_TRUE(plan_.more_to_do());
60 Edge* edge = plan_.FindWork();
62 ASSERT_EQ("in", edge->inputs_[0]->path());
63 ASSERT_EQ("mid", edge->outputs_[0]->path());
65 ASSERT_FALSE(plan_.FindWork());
67 plan_.EdgeFinished(edge);
69 edge = plan_.FindWork();
71 ASSERT_EQ("mid", edge->inputs_[0]->path());
72 ASSERT_EQ("out", edge->outputs_[0]->path());
74 plan_.EdgeFinished(edge);
76 ASSERT_FALSE(plan_.more_to_do());
77 edge = plan_.FindWork();
81 // Test that two outputs from one rule can be handled as inputs to the next.
82 TEST_F(PlanTest, DoubleOutputDirect) {
84 "build out: cat mid1 mid2\n"
85 "build mid1 mid2: cat in\n");
86 GetNode("mid1")->MarkDirty();
87 GetNode("mid2")->MarkDirty();
88 GetNode("out")->MarkDirty();
91 EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
93 ASSERT_TRUE(plan_.more_to_do());
96 edge = plan_.FindWork();
97 ASSERT_TRUE(edge); // cat in
98 plan_.EdgeFinished(edge);
100 edge = plan_.FindWork();
101 ASSERT_TRUE(edge); // cat mid1 mid2
102 plan_.EdgeFinished(edge);
104 edge = plan_.FindWork();
105 ASSERT_FALSE(edge); // done
108 // Test that two outputs from one rule can eventually be routed to another.
109 TEST_F(PlanTest, DoubleOutputIndirect) {
111 "build out: cat b1 b2\n"
114 "build a1 a2: cat in\n");
115 GetNode("a1")->MarkDirty();
116 GetNode("a2")->MarkDirty();
117 GetNode("b1")->MarkDirty();
118 GetNode("b2")->MarkDirty();
119 GetNode("out")->MarkDirty();
121 EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
123 ASSERT_TRUE(plan_.more_to_do());
126 edge = plan_.FindWork();
127 ASSERT_TRUE(edge); // cat in
128 plan_.EdgeFinished(edge);
130 edge = plan_.FindWork();
131 ASSERT_TRUE(edge); // cat a1
132 plan_.EdgeFinished(edge);
134 edge = plan_.FindWork();
135 ASSERT_TRUE(edge); // cat a2
136 plan_.EdgeFinished(edge);
138 edge = plan_.FindWork();
139 ASSERT_TRUE(edge); // cat b1 b2
140 plan_.EdgeFinished(edge);
142 edge = plan_.FindWork();
143 ASSERT_FALSE(edge); // done
146 // Test that two edges from one output can both execute.
147 TEST_F(PlanTest, DoubleDependent) {
149 "build out: cat a1 a2\n"
150 "build a1: cat mid\n"
151 "build a2: cat mid\n"
152 "build mid: cat in\n");
153 GetNode("mid")->MarkDirty();
154 GetNode("a1")->MarkDirty();
155 GetNode("a2")->MarkDirty();
156 GetNode("out")->MarkDirty();
159 EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
161 ASSERT_TRUE(plan_.more_to_do());
164 edge = plan_.FindWork();
165 ASSERT_TRUE(edge); // cat in
166 plan_.EdgeFinished(edge);
168 edge = plan_.FindWork();
169 ASSERT_TRUE(edge); // cat mid
170 plan_.EdgeFinished(edge);
172 edge = plan_.FindWork();
173 ASSERT_TRUE(edge); // cat mid
174 plan_.EdgeFinished(edge);
176 edge = plan_.FindWork();
177 ASSERT_TRUE(edge); // cat a1 a2
178 plan_.EdgeFinished(edge);
180 edge = plan_.FindWork();
181 ASSERT_FALSE(edge); // done
184 TEST_F(PlanTest, DependencyCycle) {
186 "build out: cat mid\n"
187 "build mid: cat in\n"
188 "build in: cat pre\n"
189 "build pre: cat out\n");
190 GetNode("out")->MarkDirty();
191 GetNode("mid")->MarkDirty();
192 GetNode("in")->MarkDirty();
193 GetNode("pre")->MarkDirty();
196 EXPECT_FALSE(plan_.AddTarget(GetNode("out"), &err));
197 ASSERT_EQ("dependency cycle: out -> mid -> in -> pre -> out", err);
200 TEST_F(PlanTest, PoolWithDepthOne) {
201 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
205 " command = cat $in > $out\n"
207 "build out1: poolcat in\n"
208 "build out2: poolcat in\n"));
209 GetNode("out1")->MarkDirty();
210 GetNode("out2")->MarkDirty();
212 EXPECT_TRUE(plan_.AddTarget(GetNode("out1"), &err));
214 EXPECT_TRUE(plan_.AddTarget(GetNode("out2"), &err));
216 ASSERT_TRUE(plan_.more_to_do());
218 Edge* edge = plan_.FindWork();
220 ASSERT_EQ("in", edge->inputs_[0]->path());
221 ASSERT_EQ("out1", edge->outputs_[0]->path());
223 // This will be false since poolcat is serialized
224 ASSERT_FALSE(plan_.FindWork());
226 plan_.EdgeFinished(edge);
228 edge = plan_.FindWork();
230 ASSERT_EQ("in", edge->inputs_[0]->path());
231 ASSERT_EQ("out2", edge->outputs_[0]->path());
233 ASSERT_FALSE(plan_.FindWork());
235 plan_.EdgeFinished(edge);
237 ASSERT_FALSE(plan_.more_to_do());
238 edge = plan_.FindWork();
242 TEST_F(PlanTest, PoolsWithDepthTwo) {
243 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
249 " command = cat $in > $out\n"
252 " command = cat $in > $out\n"
254 "build out1: foocat in\n"
255 "build out2: foocat in\n"
256 "build out3: foocat in\n"
257 "build outb1: bazcat in\n"
258 "build outb2: bazcat in\n"
259 "build outb3: bazcat in\n"
261 "build allTheThings: cat out1 out2 out3 outb1 outb2 outb3\n"
263 // Mark all the out* nodes dirty
264 for (int i = 0; i < 3; ++i) {
265 GetNode("out" + string(1, '1' + static_cast<char>(i)))->MarkDirty();
266 GetNode("outb" + string(1, '1' + static_cast<char>(i)))->MarkDirty();
268 GetNode("allTheThings")->MarkDirty();
271 EXPECT_TRUE(plan_.AddTarget(GetNode("allTheThings"), &err));
275 FindWorkSorted(&edges, 5);
277 for (int i = 0; i < 4; ++i) {
278 Edge *edge = edges[i];
279 ASSERT_EQ("in", edge->inputs_[0]->path());
280 string base_name(i < 2 ? "out" : "outb");
281 ASSERT_EQ(base_name + string(1, '1' + (i % 2)), edge->outputs_[0]->path());
284 // outb3 is exempt because it has an empty pool
285 Edge* edge = edges[4];
287 ASSERT_EQ("in", edge->inputs_[0]->path());
288 ASSERT_EQ("outb3", edge->outputs_[0]->path());
291 plan_.EdgeFinished(edges.front());
294 // out3 should be available
295 Edge* out3 = plan_.FindWork();
297 ASSERT_EQ("in", out3->inputs_[0]->path());
298 ASSERT_EQ("out3", out3->outputs_[0]->path());
300 ASSERT_FALSE(plan_.FindWork());
302 plan_.EdgeFinished(out3);
304 ASSERT_FALSE(plan_.FindWork());
306 for (deque<Edge*>::iterator it = edges.begin(); it != edges.end(); ++it) {
307 plan_.EdgeFinished(*it);
310 Edge* last = plan_.FindWork();
312 ASSERT_EQ("allTheThings", last->outputs_[0]->path());
314 plan_.EdgeFinished(last);
316 ASSERT_FALSE(plan_.more_to_do());
317 ASSERT_FALSE(plan_.FindWork());
320 TEST_F(PlanTest, PoolWithRedundantEdges) {
321 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
325 " command = touch foo.cpp\n"
327 " command = touch bar.cpp\n"
329 " command = echo $out > $out\n"
330 "build foo.cpp.obj: echo foo.cpp || foo.cpp\n"
332 "build bar.cpp.obj: echo bar.cpp || bar.cpp\n"
334 "build libfoo.a: echo foo.cpp.obj bar.cpp.obj\n"
335 "build foo.cpp: gen_foo\n"
336 "build bar.cpp: gen_bar\n"
337 "build all: phony libfoo.a\n"));
338 GetNode("foo.cpp")->MarkDirty();
339 GetNode("foo.cpp.obj")->MarkDirty();
340 GetNode("bar.cpp")->MarkDirty();
341 GetNode("bar.cpp.obj")->MarkDirty();
342 GetNode("libfoo.a")->MarkDirty();
343 GetNode("all")->MarkDirty();
345 EXPECT_TRUE(plan_.AddTarget(GetNode("all"), &err));
347 ASSERT_TRUE(plan_.more_to_do());
351 deque<Edge*> initial_edges;
352 FindWorkSorted(&initial_edges, 2);
354 edge = initial_edges[1]; // Foo first
355 ASSERT_EQ("foo.cpp", edge->outputs_[0]->path());
356 plan_.EdgeFinished(edge);
358 edge = plan_.FindWork();
360 ASSERT_FALSE(plan_.FindWork());
361 ASSERT_EQ("foo.cpp", edge->inputs_[0]->path());
362 ASSERT_EQ("foo.cpp", edge->inputs_[1]->path());
363 ASSERT_EQ("foo.cpp.obj", edge->outputs_[0]->path());
364 plan_.EdgeFinished(edge);
366 edge = initial_edges[0]; // Now for bar
367 ASSERT_EQ("bar.cpp", edge->outputs_[0]->path());
368 plan_.EdgeFinished(edge);
370 edge = plan_.FindWork();
372 ASSERT_FALSE(plan_.FindWork());
373 ASSERT_EQ("bar.cpp", edge->inputs_[0]->path());
374 ASSERT_EQ("bar.cpp", edge->inputs_[1]->path());
375 ASSERT_EQ("bar.cpp.obj", edge->outputs_[0]->path());
376 plan_.EdgeFinished(edge);
378 edge = plan_.FindWork();
380 ASSERT_FALSE(plan_.FindWork());
381 ASSERT_EQ("foo.cpp.obj", edge->inputs_[0]->path());
382 ASSERT_EQ("bar.cpp.obj", edge->inputs_[1]->path());
383 ASSERT_EQ("libfoo.a", edge->outputs_[0]->path());
384 plan_.EdgeFinished(edge);
386 edge = plan_.FindWork();
388 ASSERT_FALSE(plan_.FindWork());
389 ASSERT_EQ("libfoo.a", edge->inputs_[0]->path());
390 ASSERT_EQ("all", edge->outputs_[0]->path());
391 plan_.EdgeFinished(edge);
393 edge = plan_.FindWork();
395 ASSERT_FALSE(plan_.more_to_do());
398 /// Fake implementation of CommandRunner, useful for tests.
399 struct FakeCommandRunner : public CommandRunner {
400 explicit FakeCommandRunner(VirtualFileSystem* fs) :
401 last_command_(NULL), fs_(fs) {}
403 // CommandRunner impl
404 virtual bool CanRunMore();
405 virtual bool StartCommand(Edge* edge);
406 virtual bool WaitForCommand(Result* result);
407 virtual vector<Edge*> GetActiveEdges();
408 virtual void Abort();
410 vector<string> commands_ran_;
412 VirtualFileSystem* fs_;
415 struct BuildTest : public StateTestWithBuiltinRules {
416 BuildTest() : config_(MakeConfig()), command_runner_(&fs_),
417 builder_(&state_, config_, NULL, NULL, &fs_),
421 virtual void SetUp() {
422 StateTestWithBuiltinRules::SetUp();
424 builder_.command_runner_.reset(&command_runner_);
426 "build cat1: cat in1\n"
427 "build cat2: cat in1 in2\n"
428 "build cat12: cat cat1 cat2\n");
430 fs_.Create("in1", "");
431 fs_.Create("in2", "");
435 builder_.command_runner_.release();
438 // Mark a path dirty.
439 void Dirty(const string& path);
441 BuildConfig MakeConfig() {
443 config.verbosity = BuildConfig::QUIET;
448 FakeCommandRunner command_runner_;
449 VirtualFileSystem fs_;
455 bool FakeCommandRunner::CanRunMore() {
456 // Only run one at a time.
457 return last_command_ == NULL;
460 bool FakeCommandRunner::StartCommand(Edge* edge) {
461 assert(!last_command_);
462 commands_ran_.push_back(edge->EvaluateCommand());
463 if (edge->rule().name() == "cat" ||
464 edge->rule().name() == "cat_rsp" ||
465 edge->rule().name() == "cc" ||
466 edge->rule().name() == "touch" ||
467 edge->rule().name() == "touch-interrupt") {
468 for (vector<Node*>::iterator out = edge->outputs_.begin();
469 out != edge->outputs_.end(); ++out) {
470 fs_->Create((*out)->path(), "");
472 } else if (edge->rule().name() == "true" ||
473 edge->rule().name() == "fail" ||
474 edge->rule().name() == "interrupt") {
475 // Don't do anything.
477 printf("unknown command\n");
481 last_command_ = edge;
485 bool FakeCommandRunner::WaitForCommand(Result* result) {
489 Edge* edge = last_command_;
492 if (edge->rule().name() == "interrupt" ||
493 edge->rule().name() == "touch-interrupt") {
494 result->status = ExitInterrupted;
498 if (edge->rule().name() == "fail")
499 result->status = ExitFailure;
501 result->status = ExitSuccess;
502 last_command_ = NULL;
506 vector<Edge*> FakeCommandRunner::GetActiveEdges() {
509 edges.push_back(last_command_);
513 void FakeCommandRunner::Abort() {
514 last_command_ = NULL;
517 void BuildTest::Dirty(const string& path) {
518 Node* node = GetNode(path);
521 // If it's an input file, mark that we've already stat()ed it and
523 if (!node->in_edge())
527 TEST_F(BuildTest, NoWork) {
529 EXPECT_TRUE(builder_.AlreadyUpToDate());
532 TEST_F(BuildTest, OneStep) {
533 // Given a dirty target with one ready input,
534 // we should rebuild the target.
537 EXPECT_TRUE(builder_.AddTarget("cat1", &err));
539 EXPECT_TRUE(builder_.Build(&err));
542 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
543 EXPECT_EQ("cat in1 > cat1", command_runner_.commands_ran_[0]);
546 TEST_F(BuildTest, OneStep2) {
547 // Given a target with one dirty input,
548 // we should rebuild the target.
551 EXPECT_TRUE(builder_.AddTarget("cat1", &err));
553 EXPECT_TRUE(builder_.Build(&err));
556 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
557 EXPECT_EQ("cat in1 > cat1", command_runner_.commands_ran_[0]);
560 TEST_F(BuildTest, TwoStep) {
562 EXPECT_TRUE(builder_.AddTarget("cat12", &err));
564 EXPECT_TRUE(builder_.Build(&err));
566 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
567 // Depending on how the pointers work out, we could've ran
568 // the first two commands in either order.
569 EXPECT_TRUE((command_runner_.commands_ran_[0] == "cat in1 > cat1" &&
570 command_runner_.commands_ran_[1] == "cat in1 in2 > cat2") ||
571 (command_runner_.commands_ran_[1] == "cat in1 > cat1" &&
572 command_runner_.commands_ran_[0] == "cat in1 in2 > cat2"));
574 EXPECT_EQ("cat cat1 cat2 > cat12", command_runner_.commands_ran_[2]);
578 // Modifying in2 requires rebuilding one intermediate file
579 // and the final file.
580 fs_.Create("in2", "");
582 EXPECT_TRUE(builder_.AddTarget("cat12", &err));
584 EXPECT_TRUE(builder_.Build(&err));
586 ASSERT_EQ(5u, command_runner_.commands_ran_.size());
587 EXPECT_EQ("cat in1 in2 > cat2", command_runner_.commands_ran_[3]);
588 EXPECT_EQ("cat cat1 cat2 > cat12", command_runner_.commands_ran_[4]);
591 TEST_F(BuildTest, TwoOutputs) {
592 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
594 " command = touch $out\n"
595 "build out1 out2: touch in.txt\n"));
597 fs_.Create("in.txt", "");
600 EXPECT_TRUE(builder_.AddTarget("out1", &err));
602 EXPECT_TRUE(builder_.Build(&err));
604 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
605 EXPECT_EQ("touch out1 out2", command_runner_.commands_ran_[0]);
609 // https://github.com/martine/ninja/issues/148
610 TEST_F(BuildTest, MultiOutIn) {
611 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
613 " command = touch $out\n"
614 "build in1 otherfile: touch in\n"
615 "build out: touch in | in1\n"));
617 fs_.Create("in", "");
619 fs_.Create("in1", "");
622 EXPECT_TRUE(builder_.AddTarget("out", &err));
624 EXPECT_TRUE(builder_.Build(&err));
628 TEST_F(BuildTest, Chain) {
629 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
633 "build c5: cat c4\n"));
635 fs_.Create("c1", "");
638 EXPECT_TRUE(builder_.AddTarget("c5", &err));
640 EXPECT_TRUE(builder_.Build(&err));
642 ASSERT_EQ(4u, command_runner_.commands_ran_.size());
645 command_runner_.commands_ran_.clear();
647 EXPECT_TRUE(builder_.AddTarget("c5", &err));
649 EXPECT_TRUE(builder_.AlreadyUpToDate());
653 fs_.Create("c3", "");
655 command_runner_.commands_ran_.clear();
657 EXPECT_TRUE(builder_.AddTarget("c5", &err));
659 EXPECT_FALSE(builder_.AlreadyUpToDate());
660 EXPECT_TRUE(builder_.Build(&err));
661 ASSERT_EQ(2u, command_runner_.commands_ran_.size()); // 3->4, 4->5
664 TEST_F(BuildTest, MissingInput) {
665 // Input is referenced by build file, but no rule for it.
668 EXPECT_FALSE(builder_.AddTarget("cat1", &err));
669 EXPECT_EQ("'in1', needed by 'cat1', missing and no known rule to make it",
673 TEST_F(BuildTest, MissingTarget) {
674 // Target is not referenced by build file.
676 EXPECT_FALSE(builder_.AddTarget("meow", &err));
677 EXPECT_EQ("unknown target: 'meow'", err);
680 TEST_F(BuildTest, MakeDirs) {
684 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
685 "build subdir\\dir2\\file: cat in1\n"));
686 EXPECT_TRUE(builder_.AddTarget("subdir\\dir2\\file", &err));
688 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
689 "build subdir/dir2/file: cat in1\n"));
690 EXPECT_TRUE(builder_.AddTarget("subdir/dir2/file", &err));
694 EXPECT_TRUE(builder_.Build(&err));
696 ASSERT_EQ(2u, fs_.directories_made_.size());
697 EXPECT_EQ("subdir", fs_.directories_made_[0]);
699 EXPECT_EQ("subdir\\dir2", fs_.directories_made_[1]);
701 EXPECT_EQ("subdir/dir2", fs_.directories_made_[1]);
705 TEST_F(BuildTest, DepFileMissing) {
707 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
708 "rule cc\n command = cc $in\n depfile = $out.d\n"
709 "build foo.o: cc foo.c\n"));
710 fs_.Create("foo.c", "");
712 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
714 ASSERT_EQ(1u, fs_.files_read_.size());
715 EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
718 TEST_F(BuildTest, DepFileOK) {
720 int orig_edges = state_.edges_.size();
721 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
722 "rule cc\n command = cc $in\n depfile = $out.d\n"
723 "build foo.o: cc foo.c\n"));
724 Edge* edge = state_.edges_.back();
726 fs_.Create("foo.c", "");
727 GetNode("bar.h")->MarkDirty(); // Mark bar.h as missing.
728 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
729 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
731 ASSERT_EQ(1u, fs_.files_read_.size());
732 EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
734 // Expect three new edges: one generating foo.o, and two more from
735 // loading the depfile.
736 ASSERT_EQ(orig_edges + 3, (int)state_.edges_.size());
737 // Expect our edge to now have three inputs: foo.c and two headers.
738 ASSERT_EQ(3u, edge->inputs_.size());
740 // Expect the command line we generate to only use the original input.
741 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
744 TEST_F(BuildTest, DepFileParseError) {
746 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
747 "rule cc\n command = cc $in\n depfile = $out.d\n"
748 "build foo.o: cc foo.c\n"));
749 fs_.Create("foo.c", "");
750 fs_.Create("foo.o.d", "randomtext\n");
751 EXPECT_FALSE(builder_.AddTarget("foo.o", &err));
752 EXPECT_EQ("expected depfile 'foo.o.d' to mention 'foo.o', got 'randomtext'",
756 TEST_F(BuildTest, OrderOnlyDeps) {
758 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
759 "rule cc\n command = cc $in\n depfile = $out.d\n"
760 "build foo.o: cc foo.c || otherfile\n"));
761 Edge* edge = state_.edges_.back();
763 fs_.Create("foo.c", "");
764 fs_.Create("otherfile", "");
765 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
766 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
769 // One explicit, two implicit, one order only.
770 ASSERT_EQ(4u, edge->inputs_.size());
771 EXPECT_EQ(2, edge->implicit_deps_);
772 EXPECT_EQ(1, edge->order_only_deps_);
773 // Verify the inputs are in the order we expect
774 // (explicit then implicit then orderonly).
775 EXPECT_EQ("foo.c", edge->inputs_[0]->path());
776 EXPECT_EQ("blah.h", edge->inputs_[1]->path());
777 EXPECT_EQ("bar.h", edge->inputs_[2]->path());
778 EXPECT_EQ("otherfile", edge->inputs_[3]->path());
780 // Expect the command line we generate to only use the original input.
781 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
783 // explicit dep dirty, expect a rebuild.
784 EXPECT_TRUE(builder_.Build(&err));
786 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
790 // Recreate the depfile, as it should have been deleted by the build.
791 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
793 // implicit dep dirty, expect a rebuild.
794 fs_.Create("blah.h", "");
795 fs_.Create("bar.h", "");
796 command_runner_.commands_ran_.clear();
798 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
799 EXPECT_TRUE(builder_.Build(&err));
801 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
805 // Recreate the depfile, as it should have been deleted by the build.
806 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
808 // order only dep dirty, no rebuild.
809 fs_.Create("otherfile", "");
810 command_runner_.commands_ran_.clear();
812 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
814 EXPECT_TRUE(builder_.AlreadyUpToDate());
816 // implicit dep missing, expect rebuild.
817 fs_.RemoveFile("bar.h");
818 command_runner_.commands_ran_.clear();
820 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
821 EXPECT_TRUE(builder_.Build(&err));
823 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
826 TEST_F(BuildTest, RebuildOrderOnlyDeps) {
828 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
829 "rule cc\n command = cc $in\n"
830 "rule true\n command = true\n"
831 "build oo.h: cc oo.h.in\n"
832 "build foo.o: cc foo.c || oo.h\n"));
834 fs_.Create("foo.c", "");
835 fs_.Create("oo.h.in", "");
837 // foo.o and order-only dep dirty, build both.
838 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
839 EXPECT_TRUE(builder_.Build(&err));
841 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
843 // all clean, no rebuild.
844 command_runner_.commands_ran_.clear();
846 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
848 EXPECT_TRUE(builder_.AlreadyUpToDate());
850 // order-only dep missing, build it only.
851 fs_.RemoveFile("oo.h");
852 command_runner_.commands_ran_.clear();
854 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
855 EXPECT_TRUE(builder_.Build(&err));
857 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
858 ASSERT_EQ("cc oo.h.in", command_runner_.commands_ran_[0]);
862 // order-only dep dirty, build it only.
863 fs_.Create("oo.h.in", "");
864 command_runner_.commands_ran_.clear();
866 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
867 EXPECT_TRUE(builder_.Build(&err));
869 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
870 ASSERT_EQ("cc oo.h.in", command_runner_.commands_ran_[0]);
873 TEST_F(BuildTest, Phony) {
875 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
876 "build out: cat bar.cc\n"
877 "build all: phony out\n"));
878 fs_.Create("bar.cc", "");
880 EXPECT_TRUE(builder_.AddTarget("all", &err));
883 // Only one command to run, because phony runs no command.
884 EXPECT_FALSE(builder_.AlreadyUpToDate());
885 EXPECT_TRUE(builder_.Build(&err));
887 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
890 TEST_F(BuildTest, PhonyNoWork) {
892 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
893 "build out: cat bar.cc\n"
894 "build all: phony out\n"));
895 fs_.Create("bar.cc", "");
896 fs_.Create("out", "");
898 EXPECT_TRUE(builder_.AddTarget("all", &err));
900 EXPECT_TRUE(builder_.AlreadyUpToDate());
903 TEST_F(BuildTest, Fail) {
904 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
907 "build out1: fail\n"));
910 EXPECT_TRUE(builder_.AddTarget("out1", &err));
913 EXPECT_FALSE(builder_.Build(&err));
914 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
915 ASSERT_EQ("subcommand failed", err);
918 TEST_F(BuildTest, SwallowFailures) {
919 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
925 "build all: phony out1 out2 out3\n"));
927 // Swallow two failures, die on the third.
928 config_.failures_allowed = 3;
931 EXPECT_TRUE(builder_.AddTarget("all", &err));
934 EXPECT_FALSE(builder_.Build(&err));
935 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
936 ASSERT_EQ("subcommands failed", err);
939 TEST_F(BuildTest, SwallowFailuresLimit) {
940 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
946 "build final: cat out1 out2 out3\n"));
948 // Swallow ten failures; we should stop before building final.
949 config_.failures_allowed = 11;
952 EXPECT_TRUE(builder_.AddTarget("final", &err));
955 EXPECT_FALSE(builder_.Build(&err));
956 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
957 ASSERT_EQ("cannot make progress due to previous errors", err);
960 struct BuildWithLogTest : public BuildTest {
962 builder_.SetBuildLog(&build_log_);
968 TEST_F(BuildWithLogTest, NotInLogButOnDisk) {
969 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
972 "build out1: cc in\n"));
974 // Create input/output that would be considered up to date when
975 // not considering the command line hash.
976 fs_.Create("in", "");
977 fs_.Create("out1", "");
980 // Because it's not in the log, it should not be up-to-date until
982 EXPECT_TRUE(builder_.AddTarget("out1", &err));
983 EXPECT_FALSE(builder_.AlreadyUpToDate());
985 command_runner_.commands_ran_.clear();
988 EXPECT_TRUE(builder_.AddTarget("out1", &err));
989 EXPECT_TRUE(builder_.Build(&err));
990 EXPECT_TRUE(builder_.AlreadyUpToDate());
993 TEST_F(BuildWithLogTest, RestatTest) {
994 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1001 "build out1: cc in\n"
1002 "build out2: true out1\n"
1003 "build out3: cat out2\n"));
1005 fs_.Create("out1", "");
1006 fs_.Create("out2", "");
1007 fs_.Create("out3", "");
1011 fs_.Create("in", "");
1013 // Do a pre-build so that there's commands in the log for the outputs,
1014 // otherwise, the lack of an entry in the build log will cause out3 to rebuild
1015 // regardless of restat.
1017 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1019 EXPECT_TRUE(builder_.Build(&err));
1021 command_runner_.commands_ran_.clear();
1026 fs_.Create("in", "");
1027 // "cc" touches out1, so we should build out2. But because "true" does not
1028 // touch out2, we should cancel the build of out3.
1029 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1031 EXPECT_TRUE(builder_.Build(&err));
1032 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1034 // If we run again, it should be a no-op, because the build log has recorded
1035 // that we've already built out2 with an input timestamp of 2 (from out1).
1036 command_runner_.commands_ran_.clear();
1038 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1040 EXPECT_TRUE(builder_.AlreadyUpToDate());
1044 fs_.Create("in", "");
1046 // The build log entry should not, however, prevent us from rebuilding out2
1048 command_runner_.commands_ran_.clear();
1050 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1052 EXPECT_TRUE(builder_.Build(&err));
1053 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1056 TEST_F(BuildWithLogTest, RestatMissingFile) {
1057 // If a restat rule doesn't create its output, and the output didn't
1058 // exist before the rule was run, consider that behavior equivalent
1059 // to a rule that doesn't modify its existent output file.
1061 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1067 "build out1: true in\n"
1068 "build out2: cc out1\n"));
1070 fs_.Create("in", "");
1071 fs_.Create("out2", "");
1073 // Do a pre-build so that there's commands in the log for the outputs,
1074 // otherwise, the lack of an entry in the build log will cause out2 to rebuild
1075 // regardless of restat.
1077 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1079 EXPECT_TRUE(builder_.Build(&err));
1081 command_runner_.commands_ran_.clear();
1085 fs_.Create("in", "");
1086 fs_.Create("out2", "");
1088 // Run a build, expect only the first command to run.
1089 // It doesn't touch its output (due to being the "true" command), so
1090 // we shouldn't run the dependent build.
1091 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1093 EXPECT_TRUE(builder_.Build(&err));
1094 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1097 // Test scenario, in which an input file is removed, but output isn't changed
1098 // https://github.com/martine/ninja/issues/295
1099 TEST_F(BuildWithLogTest, RestatMissingInput) {
1100 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1103 " depfile = $out.d\n"
1107 "build out1: true in\n"
1108 "build out2: cc out1\n"));
1110 // Create all necessary files
1111 fs_.Create("in", "");
1113 // The implicit dependencies and the depfile itself
1114 // are newer than the output
1115 TimeStamp restat_mtime = fs_.Tick();
1116 fs_.Create("out1.d", "out1: will.be.deleted restat.file\n");
1117 fs_.Create("will.be.deleted", "");
1118 fs_.Create("restat.file", "");
1120 // Run the build, out1 and out2 get built
1122 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1124 EXPECT_TRUE(builder_.Build(&err));
1125 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1127 // See that an entry in the logfile is created, capturing
1129 BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out1");
1130 ASSERT_TRUE(NULL != log_entry);
1131 ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
1133 // Now remove a file, referenced from depfile, so that target becomes
1134 // dirty, but the output does not change
1135 fs_.RemoveFile("will.be.deleted");
1137 // Trigger the build again - only out1 gets built
1138 command_runner_.commands_ran_.clear();
1140 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1142 EXPECT_TRUE(builder_.Build(&err));
1143 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1145 // Check that the logfile entry remains correctly set
1146 log_entry = build_log_.LookupByOutput("out1");
1147 ASSERT_TRUE(NULL != log_entry);
1148 ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
1151 struct BuildDryRun : public BuildWithLogTest {
1153 config_.dry_run = true;
1157 TEST_F(BuildDryRun, AllCommandsShown) {
1158 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1165 "build out1: cc in\n"
1166 "build out2: true out1\n"
1167 "build out3: cat out2\n"));
1169 fs_.Create("out1", "");
1170 fs_.Create("out2", "");
1171 fs_.Create("out3", "");
1175 fs_.Create("in", "");
1177 // "cc" touches out1, so we should build out2. But because "true" does not
1178 // touch out2, we should cancel the build of out3.
1180 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1182 EXPECT_TRUE(builder_.Build(&err));
1183 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1186 // Test that RSP files are created when & where appropriate and deleted after
1187 // successful execution.
1188 TEST_F(BuildTest, RspFileSuccess)
1190 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1192 " command = cat $rspfile > $out\n"
1193 " rspfile = $rspfile\n"
1194 " rspfile_content = $long_command\n"
1195 "build out1: cat in\n"
1196 "build out2: cat_rsp in\n"
1197 " rspfile = out2.rsp\n"
1198 " long_command = Some very long command\n"));
1200 fs_.Create("out1", "");
1201 fs_.Create("out2", "");
1202 fs_.Create("out3", "");
1206 fs_.Create("in", "");
1209 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1211 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1214 size_t files_created = fs_.files_created_.size();
1215 size_t files_removed = fs_.files_removed_.size();
1217 EXPECT_TRUE(builder_.Build(&err));
1218 ASSERT_EQ(2u, command_runner_.commands_ran_.size()); // cat + cat_rsp
1220 // The RSP file was created
1221 ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1222 ASSERT_EQ(1u, fs_.files_created_.count("out2.rsp"));
1224 // The RSP file was removed
1225 ASSERT_EQ(files_removed + 1, fs_.files_removed_.size());
1226 ASSERT_EQ(1u, fs_.files_removed_.count("out2.rsp"));
1229 // Test that RSP file is created but not removed for commands, which fail
1230 TEST_F(BuildTest, RspFileFailure) {
1231 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1234 " rspfile = $rspfile\n"
1235 " rspfile_content = $long_command\n"
1236 "build out: fail in\n"
1237 " rspfile = out.rsp\n"
1238 " long_command = Another very long command\n"));
1240 fs_.Create("out", "");
1242 fs_.Create("in", "");
1245 EXPECT_TRUE(builder_.AddTarget("out", &err));
1248 size_t files_created = fs_.files_created_.size();
1249 size_t files_removed = fs_.files_removed_.size();
1251 EXPECT_FALSE(builder_.Build(&err));
1252 ASSERT_EQ("subcommand failed", err);
1253 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1255 // The RSP file was created
1256 ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1257 ASSERT_EQ(1u, fs_.files_created_.count("out.rsp"));
1259 // The RSP file was NOT removed
1260 ASSERT_EQ(files_removed, fs_.files_removed_.size());
1261 ASSERT_EQ(0u, fs_.files_removed_.count("out.rsp"));
1263 // The RSP file contains what it should
1264 ASSERT_EQ("Another very long command", fs_.files_["out.rsp"].contents);
1267 // Test that contens of the RSP file behaves like a regular part of
1268 // command line, i.e. triggers a rebuild if changed
1269 TEST_F(BuildWithLogTest, RspFileCmdLineChange) {
1270 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1272 " command = cat $rspfile > $out\n"
1273 " rspfile = $rspfile\n"
1274 " rspfile_content = $long_command\n"
1275 "build out: cat_rsp in\n"
1276 " rspfile = out.rsp\n"
1277 " long_command = Original very long command\n"));
1279 fs_.Create("out", "");
1281 fs_.Create("in", "");
1284 EXPECT_TRUE(builder_.AddTarget("out", &err));
1287 // 1. Build for the 1st time (-> populate log)
1288 EXPECT_TRUE(builder_.Build(&err));
1289 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1291 // 2. Build again (no change)
1292 command_runner_.commands_ran_.clear();
1294 EXPECT_TRUE(builder_.AddTarget("out", &err));
1296 ASSERT_TRUE(builder_.AlreadyUpToDate());
1298 // 3. Alter the entry in the logfile
1299 // (to simulate a change in the command line between 2 builds)
1300 BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out");
1301 ASSERT_TRUE(NULL != log_entry);
1302 ASSERT_NO_FATAL_FAILURE(AssertHash(
1303 "cat out.rsp > out;rspfile=Original very long command",
1304 log_entry->command_hash));
1305 log_entry->command_hash++; // Change the command hash to something else.
1306 // Now expect the target to be rebuilt
1307 command_runner_.commands_ran_.clear();
1309 EXPECT_TRUE(builder_.AddTarget("out", &err));
1311 EXPECT_TRUE(builder_.Build(&err));
1312 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1315 TEST_F(BuildTest, InterruptCleanup) {
1316 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1318 " command = interrupt\n"
1319 "rule touch-interrupt\n"
1320 " command = touch-interrupt\n"
1321 "build out1: interrupt in1\n"
1322 "build out2: touch-interrupt in2\n"));
1324 fs_.Create("out1", "");
1325 fs_.Create("out2", "");
1327 fs_.Create("in1", "");
1328 fs_.Create("in2", "");
1330 // An untouched output of an interrupted command should be retained.
1332 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1334 EXPECT_FALSE(builder_.Build(&err));
1335 EXPECT_EQ("interrupted by user", err);
1337 EXPECT_GT(fs_.Stat("out1"), 0);
1340 // A touched output of an interrupted command should be deleted.
1341 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1343 EXPECT_FALSE(builder_.Build(&err));
1344 EXPECT_EQ("interrupted by user", err);
1346 EXPECT_EQ(0, fs_.Stat("out2"));
1349 TEST_F(BuildTest, PhonyWithNoInputs) {
1350 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1351 "build nonexistent: phony\n"
1352 "build out1: cat || nonexistent\n"
1353 "build out2: cat nonexistent\n"));
1354 fs_.Create("out1", "");
1355 fs_.Create("out2", "");
1357 // out1 should be up to date even though its input is dirty, because its
1358 // order-only dependency has nothing to do.
1360 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1362 EXPECT_TRUE(builder_.AlreadyUpToDate());
1364 // out2 should still be out of date though, because its input is dirty.
1366 command_runner_.commands_ran_.clear();
1368 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1370 EXPECT_TRUE(builder_.Build(&err));
1372 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1375 TEST_F(BuildTest, DepsGccWithEmptyDepfileErrorsOut) {
1376 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1380 "build out: cc\n"));
1384 EXPECT_TRUE(builder_.AddTarget("out", &err));
1386 EXPECT_FALSE(builder_.AlreadyUpToDate());
1388 EXPECT_FALSE(builder_.Build(&err));
1389 ASSERT_EQ("subcommand failed", err);
1390 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1393 TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
1394 EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
1395 status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]"));
1398 TEST_F(BuildTest, FailedDepsParse) {
1399 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1400 "build bad_deps.o: cat in1\n"
1402 " depfile = in1.d\n"));
1405 EXPECT_TRUE(builder_.AddTarget("bad_deps.o", &err));
1408 // These deps will fail to parse, as they should only have one
1409 // path to the left of the colon.
1410 fs_.Create("in1.d", "AAA BBB");
1412 EXPECT_FALSE(builder_.Build(&err));
1413 EXPECT_EQ("subcommand failed", err);
1416 /// Tests of builds involving deps logs necessarily must span
1417 /// multiple builds. We reuse methods on BuildTest but not the
1418 /// builder_ it sets up, because we want pristine objects for
1420 struct BuildWithDepsLogTest : public BuildTest {
1421 BuildWithDepsLogTest() {}
1423 virtual void SetUp() {
1426 temp_dir_.CreateAndEnter("BuildWithDepsLogTest");
1429 virtual void TearDown() {
1430 temp_dir_.Cleanup();
1433 ScopedTempDir temp_dir_;
1435 /// Shadow parent class builder_ so we don't accidentally use it.
1439 /// Run a straightforwad build where the deps log is used.
1440 TEST_F(BuildWithDepsLogTest, Straightforward) {
1442 // Note: in1 was created by the superclass SetUp().
1443 const char* manifest =
1444 "build out: cat in1\n"
1446 " depfile = in1.d\n";
1449 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1450 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1452 // Run the build once, everything should be ok.
1454 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1457 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1458 builder.command_runner_.reset(&command_runner_);
1459 EXPECT_TRUE(builder.AddTarget("out", &err));
1461 fs_.Create("in1.d", "out: in2");
1462 EXPECT_TRUE(builder.Build(&err));
1465 // The deps file should have been removed.
1466 EXPECT_EQ(0, fs_.Stat("in1.d"));
1467 // Recreate it for the next step.
1468 fs_.Create("in1.d", "out: in2");
1470 builder.command_runner_.release();
1475 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1476 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1478 // Touch the file only mentioned in the deps.
1480 fs_.Create("in2", "");
1482 // Run the build again.
1484 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1485 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1487 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1488 builder.command_runner_.reset(&command_runner_);
1489 command_runner_.commands_ran_.clear();
1490 EXPECT_TRUE(builder.AddTarget("out", &err));
1492 EXPECT_TRUE(builder.Build(&err));
1495 // We should have rebuilt the output due to in2 being
1497 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1499 builder.command_runner_.release();
1503 /// Verify that obsolete dependency info causes a rebuild.
1504 /// 1) Run a successful build where everything has time t, record deps.
1505 /// 2) Move input/output to time t+1 -- despite files in alignment,
1506 /// should still need to rebuild due to deps at older time.
1507 TEST_F(BuildWithDepsLogTest, ObsoleteDeps) {
1509 // Note: in1 was created by the superclass SetUp().
1510 const char* manifest =
1511 "build out: cat in1\n"
1513 " depfile = in1.d\n";
1515 // Run an ordinary build that gathers dependencies.
1516 fs_.Create("in1", "");
1517 fs_.Create("in1.d", "out: ");
1520 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1521 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1523 // Run the build once, everything should be ok.
1525 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1528 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1529 builder.command_runner_.reset(&command_runner_);
1530 EXPECT_TRUE(builder.AddTarget("out", &err));
1532 EXPECT_TRUE(builder.Build(&err));
1536 builder.command_runner_.release();
1539 // Push all files one tick forward so that only the deps are out
1542 fs_.Create("in1", "");
1543 fs_.Create("out", "");
1545 // The deps file should have been removed, so no need to timestamp it.
1546 EXPECT_EQ(0, fs_.Stat("in1.d"));
1550 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1551 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1554 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1555 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1557 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1558 builder.command_runner_.reset(&command_runner_);
1559 command_runner_.commands_ran_.clear();
1560 EXPECT_TRUE(builder.AddTarget("out", &err));
1563 // Recreate the deps file here because the build expects them to exist.
1564 fs_.Create("in1.d", "out: ");
1566 EXPECT_TRUE(builder.Build(&err));
1569 // We should have rebuilt the output due to the deps being
1571 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1573 builder.command_runner_.release();
1577 TEST_F(BuildWithDepsLogTest, DepsIgnoredInDryRun) {
1578 const char* manifest =
1579 "build out: cat in1\n"
1581 " depfile = in1.d\n";
1583 fs_.Create("out", "");
1585 fs_.Create("in1", "");
1588 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1589 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1591 // The deps log is NULL in dry runs.
1592 config_.dry_run = true;
1593 Builder builder(&state, config_, NULL, NULL, &fs_);
1594 builder.command_runner_.reset(&command_runner_);
1595 command_runner_.commands_ran_.clear();
1598 EXPECT_TRUE(builder.AddTarget("out", &err));
1600 EXPECT_TRUE(builder.Build(&err));
1601 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1603 builder.command_runner_.release();
1606 /// Check that a restat rule generating a header cancels compilations correctly.
1607 TEST_F(BuildTest, RestatDepfileDependency) {
1608 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1610 " command = true\n" // Would be "write if out-of-date" in reality.
1612 "build header.h: true header.in\n"
1613 "build out: cat in1\n"
1614 " depfile = in1.d\n"));
1616 fs_.Create("header.h", "");
1617 fs_.Create("in1.d", "out: header.h");
1619 fs_.Create("header.in", "");
1622 EXPECT_TRUE(builder_.AddTarget("out", &err));
1624 EXPECT_TRUE(builder_.Build(&err));
1628 /// Check that a restat rule generating a header cancels compilations correctly,
1630 TEST_F(BuildWithDepsLogTest, RestatDepfileDependencyDepsLog) {
1632 // Note: in1 was created by the superclass SetUp().
1633 const char* manifest =
1635 " command = true\n" // Would be "write if out-of-date" in reality.
1637 "build header.h: true header.in\n"
1638 "build out: cat in1\n"
1640 " depfile = in1.d\n";
1643 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1644 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1646 // Run the build once, everything should be ok.
1648 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1651 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1652 builder.command_runner_.reset(&command_runner_);
1653 EXPECT_TRUE(builder.AddTarget("out", &err));
1655 fs_.Create("in1.d", "out: header.h");
1656 EXPECT_TRUE(builder.Build(&err));
1660 builder.command_runner_.release();
1665 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1666 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1668 // Touch the input of the restat rule.
1670 fs_.Create("header.in", "");
1672 // Run the build again.
1674 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1675 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1677 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1678 builder.command_runner_.reset(&command_runner_);
1679 command_runner_.commands_ran_.clear();
1680 EXPECT_TRUE(builder.AddTarget("out", &err));
1682 EXPECT_TRUE(builder.Build(&err));
1685 // Rule "true" should have run again, but the build of "out" should have
1686 // been cancelled due to restat propagating through the depfile header.
1687 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1689 builder.command_runner_.release();
1693 TEST_F(BuildWithDepsLogTest, DepFileOKDepsLog) {
1695 const char* manifest =
1696 "rule cc\n command = cc $in\n depfile = $out.d\n deps = gcc\n"
1697 "build foo.o: cc foo.c\n";
1699 fs_.Create("foo.c", "");
1703 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1705 // Run the build once, everything should be ok.
1707 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1710 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1711 builder.command_runner_.reset(&command_runner_);
1712 EXPECT_TRUE(builder.AddTarget("foo.o", &err));
1714 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
1715 EXPECT_TRUE(builder.Build(&err));
1719 builder.command_runner_.release();
1724 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1727 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1728 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1731 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1732 builder.command_runner_.reset(&command_runner_);
1734 Edge* edge = state.edges_.back();
1736 state.GetNode("bar.h")->MarkDirty(); // Mark bar.h as missing.
1737 EXPECT_TRUE(builder.AddTarget("foo.o", &err));
1740 // Expect three new edges: one generating foo.o, and two more from
1741 // loading the depfile.
1742 ASSERT_EQ(3, (int)state.edges_.size());
1743 // Expect our edge to now have three inputs: foo.c and two headers.
1744 ASSERT_EQ(3u, edge->inputs_.size());
1746 // Expect the command line we generate to only use the original input.
1747 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
1750 builder.command_runner_.release();