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.
19 #include "build_log.h"
24 struct CompareEdgesByOutput {
25 static bool cmp(const Edge* a, const Edge* b) {
26 return a->outputs_[0]->path() < b->outputs_[0]->path();
30 /// Fixture for tests involving Plan.
31 // Though Plan doesn't use State, it's useful to have one around
32 // to create Nodes and Edges.
33 struct PlanTest : public StateTestWithBuiltinRules {
36 /// Because FindWork does not return Edges in any sort of predictable order,
37 // provide a means to get available Edges in order and in a format which is
38 // easy to write tests around.
39 void FindWorkSorted(deque<Edge*>* ret, int count) {
40 for (int i = 0; i < count; ++i) {
41 ASSERT_TRUE(plan_.more_to_do());
42 Edge* edge = plan_.FindWork();
46 ASSERT_FALSE(plan_.FindWork());
47 sort(ret->begin(), ret->end(), CompareEdgesByOutput::cmp);
50 void TestPoolWithDepthOne(const char *test_case);
53 TEST_F(PlanTest, Basic) {
54 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
55 "build out: cat mid\n"
56 "build mid: cat in\n"));
57 GetNode("mid")->MarkDirty();
58 GetNode("out")->MarkDirty();
60 EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
62 ASSERT_TRUE(plan_.more_to_do());
64 Edge* edge = plan_.FindWork();
66 ASSERT_EQ("in", edge->inputs_[0]->path());
67 ASSERT_EQ("mid", edge->outputs_[0]->path());
69 ASSERT_FALSE(plan_.FindWork());
71 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
74 edge = plan_.FindWork();
76 ASSERT_EQ("mid", edge->inputs_[0]->path());
77 ASSERT_EQ("out", edge->outputs_[0]->path());
79 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
82 ASSERT_FALSE(plan_.more_to_do());
83 edge = plan_.FindWork();
87 // Test that two outputs from one rule can be handled as inputs to the next.
88 TEST_F(PlanTest, DoubleOutputDirect) {
89 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
90 "build out: cat mid1 mid2\n"
91 "build mid1 mid2: cat in\n"));
92 GetNode("mid1")->MarkDirty();
93 GetNode("mid2")->MarkDirty();
94 GetNode("out")->MarkDirty();
97 EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
99 ASSERT_TRUE(plan_.more_to_do());
102 edge = plan_.FindWork();
103 ASSERT_TRUE(edge); // cat in
104 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
107 edge = plan_.FindWork();
108 ASSERT_TRUE(edge); // cat mid1 mid2
109 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
112 edge = plan_.FindWork();
113 ASSERT_FALSE(edge); // done
116 // Test that two outputs from one rule can eventually be routed to another.
117 TEST_F(PlanTest, DoubleOutputIndirect) {
118 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
119 "build out: cat b1 b2\n"
122 "build a1 a2: cat in\n"));
123 GetNode("a1")->MarkDirty();
124 GetNode("a2")->MarkDirty();
125 GetNode("b1")->MarkDirty();
126 GetNode("b2")->MarkDirty();
127 GetNode("out")->MarkDirty();
129 EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
131 ASSERT_TRUE(plan_.more_to_do());
134 edge = plan_.FindWork();
135 ASSERT_TRUE(edge); // cat in
136 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
139 edge = plan_.FindWork();
140 ASSERT_TRUE(edge); // cat a1
141 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
144 edge = plan_.FindWork();
145 ASSERT_TRUE(edge); // cat a2
146 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
149 edge = plan_.FindWork();
150 ASSERT_TRUE(edge); // cat b1 b2
151 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
154 edge = plan_.FindWork();
155 ASSERT_FALSE(edge); // done
158 // Test that two edges from one output can both execute.
159 TEST_F(PlanTest, DoubleDependent) {
160 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
161 "build out: cat a1 a2\n"
162 "build a1: cat mid\n"
163 "build a2: cat mid\n"
164 "build mid: cat in\n"));
165 GetNode("mid")->MarkDirty();
166 GetNode("a1")->MarkDirty();
167 GetNode("a2")->MarkDirty();
168 GetNode("out")->MarkDirty();
171 EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
173 ASSERT_TRUE(plan_.more_to_do());
176 edge = plan_.FindWork();
177 ASSERT_TRUE(edge); // cat in
178 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
181 edge = plan_.FindWork();
182 ASSERT_TRUE(edge); // cat mid
183 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
186 edge = plan_.FindWork();
187 ASSERT_TRUE(edge); // cat mid
188 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
191 edge = plan_.FindWork();
192 ASSERT_TRUE(edge); // cat a1 a2
193 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
196 edge = plan_.FindWork();
197 ASSERT_FALSE(edge); // done
200 void PlanTest::TestPoolWithDepthOne(const char* test_case) {
201 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, test_case));
202 GetNode("out1")->MarkDirty();
203 GetNode("out2")->MarkDirty();
205 EXPECT_TRUE(plan_.AddTarget(GetNode("out1"), &err));
207 EXPECT_TRUE(plan_.AddTarget(GetNode("out2"), &err));
209 ASSERT_TRUE(plan_.more_to_do());
211 Edge* edge = plan_.FindWork();
213 ASSERT_EQ("in", edge->inputs_[0]->path());
214 ASSERT_EQ("out1", edge->outputs_[0]->path());
216 // This will be false since poolcat is serialized
217 ASSERT_FALSE(plan_.FindWork());
219 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
222 edge = plan_.FindWork();
224 ASSERT_EQ("in", edge->inputs_[0]->path());
225 ASSERT_EQ("out2", edge->outputs_[0]->path());
227 ASSERT_FALSE(plan_.FindWork());
229 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
232 ASSERT_FALSE(plan_.more_to_do());
233 edge = plan_.FindWork();
237 TEST_F(PlanTest, PoolWithDepthOne) {
238 TestPoolWithDepthOne(
242 " command = cat $in > $out\n"
244 "build out1: poolcat in\n"
245 "build out2: poolcat in\n");
248 TEST_F(PlanTest, ConsolePool) {
249 TestPoolWithDepthOne(
251 " command = cat $in > $out\n"
253 "build out1: poolcat in\n"
254 "build out2: poolcat in\n");
257 TEST_F(PlanTest, PoolsWithDepthTwo) {
258 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
264 " command = cat $in > $out\n"
267 " command = cat $in > $out\n"
269 "build out1: foocat in\n"
270 "build out2: foocat in\n"
271 "build out3: foocat in\n"
272 "build outb1: bazcat in\n"
273 "build outb2: bazcat in\n"
274 "build outb3: bazcat in\n"
276 "build allTheThings: cat out1 out2 out3 outb1 outb2 outb3\n"
278 // Mark all the out* nodes dirty
279 for (int i = 0; i < 3; ++i) {
280 GetNode("out" + string(1, '1' + static_cast<char>(i)))->MarkDirty();
281 GetNode("outb" + string(1, '1' + static_cast<char>(i)))->MarkDirty();
283 GetNode("allTheThings")->MarkDirty();
286 EXPECT_TRUE(plan_.AddTarget(GetNode("allTheThings"), &err));
290 FindWorkSorted(&edges, 5);
292 for (int i = 0; i < 4; ++i) {
293 Edge *edge = edges[i];
294 ASSERT_EQ("in", edge->inputs_[0]->path());
295 string base_name(i < 2 ? "out" : "outb");
296 ASSERT_EQ(base_name + string(1, '1' + (i % 2)), edge->outputs_[0]->path());
299 // outb3 is exempt because it has an empty pool
300 Edge* edge = edges[4];
302 ASSERT_EQ("in", edge->inputs_[0]->path());
303 ASSERT_EQ("outb3", edge->outputs_[0]->path());
306 plan_.EdgeFinished(edges.front(), Plan::kEdgeSucceeded, &err);
310 // out3 should be available
311 Edge* out3 = plan_.FindWork();
313 ASSERT_EQ("in", out3->inputs_[0]->path());
314 ASSERT_EQ("out3", out3->outputs_[0]->path());
316 ASSERT_FALSE(plan_.FindWork());
318 plan_.EdgeFinished(out3, Plan::kEdgeSucceeded, &err);
321 ASSERT_FALSE(plan_.FindWork());
323 for (deque<Edge*>::iterator it = edges.begin(); it != edges.end(); ++it) {
324 plan_.EdgeFinished(*it, Plan::kEdgeSucceeded, &err);
328 Edge* last = plan_.FindWork();
330 ASSERT_EQ("allTheThings", last->outputs_[0]->path());
332 plan_.EdgeFinished(last, Plan::kEdgeSucceeded, &err);
335 ASSERT_FALSE(plan_.more_to_do());
336 ASSERT_FALSE(plan_.FindWork());
339 TEST_F(PlanTest, PoolWithRedundantEdges) {
340 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
344 " command = touch foo.cpp\n"
346 " command = touch bar.cpp\n"
348 " command = echo $out > $out\n"
349 "build foo.cpp.obj: echo foo.cpp || foo.cpp\n"
351 "build bar.cpp.obj: echo bar.cpp || bar.cpp\n"
353 "build libfoo.a: echo foo.cpp.obj bar.cpp.obj\n"
354 "build foo.cpp: gen_foo\n"
355 "build bar.cpp: gen_bar\n"
356 "build all: phony libfoo.a\n"));
357 GetNode("foo.cpp")->MarkDirty();
358 GetNode("foo.cpp.obj")->MarkDirty();
359 GetNode("bar.cpp")->MarkDirty();
360 GetNode("bar.cpp.obj")->MarkDirty();
361 GetNode("libfoo.a")->MarkDirty();
362 GetNode("all")->MarkDirty();
364 EXPECT_TRUE(plan_.AddTarget(GetNode("all"), &err));
366 ASSERT_TRUE(plan_.more_to_do());
370 deque<Edge*> initial_edges;
371 FindWorkSorted(&initial_edges, 2);
373 edge = initial_edges[1]; // Foo first
374 ASSERT_EQ("foo.cpp", edge->outputs_[0]->path());
375 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
378 edge = plan_.FindWork();
380 ASSERT_FALSE(plan_.FindWork());
381 ASSERT_EQ("foo.cpp", edge->inputs_[0]->path());
382 ASSERT_EQ("foo.cpp", edge->inputs_[1]->path());
383 ASSERT_EQ("foo.cpp.obj", edge->outputs_[0]->path());
384 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
387 edge = initial_edges[0]; // Now for bar
388 ASSERT_EQ("bar.cpp", edge->outputs_[0]->path());
389 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
392 edge = plan_.FindWork();
394 ASSERT_FALSE(plan_.FindWork());
395 ASSERT_EQ("bar.cpp", edge->inputs_[0]->path());
396 ASSERT_EQ("bar.cpp", edge->inputs_[1]->path());
397 ASSERT_EQ("bar.cpp.obj", edge->outputs_[0]->path());
398 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
401 edge = plan_.FindWork();
403 ASSERT_FALSE(plan_.FindWork());
404 ASSERT_EQ("foo.cpp.obj", edge->inputs_[0]->path());
405 ASSERT_EQ("bar.cpp.obj", edge->inputs_[1]->path());
406 ASSERT_EQ("libfoo.a", edge->outputs_[0]->path());
407 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
410 edge = plan_.FindWork();
412 ASSERT_FALSE(plan_.FindWork());
413 ASSERT_EQ("libfoo.a", edge->inputs_[0]->path());
414 ASSERT_EQ("all", edge->outputs_[0]->path());
415 plan_.EdgeFinished(edge, Plan::kEdgeSucceeded, &err);
418 edge = plan_.FindWork();
420 ASSERT_FALSE(plan_.more_to_do());
423 TEST_F(PlanTest, PoolWithFailingEdge) {
424 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
428 " command = cat $in > $out\n"
430 "build out1: poolcat in\n"
431 "build out2: poolcat in\n"));
432 GetNode("out1")->MarkDirty();
433 GetNode("out2")->MarkDirty();
435 EXPECT_TRUE(plan_.AddTarget(GetNode("out1"), &err));
437 EXPECT_TRUE(plan_.AddTarget(GetNode("out2"), &err));
439 ASSERT_TRUE(plan_.more_to_do());
441 Edge* edge = plan_.FindWork();
443 ASSERT_EQ("in", edge->inputs_[0]->path());
444 ASSERT_EQ("out1", edge->outputs_[0]->path());
446 // This will be false since poolcat is serialized
447 ASSERT_FALSE(plan_.FindWork());
449 plan_.EdgeFinished(edge, Plan::kEdgeFailed, &err);
452 edge = plan_.FindWork();
454 ASSERT_EQ("in", edge->inputs_[0]->path());
455 ASSERT_EQ("out2", edge->outputs_[0]->path());
457 ASSERT_FALSE(plan_.FindWork());
459 plan_.EdgeFinished(edge, Plan::kEdgeFailed, &err);
462 ASSERT_TRUE(plan_.more_to_do()); // Jobs have failed
463 edge = plan_.FindWork();
467 /// Fake implementation of CommandRunner, useful for tests.
468 struct FakeCommandRunner : public CommandRunner {
469 explicit FakeCommandRunner(VirtualFileSystem* fs) :
470 max_active_edges_(1), fs_(fs) {}
472 // CommandRunner impl
473 virtual bool CanRunMore() const;
474 virtual bool StartCommand(Edge* edge);
475 virtual bool WaitForCommand(Result* result);
476 virtual vector<Edge*> GetActiveEdges();
477 virtual void Abort();
479 vector<string> commands_ran_;
480 vector<Edge*> active_edges_;
481 size_t max_active_edges_;
482 VirtualFileSystem* fs_;
485 struct BuildTest : public StateTestWithBuiltinRules, public BuildLogUser {
486 BuildTest() : config_(MakeConfig()), command_runner_(&fs_),
487 builder_(&state_, config_, NULL, NULL, &fs_),
491 BuildTest(DepsLog* log) : config_(MakeConfig()), command_runner_(&fs_),
492 builder_(&state_, config_, NULL, log, &fs_),
496 virtual void SetUp() {
497 StateTestWithBuiltinRules::SetUp();
499 builder_.command_runner_.reset(&command_runner_);
501 "build cat1: cat in1\n"
502 "build cat2: cat in1 in2\n"
503 "build cat12: cat cat1 cat2\n");
505 fs_.Create("in1", "");
506 fs_.Create("in2", "");
510 builder_.command_runner_.release();
513 virtual bool IsPathDead(StringPiece s) const { return false; }
515 /// Rebuild target in the 'working tree' (fs_).
516 /// State of command_runner_ and logs contents (if specified) ARE MODIFIED.
517 /// Handy to check for NOOP builds, and higher-level rebuild tests.
518 void RebuildTarget(const string& target, const char* manifest,
519 const char* log_path = NULL, const char* deps_path = NULL,
520 State* state = NULL);
522 // Mark a path dirty.
523 void Dirty(const string& path);
525 BuildConfig MakeConfig() {
527 config.verbosity = BuildConfig::QUIET;
532 FakeCommandRunner command_runner_;
533 VirtualFileSystem fs_;
539 void BuildTest::RebuildTarget(const string& target, const char* manifest,
540 const char* log_path, const char* deps_path,
542 State local_state, *pstate = &local_state;
545 ASSERT_NO_FATAL_FAILURE(AddCatRule(pstate));
546 AssertParse(pstate, manifest);
549 BuildLog build_log, *pbuild_log = NULL;
551 ASSERT_TRUE(build_log.Load(log_path, &err));
552 ASSERT_TRUE(build_log.OpenForWrite(log_path, *this, &err));
554 pbuild_log = &build_log;
557 DepsLog deps_log, *pdeps_log = NULL;
559 ASSERT_TRUE(deps_log.Load(deps_path, pstate, &err));
560 ASSERT_TRUE(deps_log.OpenForWrite(deps_path, &err));
562 pdeps_log = &deps_log;
565 Builder builder(pstate, config_, pbuild_log, pdeps_log, &fs_);
566 EXPECT_TRUE(builder.AddTarget(target, &err));
568 command_runner_.commands_ran_.clear();
569 builder.command_runner_.reset(&command_runner_);
570 if (!builder.AlreadyUpToDate()) {
571 bool build_res = builder.Build(&err);
572 EXPECT_TRUE(build_res);
574 builder.command_runner_.release();
577 bool FakeCommandRunner::CanRunMore() const {
578 return active_edges_.size() < max_active_edges_;
581 bool FakeCommandRunner::StartCommand(Edge* edge) {
582 assert(active_edges_.size() < max_active_edges_);
583 assert(find(active_edges_.begin(), active_edges_.end(), edge)
584 == active_edges_.end());
585 commands_ran_.push_back(edge->EvaluateCommand());
586 if (edge->rule().name() == "cat" ||
587 edge->rule().name() == "cat_rsp" ||
588 edge->rule().name() == "cat_rsp_out" ||
589 edge->rule().name() == "cc" ||
590 edge->rule().name() == "cp_multi_msvc" ||
591 edge->rule().name() == "cp_multi_gcc" ||
592 edge->rule().name() == "touch" ||
593 edge->rule().name() == "touch-interrupt" ||
594 edge->rule().name() == "touch-fail-tick2") {
595 for (vector<Node*>::iterator out = edge->outputs_.begin();
596 out != edge->outputs_.end(); ++out) {
597 fs_->Create((*out)->path(), "");
599 } else if (edge->rule().name() == "true" ||
600 edge->rule().name() == "fail" ||
601 edge->rule().name() == "interrupt" ||
602 edge->rule().name() == "console") {
603 // Don't do anything.
604 } else if (edge->rule().name() == "cp") {
605 assert(!edge->inputs_.empty());
606 assert(edge->outputs_.size() == 1);
609 if (fs_->ReadFile(edge->inputs_[0]->path(), &content, &err) ==
611 fs_->WriteFile(edge->outputs_[0]->path(), content);
613 printf("unknown command\n");
617 active_edges_.push_back(edge);
619 // Allow tests to control the order by the name of the first output.
620 sort(active_edges_.begin(), active_edges_.end(),
621 CompareEdgesByOutput::cmp);
626 bool FakeCommandRunner::WaitForCommand(Result* result) {
627 if (active_edges_.empty())
630 // All active edges were already completed immediately when started,
631 // so we can pick any edge here. Pick the last edge. Tests can
632 // control the order of edges by the name of the first output.
633 vector<Edge*>::iterator edge_iter = active_edges_.end() - 1;
635 Edge* edge = *edge_iter;
638 if (edge->rule().name() == "interrupt" ||
639 edge->rule().name() == "touch-interrupt") {
640 result->status = ExitInterrupted;
644 if (edge->rule().name() == "console") {
645 if (edge->use_console())
646 result->status = ExitSuccess;
648 result->status = ExitFailure;
649 active_edges_.erase(edge_iter);
653 if (edge->rule().name() == "cp_multi_msvc") {
654 const std::string prefix = edge->GetBinding("msvc_deps_prefix");
655 for (std::vector<Node*>::iterator in = edge->inputs_.begin();
656 in != edge->inputs_.end(); ++in) {
657 result->output += prefix + (*in)->path() + '\n';
661 if (edge->rule().name() == "fail" ||
662 (edge->rule().name() == "touch-fail-tick2" && fs_->now_ == 2))
663 result->status = ExitFailure;
665 result->status = ExitSuccess;
667 // Provide a way for test cases to verify when an edge finishes that
668 // some other edge is still active. This is useful for test cases
669 // covering behavior involving multiple active edges.
670 const string& verify_active_edge = edge->GetBinding("verify_active_edge");
671 if (!verify_active_edge.empty()) {
672 bool verify_active_edge_found = false;
673 for (vector<Edge*>::iterator i = active_edges_.begin();
674 i != active_edges_.end(); ++i) {
675 if (!(*i)->outputs_.empty() &&
676 (*i)->outputs_[0]->path() == verify_active_edge) {
677 verify_active_edge_found = true;
680 EXPECT_TRUE(verify_active_edge_found);
683 active_edges_.erase(edge_iter);
687 vector<Edge*> FakeCommandRunner::GetActiveEdges() {
688 return active_edges_;
691 void FakeCommandRunner::Abort() {
692 active_edges_.clear();
695 void BuildTest::Dirty(const string& path) {
696 Node* node = GetNode(path);
699 // If it's an input file, mark that we've already stat()ed it and
701 if (!node->in_edge())
705 TEST_F(BuildTest, NoWork) {
707 EXPECT_TRUE(builder_.AlreadyUpToDate());
710 TEST_F(BuildTest, OneStep) {
711 // Given a dirty target with one ready input,
712 // we should rebuild the target.
715 EXPECT_TRUE(builder_.AddTarget("cat1", &err));
717 EXPECT_TRUE(builder_.Build(&err));
720 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
721 EXPECT_EQ("cat in1 > cat1", command_runner_.commands_ran_[0]);
724 TEST_F(BuildTest, OneStep2) {
725 // Given a target with one dirty input,
726 // we should rebuild the target.
729 EXPECT_TRUE(builder_.AddTarget("cat1", &err));
731 EXPECT_TRUE(builder_.Build(&err));
734 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
735 EXPECT_EQ("cat in1 > cat1", command_runner_.commands_ran_[0]);
738 TEST_F(BuildTest, TwoStep) {
740 EXPECT_TRUE(builder_.AddTarget("cat12", &err));
742 EXPECT_TRUE(builder_.Build(&err));
744 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
745 // Depending on how the pointers work out, we could've ran
746 // the first two commands in either order.
747 EXPECT_TRUE((command_runner_.commands_ran_[0] == "cat in1 > cat1" &&
748 command_runner_.commands_ran_[1] == "cat in1 in2 > cat2") ||
749 (command_runner_.commands_ran_[1] == "cat in1 > cat1" &&
750 command_runner_.commands_ran_[0] == "cat in1 in2 > cat2"));
752 EXPECT_EQ("cat cat1 cat2 > cat12", command_runner_.commands_ran_[2]);
756 // Modifying in2 requires rebuilding one intermediate file
757 // and the final file.
758 fs_.Create("in2", "");
760 EXPECT_TRUE(builder_.AddTarget("cat12", &err));
762 EXPECT_TRUE(builder_.Build(&err));
764 ASSERT_EQ(5u, command_runner_.commands_ran_.size());
765 EXPECT_EQ("cat in1 in2 > cat2", command_runner_.commands_ran_[3]);
766 EXPECT_EQ("cat cat1 cat2 > cat12", command_runner_.commands_ran_[4]);
769 TEST_F(BuildTest, TwoOutputs) {
770 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
772 " command = touch $out\n"
773 "build out1 out2: touch in.txt\n"));
775 fs_.Create("in.txt", "");
778 EXPECT_TRUE(builder_.AddTarget("out1", &err));
780 EXPECT_TRUE(builder_.Build(&err));
782 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
783 EXPECT_EQ("touch out1 out2", command_runner_.commands_ran_[0]);
786 TEST_F(BuildTest, ImplicitOutput) {
787 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
789 " command = touch $out $out.imp\n"
790 "build out | out.imp: touch in.txt\n"));
791 fs_.Create("in.txt", "");
794 EXPECT_TRUE(builder_.AddTarget("out.imp", &err));
796 EXPECT_TRUE(builder_.Build(&err));
798 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
799 EXPECT_EQ("touch out out.imp", command_runner_.commands_ran_[0]);
803 // https://github.com/ninja-build/ninja/issues/148
804 TEST_F(BuildTest, MultiOutIn) {
805 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
807 " command = touch $out\n"
808 "build in1 otherfile: touch in\n"
809 "build out: touch in | in1\n"));
811 fs_.Create("in", "");
813 fs_.Create("in1", "");
816 EXPECT_TRUE(builder_.AddTarget("out", &err));
818 EXPECT_TRUE(builder_.Build(&err));
822 TEST_F(BuildTest, Chain) {
823 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
827 "build c5: cat c4\n"));
829 fs_.Create("c1", "");
832 EXPECT_TRUE(builder_.AddTarget("c5", &err));
834 EXPECT_TRUE(builder_.Build(&err));
836 ASSERT_EQ(4u, command_runner_.commands_ran_.size());
839 command_runner_.commands_ran_.clear();
841 EXPECT_TRUE(builder_.AddTarget("c5", &err));
843 EXPECT_TRUE(builder_.AlreadyUpToDate());
847 fs_.Create("c3", "");
849 command_runner_.commands_ran_.clear();
851 EXPECT_TRUE(builder_.AddTarget("c5", &err));
853 EXPECT_FALSE(builder_.AlreadyUpToDate());
854 EXPECT_TRUE(builder_.Build(&err));
855 ASSERT_EQ(2u, command_runner_.commands_ran_.size()); // 3->4, 4->5
858 TEST_F(BuildTest, MissingInput) {
859 // Input is referenced by build file, but no rule for it.
862 EXPECT_FALSE(builder_.AddTarget("cat1", &err));
863 EXPECT_EQ("'in1', needed by 'cat1', missing and no known rule to make it",
867 TEST_F(BuildTest, MissingTarget) {
868 // Target is not referenced by build file.
870 EXPECT_FALSE(builder_.AddTarget("meow", &err));
871 EXPECT_EQ("unknown target: 'meow'", err);
874 TEST_F(BuildTest, MakeDirs) {
878 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
879 "build subdir\\dir2\\file: cat in1\n"));
881 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
882 "build subdir/dir2/file: cat in1\n"));
884 EXPECT_TRUE(builder_.AddTarget("subdir/dir2/file", &err));
887 EXPECT_TRUE(builder_.Build(&err));
889 ASSERT_EQ(2u, fs_.directories_made_.size());
890 EXPECT_EQ("subdir", fs_.directories_made_[0]);
891 EXPECT_EQ("subdir/dir2", fs_.directories_made_[1]);
894 TEST_F(BuildTest, DepFileMissing) {
896 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
897 "rule cc\n command = cc $in\n depfile = $out.d\n"
898 "build fo$ o.o: cc foo.c\n"));
899 fs_.Create("foo.c", "");
901 EXPECT_TRUE(builder_.AddTarget("fo o.o", &err));
903 ASSERT_EQ(1u, fs_.files_read_.size());
904 EXPECT_EQ("fo o.o.d", fs_.files_read_[0]);
907 TEST_F(BuildTest, DepFileOK) {
909 int orig_edges = state_.edges_.size();
910 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
911 "rule cc\n command = cc $in\n depfile = $out.d\n"
912 "build foo.o: cc foo.c\n"));
913 Edge* edge = state_.edges_.back();
915 fs_.Create("foo.c", "");
916 GetNode("bar.h")->MarkDirty(); // Mark bar.h as missing.
917 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
918 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
920 ASSERT_EQ(1u, fs_.files_read_.size());
921 EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
923 // Expect three new edges: one generating foo.o, and two more from
924 // loading the depfile.
925 ASSERT_EQ(orig_edges + 3, (int)state_.edges_.size());
926 // Expect our edge to now have three inputs: foo.c and two headers.
927 ASSERT_EQ(3u, edge->inputs_.size());
929 // Expect the command line we generate to only use the original input.
930 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
933 TEST_F(BuildTest, DepFileParseError) {
935 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
936 "rule cc\n command = cc $in\n depfile = $out.d\n"
937 "build foo.o: cc foo.c\n"));
938 fs_.Create("foo.c", "");
939 fs_.Create("foo.o.d", "randomtext\n");
940 EXPECT_FALSE(builder_.AddTarget("foo.o", &err));
941 EXPECT_EQ("foo.o.d: expected ':' in depfile", err);
944 TEST_F(BuildTest, EncounterReadyTwice) {
946 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
948 " command = touch $out\n"
950 "build b: touch || c\n"
951 "build a: touch | b || c\n"));
953 vector<Edge*> c_out = GetNode("c")->out_edges();
954 ASSERT_EQ(2u, c_out.size());
955 EXPECT_EQ("b", c_out[0]->outputs_[0]->path());
956 EXPECT_EQ("a", c_out[1]->outputs_[0]->path());
959 EXPECT_TRUE(builder_.AddTarget("a", &err));
962 EXPECT_TRUE(builder_.Build(&err));
964 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
967 TEST_F(BuildTest, OrderOnlyDeps) {
969 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
970 "rule cc\n command = cc $in\n depfile = $out.d\n"
971 "build foo.o: cc foo.c || otherfile\n"));
972 Edge* edge = state_.edges_.back();
974 fs_.Create("foo.c", "");
975 fs_.Create("otherfile", "");
976 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
977 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
980 // One explicit, two implicit, one order only.
981 ASSERT_EQ(4u, edge->inputs_.size());
982 EXPECT_EQ(2, edge->implicit_deps_);
983 EXPECT_EQ(1, edge->order_only_deps_);
984 // Verify the inputs are in the order we expect
985 // (explicit then implicit then orderonly).
986 EXPECT_EQ("foo.c", edge->inputs_[0]->path());
987 EXPECT_EQ("blah.h", edge->inputs_[1]->path());
988 EXPECT_EQ("bar.h", edge->inputs_[2]->path());
989 EXPECT_EQ("otherfile", edge->inputs_[3]->path());
991 // Expect the command line we generate to only use the original input.
992 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
994 // explicit dep dirty, expect a rebuild.
995 EXPECT_TRUE(builder_.Build(&err));
997 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1001 // Recreate the depfile, as it should have been deleted by the build.
1002 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
1004 // implicit dep dirty, expect a rebuild.
1005 fs_.Create("blah.h", "");
1006 fs_.Create("bar.h", "");
1007 command_runner_.commands_ran_.clear();
1009 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
1010 EXPECT_TRUE(builder_.Build(&err));
1012 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1016 // Recreate the depfile, as it should have been deleted by the build.
1017 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
1019 // order only dep dirty, no rebuild.
1020 fs_.Create("otherfile", "");
1021 command_runner_.commands_ran_.clear();
1023 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
1025 EXPECT_TRUE(builder_.AlreadyUpToDate());
1027 // implicit dep missing, expect rebuild.
1028 fs_.RemoveFile("bar.h");
1029 command_runner_.commands_ran_.clear();
1031 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
1032 EXPECT_TRUE(builder_.Build(&err));
1034 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1037 TEST_F(BuildTest, RebuildOrderOnlyDeps) {
1039 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1040 "rule cc\n command = cc $in\n"
1041 "rule true\n command = true\n"
1042 "build oo.h: cc oo.h.in\n"
1043 "build foo.o: cc foo.c || oo.h\n"));
1045 fs_.Create("foo.c", "");
1046 fs_.Create("oo.h.in", "");
1048 // foo.o and order-only dep dirty, build both.
1049 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
1050 EXPECT_TRUE(builder_.Build(&err));
1052 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1054 // all clean, no rebuild.
1055 command_runner_.commands_ran_.clear();
1057 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
1059 EXPECT_TRUE(builder_.AlreadyUpToDate());
1061 // order-only dep missing, build it only.
1062 fs_.RemoveFile("oo.h");
1063 command_runner_.commands_ran_.clear();
1065 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
1066 EXPECT_TRUE(builder_.Build(&err));
1068 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1069 ASSERT_EQ("cc oo.h.in", command_runner_.commands_ran_[0]);
1073 // order-only dep dirty, build it only.
1074 fs_.Create("oo.h.in", "");
1075 command_runner_.commands_ran_.clear();
1077 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
1078 EXPECT_TRUE(builder_.Build(&err));
1080 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1081 ASSERT_EQ("cc oo.h.in", command_runner_.commands_ran_[0]);
1085 TEST_F(BuildTest, DepFileCanonicalize) {
1087 int orig_edges = state_.edges_.size();
1088 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1089 "rule cc\n command = cc $in\n depfile = $out.d\n"
1090 "build gen/stuff\\things/foo.o: cc x\\y/z\\foo.c\n"));
1091 Edge* edge = state_.edges_.back();
1093 fs_.Create("x/y/z/foo.c", "");
1094 GetNode("bar.h")->MarkDirty(); // Mark bar.h as missing.
1095 // Note, different slashes from manifest.
1096 fs_.Create("gen/stuff\\things/foo.o.d",
1097 "gen\\stuff\\things\\foo.o: blah.h bar.h\n");
1098 EXPECT_TRUE(builder_.AddTarget("gen/stuff/things/foo.o", &err));
1100 ASSERT_EQ(1u, fs_.files_read_.size());
1101 // The depfile path does not get Canonicalize as it seems unnecessary.
1102 EXPECT_EQ("gen/stuff\\things/foo.o.d", fs_.files_read_[0]);
1104 // Expect three new edges: one generating foo.o, and two more from
1105 // loading the depfile.
1106 ASSERT_EQ(orig_edges + 3, (int)state_.edges_.size());
1107 // Expect our edge to now have three inputs: foo.c and two headers.
1108 ASSERT_EQ(3u, edge->inputs_.size());
1110 // Expect the command line we generate to only use the original input, and
1111 // using the slashes from the manifest.
1112 ASSERT_EQ("cc x\\y/z\\foo.c", edge->EvaluateCommand());
1116 TEST_F(BuildTest, Phony) {
1118 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1119 "build out: cat bar.cc\n"
1120 "build all: phony out\n"));
1121 fs_.Create("bar.cc", "");
1123 EXPECT_TRUE(builder_.AddTarget("all", &err));
1126 // Only one command to run, because phony runs no command.
1127 EXPECT_FALSE(builder_.AlreadyUpToDate());
1128 EXPECT_TRUE(builder_.Build(&err));
1130 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1133 TEST_F(BuildTest, PhonyNoWork) {
1135 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1136 "build out: cat bar.cc\n"
1137 "build all: phony out\n"));
1138 fs_.Create("bar.cc", "");
1139 fs_.Create("out", "");
1141 EXPECT_TRUE(builder_.AddTarget("all", &err));
1143 EXPECT_TRUE(builder_.AlreadyUpToDate());
1146 // Test a self-referencing phony. Ideally this should not work, but
1147 // ninja 1.7 and below tolerated and CMake 2.8.12.x and 3.0.x both
1148 // incorrectly produce it. We tolerate it for compatibility.
1149 TEST_F(BuildTest, PhonySelfReference) {
1151 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1152 "build a: phony a\n"));
1154 EXPECT_TRUE(builder_.AddTarget("a", &err));
1156 EXPECT_TRUE(builder_.AlreadyUpToDate());
1159 TEST_F(BuildTest, Fail) {
1160 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1163 "build out1: fail\n"));
1166 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1169 EXPECT_FALSE(builder_.Build(&err));
1170 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1171 ASSERT_EQ("subcommand failed", err);
1174 TEST_F(BuildTest, SwallowFailures) {
1175 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1178 "build out1: fail\n"
1179 "build out2: fail\n"
1180 "build out3: fail\n"
1181 "build all: phony out1 out2 out3\n"));
1183 // Swallow two failures, die on the third.
1184 config_.failures_allowed = 3;
1187 EXPECT_TRUE(builder_.AddTarget("all", &err));
1190 EXPECT_FALSE(builder_.Build(&err));
1191 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1192 ASSERT_EQ("subcommands failed", err);
1195 TEST_F(BuildTest, SwallowFailuresLimit) {
1196 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1199 "build out1: fail\n"
1200 "build out2: fail\n"
1201 "build out3: fail\n"
1202 "build final: cat out1 out2 out3\n"));
1204 // Swallow ten failures; we should stop before building final.
1205 config_.failures_allowed = 11;
1208 EXPECT_TRUE(builder_.AddTarget("final", &err));
1211 EXPECT_FALSE(builder_.Build(&err));
1212 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1213 ASSERT_EQ("cannot make progress due to previous errors", err);
1216 TEST_F(BuildTest, SwallowFailuresPool) {
1217 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1222 " pool = failpool\n"
1223 "build out1: fail\n"
1224 "build out2: fail\n"
1225 "build out3: fail\n"
1226 "build final: cat out1 out2 out3\n"));
1228 // Swallow ten failures; we should stop before building final.
1229 config_.failures_allowed = 11;
1232 EXPECT_TRUE(builder_.AddTarget("final", &err));
1235 EXPECT_FALSE(builder_.Build(&err));
1236 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1237 ASSERT_EQ("cannot make progress due to previous errors", err);
1240 TEST_F(BuildTest, PoolEdgesReadyButNotWanted) {
1241 fs_.Create("x", "");
1243 const char* manifest =
1247 " command = touch $out\n"
1248 " pool = some_pool\n"
1250 " command = touch grit\n"
1252 "build B.d.stamp: cc | x\n"
1253 "build C.stamp: touch B.d.stamp\n"
1254 "build final.stamp: touch || C.stamp\n";
1256 RebuildTarget("final.stamp", manifest);
1258 fs_.RemoveFile("B.d.stamp");
1261 RebuildTarget("final.stamp", manifest, NULL, NULL, &save_state);
1262 EXPECT_GE(save_state.LookupPool("some_pool")->current_use(), 0);
1265 struct BuildWithLogTest : public BuildTest {
1266 BuildWithLogTest() {
1267 builder_.SetBuildLog(&build_log_);
1270 BuildLog build_log_;
1273 TEST_F(BuildWithLogTest, NotInLogButOnDisk) {
1274 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1277 "build out1: cc in\n"));
1279 // Create input/output that would be considered up to date when
1280 // not considering the command line hash.
1281 fs_.Create("in", "");
1282 fs_.Create("out1", "");
1285 // Because it's not in the log, it should not be up-to-date until
1287 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1288 EXPECT_FALSE(builder_.AlreadyUpToDate());
1290 command_runner_.commands_ran_.clear();
1293 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1294 EXPECT_TRUE(builder_.Build(&err));
1295 EXPECT_TRUE(builder_.AlreadyUpToDate());
1298 TEST_F(BuildWithLogTest, RebuildAfterFailure) {
1299 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1300 "rule touch-fail-tick2\n"
1301 " command = touch-fail-tick2\n"
1302 "build out1: touch-fail-tick2 in\n"));
1306 fs_.Create("in", "");
1308 // Run once successfully to get out1 in the log
1309 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1310 EXPECT_TRUE(builder_.Build(&err));
1312 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1314 command_runner_.commands_ran_.clear();
1317 builder_.plan_.Reset();
1320 fs_.Create("in", "");
1322 // Run again with a failure that updates the output file timestamp
1323 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1324 EXPECT_FALSE(builder_.Build(&err));
1325 EXPECT_EQ("subcommand failed", err);
1326 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1328 command_runner_.commands_ran_.clear();
1331 builder_.plan_.Reset();
1335 // Run again, should rerun even though the output file is up to date on disk
1336 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1337 EXPECT_FALSE(builder_.AlreadyUpToDate());
1338 EXPECT_TRUE(builder_.Build(&err));
1339 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1343 TEST_F(BuildWithLogTest, RebuildWithNoInputs) {
1344 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1346 " command = touch\n"
1347 "build out1: touch\n"
1348 "build out2: touch in\n"));
1352 fs_.Create("in", "");
1354 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1355 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1356 EXPECT_TRUE(builder_.Build(&err));
1358 EXPECT_EQ(2u, command_runner_.commands_ran_.size());
1360 command_runner_.commands_ran_.clear();
1365 fs_.Create("in", "");
1367 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1368 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1369 EXPECT_TRUE(builder_.Build(&err));
1371 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1374 TEST_F(BuildWithLogTest, RestatTest) {
1375 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1382 "build out1: cc in\n"
1383 "build out2: true out1\n"
1384 "build out3: cat out2\n"));
1386 fs_.Create("out1", "");
1387 fs_.Create("out2", "");
1388 fs_.Create("out3", "");
1392 fs_.Create("in", "");
1394 // Do a pre-build so that there's commands in the log for the outputs,
1395 // otherwise, the lack of an entry in the build log will cause out3 to rebuild
1396 // regardless of restat.
1398 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1400 EXPECT_TRUE(builder_.Build(&err));
1402 EXPECT_EQ("[3/3]", builder_.status_->FormatProgressStatus("[%s/%t]",
1403 BuildStatus::kEdgeStarted));
1404 command_runner_.commands_ran_.clear();
1409 fs_.Create("in", "");
1410 // "cc" touches out1, so we should build out2. But because "true" does not
1411 // touch out2, we should cancel the build of out3.
1412 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1414 EXPECT_TRUE(builder_.Build(&err));
1415 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1417 // If we run again, it should be a no-op, because the build log has recorded
1418 // that we've already built out2 with an input timestamp of 2 (from out1).
1419 command_runner_.commands_ran_.clear();
1421 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1423 EXPECT_TRUE(builder_.AlreadyUpToDate());
1427 fs_.Create("in", "");
1429 // The build log entry should not, however, prevent us from rebuilding out2
1431 command_runner_.commands_ran_.clear();
1433 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1435 EXPECT_TRUE(builder_.Build(&err));
1436 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1439 TEST_F(BuildWithLogTest, RestatMissingFile) {
1440 // If a restat rule doesn't create its output, and the output didn't
1441 // exist before the rule was run, consider that behavior equivalent
1442 // to a rule that doesn't modify its existent output file.
1444 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1450 "build out1: true in\n"
1451 "build out2: cc out1\n"));
1453 fs_.Create("in", "");
1454 fs_.Create("out2", "");
1456 // Do a pre-build so that there's commands in the log for the outputs,
1457 // otherwise, the lack of an entry in the build log will cause out2 to rebuild
1458 // regardless of restat.
1460 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1462 EXPECT_TRUE(builder_.Build(&err));
1464 command_runner_.commands_ran_.clear();
1468 fs_.Create("in", "");
1469 fs_.Create("out2", "");
1471 // Run a build, expect only the first command to run.
1472 // It doesn't touch its output (due to being the "true" command), so
1473 // we shouldn't run the dependent build.
1474 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1476 EXPECT_TRUE(builder_.Build(&err));
1477 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1480 TEST_F(BuildWithLogTest, RestatSingleDependentOutputDirty) {
1481 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1486 " command = touch\n"
1487 "build out1: true in\n"
1488 "build out2 out3: touch out1\n"
1489 "build out4: touch out2\n"
1492 // Create the necessary files
1493 fs_.Create("in", "");
1496 EXPECT_TRUE(builder_.AddTarget("out4", &err));
1498 EXPECT_TRUE(builder_.Build(&err));
1500 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1503 fs_.Create("in", "");
1504 fs_.RemoveFile("out3");
1506 // Since "in" is missing, out1 will be built. Since "out3" is missing,
1507 // out2 and out3 will be built even though "in" is not touched when built.
1508 // Then, since out2 is rebuilt, out4 should be rebuilt -- the restat on the
1509 // "true" rule should not lead to the "touch" edge writing out2 and out3 being
1511 command_runner_.commands_ran_.clear();
1513 EXPECT_TRUE(builder_.AddTarget("out4", &err));
1515 EXPECT_TRUE(builder_.Build(&err));
1517 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1520 // Test scenario, in which an input file is removed, but output isn't changed
1521 // https://github.com/ninja-build/ninja/issues/295
1522 TEST_F(BuildWithLogTest, RestatMissingInput) {
1523 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1526 " depfile = $out.d\n"
1530 "build out1: true in\n"
1531 "build out2: cc out1\n"));
1533 // Create all necessary files
1534 fs_.Create("in", "");
1536 // The implicit dependencies and the depfile itself
1537 // are newer than the output
1538 TimeStamp restat_mtime = fs_.Tick();
1539 fs_.Create("out1.d", "out1: will.be.deleted restat.file\n");
1540 fs_.Create("will.be.deleted", "");
1541 fs_.Create("restat.file", "");
1543 // Run the build, out1 and out2 get built
1545 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1547 EXPECT_TRUE(builder_.Build(&err));
1548 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1550 // See that an entry in the logfile is created, capturing
1552 BuildLog::LogEntry* log_entry = build_log_.LookupByOutput("out1");
1553 ASSERT_TRUE(NULL != log_entry);
1554 ASSERT_EQ(restat_mtime, log_entry->mtime);
1556 // Now remove a file, referenced from depfile, so that target becomes
1557 // dirty, but the output does not change
1558 fs_.RemoveFile("will.be.deleted");
1560 // Trigger the build again - only out1 gets built
1561 command_runner_.commands_ran_.clear();
1563 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1565 EXPECT_TRUE(builder_.Build(&err));
1566 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1568 // Check that the logfile entry remains correctly set
1569 log_entry = build_log_.LookupByOutput("out1");
1570 ASSERT_TRUE(NULL != log_entry);
1571 ASSERT_EQ(restat_mtime, log_entry->mtime);
1574 struct BuildDryRun : public BuildWithLogTest {
1576 config_.dry_run = true;
1580 TEST_F(BuildDryRun, AllCommandsShown) {
1581 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1588 "build out1: cc in\n"
1589 "build out2: true out1\n"
1590 "build out3: cat out2\n"));
1592 fs_.Create("out1", "");
1593 fs_.Create("out2", "");
1594 fs_.Create("out3", "");
1598 fs_.Create("in", "");
1600 // "cc" touches out1, so we should build out2. But because "true" does not
1601 // touch out2, we should cancel the build of out3.
1603 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1605 EXPECT_TRUE(builder_.Build(&err));
1606 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1609 // Test that RSP files are created when & where appropriate and deleted after
1610 // successful execution.
1611 TEST_F(BuildTest, RspFileSuccess)
1613 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1615 " command = cat $rspfile > $out\n"
1616 " rspfile = $rspfile\n"
1617 " rspfile_content = $long_command\n"
1618 "rule cat_rsp_out\n"
1619 " command = cat $rspfile > $out\n"
1620 " rspfile = $out.rsp\n"
1621 " rspfile_content = $long_command\n"
1622 "build out1: cat in\n"
1623 "build out2: cat_rsp in\n"
1624 " rspfile = out 2.rsp\n"
1625 " long_command = Some very long command\n"
1626 "build out$ 3: cat_rsp_out in\n"
1627 " long_command = Some very long command\n"));
1629 fs_.Create("out1", "");
1630 fs_.Create("out2", "");
1631 fs_.Create("out 3", "");
1635 fs_.Create("in", "");
1638 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1640 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1642 EXPECT_TRUE(builder_.AddTarget("out 3", &err));
1645 size_t files_created = fs_.files_created_.size();
1646 size_t files_removed = fs_.files_removed_.size();
1648 EXPECT_TRUE(builder_.Build(&err));
1649 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1651 // The RSP files were created
1652 ASSERT_EQ(files_created + 2, fs_.files_created_.size());
1653 ASSERT_EQ(1u, fs_.files_created_.count("out 2.rsp"));
1654 ASSERT_EQ(1u, fs_.files_created_.count("out 3.rsp"));
1656 // The RSP files were removed
1657 ASSERT_EQ(files_removed + 2, fs_.files_removed_.size());
1658 ASSERT_EQ(1u, fs_.files_removed_.count("out 2.rsp"));
1659 ASSERT_EQ(1u, fs_.files_removed_.count("out 3.rsp"));
1662 // Test that RSP file is created but not removed for commands, which fail
1663 TEST_F(BuildTest, RspFileFailure) {
1664 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1667 " rspfile = $rspfile\n"
1668 " rspfile_content = $long_command\n"
1669 "build out: fail in\n"
1670 " rspfile = out.rsp\n"
1671 " long_command = Another very long command\n"));
1673 fs_.Create("out", "");
1675 fs_.Create("in", "");
1678 EXPECT_TRUE(builder_.AddTarget("out", &err));
1681 size_t files_created = fs_.files_created_.size();
1682 size_t files_removed = fs_.files_removed_.size();
1684 EXPECT_FALSE(builder_.Build(&err));
1685 ASSERT_EQ("subcommand failed", err);
1686 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1688 // The RSP file was created
1689 ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1690 ASSERT_EQ(1u, fs_.files_created_.count("out.rsp"));
1692 // The RSP file was NOT removed
1693 ASSERT_EQ(files_removed, fs_.files_removed_.size());
1694 ASSERT_EQ(0u, fs_.files_removed_.count("out.rsp"));
1696 // The RSP file contains what it should
1697 ASSERT_EQ("Another very long command", fs_.files_["out.rsp"].contents);
1700 // Test that contents of the RSP file behaves like a regular part of
1701 // command line, i.e. triggers a rebuild if changed
1702 TEST_F(BuildWithLogTest, RspFileCmdLineChange) {
1703 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1705 " command = cat $rspfile > $out\n"
1706 " rspfile = $rspfile\n"
1707 " rspfile_content = $long_command\n"
1708 "build out: cat_rsp in\n"
1709 " rspfile = out.rsp\n"
1710 " long_command = Original very long command\n"));
1712 fs_.Create("out", "");
1714 fs_.Create("in", "");
1717 EXPECT_TRUE(builder_.AddTarget("out", &err));
1720 // 1. Build for the 1st time (-> populate log)
1721 EXPECT_TRUE(builder_.Build(&err));
1722 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1724 // 2. Build again (no change)
1725 command_runner_.commands_ran_.clear();
1727 EXPECT_TRUE(builder_.AddTarget("out", &err));
1729 ASSERT_TRUE(builder_.AlreadyUpToDate());
1731 // 3. Alter the entry in the logfile
1732 // (to simulate a change in the command line between 2 builds)
1733 BuildLog::LogEntry* log_entry = build_log_.LookupByOutput("out");
1734 ASSERT_TRUE(NULL != log_entry);
1735 ASSERT_NO_FATAL_FAILURE(AssertHash(
1736 "cat out.rsp > out;rspfile=Original very long command",
1737 log_entry->command_hash));
1738 log_entry->command_hash++; // Change the command hash to something else.
1739 // Now expect the target to be rebuilt
1740 command_runner_.commands_ran_.clear();
1742 EXPECT_TRUE(builder_.AddTarget("out", &err));
1744 EXPECT_TRUE(builder_.Build(&err));
1745 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1748 TEST_F(BuildTest, InterruptCleanup) {
1749 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1751 " command = interrupt\n"
1752 "rule touch-interrupt\n"
1753 " command = touch-interrupt\n"
1754 "build out1: interrupt in1\n"
1755 "build out2: touch-interrupt in2\n"));
1757 fs_.Create("out1", "");
1758 fs_.Create("out2", "");
1760 fs_.Create("in1", "");
1761 fs_.Create("in2", "");
1763 // An untouched output of an interrupted command should be retained.
1765 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1767 EXPECT_FALSE(builder_.Build(&err));
1768 EXPECT_EQ("interrupted by user", err);
1770 EXPECT_GT(fs_.Stat("out1", &err), 0);
1773 // A touched output of an interrupted command should be deleted.
1774 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1776 EXPECT_FALSE(builder_.Build(&err));
1777 EXPECT_EQ("interrupted by user", err);
1779 EXPECT_EQ(0, fs_.Stat("out2", &err));
1782 TEST_F(BuildTest, StatFailureAbortsBuild) {
1783 const string kTooLongToStat(400, 'i');
1784 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1785 ("build " + kTooLongToStat + ": cat in\n").c_str()));
1786 fs_.Create("in", "");
1788 // This simulates a stat failure:
1789 fs_.files_[kTooLongToStat].mtime = -1;
1790 fs_.files_[kTooLongToStat].stat_error = "stat failed";
1793 EXPECT_FALSE(builder_.AddTarget(kTooLongToStat, &err));
1794 EXPECT_EQ("stat failed", err);
1797 TEST_F(BuildTest, PhonyWithNoInputs) {
1798 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1799 "build nonexistent: phony\n"
1800 "build out1: cat || nonexistent\n"
1801 "build out2: cat nonexistent\n"));
1802 fs_.Create("out1", "");
1803 fs_.Create("out2", "");
1805 // out1 should be up to date even though its input is dirty, because its
1806 // order-only dependency has nothing to do.
1808 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1810 EXPECT_TRUE(builder_.AlreadyUpToDate());
1812 // out2 should still be out of date though, because its input is dirty.
1814 command_runner_.commands_ran_.clear();
1816 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1818 EXPECT_TRUE(builder_.Build(&err));
1820 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1823 TEST_F(BuildTest, DepsGccWithEmptyDepfileErrorsOut) {
1824 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1828 "build out: cc\n"));
1832 EXPECT_TRUE(builder_.AddTarget("out", &err));
1834 EXPECT_FALSE(builder_.AlreadyUpToDate());
1836 EXPECT_FALSE(builder_.Build(&err));
1837 ASSERT_EQ("subcommand failed", err);
1838 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1841 TEST_F(BuildTest, StatusFormatElapsed) {
1842 status_.BuildStarted();
1843 // Before any task is done, the elapsed time must be zero.
1844 EXPECT_EQ("[%/e0.000]",
1845 status_.FormatProgressStatus("[%%/e%e]",
1846 BuildStatus::kEdgeStarted));
1849 TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
1850 EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
1851 status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]",
1852 BuildStatus::kEdgeStarted));
1855 TEST_F(BuildTest, FailedDepsParse) {
1856 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1857 "build bad_deps.o: cat in1\n"
1859 " depfile = in1.d\n"));
1862 EXPECT_TRUE(builder_.AddTarget("bad_deps.o", &err));
1865 // These deps will fail to parse, as they should only have one
1866 // path to the left of the colon.
1867 fs_.Create("in1.d", "AAA BBB");
1869 EXPECT_FALSE(builder_.Build(&err));
1870 EXPECT_EQ("subcommand failed", err);
1873 struct BuildWithQueryDepsLogTest : public BuildTest {
1874 BuildWithQueryDepsLogTest() : BuildTest(&log_) {
1877 ~BuildWithQueryDepsLogTest() {
1881 virtual void SetUp() {
1884 temp_dir_.CreateAndEnter("BuildWithQueryDepsLogTest");
1887 ASSERT_TRUE(log_.OpenForWrite("ninja_deps", &err));
1891 ScopedTempDir temp_dir_;
1896 /// Test a MSVC-style deps log with multiple outputs.
1897 TEST_F(BuildWithQueryDepsLogTest, TwoOutputsDepFileMSVC) {
1898 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1899 "rule cp_multi_msvc\n"
1900 " command = echo 'using $in' && for file in $out; do cp $in $$file; done\n"
1902 " msvc_deps_prefix = using \n"
1903 "build out1 out2: cp_multi_msvc in1\n"));
1906 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1908 EXPECT_TRUE(builder_.Build(&err));
1910 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1911 EXPECT_EQ("echo 'using in1' && for file in out1 out2; do cp in1 $file; done", command_runner_.commands_ran_[0]);
1913 Node* out1_node = state_.LookupNode("out1");
1914 DepsLog::Deps* out1_deps = log_.GetDeps(out1_node);
1915 EXPECT_EQ(1, out1_deps->node_count);
1916 EXPECT_EQ("in1", out1_deps->nodes[0]->path());
1918 Node* out2_node = state_.LookupNode("out2");
1919 DepsLog::Deps* out2_deps = log_.GetDeps(out2_node);
1920 EXPECT_EQ(1, out2_deps->node_count);
1921 EXPECT_EQ("in1", out2_deps->nodes[0]->path());
1924 /// Test a GCC-style deps log with multiple outputs.
1925 TEST_F(BuildWithQueryDepsLogTest, TwoOutputsDepFileGCCOneLine) {
1926 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1927 "rule cp_multi_gcc\n"
1928 " command = echo '$out: $in' > in.d && for file in $out; do cp in1 $$file; done\n"
1931 "build out1 out2: cp_multi_gcc in1 in2\n"));
1934 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1936 fs_.Create("in.d", "out1 out2: in1 in2");
1937 EXPECT_TRUE(builder_.Build(&err));
1939 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1940 EXPECT_EQ("echo 'out1 out2: in1 in2' > in.d && for file in out1 out2; do cp in1 $file; done", command_runner_.commands_ran_[0]);
1942 Node* out1_node = state_.LookupNode("out1");
1943 DepsLog::Deps* out1_deps = log_.GetDeps(out1_node);
1944 EXPECT_EQ(2, out1_deps->node_count);
1945 EXPECT_EQ("in1", out1_deps->nodes[0]->path());
1946 EXPECT_EQ("in2", out1_deps->nodes[1]->path());
1948 Node* out2_node = state_.LookupNode("out2");
1949 DepsLog::Deps* out2_deps = log_.GetDeps(out2_node);
1950 EXPECT_EQ(2, out2_deps->node_count);
1951 EXPECT_EQ("in1", out2_deps->nodes[0]->path());
1952 EXPECT_EQ("in2", out2_deps->nodes[1]->path());
1955 /// Test a GCC-style deps log with multiple outputs using a line per input.
1956 TEST_F(BuildWithQueryDepsLogTest, TwoOutputsDepFileGCCMultiLineInput) {
1957 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1958 "rule cp_multi_gcc\n"
1959 " command = echo '$out: in1\\n$out: in2' > in.d && for file in $out; do cp in1 $$file; done\n"
1962 "build out1 out2: cp_multi_gcc in1 in2\n"));
1965 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1967 fs_.Create("in.d", "out1 out2: in1\nout1 out2: in2");
1968 EXPECT_TRUE(builder_.Build(&err));
1970 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1971 EXPECT_EQ("echo 'out1 out2: in1\\nout1 out2: in2' > in.d && for file in out1 out2; do cp in1 $file; done", command_runner_.commands_ran_[0]);
1973 Node* out1_node = state_.LookupNode("out1");
1974 DepsLog::Deps* out1_deps = log_.GetDeps(out1_node);
1975 EXPECT_EQ(2, out1_deps->node_count);
1976 EXPECT_EQ("in1", out1_deps->nodes[0]->path());
1977 EXPECT_EQ("in2", out1_deps->nodes[1]->path());
1979 Node* out2_node = state_.LookupNode("out2");
1980 DepsLog::Deps* out2_deps = log_.GetDeps(out2_node);
1981 EXPECT_EQ(2, out2_deps->node_count);
1982 EXPECT_EQ("in1", out2_deps->nodes[0]->path());
1983 EXPECT_EQ("in2", out2_deps->nodes[1]->path());
1986 /// Test a GCC-style deps log with multiple outputs using a line per output.
1987 TEST_F(BuildWithQueryDepsLogTest, TwoOutputsDepFileGCCMultiLineOutput) {
1988 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1989 "rule cp_multi_gcc\n"
1990 " command = echo 'out1: $in\\nout2: $in' > in.d && for file in $out; do cp in1 $$file; done\n"
1993 "build out1 out2: cp_multi_gcc in1 in2\n"));
1996 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1998 fs_.Create("in.d", "out1: in1 in2\nout2: in1 in2");
1999 EXPECT_TRUE(builder_.Build(&err));
2001 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
2002 EXPECT_EQ("echo 'out1: in1 in2\\nout2: in1 in2' > in.d && for file in out1 out2; do cp in1 $file; done", command_runner_.commands_ran_[0]);
2004 Node* out1_node = state_.LookupNode("out1");
2005 DepsLog::Deps* out1_deps = log_.GetDeps(out1_node);
2006 EXPECT_EQ(2, out1_deps->node_count);
2007 EXPECT_EQ("in1", out1_deps->nodes[0]->path());
2008 EXPECT_EQ("in2", out1_deps->nodes[1]->path());
2010 Node* out2_node = state_.LookupNode("out2");
2011 DepsLog::Deps* out2_deps = log_.GetDeps(out2_node);
2012 EXPECT_EQ(2, out2_deps->node_count);
2013 EXPECT_EQ("in1", out2_deps->nodes[0]->path());
2014 EXPECT_EQ("in2", out2_deps->nodes[1]->path());
2017 /// Test a GCC-style deps log with multiple outputs mentioning only the main output.
2018 TEST_F(BuildWithQueryDepsLogTest, TwoOutputsDepFileGCCOnlyMainOutput) {
2019 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2020 "rule cp_multi_gcc\n"
2021 " command = echo 'out1: $in' > in.d && for file in $out; do cp in1 $$file; done\n"
2024 "build out1 out2: cp_multi_gcc in1 in2\n"));
2027 EXPECT_TRUE(builder_.AddTarget("out1", &err));
2029 fs_.Create("in.d", "out1: in1 in2");
2030 EXPECT_TRUE(builder_.Build(&err));
2032 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
2033 EXPECT_EQ("echo 'out1: in1 in2' > in.d && for file in out1 out2; do cp in1 $file; done", command_runner_.commands_ran_[0]);
2035 Node* out1_node = state_.LookupNode("out1");
2036 DepsLog::Deps* out1_deps = log_.GetDeps(out1_node);
2037 EXPECT_EQ(2, out1_deps->node_count);
2038 EXPECT_EQ("in1", out1_deps->nodes[0]->path());
2039 EXPECT_EQ("in2", out1_deps->nodes[1]->path());
2041 Node* out2_node = state_.LookupNode("out2");
2042 DepsLog::Deps* out2_deps = log_.GetDeps(out2_node);
2043 EXPECT_EQ(2, out2_deps->node_count);
2044 EXPECT_EQ("in1", out2_deps->nodes[0]->path());
2045 EXPECT_EQ("in2", out2_deps->nodes[1]->path());
2048 /// Test a GCC-style deps log with multiple outputs mentioning only the secondary output.
2049 TEST_F(BuildWithQueryDepsLogTest, TwoOutputsDepFileGCCOnlySecondaryOutput) {
2050 // Note: This ends up short-circuiting the node creation due to the primary
2051 // output not being present, but it should still work.
2052 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2053 "rule cp_multi_gcc\n"
2054 " command = echo 'out2: $in' > in.d && for file in $out; do cp in1 $$file; done\n"
2057 "build out1 out2: cp_multi_gcc in1 in2\n"));
2060 EXPECT_TRUE(builder_.AddTarget("out1", &err));
2062 fs_.Create("in.d", "out2: in1 in2");
2063 EXPECT_TRUE(builder_.Build(&err));
2065 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
2066 EXPECT_EQ("echo 'out2: in1 in2' > in.d && for file in out1 out2; do cp in1 $file; done", command_runner_.commands_ran_[0]);
2068 Node* out1_node = state_.LookupNode("out1");
2069 DepsLog::Deps* out1_deps = log_.GetDeps(out1_node);
2070 EXPECT_EQ(2, out1_deps->node_count);
2071 EXPECT_EQ("in1", out1_deps->nodes[0]->path());
2072 EXPECT_EQ("in2", out1_deps->nodes[1]->path());
2074 Node* out2_node = state_.LookupNode("out2");
2075 DepsLog::Deps* out2_deps = log_.GetDeps(out2_node);
2076 EXPECT_EQ(2, out2_deps->node_count);
2077 EXPECT_EQ("in1", out2_deps->nodes[0]->path());
2078 EXPECT_EQ("in2", out2_deps->nodes[1]->path());
2081 /// Tests of builds involving deps logs necessarily must span
2082 /// multiple builds. We reuse methods on BuildTest but not the
2083 /// builder_ it sets up, because we want pristine objects for
2085 struct BuildWithDepsLogTest : public BuildTest {
2086 BuildWithDepsLogTest() {}
2088 virtual void SetUp() {
2091 temp_dir_.CreateAndEnter("BuildWithDepsLogTest");
2094 virtual void TearDown() {
2095 temp_dir_.Cleanup();
2098 ScopedTempDir temp_dir_;
2100 /// Shadow parent class builder_ so we don't accidentally use it.
2104 /// Run a straightforwad build where the deps log is used.
2105 TEST_F(BuildWithDepsLogTest, Straightforward) {
2107 // Note: in1 was created by the superclass SetUp().
2108 const char* manifest =
2109 "build out: cat in1\n"
2111 " depfile = in1.d\n";
2114 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
2115 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2117 // Run the build once, everything should be ok.
2119 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2122 Builder builder(&state, config_, NULL, &deps_log, &fs_);
2123 builder.command_runner_.reset(&command_runner_);
2124 EXPECT_TRUE(builder.AddTarget("out", &err));
2126 fs_.Create("in1.d", "out: in2");
2127 EXPECT_TRUE(builder.Build(&err));
2130 // The deps file should have been removed.
2131 EXPECT_EQ(0, fs_.Stat("in1.d", &err));
2132 // Recreate it for the next step.
2133 fs_.Create("in1.d", "out: in2");
2135 builder.command_runner_.release();
2140 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
2141 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2143 // Touch the file only mentioned in the deps.
2145 fs_.Create("in2", "");
2147 // Run the build again.
2149 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
2150 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2152 Builder builder(&state, config_, NULL, &deps_log, &fs_);
2153 builder.command_runner_.reset(&command_runner_);
2154 command_runner_.commands_ran_.clear();
2155 EXPECT_TRUE(builder.AddTarget("out", &err));
2157 EXPECT_TRUE(builder.Build(&err));
2160 // We should have rebuilt the output due to in2 being
2162 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
2164 builder.command_runner_.release();
2168 /// Verify that obsolete dependency info causes a rebuild.
2169 /// 1) Run a successful build where everything has time t, record deps.
2170 /// 2) Move input/output to time t+1 -- despite files in alignment,
2171 /// should still need to rebuild due to deps at older time.
2172 TEST_F(BuildWithDepsLogTest, ObsoleteDeps) {
2174 // Note: in1 was created by the superclass SetUp().
2175 const char* manifest =
2176 "build out: cat in1\n"
2178 " depfile = in1.d\n";
2180 // Run an ordinary build that gathers dependencies.
2181 fs_.Create("in1", "");
2182 fs_.Create("in1.d", "out: ");
2185 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
2186 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2188 // Run the build once, everything should be ok.
2190 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2193 Builder builder(&state, config_, NULL, &deps_log, &fs_);
2194 builder.command_runner_.reset(&command_runner_);
2195 EXPECT_TRUE(builder.AddTarget("out", &err));
2197 EXPECT_TRUE(builder.Build(&err));
2201 builder.command_runner_.release();
2204 // Push all files one tick forward so that only the deps are out
2207 fs_.Create("in1", "");
2208 fs_.Create("out", "");
2210 // The deps file should have been removed, so no need to timestamp it.
2211 EXPECT_EQ(0, fs_.Stat("in1.d", &err));
2215 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
2216 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2219 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
2220 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2222 Builder builder(&state, config_, NULL, &deps_log, &fs_);
2223 builder.command_runner_.reset(&command_runner_);
2224 command_runner_.commands_ran_.clear();
2225 EXPECT_TRUE(builder.AddTarget("out", &err));
2228 // Recreate the deps file here because the build expects them to exist.
2229 fs_.Create("in1.d", "out: ");
2231 EXPECT_TRUE(builder.Build(&err));
2234 // We should have rebuilt the output due to the deps being
2236 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
2238 builder.command_runner_.release();
2242 TEST_F(BuildWithDepsLogTest, DepsIgnoredInDryRun) {
2243 const char* manifest =
2244 "build out: cat in1\n"
2246 " depfile = in1.d\n";
2248 fs_.Create("out", "");
2250 fs_.Create("in1", "");
2253 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
2254 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2256 // The deps log is NULL in dry runs.
2257 config_.dry_run = true;
2258 Builder builder(&state, config_, NULL, NULL, &fs_);
2259 builder.command_runner_.reset(&command_runner_);
2260 command_runner_.commands_ran_.clear();
2263 EXPECT_TRUE(builder.AddTarget("out", &err));
2265 EXPECT_TRUE(builder.Build(&err));
2266 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
2268 builder.command_runner_.release();
2271 /// Check that a restat rule generating a header cancels compilations correctly.
2272 TEST_F(BuildTest, RestatDepfileDependency) {
2273 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2275 " command = true\n" // Would be "write if out-of-date" in reality.
2277 "build header.h: true header.in\n"
2278 "build out: cat in1\n"
2279 " depfile = in1.d\n"));
2281 fs_.Create("header.h", "");
2282 fs_.Create("in1.d", "out: header.h");
2284 fs_.Create("header.in", "");
2287 EXPECT_TRUE(builder_.AddTarget("out", &err));
2289 EXPECT_TRUE(builder_.Build(&err));
2293 /// Check that a restat rule generating a header cancels compilations correctly,
2295 TEST_F(BuildWithDepsLogTest, RestatDepfileDependencyDepsLog) {
2297 // Note: in1 was created by the superclass SetUp().
2298 const char* manifest =
2300 " command = true\n" // Would be "write if out-of-date" in reality.
2302 "build header.h: true header.in\n"
2303 "build out: cat in1\n"
2305 " depfile = in1.d\n";
2308 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
2309 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2311 // Run the build once, everything should be ok.
2313 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2316 Builder builder(&state, config_, NULL, &deps_log, &fs_);
2317 builder.command_runner_.reset(&command_runner_);
2318 EXPECT_TRUE(builder.AddTarget("out", &err));
2320 fs_.Create("in1.d", "out: header.h");
2321 EXPECT_TRUE(builder.Build(&err));
2325 builder.command_runner_.release();
2330 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
2331 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2333 // Touch the input of the restat rule.
2335 fs_.Create("header.in", "");
2337 // Run the build again.
2339 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
2340 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2342 Builder builder(&state, config_, NULL, &deps_log, &fs_);
2343 builder.command_runner_.reset(&command_runner_);
2344 command_runner_.commands_ran_.clear();
2345 EXPECT_TRUE(builder.AddTarget("out", &err));
2347 EXPECT_TRUE(builder.Build(&err));
2350 // Rule "true" should have run again, but the build of "out" should have
2351 // been cancelled due to restat propagating through the depfile header.
2352 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
2354 builder.command_runner_.release();
2358 TEST_F(BuildWithDepsLogTest, DepFileOKDepsLog) {
2360 const char* manifest =
2361 "rule cc\n command = cc $in\n depfile = $out.d\n deps = gcc\n"
2362 "build fo$ o.o: cc foo.c\n";
2364 fs_.Create("foo.c", "");
2368 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2370 // Run the build once, everything should be ok.
2372 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2375 Builder builder(&state, config_, NULL, &deps_log, &fs_);
2376 builder.command_runner_.reset(&command_runner_);
2377 EXPECT_TRUE(builder.AddTarget("fo o.o", &err));
2379 fs_.Create("fo o.o.d", "fo\\ o.o: blah.h bar.h\n");
2380 EXPECT_TRUE(builder.Build(&err));
2384 builder.command_runner_.release();
2389 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2392 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
2393 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2396 Builder builder(&state, config_, NULL, &deps_log, &fs_);
2397 builder.command_runner_.reset(&command_runner_);
2399 Edge* edge = state.edges_.back();
2401 state.GetNode("bar.h", 0)->MarkDirty(); // Mark bar.h as missing.
2402 EXPECT_TRUE(builder.AddTarget("fo o.o", &err));
2405 // Expect three new edges: one generating fo o.o, and two more from
2406 // loading the depfile.
2407 ASSERT_EQ(3u, state.edges_.size());
2408 // Expect our edge to now have three inputs: foo.c and two headers.
2409 ASSERT_EQ(3u, edge->inputs_.size());
2411 // Expect the command line we generate to only use the original input.
2412 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
2415 builder.command_runner_.release();
2420 TEST_F(BuildWithDepsLogTest, DepFileDepsLogCanonicalize) {
2422 const char* manifest =
2423 "rule cc\n command = cc $in\n depfile = $out.d\n deps = gcc\n"
2424 "build a/b\\c\\d/e/fo$ o.o: cc x\\y/z\\foo.c\n";
2426 fs_.Create("x/y/z/foo.c", "");
2430 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2432 // Run the build once, everything should be ok.
2434 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2437 Builder builder(&state, config_, NULL, &deps_log, &fs_);
2438 builder.command_runner_.reset(&command_runner_);
2439 EXPECT_TRUE(builder.AddTarget("a/b/c/d/e/fo o.o", &err));
2441 // Note, different slashes from manifest.
2442 fs_.Create("a/b\\c\\d/e/fo o.o.d",
2443 "a\\b\\c\\d\\e\\fo\\ o.o: blah.h bar.h\n");
2444 EXPECT_TRUE(builder.Build(&err));
2448 builder.command_runner_.release();
2453 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
2456 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
2457 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
2460 Builder builder(&state, config_, NULL, &deps_log, &fs_);
2461 builder.command_runner_.reset(&command_runner_);
2463 Edge* edge = state.edges_.back();
2465 state.GetNode("bar.h", 0)->MarkDirty(); // Mark bar.h as missing.
2466 EXPECT_TRUE(builder.AddTarget("a/b/c/d/e/fo o.o", &err));
2469 // Expect three new edges: one generating fo o.o, and two more from
2470 // loading the depfile.
2471 ASSERT_EQ(3u, state.edges_.size());
2472 // Expect our edge to now have three inputs: foo.c and two headers.
2473 ASSERT_EQ(3u, edge->inputs_.size());
2475 // Expect the command line we generate to only use the original input.
2476 // Note, slashes from manifest, not .d.
2477 ASSERT_EQ("cc x\\y/z\\foo.c", edge->EvaluateCommand());
2480 builder.command_runner_.release();
2485 /// Check that a restat rule doesn't clear an edge if the depfile is missing.
2486 /// Follows from: https://github.com/ninja-build/ninja/issues/603
2487 TEST_F(BuildTest, RestatMissingDepfile) {
2488 const char* manifest =
2490 " command = true\n" // Would be "write if out-of-date" in reality.
2492 "build header.h: true header.in\n"
2493 "build out: cat header.h\n"
2494 " depfile = out.d\n";
2496 fs_.Create("header.h", "");
2498 fs_.Create("out", "");
2499 fs_.Create("header.in", "");
2501 // Normally, only 'header.h' would be rebuilt, as
2502 // its rule doesn't touch the output and has 'restat=1' set.
2503 // But we are also missing the depfile for 'out',
2504 // which should force its command to run anyway!
2505 RebuildTarget("out", manifest);
2506 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
2509 /// Check that a restat rule doesn't clear an edge if the deps are missing.
2510 /// https://github.com/ninja-build/ninja/issues/603
2511 TEST_F(BuildWithDepsLogTest, RestatMissingDepfileDepslog) {
2513 const char* manifest =
2515 " command = true\n" // Would be "write if out-of-date" in reality.
2517 "build header.h: true header.in\n"
2518 "build out: cat header.h\n"
2520 " depfile = out.d\n";
2522 // Build once to populate ninja deps logs from out.d
2523 fs_.Create("header.in", "");
2524 fs_.Create("out.d", "out: header.h");
2525 fs_.Create("header.h", "");
2527 RebuildTarget("out", manifest, "build_log", "ninja_deps");
2528 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
2530 // Sanity: this rebuild should be NOOP
2531 RebuildTarget("out", manifest, "build_log", "ninja_deps");
2532 ASSERT_EQ(0u, command_runner_.commands_ran_.size());
2534 // Touch 'header.in', blank dependencies log (create a different one).
2535 // Building header.h triggers 'restat' outputs cleanup.
2536 // Validate that out is rebuilt netherless, as deps are missing.
2538 fs_.Create("header.in", "");
2540 // (switch to a new blank deps_log "ninja_deps2")
2541 RebuildTarget("out", manifest, "build_log", "ninja_deps2");
2542 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
2544 // Sanity: this build should be NOOP
2545 RebuildTarget("out", manifest, "build_log", "ninja_deps2");
2546 ASSERT_EQ(0u, command_runner_.commands_ran_.size());
2548 // Check that invalidating deps by target timestamp also works here
2549 // Repeat the test but touch target instead of blanking the log.
2551 fs_.Create("header.in", "");
2552 fs_.Create("out", "");
2553 RebuildTarget("out", manifest, "build_log", "ninja_deps2");
2554 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
2556 // And this build should be NOOP again
2557 RebuildTarget("out", manifest, "build_log", "ninja_deps2");
2558 ASSERT_EQ(0u, command_runner_.commands_ran_.size());
2561 TEST_F(BuildTest, WrongOutputInDepfileCausesRebuild) {
2563 const char* manifest =
2565 " command = cc $in\n"
2566 " depfile = $out.d\n"
2567 "build foo.o: cc foo.c\n";
2569 fs_.Create("foo.c", "");
2570 fs_.Create("foo.o", "");
2571 fs_.Create("header.h", "");
2572 fs_.Create("foo.o.d", "bar.o.d: header.h\n");
2574 RebuildTarget("foo.o", manifest, "build_log", "ninja_deps");
2575 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
2578 TEST_F(BuildTest, Console) {
2579 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2581 " command = console\n"
2583 "build cons: console in.txt\n"));
2585 fs_.Create("in.txt", "");
2588 EXPECT_TRUE(builder_.AddTarget("cons", &err));
2590 EXPECT_TRUE(builder_.Build(&err));
2592 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
2595 TEST_F(BuildTest, DyndepMissingAndNoRule) {
2596 // Verify that we can diagnose when a dyndep file is missing and
2597 // has no rule to build it.
2598 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2600 " command = touch $out\n"
2601 "build out: touch || dd\n"
2606 EXPECT_FALSE(builder_.AddTarget("out", &err));
2607 EXPECT_EQ("loading 'dd': No such file or directory", err);
2610 TEST_F(BuildTest, DyndepReadyImplicitConnection) {
2611 // Verify that a dyndep file can be loaded immediately to discover
2612 // that one edge has an implicit output that is also an implicit
2613 // input of another edge.
2614 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2616 " command = touch $out $out.imp\n"
2617 "build tmp: touch || dd\n"
2619 "build out: touch || dd\n"
2623 "ninja_dyndep_version = 1\n"
2624 "build out | out.imp: dyndep | tmp.imp\n"
2625 "build tmp | tmp.imp: dyndep\n"
2629 EXPECT_TRUE(builder_.AddTarget("out", &err));
2631 EXPECT_TRUE(builder_.Build(&err));
2633 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
2634 EXPECT_EQ("touch tmp tmp.imp", command_runner_.commands_ran_[0]);
2635 EXPECT_EQ("touch out out.imp", command_runner_.commands_ran_[1]);
2638 TEST_F(BuildTest, DyndepReadySyntaxError) {
2639 // Verify that a dyndep file can be loaded immediately to discover
2640 // and reject a syntax error in it.
2641 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2643 " command = touch $out\n"
2644 "build out: touch || dd\n"
2648 "build out: dyndep\n"
2652 EXPECT_FALSE(builder_.AddTarget("out", &err));
2653 EXPECT_EQ("dd:1: expected 'ninja_dyndep_version = ...'\n", err);
2656 TEST_F(BuildTest, DyndepReadyCircular) {
2657 // Verify that a dyndep file can be loaded immediately to discover
2658 // and reject a circular dependency.
2659 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2661 " command = unused\n"
2662 "build out: r in || dd\n"
2664 "build in: r circ\n"
2667 "ninja_dyndep_version = 1\n"
2668 "build out | circ: dyndep\n"
2670 fs_.Create("out", "");
2673 EXPECT_FALSE(builder_.AddTarget("out", &err));
2674 EXPECT_EQ("dependency cycle: circ -> in -> circ", err);
2677 TEST_F(BuildTest, DyndepBuild) {
2678 // Verify that a dyndep file can be built and loaded to discover nothing.
2679 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2681 " command = touch $out\n"
2683 " command = cp $in $out\n"
2684 "build dd: cp dd-in\n"
2685 "build out: touch || dd\n"
2689 "ninja_dyndep_version = 1\n"
2690 "build out: dyndep\n"
2694 EXPECT_TRUE(builder_.AddTarget("out", &err));
2697 size_t files_created = fs_.files_created_.size();
2698 EXPECT_TRUE(builder_.Build(&err));
2701 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
2702 EXPECT_EQ("cp dd-in dd", command_runner_.commands_ran_[0]);
2703 EXPECT_EQ("touch out", command_runner_.commands_ran_[1]);
2704 ASSERT_EQ(2u, fs_.files_read_.size());
2705 EXPECT_EQ("dd-in", fs_.files_read_[0]);
2706 EXPECT_EQ("dd", fs_.files_read_[1]);
2707 ASSERT_EQ(2u + files_created, fs_.files_created_.size());
2708 EXPECT_EQ(1u, fs_.files_created_.count("dd"));
2709 EXPECT_EQ(1u, fs_.files_created_.count("out"));
2712 TEST_F(BuildTest, DyndepBuildSyntaxError) {
2713 // Verify that a dyndep file can be built and loaded to discover
2714 // and reject a syntax error in it.
2715 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2717 " command = touch $out\n"
2719 " command = cp $in $out\n"
2720 "build dd: cp dd-in\n"
2721 "build out: touch || dd\n"
2725 "build out: dyndep\n"
2729 EXPECT_TRUE(builder_.AddTarget("out", &err));
2732 EXPECT_FALSE(builder_.Build(&err));
2733 EXPECT_EQ("dd:1: expected 'ninja_dyndep_version = ...'\n", err);
2736 TEST_F(BuildTest, DyndepBuildUnrelatedOutput) {
2737 // Verify that a dyndep file can have dependents that do not specify
2738 // it as their dyndep binding.
2739 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2741 " command = touch $out\n"
2743 " command = cp $in $out\n"
2744 "build dd: cp dd-in\n"
2745 "build unrelated: touch || dd\n"
2746 "build out: touch unrelated || dd\n"
2750 "ninja_dyndep_version = 1\n"
2751 "build out: dyndep\n"
2754 fs_.Create("out", "");
2757 EXPECT_TRUE(builder_.AddTarget("out", &err));
2760 EXPECT_TRUE(builder_.Build(&err));
2762 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
2763 EXPECT_EQ("cp dd-in dd", command_runner_.commands_ran_[0]);
2764 EXPECT_EQ("touch unrelated", command_runner_.commands_ran_[1]);
2765 EXPECT_EQ("touch out", command_runner_.commands_ran_[2]);
2768 TEST_F(BuildTest, DyndepBuildDiscoverNewOutput) {
2769 // Verify that a dyndep file can be built and loaded to discover
2770 // a new output of an edge.
2771 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2773 " command = touch $out $out.imp\n"
2775 " command = cp $in $out\n"
2776 "build dd: cp dd-in\n"
2777 "build out: touch in || dd\n"
2780 fs_.Create("in", "");
2782 "ninja_dyndep_version = 1\n"
2783 "build out | out.imp: dyndep\n"
2786 fs_.Create("out", "");
2789 EXPECT_TRUE(builder_.AddTarget("out", &err));
2792 EXPECT_TRUE(builder_.Build(&err));
2794 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
2795 EXPECT_EQ("cp dd-in dd", command_runner_.commands_ran_[0]);
2796 EXPECT_EQ("touch out out.imp", command_runner_.commands_ran_[1]);
2799 TEST_F(BuildTest, DyndepBuildDiscoverNewOutputWithMultipleRules1) {
2800 // Verify that a dyndep file can be built and loaded to discover
2801 // a new output of an edge that is already the output of another edge.
2802 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2804 " command = touch $out $out.imp\n"
2806 " command = cp $in $out\n"
2807 "build dd: cp dd-in\n"
2808 "build out1 | out-twice.imp: touch in\n"
2809 "build out2: touch in || dd\n"
2812 fs_.Create("in", "");
2814 "ninja_dyndep_version = 1\n"
2815 "build out2 | out-twice.imp: dyndep\n"
2818 fs_.Create("out1", "");
2819 fs_.Create("out2", "");
2822 EXPECT_TRUE(builder_.AddTarget("out1", &err));
2823 EXPECT_TRUE(builder_.AddTarget("out2", &err));
2826 EXPECT_FALSE(builder_.Build(&err));
2827 EXPECT_EQ("multiple rules generate out-twice.imp", err);
2830 TEST_F(BuildTest, DyndepBuildDiscoverNewOutputWithMultipleRules2) {
2831 // Verify that a dyndep file can be built and loaded to discover
2832 // a new output of an edge that is already the output of another
2833 // edge also discovered by dyndep.
2834 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2836 " command = touch $out $out.imp\n"
2838 " command = cp $in $out\n"
2839 "build dd1: cp dd1-in\n"
2840 "build out1: touch || dd1\n"
2842 "build dd2: cp dd2-in || dd1\n" // make order predictable for test
2843 "build out2: touch || dd2\n"
2846 fs_.Create("out1", "");
2847 fs_.Create("out2", "");
2848 fs_.Create("dd1-in",
2849 "ninja_dyndep_version = 1\n"
2850 "build out1 | out-twice.imp: dyndep\n"
2852 fs_.Create("dd2-in", "");
2854 "ninja_dyndep_version = 1\n"
2855 "build out2 | out-twice.imp: dyndep\n"
2858 fs_.Create("out1", "");
2859 fs_.Create("out2", "");
2862 EXPECT_TRUE(builder_.AddTarget("out1", &err));
2863 EXPECT_TRUE(builder_.AddTarget("out2", &err));
2866 EXPECT_FALSE(builder_.Build(&err));
2867 EXPECT_EQ("multiple rules generate out-twice.imp", err);
2870 TEST_F(BuildTest, DyndepBuildDiscoverNewInput) {
2871 // Verify that a dyndep file can be built and loaded to discover
2872 // a new input to an edge.
2873 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2875 " command = touch $out\n"
2877 " command = cp $in $out\n"
2878 "build dd: cp dd-in\n"
2880 "build out: touch || dd\n"
2884 "ninja_dyndep_version = 1\n"
2885 "build out: dyndep | in\n"
2888 fs_.Create("out", "");
2891 EXPECT_TRUE(builder_.AddTarget("out", &err));
2894 EXPECT_TRUE(builder_.Build(&err));
2896 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
2897 EXPECT_EQ("cp dd-in dd", command_runner_.commands_ran_[0]);
2898 EXPECT_EQ("touch in", command_runner_.commands_ran_[1]);
2899 EXPECT_EQ("touch out", command_runner_.commands_ran_[2]);
2902 TEST_F(BuildTest, DyndepBuildDiscoverImplicitConnection) {
2903 // Verify that a dyndep file can be built and loaded to discover
2904 // that one edge has an implicit output that is also an implicit
2905 // input of another edge.
2906 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2908 " command = touch $out $out.imp\n"
2910 " command = cp $in $out\n"
2911 "build dd: cp dd-in\n"
2912 "build tmp: touch || dd\n"
2914 "build out: touch || dd\n"
2918 "ninja_dyndep_version = 1\n"
2919 "build out | out.imp: dyndep | tmp.imp\n"
2920 "build tmp | tmp.imp: dyndep\n"
2924 EXPECT_TRUE(builder_.AddTarget("out", &err));
2926 EXPECT_TRUE(builder_.Build(&err));
2928 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
2929 EXPECT_EQ("cp dd-in dd", command_runner_.commands_ran_[0]);
2930 EXPECT_EQ("touch tmp tmp.imp", command_runner_.commands_ran_[1]);
2931 EXPECT_EQ("touch out out.imp", command_runner_.commands_ran_[2]);
2934 TEST_F(BuildTest, DyndepBuildDiscoverNowWantEdge) {
2935 // Verify that a dyndep file can be built and loaded to discover
2936 // that an edge is actually wanted due to a missing implicit output.
2937 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2939 " command = touch $out $out.imp\n"
2941 " command = cp $in $out\n"
2942 "build dd: cp dd-in\n"
2943 "build tmp: touch || dd\n"
2945 "build out: touch tmp || dd\n"
2948 fs_.Create("tmp", "");
2949 fs_.Create("out", "");
2951 "ninja_dyndep_version = 1\n"
2952 "build out: dyndep\n"
2953 "build tmp | tmp.imp: dyndep\n"
2957 EXPECT_TRUE(builder_.AddTarget("out", &err));
2959 EXPECT_TRUE(builder_.Build(&err));
2961 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
2962 EXPECT_EQ("cp dd-in dd", command_runner_.commands_ran_[0]);
2963 EXPECT_EQ("touch tmp tmp.imp", command_runner_.commands_ran_[1]);
2964 EXPECT_EQ("touch out out.imp", command_runner_.commands_ran_[2]);
2967 TEST_F(BuildTest, DyndepBuildDiscoverNowWantEdgeAndDependent) {
2968 // Verify that a dyndep file can be built and loaded to discover
2969 // that an edge and a dependent are actually wanted.
2970 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2972 " command = touch $out $out.imp\n"
2974 " command = cp $in $out\n"
2975 "build dd: cp dd-in\n"
2976 "build tmp: touch || dd\n"
2978 "build out: touch tmp\n"
2980 fs_.Create("tmp", "");
2981 fs_.Create("out", "");
2983 "ninja_dyndep_version = 1\n"
2984 "build tmp | tmp.imp: dyndep\n"
2988 EXPECT_TRUE(builder_.AddTarget("out", &err));
2990 EXPECT_TRUE(builder_.Build(&err));
2992 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
2993 EXPECT_EQ("cp dd-in dd", command_runner_.commands_ran_[0]);
2994 EXPECT_EQ("touch tmp tmp.imp", command_runner_.commands_ran_[1]);
2995 EXPECT_EQ("touch out out.imp", command_runner_.commands_ran_[2]);
2998 TEST_F(BuildTest, DyndepBuildDiscoverCircular) {
2999 // Verify that a dyndep file can be built and loaded to discover
3000 // and reject a circular dependency.
3001 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
3003 " command = unused\n"
3005 " command = cp $in $out\n"
3006 "build dd: cp dd-in\n"
3007 "build out: r in || dd\n"
3008 " depfile = out.d\n"
3010 "build in: r || dd\n"
3013 fs_.Create("out.d", "out: inimp\n");
3015 "ninja_dyndep_version = 1\n"
3016 "build out | circ: dyndep\n"
3017 "build in: dyndep | circ\n"
3019 fs_.Create("out", "");
3022 EXPECT_TRUE(builder_.AddTarget("out", &err));
3025 EXPECT_FALSE(builder_.Build(&err));
3026 // Depending on how the pointers in Plan::ready_ work out, we could have
3027 // discovered the cycle from either starting point.
3028 EXPECT_TRUE(err == "dependency cycle: circ -> in -> circ" ||
3029 err == "dependency cycle: in -> circ -> in");
3032 TEST_F(BuildWithLogTest, DyndepBuildDiscoverRestat) {
3033 // Verify that a dyndep file can be built and loaded to discover
3034 // that an edge has a restat binding.
3035 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
3039 " command = cp $in $out\n"
3040 "build dd: cp dd-in\n"
3041 "build out1: true in || dd\n"
3043 "build out2: cat out1\n"));
3045 fs_.Create("out1", "");
3046 fs_.Create("out2", "");
3048 "ninja_dyndep_version = 1\n"
3049 "build out1: dyndep\n"
3053 fs_.Create("in", "");
3055 // Do a pre-build so that there's commands in the log for the outputs,
3056 // otherwise, the lack of an entry in the build log will cause "out2" to
3057 // rebuild regardless of restat.
3059 EXPECT_TRUE(builder_.AddTarget("out2", &err));
3061 EXPECT_TRUE(builder_.Build(&err));
3063 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
3064 EXPECT_EQ("cp dd-in dd", command_runner_.commands_ran_[0]);
3065 EXPECT_EQ("true", command_runner_.commands_ran_[1]);
3066 EXPECT_EQ("cat out1 > out2", command_runner_.commands_ran_[2]);
3068 command_runner_.commands_ran_.clear();
3071 fs_.Create("in", "");
3073 // We touched "in", so we should build "out1". But because "true" does not
3074 // touch "out1", we should cancel the build of "out2".
3075 EXPECT_TRUE(builder_.AddTarget("out2", &err));
3077 EXPECT_TRUE(builder_.Build(&err));
3078 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
3079 EXPECT_EQ("true", command_runner_.commands_ran_[0]);
3082 TEST_F(BuildTest, DyndepBuildDiscoverScheduledEdge) {
3083 // Verify that a dyndep file can be built and loaded to discover a
3084 // new input that itself is an output from an edge that has already
3085 // been scheduled but not finished. We should not re-schedule it.
3086 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
3088 " command = touch $out $out.imp\n"
3090 " command = cp $in $out\n"
3091 "build out1 | out1.imp: touch\n"
3092 "build zdd: cp zdd-in\n"
3093 " verify_active_edge = out1\n" // verify out1 is active when zdd is finished
3094 "build out2: cp out1 || zdd\n"
3097 fs_.Create("zdd-in",
3098 "ninja_dyndep_version = 1\n"
3099 "build out2: dyndep | out1.imp\n"
3102 // Enable concurrent builds so that we can load the dyndep file
3103 // while another edge is still active.
3104 command_runner_.max_active_edges_ = 2;
3106 // During the build "out1" and "zdd" should be built concurrently.
3107 // The fake command runner will finish these in reverse order
3108 // of the names of the first outputs, so "zdd" will finish first
3109 // and we will load the dyndep file while the edge for "out1" is
3110 // still active. This will add a new dependency on "out1.imp",
3111 // also produced by the active edge. The builder should not
3112 // re-schedule the already-active edge.
3115 EXPECT_TRUE(builder_.AddTarget("out1", &err));
3116 EXPECT_TRUE(builder_.AddTarget("out2", &err));
3118 EXPECT_TRUE(builder_.Build(&err));
3120 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
3121 // Depending on how the pointers in Plan::ready_ work out, the first
3122 // two commands may have run in either order.
3123 EXPECT_TRUE((command_runner_.commands_ran_[0] == "touch out1 out1.imp" &&
3124 command_runner_.commands_ran_[1] == "cp zdd-in zdd") ||
3125 (command_runner_.commands_ran_[1] == "touch out1 out1.imp" &&
3126 command_runner_.commands_ran_[0] == "cp zdd-in zdd"));
3127 EXPECT_EQ("cp out1 out2", command_runner_.commands_ran_[2]);
3130 TEST_F(BuildTest, DyndepTwoLevelDirect) {
3131 // Verify that a clean dyndep file can depend on a dirty dyndep file
3132 // and be loaded properly after the dirty one is built and loaded.
3133 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
3135 " command = touch $out $out.imp\n"
3137 " command = cp $in $out\n"
3138 "build dd1: cp dd1-in\n"
3139 "build out1 | out1.imp: touch || dd1\n"
3141 "build dd2: cp dd2-in || dd1\n" // direct order-only dep on dd1
3142 "build out2: touch || dd2\n"
3145 fs_.Create("out1.imp", "");
3146 fs_.Create("out2", "");
3147 fs_.Create("out2.imp", "");
3148 fs_.Create("dd1-in",
3149 "ninja_dyndep_version = 1\n"
3150 "build out1: dyndep\n"
3152 fs_.Create("dd2-in", "");
3154 "ninja_dyndep_version = 1\n"
3155 "build out2 | out2.imp: dyndep | out1.imp\n"
3158 // During the build dd1 should be built and loaded. The RecomputeDirty
3159 // called as a result of loading dd1 should not cause dd2 to be loaded
3160 // because the builder will never get a chance to update the build plan
3161 // to account for dd2. Instead dd2 should only be later loaded once the
3162 // builder recognizes that it is now ready (as its order-only dependency
3163 // on dd1 has been satisfied). This test case verifies that each dyndep
3164 // file is loaded to update the build graph independently.
3167 EXPECT_TRUE(builder_.AddTarget("out2", &err));
3169 EXPECT_TRUE(builder_.Build(&err));
3171 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
3172 EXPECT_EQ("cp dd1-in dd1", command_runner_.commands_ran_[0]);
3173 EXPECT_EQ("touch out1 out1.imp", command_runner_.commands_ran_[1]);
3174 EXPECT_EQ("touch out2 out2.imp", command_runner_.commands_ran_[2]);
3177 TEST_F(BuildTest, DyndepTwoLevelIndirect) {
3178 // Verify that dyndep files can add to an edge new implicit inputs that
3179 // correspond to implicit outputs added to other edges by other dyndep
3180 // files on which they (order-only) depend.
3181 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
3183 " command = touch $out $out.imp\n"
3185 " command = cp $in $out\n"
3186 "build dd1: cp dd1-in\n"
3187 "build out1: touch || dd1\n"
3189 "build dd2: cp dd2-in || out1\n" // indirect order-only dep on dd1
3190 "build out2: touch || dd2\n"
3193 fs_.Create("out1.imp", "");
3194 fs_.Create("out2", "");
3195 fs_.Create("out2.imp", "");
3196 fs_.Create("dd1-in",
3197 "ninja_dyndep_version = 1\n"
3198 "build out1 | out1.imp: dyndep\n"
3200 fs_.Create("dd2-in", "");
3202 "ninja_dyndep_version = 1\n"
3203 "build out2 | out2.imp: dyndep | out1.imp\n"
3206 // During the build dd1 should be built and loaded. Then dd2 should
3207 // be built and loaded. Loading dd2 should cause the builder to
3208 // recognize that out2 needs to be built even though it was originally
3209 // clean without dyndep info.
3212 EXPECT_TRUE(builder_.AddTarget("out2", &err));
3214 EXPECT_TRUE(builder_.Build(&err));
3216 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
3217 EXPECT_EQ("cp dd1-in dd1", command_runner_.commands_ran_[0]);
3218 EXPECT_EQ("touch out1 out1.imp", command_runner_.commands_ran_[1]);
3219 EXPECT_EQ("touch out2 out2.imp", command_runner_.commands_ran_[2]);
3222 TEST_F(BuildTest, DyndepTwoLevelDiscoveredReady) {
3223 // Verify that a dyndep file can discover a new input whose
3224 // edge also has a dyndep file that is ready to load immediately.
3225 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
3227 " command = touch $out\n"
3229 " command = cp $in $out\n"
3230 "build dd0: cp dd0-in\n"
3231 "build dd1: cp dd1-in\n"
3233 "build tmp: touch || dd0\n"
3235 "build out: touch || dd1\n"
3238 fs_.Create("dd1-in",
3239 "ninja_dyndep_version = 1\n"
3240 "build out: dyndep | tmp\n"
3242 fs_.Create("dd0-in", "");
3244 "ninja_dyndep_version = 1\n"
3245 "build tmp: dyndep | in\n"
3248 fs_.Create("out", "");
3251 EXPECT_TRUE(builder_.AddTarget("out", &err));
3254 EXPECT_TRUE(builder_.Build(&err));
3256 ASSERT_EQ(4u, command_runner_.commands_ran_.size());
3257 EXPECT_EQ("cp dd1-in dd1", command_runner_.commands_ran_[0]);
3258 EXPECT_EQ("touch in", command_runner_.commands_ran_[1]);
3259 EXPECT_EQ("touch tmp", command_runner_.commands_ran_[2]);
3260 EXPECT_EQ("touch out", command_runner_.commands_ran_[3]);
3263 TEST_F(BuildTest, DyndepTwoLevelDiscoveredDirty) {
3264 // Verify that a dyndep file can discover a new input whose
3265 // edge also has a dyndep file that needs to be built.
3266 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
3268 " command = touch $out\n"
3270 " command = cp $in $out\n"
3271 "build dd0: cp dd0-in\n"
3272 "build dd1: cp dd1-in\n"
3274 "build tmp: touch || dd0\n"
3276 "build out: touch || dd1\n"
3279 fs_.Create("dd1-in",
3280 "ninja_dyndep_version = 1\n"
3281 "build out: dyndep | tmp\n"
3283 fs_.Create("dd0-in",
3284 "ninja_dyndep_version = 1\n"
3285 "build tmp: dyndep | in\n"
3288 fs_.Create("out", "");
3291 EXPECT_TRUE(builder_.AddTarget("out", &err));
3294 EXPECT_TRUE(builder_.Build(&err));
3296 ASSERT_EQ(5u, command_runner_.commands_ran_.size());
3297 EXPECT_EQ("cp dd1-in dd1", command_runner_.commands_ran_[0]);
3298 EXPECT_EQ("cp dd0-in dd0", command_runner_.commands_ran_[1]);
3299 EXPECT_EQ("touch in", command_runner_.commands_ran_[2]);
3300 EXPECT_EQ("touch tmp", command_runner_.commands_ran_[3]);
3301 EXPECT_EQ("touch out", command_runner_.commands_ran_[4]);