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 /// Rebuild target in the 'working tree' (fs_).
439 /// State of command_runner_ and logs contents (if specified) ARE MODIFIED.
440 /// Handy to check for NOOP builds, and higher-level rebuild tests.
441 void RebuildTarget(const string& target, const char* manifest,
442 const char* log_path = NULL,
443 const char* deps_path = NULL);
445 // Mark a path dirty.
446 void Dirty(const string& path);
448 BuildConfig MakeConfig() {
450 config.verbosity = BuildConfig::QUIET;
455 FakeCommandRunner command_runner_;
456 VirtualFileSystem fs_;
462 void BuildTest::RebuildTarget(const string& target, const char* manifest,
463 const char* log_path, const char* deps_path) {
465 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
466 AssertParse(&state, manifest);
469 BuildLog build_log, *pbuild_log = NULL;
471 ASSERT_TRUE(build_log.Load(log_path, &err));
472 ASSERT_TRUE(build_log.OpenForWrite(log_path, &err));
474 pbuild_log = &build_log;
477 DepsLog deps_log, *pdeps_log = NULL;
479 ASSERT_TRUE(deps_log.Load(deps_path, &state, &err));
480 ASSERT_TRUE(deps_log.OpenForWrite(deps_path, &err));
482 pdeps_log = &deps_log;
485 Builder builder(&state, config_, pbuild_log, pdeps_log, &fs_);
486 EXPECT_TRUE(builder.AddTarget(target, &err));
488 command_runner_.commands_ran_.clear();
489 builder.command_runner_.reset(&command_runner_);
490 bool build_res = builder.Build(&err);
491 builder.command_runner_.release();
492 EXPECT_TRUE(build_res) << "builder.Build(&err)";
495 bool FakeCommandRunner::CanRunMore() {
496 // Only run one at a time.
497 return last_command_ == NULL;
500 bool FakeCommandRunner::StartCommand(Edge* edge) {
501 assert(!last_command_);
502 commands_ran_.push_back(edge->EvaluateCommand());
503 if (edge->rule().name() == "cat" ||
504 edge->rule().name() == "cat_rsp" ||
505 edge->rule().name() == "cc" ||
506 edge->rule().name() == "touch" ||
507 edge->rule().name() == "touch-interrupt") {
508 for (vector<Node*>::iterator out = edge->outputs_.begin();
509 out != edge->outputs_.end(); ++out) {
510 fs_->Create((*out)->path(), "");
512 } else if (edge->rule().name() == "true" ||
513 edge->rule().name() == "fail" ||
514 edge->rule().name() == "interrupt") {
515 // Don't do anything.
517 printf("unknown command\n");
521 last_command_ = edge;
525 bool FakeCommandRunner::WaitForCommand(Result* result) {
529 Edge* edge = last_command_;
532 if (edge->rule().name() == "interrupt" ||
533 edge->rule().name() == "touch-interrupt") {
534 result->status = ExitInterrupted;
538 if (edge->rule().name() == "fail")
539 result->status = ExitFailure;
541 result->status = ExitSuccess;
542 last_command_ = NULL;
546 vector<Edge*> FakeCommandRunner::GetActiveEdges() {
549 edges.push_back(last_command_);
553 void FakeCommandRunner::Abort() {
554 last_command_ = NULL;
557 void BuildTest::Dirty(const string& path) {
558 Node* node = GetNode(path);
561 // If it's an input file, mark that we've already stat()ed it and
563 if (!node->in_edge())
567 TEST_F(BuildTest, NoWork) {
569 EXPECT_TRUE(builder_.AlreadyUpToDate());
572 TEST_F(BuildTest, OneStep) {
573 // Given a dirty target with one ready input,
574 // we should rebuild the target.
577 EXPECT_TRUE(builder_.AddTarget("cat1", &err));
579 EXPECT_TRUE(builder_.Build(&err));
582 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
583 EXPECT_EQ("cat in1 > cat1", command_runner_.commands_ran_[0]);
586 TEST_F(BuildTest, OneStep2) {
587 // Given a target with one dirty input,
588 // we should rebuild the target.
591 EXPECT_TRUE(builder_.AddTarget("cat1", &err));
593 EXPECT_TRUE(builder_.Build(&err));
596 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
597 EXPECT_EQ("cat in1 > cat1", command_runner_.commands_ran_[0]);
600 TEST_F(BuildTest, TwoStep) {
602 EXPECT_TRUE(builder_.AddTarget("cat12", &err));
604 EXPECT_TRUE(builder_.Build(&err));
606 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
607 // Depending on how the pointers work out, we could've ran
608 // the first two commands in either order.
609 EXPECT_TRUE((command_runner_.commands_ran_[0] == "cat in1 > cat1" &&
610 command_runner_.commands_ran_[1] == "cat in1 in2 > cat2") ||
611 (command_runner_.commands_ran_[1] == "cat in1 > cat1" &&
612 command_runner_.commands_ran_[0] == "cat in1 in2 > cat2"));
614 EXPECT_EQ("cat cat1 cat2 > cat12", command_runner_.commands_ran_[2]);
618 // Modifying in2 requires rebuilding one intermediate file
619 // and the final file.
620 fs_.Create("in2", "");
622 EXPECT_TRUE(builder_.AddTarget("cat12", &err));
624 EXPECT_TRUE(builder_.Build(&err));
626 ASSERT_EQ(5u, command_runner_.commands_ran_.size());
627 EXPECT_EQ("cat in1 in2 > cat2", command_runner_.commands_ran_[3]);
628 EXPECT_EQ("cat cat1 cat2 > cat12", command_runner_.commands_ran_[4]);
631 TEST_F(BuildTest, TwoOutputs) {
632 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
634 " command = touch $out\n"
635 "build out1 out2: touch in.txt\n"));
637 fs_.Create("in.txt", "");
640 EXPECT_TRUE(builder_.AddTarget("out1", &err));
642 EXPECT_TRUE(builder_.Build(&err));
644 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
645 EXPECT_EQ("touch out1 out2", command_runner_.commands_ran_[0]);
649 // https://github.com/martine/ninja/issues/148
650 TEST_F(BuildTest, MultiOutIn) {
651 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
653 " command = touch $out\n"
654 "build in1 otherfile: touch in\n"
655 "build out: touch in | in1\n"));
657 fs_.Create("in", "");
659 fs_.Create("in1", "");
662 EXPECT_TRUE(builder_.AddTarget("out", &err));
664 EXPECT_TRUE(builder_.Build(&err));
668 TEST_F(BuildTest, Chain) {
669 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
673 "build c5: cat c4\n"));
675 fs_.Create("c1", "");
678 EXPECT_TRUE(builder_.AddTarget("c5", &err));
680 EXPECT_TRUE(builder_.Build(&err));
682 ASSERT_EQ(4u, command_runner_.commands_ran_.size());
685 command_runner_.commands_ran_.clear();
687 EXPECT_TRUE(builder_.AddTarget("c5", &err));
689 EXPECT_TRUE(builder_.AlreadyUpToDate());
693 fs_.Create("c3", "");
695 command_runner_.commands_ran_.clear();
697 EXPECT_TRUE(builder_.AddTarget("c5", &err));
699 EXPECT_FALSE(builder_.AlreadyUpToDate());
700 EXPECT_TRUE(builder_.Build(&err));
701 ASSERT_EQ(2u, command_runner_.commands_ran_.size()); // 3->4, 4->5
704 TEST_F(BuildTest, MissingInput) {
705 // Input is referenced by build file, but no rule for it.
708 EXPECT_FALSE(builder_.AddTarget("cat1", &err));
709 EXPECT_EQ("'in1', needed by 'cat1', missing and no known rule to make it",
713 TEST_F(BuildTest, MissingTarget) {
714 // Target is not referenced by build file.
716 EXPECT_FALSE(builder_.AddTarget("meow", &err));
717 EXPECT_EQ("unknown target: 'meow'", err);
720 TEST_F(BuildTest, MakeDirs) {
724 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
725 "build subdir\\dir2\\file: cat in1\n"));
726 EXPECT_TRUE(builder_.AddTarget("subdir\\dir2\\file", &err));
728 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
729 "build subdir/dir2/file: cat in1\n"));
730 EXPECT_TRUE(builder_.AddTarget("subdir/dir2/file", &err));
734 EXPECT_TRUE(builder_.Build(&err));
736 ASSERT_EQ(2u, fs_.directories_made_.size());
737 EXPECT_EQ("subdir", fs_.directories_made_[0]);
739 EXPECT_EQ("subdir\\dir2", fs_.directories_made_[1]);
741 EXPECT_EQ("subdir/dir2", fs_.directories_made_[1]);
745 TEST_F(BuildTest, DepFileMissing) {
747 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
748 "rule cc\n command = cc $in\n depfile = $out.d\n"
749 "build foo.o: cc foo.c\n"));
750 fs_.Create("foo.c", "");
752 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
754 ASSERT_EQ(1u, fs_.files_read_.size());
755 EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
758 TEST_F(BuildTest, DepFileOK) {
760 int orig_edges = state_.edges_.size();
761 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
762 "rule cc\n command = cc $in\n depfile = $out.d\n"
763 "build foo.o: cc foo.c\n"));
764 Edge* edge = state_.edges_.back();
766 fs_.Create("foo.c", "");
767 GetNode("bar.h")->MarkDirty(); // Mark bar.h as missing.
768 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
769 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
771 ASSERT_EQ(1u, fs_.files_read_.size());
772 EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
774 // Expect three new edges: one generating foo.o, and two more from
775 // loading the depfile.
776 ASSERT_EQ(orig_edges + 3, (int)state_.edges_.size());
777 // Expect our edge to now have three inputs: foo.c and two headers.
778 ASSERT_EQ(3u, edge->inputs_.size());
780 // Expect the command line we generate to only use the original input.
781 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
784 TEST_F(BuildTest, DepFileParseError) {
786 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
787 "rule cc\n command = cc $in\n depfile = $out.d\n"
788 "build foo.o: cc foo.c\n"));
789 fs_.Create("foo.c", "");
790 fs_.Create("foo.o.d", "randomtext\n");
791 EXPECT_FALSE(builder_.AddTarget("foo.o", &err));
792 EXPECT_EQ("expected depfile 'foo.o.d' to mention 'foo.o', got 'randomtext'",
796 TEST_F(BuildTest, OrderOnlyDeps) {
798 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
799 "rule cc\n command = cc $in\n depfile = $out.d\n"
800 "build foo.o: cc foo.c || otherfile\n"));
801 Edge* edge = state_.edges_.back();
803 fs_.Create("foo.c", "");
804 fs_.Create("otherfile", "");
805 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
806 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
809 // One explicit, two implicit, one order only.
810 ASSERT_EQ(4u, edge->inputs_.size());
811 EXPECT_EQ(2, edge->implicit_deps_);
812 EXPECT_EQ(1, edge->order_only_deps_);
813 // Verify the inputs are in the order we expect
814 // (explicit then implicit then orderonly).
815 EXPECT_EQ("foo.c", edge->inputs_[0]->path());
816 EXPECT_EQ("blah.h", edge->inputs_[1]->path());
817 EXPECT_EQ("bar.h", edge->inputs_[2]->path());
818 EXPECT_EQ("otherfile", edge->inputs_[3]->path());
820 // Expect the command line we generate to only use the original input.
821 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
823 // explicit dep dirty, expect a rebuild.
824 EXPECT_TRUE(builder_.Build(&err));
826 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
830 // Recreate the depfile, as it should have been deleted by the build.
831 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
833 // implicit dep dirty, expect a rebuild.
834 fs_.Create("blah.h", "");
835 fs_.Create("bar.h", "");
836 command_runner_.commands_ran_.clear();
838 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
839 EXPECT_TRUE(builder_.Build(&err));
841 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
845 // Recreate the depfile, as it should have been deleted by the build.
846 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
848 // order only dep dirty, no rebuild.
849 fs_.Create("otherfile", "");
850 command_runner_.commands_ran_.clear();
852 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
854 EXPECT_TRUE(builder_.AlreadyUpToDate());
856 // implicit dep missing, expect rebuild.
857 fs_.RemoveFile("bar.h");
858 command_runner_.commands_ran_.clear();
860 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
861 EXPECT_TRUE(builder_.Build(&err));
863 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
866 TEST_F(BuildTest, RebuildOrderOnlyDeps) {
868 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
869 "rule cc\n command = cc $in\n"
870 "rule true\n command = true\n"
871 "build oo.h: cc oo.h.in\n"
872 "build foo.o: cc foo.c || oo.h\n"));
874 fs_.Create("foo.c", "");
875 fs_.Create("oo.h.in", "");
877 // foo.o and order-only dep dirty, build both.
878 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
879 EXPECT_TRUE(builder_.Build(&err));
881 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
883 // all clean, no rebuild.
884 command_runner_.commands_ran_.clear();
886 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
888 EXPECT_TRUE(builder_.AlreadyUpToDate());
890 // order-only dep missing, build it only.
891 fs_.RemoveFile("oo.h");
892 command_runner_.commands_ran_.clear();
894 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
895 EXPECT_TRUE(builder_.Build(&err));
897 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
898 ASSERT_EQ("cc oo.h.in", command_runner_.commands_ran_[0]);
902 // order-only dep dirty, build it only.
903 fs_.Create("oo.h.in", "");
904 command_runner_.commands_ran_.clear();
906 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
907 EXPECT_TRUE(builder_.Build(&err));
909 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
910 ASSERT_EQ("cc oo.h.in", command_runner_.commands_ran_[0]);
913 TEST_F(BuildTest, Phony) {
915 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
916 "build out: cat bar.cc\n"
917 "build all: phony out\n"));
918 fs_.Create("bar.cc", "");
920 EXPECT_TRUE(builder_.AddTarget("all", &err));
923 // Only one command to run, because phony runs no command.
924 EXPECT_FALSE(builder_.AlreadyUpToDate());
925 EXPECT_TRUE(builder_.Build(&err));
927 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
930 TEST_F(BuildTest, PhonyNoWork) {
932 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
933 "build out: cat bar.cc\n"
934 "build all: phony out\n"));
935 fs_.Create("bar.cc", "");
936 fs_.Create("out", "");
938 EXPECT_TRUE(builder_.AddTarget("all", &err));
940 EXPECT_TRUE(builder_.AlreadyUpToDate());
943 TEST_F(BuildTest, Fail) {
944 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
947 "build out1: fail\n"));
950 EXPECT_TRUE(builder_.AddTarget("out1", &err));
953 EXPECT_FALSE(builder_.Build(&err));
954 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
955 ASSERT_EQ("subcommand failed", err);
958 TEST_F(BuildTest, SwallowFailures) {
959 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
965 "build all: phony out1 out2 out3\n"));
967 // Swallow two failures, die on the third.
968 config_.failures_allowed = 3;
971 EXPECT_TRUE(builder_.AddTarget("all", &err));
974 EXPECT_FALSE(builder_.Build(&err));
975 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
976 ASSERT_EQ("subcommands failed", err);
979 TEST_F(BuildTest, SwallowFailuresLimit) {
980 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
986 "build final: cat out1 out2 out3\n"));
988 // Swallow ten failures; we should stop before building final.
989 config_.failures_allowed = 11;
992 EXPECT_TRUE(builder_.AddTarget("final", &err));
995 EXPECT_FALSE(builder_.Build(&err));
996 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
997 ASSERT_EQ("cannot make progress due to previous errors", err);
1000 struct BuildWithLogTest : public BuildTest {
1001 BuildWithLogTest() {
1002 builder_.SetBuildLog(&build_log_);
1005 BuildLog build_log_;
1008 TEST_F(BuildWithLogTest, NotInLogButOnDisk) {
1009 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1012 "build out1: cc in\n"));
1014 // Create input/output that would be considered up to date when
1015 // not considering the command line hash.
1016 fs_.Create("in", "");
1017 fs_.Create("out1", "");
1020 // Because it's not in the log, it should not be up-to-date until
1022 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1023 EXPECT_FALSE(builder_.AlreadyUpToDate());
1025 command_runner_.commands_ran_.clear();
1028 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1029 EXPECT_TRUE(builder_.Build(&err));
1030 EXPECT_TRUE(builder_.AlreadyUpToDate());
1033 TEST_F(BuildWithLogTest, RestatTest) {
1034 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1041 "build out1: cc in\n"
1042 "build out2: true out1\n"
1043 "build out3: cat out2\n"));
1045 fs_.Create("out1", "");
1046 fs_.Create("out2", "");
1047 fs_.Create("out3", "");
1051 fs_.Create("in", "");
1053 // Do a pre-build so that there's commands in the log for the outputs,
1054 // otherwise, the lack of an entry in the build log will cause out3 to rebuild
1055 // regardless of restat.
1057 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1059 EXPECT_TRUE(builder_.Build(&err));
1061 EXPECT_EQ("[3/3]", builder_.status_->FormatProgressStatus("[%s/%t]"));
1062 command_runner_.commands_ran_.clear();
1067 fs_.Create("in", "");
1068 // "cc" touches out1, so we should build out2. But because "true" does not
1069 // touch out2, we should cancel the build of out3.
1070 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1072 EXPECT_TRUE(builder_.Build(&err));
1073 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1075 // If we run again, it should be a no-op, because the build log has recorded
1076 // that we've already built out2 with an input timestamp of 2 (from out1).
1077 command_runner_.commands_ran_.clear();
1079 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1081 EXPECT_TRUE(builder_.AlreadyUpToDate());
1085 fs_.Create("in", "");
1087 // The build log entry should not, however, prevent us from rebuilding out2
1089 command_runner_.commands_ran_.clear();
1091 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1093 EXPECT_TRUE(builder_.Build(&err));
1094 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1097 TEST_F(BuildWithLogTest, RestatMissingFile) {
1098 // If a restat rule doesn't create its output, and the output didn't
1099 // exist before the rule was run, consider that behavior equivalent
1100 // to a rule that doesn't modify its existent output file.
1102 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1108 "build out1: true in\n"
1109 "build out2: cc out1\n"));
1111 fs_.Create("in", "");
1112 fs_.Create("out2", "");
1114 // Do a pre-build so that there's commands in the log for the outputs,
1115 // otherwise, the lack of an entry in the build log will cause out2 to rebuild
1116 // regardless of restat.
1118 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1120 EXPECT_TRUE(builder_.Build(&err));
1122 command_runner_.commands_ran_.clear();
1126 fs_.Create("in", "");
1127 fs_.Create("out2", "");
1129 // Run a build, expect only the first command to run.
1130 // It doesn't touch its output (due to being the "true" command), so
1131 // we shouldn't run the dependent build.
1132 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1134 EXPECT_TRUE(builder_.Build(&err));
1135 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1138 TEST_F(BuildWithLogTest, RestatSingleDependentOutputDirty) {
1139 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1144 " command = touch\n"
1145 "build out1: true in\n"
1146 "build out2 out3: touch out1\n"
1147 "build out4: touch out2\n"
1150 // Create the necessary files
1151 fs_.Create("in", "");
1154 EXPECT_TRUE(builder_.AddTarget("out4", &err));
1156 EXPECT_TRUE(builder_.Build(&err));
1158 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1161 fs_.Create("in", "");
1162 fs_.RemoveFile("out3");
1164 // Since "in" is missing, out1 will be built. Since "out3" is missing,
1165 // out2 and out3 will be built even though "in" is not touched when built.
1166 // Then, since out2 is rebuilt, out4 should be rebuilt -- the restat on the
1167 // "true" rule should not lead to the "touch" edge writing out2 and out3 being
1169 command_runner_.commands_ran_.clear();
1171 EXPECT_TRUE(builder_.AddTarget("out4", &err));
1173 EXPECT_TRUE(builder_.Build(&err));
1175 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1178 // Test scenario, in which an input file is removed, but output isn't changed
1179 // https://github.com/martine/ninja/issues/295
1180 TEST_F(BuildWithLogTest, RestatMissingInput) {
1181 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1184 " depfile = $out.d\n"
1188 "build out1: true in\n"
1189 "build out2: cc out1\n"));
1191 // Create all necessary files
1192 fs_.Create("in", "");
1194 // The implicit dependencies and the depfile itself
1195 // are newer than the output
1196 TimeStamp restat_mtime = fs_.Tick();
1197 fs_.Create("out1.d", "out1: will.be.deleted restat.file\n");
1198 fs_.Create("will.be.deleted", "");
1199 fs_.Create("restat.file", "");
1201 // Run the build, out1 and out2 get built
1203 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1205 EXPECT_TRUE(builder_.Build(&err));
1206 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1208 // See that an entry in the logfile is created, capturing
1210 BuildLog::LogEntry* log_entry = build_log_.LookupByOutput("out1");
1211 ASSERT_TRUE(NULL != log_entry);
1212 ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
1214 // Now remove a file, referenced from depfile, so that target becomes
1215 // dirty, but the output does not change
1216 fs_.RemoveFile("will.be.deleted");
1218 // Trigger the build again - only out1 gets built
1219 command_runner_.commands_ran_.clear();
1221 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1223 EXPECT_TRUE(builder_.Build(&err));
1224 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1226 // Check that the logfile entry remains correctly set
1227 log_entry = build_log_.LookupByOutput("out1");
1228 ASSERT_TRUE(NULL != log_entry);
1229 ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
1232 struct BuildDryRun : public BuildWithLogTest {
1234 config_.dry_run = true;
1238 TEST_F(BuildDryRun, AllCommandsShown) {
1239 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1246 "build out1: cc in\n"
1247 "build out2: true out1\n"
1248 "build out3: cat out2\n"));
1250 fs_.Create("out1", "");
1251 fs_.Create("out2", "");
1252 fs_.Create("out3", "");
1256 fs_.Create("in", "");
1258 // "cc" touches out1, so we should build out2. But because "true" does not
1259 // touch out2, we should cancel the build of out3.
1261 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1263 EXPECT_TRUE(builder_.Build(&err));
1264 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1267 // Test that RSP files are created when & where appropriate and deleted after
1268 // successful execution.
1269 TEST_F(BuildTest, RspFileSuccess)
1271 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1273 " command = cat $rspfile > $out\n"
1274 " rspfile = $rspfile\n"
1275 " rspfile_content = $long_command\n"
1276 "build out1: cat in\n"
1277 "build out2: cat_rsp in\n"
1278 " rspfile = out2.rsp\n"
1279 " long_command = Some very long command\n"));
1281 fs_.Create("out1", "");
1282 fs_.Create("out2", "");
1283 fs_.Create("out3", "");
1287 fs_.Create("in", "");
1290 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1292 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1295 size_t files_created = fs_.files_created_.size();
1296 size_t files_removed = fs_.files_removed_.size();
1298 EXPECT_TRUE(builder_.Build(&err));
1299 ASSERT_EQ(2u, command_runner_.commands_ran_.size()); // cat + cat_rsp
1301 // The RSP file was created
1302 ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1303 ASSERT_EQ(1u, fs_.files_created_.count("out2.rsp"));
1305 // The RSP file was removed
1306 ASSERT_EQ(files_removed + 1, fs_.files_removed_.size());
1307 ASSERT_EQ(1u, fs_.files_removed_.count("out2.rsp"));
1310 // Test that RSP file is created but not removed for commands, which fail
1311 TEST_F(BuildTest, RspFileFailure) {
1312 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1315 " rspfile = $rspfile\n"
1316 " rspfile_content = $long_command\n"
1317 "build out: fail in\n"
1318 " rspfile = out.rsp\n"
1319 " long_command = Another very long command\n"));
1321 fs_.Create("out", "");
1323 fs_.Create("in", "");
1326 EXPECT_TRUE(builder_.AddTarget("out", &err));
1329 size_t files_created = fs_.files_created_.size();
1330 size_t files_removed = fs_.files_removed_.size();
1332 EXPECT_FALSE(builder_.Build(&err));
1333 ASSERT_EQ("subcommand failed", err);
1334 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1336 // The RSP file was created
1337 ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1338 ASSERT_EQ(1u, fs_.files_created_.count("out.rsp"));
1340 // The RSP file was NOT removed
1341 ASSERT_EQ(files_removed, fs_.files_removed_.size());
1342 ASSERT_EQ(0u, fs_.files_removed_.count("out.rsp"));
1344 // The RSP file contains what it should
1345 ASSERT_EQ("Another very long command", fs_.files_["out.rsp"].contents);
1348 // Test that contens of the RSP file behaves like a regular part of
1349 // command line, i.e. triggers a rebuild if changed
1350 TEST_F(BuildWithLogTest, RspFileCmdLineChange) {
1351 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1353 " command = cat $rspfile > $out\n"
1354 " rspfile = $rspfile\n"
1355 " rspfile_content = $long_command\n"
1356 "build out: cat_rsp in\n"
1357 " rspfile = out.rsp\n"
1358 " long_command = Original very long command\n"));
1360 fs_.Create("out", "");
1362 fs_.Create("in", "");
1365 EXPECT_TRUE(builder_.AddTarget("out", &err));
1368 // 1. Build for the 1st time (-> populate log)
1369 EXPECT_TRUE(builder_.Build(&err));
1370 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1372 // 2. Build again (no change)
1373 command_runner_.commands_ran_.clear();
1375 EXPECT_TRUE(builder_.AddTarget("out", &err));
1377 ASSERT_TRUE(builder_.AlreadyUpToDate());
1379 // 3. Alter the entry in the logfile
1380 // (to simulate a change in the command line between 2 builds)
1381 BuildLog::LogEntry* log_entry = build_log_.LookupByOutput("out");
1382 ASSERT_TRUE(NULL != log_entry);
1383 ASSERT_NO_FATAL_FAILURE(AssertHash(
1384 "cat out.rsp > out;rspfile=Original very long command",
1385 log_entry->command_hash));
1386 log_entry->command_hash++; // Change the command hash to something else.
1387 // Now expect the target to be rebuilt
1388 command_runner_.commands_ran_.clear();
1390 EXPECT_TRUE(builder_.AddTarget("out", &err));
1392 EXPECT_TRUE(builder_.Build(&err));
1393 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1396 TEST_F(BuildTest, InterruptCleanup) {
1397 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1399 " command = interrupt\n"
1400 "rule touch-interrupt\n"
1401 " command = touch-interrupt\n"
1402 "build out1: interrupt in1\n"
1403 "build out2: touch-interrupt in2\n"));
1405 fs_.Create("out1", "");
1406 fs_.Create("out2", "");
1408 fs_.Create("in1", "");
1409 fs_.Create("in2", "");
1411 // An untouched output of an interrupted command should be retained.
1413 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1415 EXPECT_FALSE(builder_.Build(&err));
1416 EXPECT_EQ("interrupted by user", err);
1418 EXPECT_GT(fs_.Stat("out1"), 0);
1421 // A touched output of an interrupted command should be deleted.
1422 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1424 EXPECT_FALSE(builder_.Build(&err));
1425 EXPECT_EQ("interrupted by user", err);
1427 EXPECT_EQ(0, fs_.Stat("out2"));
1430 TEST_F(BuildTest, PhonyWithNoInputs) {
1431 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1432 "build nonexistent: phony\n"
1433 "build out1: cat || nonexistent\n"
1434 "build out2: cat nonexistent\n"));
1435 fs_.Create("out1", "");
1436 fs_.Create("out2", "");
1438 // out1 should be up to date even though its input is dirty, because its
1439 // order-only dependency has nothing to do.
1441 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1443 EXPECT_TRUE(builder_.AlreadyUpToDate());
1445 // out2 should still be out of date though, because its input is dirty.
1447 command_runner_.commands_ran_.clear();
1449 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1451 EXPECT_TRUE(builder_.Build(&err));
1453 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1456 TEST_F(BuildTest, DepsGccWithEmptyDepfileErrorsOut) {
1457 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1461 "build out: cc\n"));
1465 EXPECT_TRUE(builder_.AddTarget("out", &err));
1467 EXPECT_FALSE(builder_.AlreadyUpToDate());
1469 EXPECT_FALSE(builder_.Build(&err));
1470 ASSERT_EQ("subcommand failed", err);
1471 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1474 TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
1475 EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
1476 status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]"));
1479 TEST_F(BuildTest, FailedDepsParse) {
1480 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1481 "build bad_deps.o: cat in1\n"
1483 " depfile = in1.d\n"));
1486 EXPECT_TRUE(builder_.AddTarget("bad_deps.o", &err));
1489 // These deps will fail to parse, as they should only have one
1490 // path to the left of the colon.
1491 fs_.Create("in1.d", "AAA BBB");
1493 EXPECT_FALSE(builder_.Build(&err));
1494 EXPECT_EQ("subcommand failed", err);
1497 /// Tests of builds involving deps logs necessarily must span
1498 /// multiple builds. We reuse methods on BuildTest but not the
1499 /// builder_ it sets up, because we want pristine objects for
1501 struct BuildWithDepsLogTest : public BuildTest {
1502 BuildWithDepsLogTest() {}
1504 virtual void SetUp() {
1507 temp_dir_.CreateAndEnter("BuildWithDepsLogTest");
1510 virtual void TearDown() {
1511 temp_dir_.Cleanup();
1514 ScopedTempDir temp_dir_;
1516 /// Shadow parent class builder_ so we don't accidentally use it.
1520 /// Run a straightforwad build where the deps log is used.
1521 TEST_F(BuildWithDepsLogTest, Straightforward) {
1523 // Note: in1 was created by the superclass SetUp().
1524 const char* manifest =
1525 "build out: cat in1\n"
1527 " depfile = in1.d\n";
1530 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1531 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1533 // Run the build once, everything should be ok.
1535 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1538 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1539 builder.command_runner_.reset(&command_runner_);
1540 EXPECT_TRUE(builder.AddTarget("out", &err));
1542 fs_.Create("in1.d", "out: in2");
1543 EXPECT_TRUE(builder.Build(&err));
1546 // The deps file should have been removed.
1547 EXPECT_EQ(0, fs_.Stat("in1.d"));
1548 // Recreate it for the next step.
1549 fs_.Create("in1.d", "out: in2");
1551 builder.command_runner_.release();
1556 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1557 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1559 // Touch the file only mentioned in the deps.
1561 fs_.Create("in2", "");
1563 // Run the build again.
1565 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1566 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1568 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1569 builder.command_runner_.reset(&command_runner_);
1570 command_runner_.commands_ran_.clear();
1571 EXPECT_TRUE(builder.AddTarget("out", &err));
1573 EXPECT_TRUE(builder.Build(&err));
1576 // We should have rebuilt the output due to in2 being
1578 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1580 builder.command_runner_.release();
1584 /// Verify that obsolete dependency info causes a rebuild.
1585 /// 1) Run a successful build where everything has time t, record deps.
1586 /// 2) Move input/output to time t+1 -- despite files in alignment,
1587 /// should still need to rebuild due to deps at older time.
1588 TEST_F(BuildWithDepsLogTest, ObsoleteDeps) {
1590 // Note: in1 was created by the superclass SetUp().
1591 const char* manifest =
1592 "build out: cat in1\n"
1594 " depfile = in1.d\n";
1596 // Run an ordinary build that gathers dependencies.
1597 fs_.Create("in1", "");
1598 fs_.Create("in1.d", "out: ");
1601 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1602 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1604 // Run the build once, everything should be ok.
1606 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1609 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1610 builder.command_runner_.reset(&command_runner_);
1611 EXPECT_TRUE(builder.AddTarget("out", &err));
1613 EXPECT_TRUE(builder.Build(&err));
1617 builder.command_runner_.release();
1620 // Push all files one tick forward so that only the deps are out
1623 fs_.Create("in1", "");
1624 fs_.Create("out", "");
1626 // The deps file should have been removed, so no need to timestamp it.
1627 EXPECT_EQ(0, fs_.Stat("in1.d"));
1631 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1632 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1635 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1636 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1638 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1639 builder.command_runner_.reset(&command_runner_);
1640 command_runner_.commands_ran_.clear();
1641 EXPECT_TRUE(builder.AddTarget("out", &err));
1644 // Recreate the deps file here because the build expects them to exist.
1645 fs_.Create("in1.d", "out: ");
1647 EXPECT_TRUE(builder.Build(&err));
1650 // We should have rebuilt the output due to the deps being
1652 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1654 builder.command_runner_.release();
1658 TEST_F(BuildWithDepsLogTest, DepsIgnoredInDryRun) {
1659 const char* manifest =
1660 "build out: cat in1\n"
1662 " depfile = in1.d\n";
1664 fs_.Create("out", "");
1666 fs_.Create("in1", "");
1669 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1670 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1672 // The deps log is NULL in dry runs.
1673 config_.dry_run = true;
1674 Builder builder(&state, config_, NULL, NULL, &fs_);
1675 builder.command_runner_.reset(&command_runner_);
1676 command_runner_.commands_ran_.clear();
1679 EXPECT_TRUE(builder.AddTarget("out", &err));
1681 EXPECT_TRUE(builder.Build(&err));
1682 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1684 builder.command_runner_.release();
1687 /// Check that a restat rule generating a header cancels compilations correctly.
1688 TEST_F(BuildTest, RestatDepfileDependency) {
1689 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1691 " command = true\n" // Would be "write if out-of-date" in reality.
1693 "build header.h: true header.in\n"
1694 "build out: cat in1\n"
1695 " depfile = in1.d\n"));
1697 fs_.Create("header.h", "");
1698 fs_.Create("in1.d", "out: header.h");
1700 fs_.Create("header.in", "");
1703 EXPECT_TRUE(builder_.AddTarget("out", &err));
1705 EXPECT_TRUE(builder_.Build(&err));
1709 /// Check that a restat rule generating a header cancels compilations correctly,
1711 TEST_F(BuildWithDepsLogTest, RestatDepfileDependencyDepsLog) {
1713 // Note: in1 was created by the superclass SetUp().
1714 const char* manifest =
1716 " command = true\n" // Would be "write if out-of-date" in reality.
1718 "build header.h: true header.in\n"
1719 "build out: cat in1\n"
1721 " depfile = in1.d\n";
1724 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1725 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1727 // Run the build once, everything should be ok.
1729 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1732 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1733 builder.command_runner_.reset(&command_runner_);
1734 EXPECT_TRUE(builder.AddTarget("out", &err));
1736 fs_.Create("in1.d", "out: header.h");
1737 EXPECT_TRUE(builder.Build(&err));
1741 builder.command_runner_.release();
1746 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1747 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1749 // Touch the input of the restat rule.
1751 fs_.Create("header.in", "");
1753 // Run the build again.
1755 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1756 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1758 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1759 builder.command_runner_.reset(&command_runner_);
1760 command_runner_.commands_ran_.clear();
1761 EXPECT_TRUE(builder.AddTarget("out", &err));
1763 EXPECT_TRUE(builder.Build(&err));
1766 // Rule "true" should have run again, but the build of "out" should have
1767 // been cancelled due to restat propagating through the depfile header.
1768 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1770 builder.command_runner_.release();
1774 TEST_F(BuildWithDepsLogTest, DepFileOKDepsLog) {
1776 const char* manifest =
1777 "rule cc\n command = cc $in\n depfile = $out.d\n deps = gcc\n"
1778 "build foo.o: cc foo.c\n";
1780 fs_.Create("foo.c", "");
1784 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1786 // Run the build once, everything should be ok.
1788 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1791 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1792 builder.command_runner_.reset(&command_runner_);
1793 EXPECT_TRUE(builder.AddTarget("foo.o", &err));
1795 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
1796 EXPECT_TRUE(builder.Build(&err));
1800 builder.command_runner_.release();
1805 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1808 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1809 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1812 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1813 builder.command_runner_.reset(&command_runner_);
1815 Edge* edge = state.edges_.back();
1817 state.GetNode("bar.h")->MarkDirty(); // Mark bar.h as missing.
1818 EXPECT_TRUE(builder.AddTarget("foo.o", &err));
1821 // Expect three new edges: one generating foo.o, and two more from
1822 // loading the depfile.
1823 ASSERT_EQ(3u, state.edges_.size());
1824 // Expect our edge to now have three inputs: foo.c and two headers.
1825 ASSERT_EQ(3u, edge->inputs_.size());
1827 // Expect the command line we generate to only use the original input.
1828 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
1831 builder.command_runner_.release();
1835 /// Check that a restat rule doesn't clear an edge if the depfile is missing.
1836 /// Follows from: https://github.com/martine/ninja/issues/603
1837 TEST_F(BuildTest, RestatMissingDepfile) {
1838 const char* manifest =
1840 " command = true\n" // Would be "write if out-of-date" in reality.
1842 "build header.h: true header.in\n"
1843 "build out: cat header.h\n"
1844 " depfile = out.d\n";
1846 fs_.Create("header.h", "");
1848 fs_.Create("out", "");
1849 fs_.Create("header.in", "");
1851 // Normally, only 'header.h' would be rebuilt, as
1852 // its rule doesn't touch the output and has 'restat=1' set.
1853 // But we are also missing the depfile for 'out',
1854 // which should force its command to run anyway!
1855 RebuildTarget("out", manifest);
1856 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1859 /// Check that a restat rule doesn't clear an edge if the deps are missing.
1860 /// https://github.com/martine/ninja/issues/603
1861 TEST_F(BuildWithDepsLogTest, RestatMissingDepfileDepslog) {
1863 const char* manifest =
1865 " command = true\n" // Would be "write if out-of-date" in reality.
1867 "build header.h: true header.in\n"
1868 "build out: cat header.h\n"
1870 " depfile = out.d\n";
1872 // Build once to populate ninja deps logs from out.d
1873 fs_.Create("header.in", "");
1874 fs_.Create("out.d", "out: header.h");
1875 fs_.Create("header.h", "");
1877 RebuildTarget("out", manifest, "build_log", "ninja_deps");
1878 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1880 // Sanity: this rebuild should be NOOP
1881 RebuildTarget("out", manifest, "build_log", "ninja_deps");
1882 ASSERT_EQ(0u, command_runner_.commands_ran_.size());
1884 // Touch 'header.in', blank dependencies log (create a different one).
1885 // Building header.h triggers 'restat' outputs cleanup.
1886 // Validate that out is rebuilt netherless, as deps are missing.
1888 fs_.Create("header.in", "");
1890 // (switch to a new blank deps_log "ninja_deps2")
1891 RebuildTarget("out", manifest, "build_log", "ninja_deps2");
1892 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1894 // Sanity: this build should be NOOP
1895 RebuildTarget("out", manifest, "build_log", "ninja_deps2");
1896 ASSERT_EQ(0u, command_runner_.commands_ran_.size());
1898 // Check that invalidating deps by target timestamp also works here
1899 // Repeat the test but touch target instead of blanking the log.
1901 fs_.Create("header.in", "");
1902 fs_.Create("out", "");
1903 RebuildTarget("out", manifest, "build_log", "ninja_deps2");
1904 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1906 // And this build should be NOOP again
1907 RebuildTarget("out", manifest, "build_log", "ninja_deps2");
1908 ASSERT_EQ(0u, command_runner_.commands_ran_.size());