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 EXPECT_EQ("[3/3]", builder_.status_->FormatProgressStatus("[%s/%t]"));
1022 command_runner_.commands_ran_.clear();
1027 fs_.Create("in", "");
1028 // "cc" touches out1, so we should build out2. But because "true" does not
1029 // touch out2, we should cancel the build of out3.
1030 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1032 EXPECT_TRUE(builder_.Build(&err));
1033 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1035 // If we run again, it should be a no-op, because the build log has recorded
1036 // that we've already built out2 with an input timestamp of 2 (from out1).
1037 command_runner_.commands_ran_.clear();
1039 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1041 EXPECT_TRUE(builder_.AlreadyUpToDate());
1045 fs_.Create("in", "");
1047 // The build log entry should not, however, prevent us from rebuilding out2
1049 command_runner_.commands_ran_.clear();
1051 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1053 EXPECT_TRUE(builder_.Build(&err));
1054 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1057 TEST_F(BuildWithLogTest, RestatMissingFile) {
1058 // If a restat rule doesn't create its output, and the output didn't
1059 // exist before the rule was run, consider that behavior equivalent
1060 // to a rule that doesn't modify its existent output file.
1062 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1068 "build out1: true in\n"
1069 "build out2: cc out1\n"));
1071 fs_.Create("in", "");
1072 fs_.Create("out2", "");
1074 // Do a pre-build so that there's commands in the log for the outputs,
1075 // otherwise, the lack of an entry in the build log will cause out2 to rebuild
1076 // regardless of restat.
1078 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1080 EXPECT_TRUE(builder_.Build(&err));
1082 command_runner_.commands_ran_.clear();
1086 fs_.Create("in", "");
1087 fs_.Create("out2", "");
1089 // Run a build, expect only the first command to run.
1090 // It doesn't touch its output (due to being the "true" command), so
1091 // we shouldn't run the dependent build.
1092 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1094 EXPECT_TRUE(builder_.Build(&err));
1095 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1098 TEST_F(BuildWithLogTest, RestatSingleDependentOutputDirty) {
1099 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1104 " command = touch\n"
1105 "build out1: true in\n"
1106 "build out2 out3: touch out1\n"
1107 "build out4: touch out2\n"
1110 // Create the necessary files
1111 fs_.Create("in", "");
1114 EXPECT_TRUE(builder_.AddTarget("out4", &err));
1116 EXPECT_TRUE(builder_.Build(&err));
1118 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1121 fs_.Create("in", "");
1122 fs_.RemoveFile("out3");
1124 // Since "in" is missing, out1 will be built. Since "out3" is missing,
1125 // out2 and out3 will be built even though "in" is not touched when built.
1126 // Then, since out2 is rebuilt, out4 should be rebuilt -- the restat on the
1127 // "true" rule should not lead to the "touch" edge writing out2 and out3 being
1129 command_runner_.commands_ran_.clear();
1131 EXPECT_TRUE(builder_.AddTarget("out4", &err));
1133 EXPECT_TRUE(builder_.Build(&err));
1135 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1138 // Test scenario, in which an input file is removed, but output isn't changed
1139 // https://github.com/martine/ninja/issues/295
1140 TEST_F(BuildWithLogTest, RestatMissingInput) {
1141 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1144 " depfile = $out.d\n"
1148 "build out1: true in\n"
1149 "build out2: cc out1\n"));
1151 // Create all necessary files
1152 fs_.Create("in", "");
1154 // The implicit dependencies and the depfile itself
1155 // are newer than the output
1156 TimeStamp restat_mtime = fs_.Tick();
1157 fs_.Create("out1.d", "out1: will.be.deleted restat.file\n");
1158 fs_.Create("will.be.deleted", "");
1159 fs_.Create("restat.file", "");
1161 // Run the build, out1 and out2 get built
1163 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1165 EXPECT_TRUE(builder_.Build(&err));
1166 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1168 // See that an entry in the logfile is created, capturing
1170 BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out1");
1171 ASSERT_TRUE(NULL != log_entry);
1172 ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
1174 // Now remove a file, referenced from depfile, so that target becomes
1175 // dirty, but the output does not change
1176 fs_.RemoveFile("will.be.deleted");
1178 // Trigger the build again - only out1 gets built
1179 command_runner_.commands_ran_.clear();
1181 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1183 EXPECT_TRUE(builder_.Build(&err));
1184 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1186 // Check that the logfile entry remains correctly set
1187 log_entry = build_log_.LookupByOutput("out1");
1188 ASSERT_TRUE(NULL != log_entry);
1189 ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
1192 struct BuildDryRun : public BuildWithLogTest {
1194 config_.dry_run = true;
1198 TEST_F(BuildDryRun, AllCommandsShown) {
1199 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1206 "build out1: cc in\n"
1207 "build out2: true out1\n"
1208 "build out3: cat out2\n"));
1210 fs_.Create("out1", "");
1211 fs_.Create("out2", "");
1212 fs_.Create("out3", "");
1216 fs_.Create("in", "");
1218 // "cc" touches out1, so we should build out2. But because "true" does not
1219 // touch out2, we should cancel the build of out3.
1221 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1223 EXPECT_TRUE(builder_.Build(&err));
1224 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1227 // Test that RSP files are created when & where appropriate and deleted after
1228 // successful execution.
1229 TEST_F(BuildTest, RspFileSuccess)
1231 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1233 " command = cat $rspfile > $out\n"
1234 " rspfile = $rspfile\n"
1235 " rspfile_content = $long_command\n"
1236 "build out1: cat in\n"
1237 "build out2: cat_rsp in\n"
1238 " rspfile = out2.rsp\n"
1239 " long_command = Some very long command\n"));
1241 fs_.Create("out1", "");
1242 fs_.Create("out2", "");
1243 fs_.Create("out3", "");
1247 fs_.Create("in", "");
1250 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1252 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1255 size_t files_created = fs_.files_created_.size();
1256 size_t files_removed = fs_.files_removed_.size();
1258 EXPECT_TRUE(builder_.Build(&err));
1259 ASSERT_EQ(2u, command_runner_.commands_ran_.size()); // cat + cat_rsp
1261 // The RSP file was created
1262 ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1263 ASSERT_EQ(1u, fs_.files_created_.count("out2.rsp"));
1265 // The RSP file was removed
1266 ASSERT_EQ(files_removed + 1, fs_.files_removed_.size());
1267 ASSERT_EQ(1u, fs_.files_removed_.count("out2.rsp"));
1270 // Test that RSP file is created but not removed for commands, which fail
1271 TEST_F(BuildTest, RspFileFailure) {
1272 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1275 " rspfile = $rspfile\n"
1276 " rspfile_content = $long_command\n"
1277 "build out: fail in\n"
1278 " rspfile = out.rsp\n"
1279 " long_command = Another very long command\n"));
1281 fs_.Create("out", "");
1283 fs_.Create("in", "");
1286 EXPECT_TRUE(builder_.AddTarget("out", &err));
1289 size_t files_created = fs_.files_created_.size();
1290 size_t files_removed = fs_.files_removed_.size();
1292 EXPECT_FALSE(builder_.Build(&err));
1293 ASSERT_EQ("subcommand failed", err);
1294 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1296 // The RSP file was created
1297 ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1298 ASSERT_EQ(1u, fs_.files_created_.count("out.rsp"));
1300 // The RSP file was NOT removed
1301 ASSERT_EQ(files_removed, fs_.files_removed_.size());
1302 ASSERT_EQ(0u, fs_.files_removed_.count("out.rsp"));
1304 // The RSP file contains what it should
1305 ASSERT_EQ("Another very long command", fs_.files_["out.rsp"].contents);
1308 // Test that contens of the RSP file behaves like a regular part of
1309 // command line, i.e. triggers a rebuild if changed
1310 TEST_F(BuildWithLogTest, RspFileCmdLineChange) {
1311 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1313 " command = cat $rspfile > $out\n"
1314 " rspfile = $rspfile\n"
1315 " rspfile_content = $long_command\n"
1316 "build out: cat_rsp in\n"
1317 " rspfile = out.rsp\n"
1318 " long_command = Original very long command\n"));
1320 fs_.Create("out", "");
1322 fs_.Create("in", "");
1325 EXPECT_TRUE(builder_.AddTarget("out", &err));
1328 // 1. Build for the 1st time (-> populate log)
1329 EXPECT_TRUE(builder_.Build(&err));
1330 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1332 // 2. Build again (no change)
1333 command_runner_.commands_ran_.clear();
1335 EXPECT_TRUE(builder_.AddTarget("out", &err));
1337 ASSERT_TRUE(builder_.AlreadyUpToDate());
1339 // 3. Alter the entry in the logfile
1340 // (to simulate a change in the command line between 2 builds)
1341 BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out");
1342 ASSERT_TRUE(NULL != log_entry);
1343 ASSERT_NO_FATAL_FAILURE(AssertHash(
1344 "cat out.rsp > out;rspfile=Original very long command",
1345 log_entry->command_hash));
1346 log_entry->command_hash++; // Change the command hash to something else.
1347 // Now expect the target to be rebuilt
1348 command_runner_.commands_ran_.clear();
1350 EXPECT_TRUE(builder_.AddTarget("out", &err));
1352 EXPECT_TRUE(builder_.Build(&err));
1353 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1356 TEST_F(BuildTest, InterruptCleanup) {
1357 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1359 " command = interrupt\n"
1360 "rule touch-interrupt\n"
1361 " command = touch-interrupt\n"
1362 "build out1: interrupt in1\n"
1363 "build out2: touch-interrupt in2\n"));
1365 fs_.Create("out1", "");
1366 fs_.Create("out2", "");
1368 fs_.Create("in1", "");
1369 fs_.Create("in2", "");
1371 // An untouched output of an interrupted command should be retained.
1373 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1375 EXPECT_FALSE(builder_.Build(&err));
1376 EXPECT_EQ("interrupted by user", err);
1378 EXPECT_GT(fs_.Stat("out1"), 0);
1381 // A touched output of an interrupted command should be deleted.
1382 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1384 EXPECT_FALSE(builder_.Build(&err));
1385 EXPECT_EQ("interrupted by user", err);
1387 EXPECT_EQ(0, fs_.Stat("out2"));
1390 TEST_F(BuildTest, PhonyWithNoInputs) {
1391 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1392 "build nonexistent: phony\n"
1393 "build out1: cat || nonexistent\n"
1394 "build out2: cat nonexistent\n"));
1395 fs_.Create("out1", "");
1396 fs_.Create("out2", "");
1398 // out1 should be up to date even though its input is dirty, because its
1399 // order-only dependency has nothing to do.
1401 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1403 EXPECT_TRUE(builder_.AlreadyUpToDate());
1405 // out2 should still be out of date though, because its input is dirty.
1407 command_runner_.commands_ran_.clear();
1409 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1411 EXPECT_TRUE(builder_.Build(&err));
1413 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1416 TEST_F(BuildTest, DepsGccWithEmptyDepfileErrorsOut) {
1417 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1421 "build out: cc\n"));
1425 EXPECT_TRUE(builder_.AddTarget("out", &err));
1427 EXPECT_FALSE(builder_.AlreadyUpToDate());
1429 EXPECT_FALSE(builder_.Build(&err));
1430 ASSERT_EQ("subcommand failed", err);
1431 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1434 TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
1435 EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
1436 status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]"));
1439 TEST_F(BuildTest, FailedDepsParse) {
1440 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1441 "build bad_deps.o: cat in1\n"
1443 " depfile = in1.d\n"));
1446 EXPECT_TRUE(builder_.AddTarget("bad_deps.o", &err));
1449 // These deps will fail to parse, as they should only have one
1450 // path to the left of the colon.
1451 fs_.Create("in1.d", "AAA BBB");
1453 EXPECT_FALSE(builder_.Build(&err));
1454 EXPECT_EQ("subcommand failed", err);
1457 /// Tests of builds involving deps logs necessarily must span
1458 /// multiple builds. We reuse methods on BuildTest but not the
1459 /// builder_ it sets up, because we want pristine objects for
1461 struct BuildWithDepsLogTest : public BuildTest {
1462 BuildWithDepsLogTest() {}
1464 virtual void SetUp() {
1467 temp_dir_.CreateAndEnter("BuildWithDepsLogTest");
1470 virtual void TearDown() {
1471 temp_dir_.Cleanup();
1474 ScopedTempDir temp_dir_;
1476 /// Shadow parent class builder_ so we don't accidentally use it.
1480 /// Run a straightforwad build where the deps log is used.
1481 TEST_F(BuildWithDepsLogTest, Straightforward) {
1483 // Note: in1 was created by the superclass SetUp().
1484 const char* manifest =
1485 "build out: cat in1\n"
1487 " depfile = in1.d\n";
1490 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1491 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1493 // Run the build once, everything should be ok.
1495 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1498 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1499 builder.command_runner_.reset(&command_runner_);
1500 EXPECT_TRUE(builder.AddTarget("out", &err));
1502 fs_.Create("in1.d", "out: in2");
1503 EXPECT_TRUE(builder.Build(&err));
1506 // The deps file should have been removed.
1507 EXPECT_EQ(0, fs_.Stat("in1.d"));
1508 // Recreate it for the next step.
1509 fs_.Create("in1.d", "out: in2");
1511 builder.command_runner_.release();
1516 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1517 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1519 // Touch the file only mentioned in the deps.
1521 fs_.Create("in2", "");
1523 // Run the build again.
1525 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1526 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 command_runner_.commands_ran_.clear();
1531 EXPECT_TRUE(builder.AddTarget("out", &err));
1533 EXPECT_TRUE(builder.Build(&err));
1536 // We should have rebuilt the output due to in2 being
1538 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1540 builder.command_runner_.release();
1544 /// Verify that obsolete dependency info causes a rebuild.
1545 /// 1) Run a successful build where everything has time t, record deps.
1546 /// 2) Move input/output to time t+1 -- despite files in alignment,
1547 /// should still need to rebuild due to deps at older time.
1548 TEST_F(BuildWithDepsLogTest, ObsoleteDeps) {
1550 // Note: in1 was created by the superclass SetUp().
1551 const char* manifest =
1552 "build out: cat in1\n"
1554 " depfile = in1.d\n";
1556 // Run an ordinary build that gathers dependencies.
1557 fs_.Create("in1", "");
1558 fs_.Create("in1.d", "out: ");
1561 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1562 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1564 // Run the build once, everything should be ok.
1566 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1569 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1570 builder.command_runner_.reset(&command_runner_);
1571 EXPECT_TRUE(builder.AddTarget("out", &err));
1573 EXPECT_TRUE(builder.Build(&err));
1577 builder.command_runner_.release();
1580 // Push all files one tick forward so that only the deps are out
1583 fs_.Create("in1", "");
1584 fs_.Create("out", "");
1586 // The deps file should have been removed, so no need to timestamp it.
1587 EXPECT_EQ(0, fs_.Stat("in1.d"));
1591 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1592 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1595 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1596 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1598 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1599 builder.command_runner_.reset(&command_runner_);
1600 command_runner_.commands_ran_.clear();
1601 EXPECT_TRUE(builder.AddTarget("out", &err));
1604 // Recreate the deps file here because the build expects them to exist.
1605 fs_.Create("in1.d", "out: ");
1607 EXPECT_TRUE(builder.Build(&err));
1610 // We should have rebuilt the output due to the deps being
1612 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1614 builder.command_runner_.release();
1618 TEST_F(BuildWithDepsLogTest, DepsIgnoredInDryRun) {
1619 const char* manifest =
1620 "build out: cat in1\n"
1622 " depfile = in1.d\n";
1624 fs_.Create("out", "");
1626 fs_.Create("in1", "");
1629 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1630 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1632 // The deps log is NULL in dry runs.
1633 config_.dry_run = true;
1634 Builder builder(&state, config_, NULL, NULL, &fs_);
1635 builder.command_runner_.reset(&command_runner_);
1636 command_runner_.commands_ran_.clear();
1639 EXPECT_TRUE(builder.AddTarget("out", &err));
1641 EXPECT_TRUE(builder.Build(&err));
1642 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1644 builder.command_runner_.release();
1647 /// Check that a restat rule generating a header cancels compilations correctly.
1648 TEST_F(BuildTest, RestatDepfileDependency) {
1649 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1651 " command = true\n" // Would be "write if out-of-date" in reality.
1653 "build header.h: true header.in\n"
1654 "build out: cat in1\n"
1655 " depfile = in1.d\n"));
1657 fs_.Create("header.h", "");
1658 fs_.Create("in1.d", "out: header.h");
1660 fs_.Create("header.in", "");
1663 EXPECT_TRUE(builder_.AddTarget("out", &err));
1665 EXPECT_TRUE(builder_.Build(&err));
1669 /// Check that a restat rule generating a header cancels compilations correctly,
1671 TEST_F(BuildWithDepsLogTest, RestatDepfileDependencyDepsLog) {
1673 // Note: in1 was created by the superclass SetUp().
1674 const char* manifest =
1676 " command = true\n" // Would be "write if out-of-date" in reality.
1678 "build header.h: true header.in\n"
1679 "build out: cat in1\n"
1681 " depfile = in1.d\n";
1684 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1685 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1687 // Run the build once, everything should be ok.
1689 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1692 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1693 builder.command_runner_.reset(&command_runner_);
1694 EXPECT_TRUE(builder.AddTarget("out", &err));
1696 fs_.Create("in1.d", "out: header.h");
1697 EXPECT_TRUE(builder.Build(&err));
1701 builder.command_runner_.release();
1706 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1707 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1709 // Touch the input of the restat rule.
1711 fs_.Create("header.in", "");
1713 // Run the build again.
1715 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1716 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1718 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1719 builder.command_runner_.reset(&command_runner_);
1720 command_runner_.commands_ran_.clear();
1721 EXPECT_TRUE(builder.AddTarget("out", &err));
1723 EXPECT_TRUE(builder.Build(&err));
1726 // Rule "true" should have run again, but the build of "out" should have
1727 // been cancelled due to restat propagating through the depfile header.
1728 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1730 builder.command_runner_.release();
1734 TEST_F(BuildWithDepsLogTest, DepFileOKDepsLog) {
1736 const char* manifest =
1737 "rule cc\n command = cc $in\n depfile = $out.d\n deps = gcc\n"
1738 "build foo.o: cc foo.c\n";
1740 fs_.Create("foo.c", "");
1744 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1746 // Run the build once, everything should be ok.
1748 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1751 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1752 builder.command_runner_.reset(&command_runner_);
1753 EXPECT_TRUE(builder.AddTarget("foo.o", &err));
1755 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
1756 EXPECT_TRUE(builder.Build(&err));
1760 builder.command_runner_.release();
1765 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1768 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1769 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1772 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1773 builder.command_runner_.reset(&command_runner_);
1775 Edge* edge = state.edges_.back();
1777 state.GetNode("bar.h")->MarkDirty(); // Mark bar.h as missing.
1778 EXPECT_TRUE(builder.AddTarget("foo.o", &err));
1781 // Expect three new edges: one generating foo.o, and two more from
1782 // loading the depfile.
1783 ASSERT_EQ(3, (int)state.edges_.size());
1784 // Expect our edge to now have three inputs: foo.c and two headers.
1785 ASSERT_EQ(3u, edge->inputs_.size());
1787 // Expect the command line we generate to only use the original input.
1788 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
1791 builder.command_runner_.release();