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"
21 /// Fixture for tests involving Plan.
22 // Though Plan doesn't use State, it's useful to have one around
23 // to create Nodes and Edges.
24 struct PlanTest : public StateTestWithBuiltinRules {
28 TEST_F(PlanTest, Basic) {
30 "build out: cat mid\n"
31 "build mid: cat in\n");
32 GetNode("mid")->MarkDirty();
33 GetNode("out")->MarkDirty();
35 EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
37 ASSERT_TRUE(plan_.more_to_do());
39 Edge* edge = plan_.FindWork();
41 ASSERT_EQ("in", edge->inputs_[0]->path());
42 ASSERT_EQ("mid", edge->outputs_[0]->path());
44 ASSERT_FALSE(plan_.FindWork());
46 plan_.EdgeFinished(edge);
48 edge = plan_.FindWork();
50 ASSERT_EQ("mid", edge->inputs_[0]->path());
51 ASSERT_EQ("out", edge->outputs_[0]->path());
53 plan_.EdgeFinished(edge);
55 ASSERT_FALSE(plan_.more_to_do());
56 edge = plan_.FindWork();
60 // Test that two outputs from one rule can be handled as inputs to the next.
61 TEST_F(PlanTest, DoubleOutputDirect) {
63 "build out: cat mid1 mid2\n"
64 "build mid1 mid2: cat in\n");
65 GetNode("mid1")->MarkDirty();
66 GetNode("mid2")->MarkDirty();
67 GetNode("out")->MarkDirty();
70 EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
72 ASSERT_TRUE(plan_.more_to_do());
75 edge = plan_.FindWork();
76 ASSERT_TRUE(edge); // cat in
77 plan_.EdgeFinished(edge);
79 edge = plan_.FindWork();
80 ASSERT_TRUE(edge); // cat mid1 mid2
81 plan_.EdgeFinished(edge);
83 edge = plan_.FindWork();
84 ASSERT_FALSE(edge); // done
87 // Test that two outputs from one rule can eventually be routed to another.
88 TEST_F(PlanTest, DoubleOutputIndirect) {
90 "build out: cat b1 b2\n"
93 "build a1 a2: cat in\n");
94 GetNode("a1")->MarkDirty();
95 GetNode("a2")->MarkDirty();
96 GetNode("b1")->MarkDirty();
97 GetNode("b2")->MarkDirty();
98 GetNode("out")->MarkDirty();
100 EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
102 ASSERT_TRUE(plan_.more_to_do());
105 edge = plan_.FindWork();
106 ASSERT_TRUE(edge); // cat in
107 plan_.EdgeFinished(edge);
109 edge = plan_.FindWork();
110 ASSERT_TRUE(edge); // cat a1
111 plan_.EdgeFinished(edge);
113 edge = plan_.FindWork();
114 ASSERT_TRUE(edge); // cat a2
115 plan_.EdgeFinished(edge);
117 edge = plan_.FindWork();
118 ASSERT_TRUE(edge); // cat b1 b2
119 plan_.EdgeFinished(edge);
121 edge = plan_.FindWork();
122 ASSERT_FALSE(edge); // done
125 // Test that two edges from one output can both execute.
126 TEST_F(PlanTest, DoubleDependent) {
128 "build out: cat a1 a2\n"
129 "build a1: cat mid\n"
130 "build a2: cat mid\n"
131 "build mid: cat in\n");
132 GetNode("mid")->MarkDirty();
133 GetNode("a1")->MarkDirty();
134 GetNode("a2")->MarkDirty();
135 GetNode("out")->MarkDirty();
138 EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
140 ASSERT_TRUE(plan_.more_to_do());
143 edge = plan_.FindWork();
144 ASSERT_TRUE(edge); // cat in
145 plan_.EdgeFinished(edge);
147 edge = plan_.FindWork();
148 ASSERT_TRUE(edge); // cat mid
149 plan_.EdgeFinished(edge);
151 edge = plan_.FindWork();
152 ASSERT_TRUE(edge); // cat mid
153 plan_.EdgeFinished(edge);
155 edge = plan_.FindWork();
156 ASSERT_TRUE(edge); // cat a1 a2
157 plan_.EdgeFinished(edge);
159 edge = plan_.FindWork();
160 ASSERT_FALSE(edge); // done
163 TEST_F(PlanTest, DependencyCycle) {
165 "build out: cat mid\n"
166 "build mid: cat in\n"
167 "build in: cat pre\n"
168 "build pre: cat out\n");
169 GetNode("out")->MarkDirty();
170 GetNode("mid")->MarkDirty();
171 GetNode("in")->MarkDirty();
172 GetNode("pre")->MarkDirty();
175 EXPECT_FALSE(plan_.AddTarget(GetNode("out"), &err));
176 ASSERT_EQ("dependency cycle: out -> mid -> in -> pre -> out", err);
179 TEST_F(PlanTest, PoolWithDepthOne) {
180 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
184 " command = cat $in > $out\n"
186 "build out1: poolcat in\n"
187 "build out2: poolcat in\n"));
188 GetNode("out1")->MarkDirty();
189 GetNode("out2")->MarkDirty();
191 EXPECT_TRUE(plan_.AddTarget(GetNode("out1"), &err));
193 EXPECT_TRUE(plan_.AddTarget(GetNode("out2"), &err));
195 ASSERT_TRUE(plan_.more_to_do());
197 Edge* edge = plan_.FindWork();
199 ASSERT_EQ("in", edge->inputs_[0]->path());
200 ASSERT_EQ("out1", edge->outputs_[0]->path());
202 // This will be false since poolcat is serialized
203 ASSERT_FALSE(plan_.FindWork());
205 plan_.EdgeFinished(edge);
207 edge = plan_.FindWork();
209 ASSERT_EQ("in", edge->inputs_[0]->path());
210 ASSERT_EQ("out2", edge->outputs_[0]->path());
212 ASSERT_FALSE(plan_.FindWork());
214 plan_.EdgeFinished(edge);
216 ASSERT_FALSE(plan_.more_to_do());
217 edge = plan_.FindWork();
221 TEST_F(PlanTest, PoolsWithDepthTwo) {
222 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
228 " command = cat $in > $out\n"
231 " command = cat $in > $out\n"
233 "build out1: foocat in\n"
234 "build out2: foocat in\n"
235 "build out3: foocat in\n"
236 "build outb1: bazcat in\n"
237 "build outb2: bazcat in\n"
238 "build outb3: bazcat in\n"
240 "build allTheThings: cat out1 out2 out3 outb1 outb2 outb3\n"
242 // Mark all the out* nodes dirty
243 for(int i = 0; i < 3; ++i) {
244 GetNode("out"+string(1, '1'+i))->MarkDirty();
245 GetNode("outb"+string(1, '1'+i))->MarkDirty();
247 GetNode("allTheThings")->MarkDirty();
250 EXPECT_TRUE(plan_.AddTarget(GetNode("allTheThings"), &err));
253 // Grab the first 4 edges, out1 out2 outb1 outb2
255 for(int i = 0; i < 4; ++i) {
256 ASSERT_TRUE(plan_.more_to_do());
257 Edge* edge = plan_.FindWork();
259 ASSERT_EQ("in", edge->inputs_[0]->path());
260 string base_name(i < 2 ? "out" : "outb");
261 ASSERT_EQ(base_name+string(1, '1'+(i%2)), edge->outputs_[0]->path());
262 edges.push_back(edge);
265 // outb3 is exempt because it has an empty pool
266 ASSERT_TRUE(plan_.more_to_do());
267 Edge* edge = plan_.FindWork();
269 ASSERT_EQ("in", edge->inputs_[0]->path());
270 ASSERT_EQ("outb3", edge->outputs_[0]->path());
271 edges.push_back(edge);
273 ASSERT_FALSE(plan_.FindWork());
276 plan_.EdgeFinished(edges.front());
279 // out3 should be available
280 Edge* out3 = plan_.FindWork();
282 ASSERT_EQ("in", out3->inputs_[0]->path());
283 ASSERT_EQ("out3", out3->outputs_[0]->path());
285 ASSERT_FALSE(plan_.FindWork());
287 plan_.EdgeFinished(out3);
289 ASSERT_FALSE(plan_.FindWork());
291 for(deque<Edge*>::iterator it = edges.begin(); it != edges.end(); ++it) {
292 plan_.EdgeFinished(*it);
295 Edge* final = plan_.FindWork();
297 ASSERT_EQ("allTheThings", final->outputs_[0]->path());
299 plan_.EdgeFinished(final);
301 ASSERT_FALSE(plan_.more_to_do());
302 ASSERT_FALSE(plan_.FindWork());
305 struct BuildTest : public StateTestWithBuiltinRules,
306 public CommandRunner {
307 BuildTest() : config_(MakeConfig()),
308 builder_(&state_, config_, NULL, &fs_),
309 now_(1), last_command_(NULL), status_(config_) {
310 builder_.command_runner_.reset(this);
312 "build cat1: cat in1\n"
313 "build cat2: cat in1 in2\n"
314 "build cat12: cat cat1 cat2\n");
316 fs_.Create("in1", now_, "");
317 fs_.Create("in2", now_, "");
321 builder_.command_runner_.release();
324 // Mark a path dirty.
325 void Dirty(const string& path);
327 // CommandRunner impl
328 virtual bool CanRunMore();
329 virtual bool StartCommand(Edge* edge);
330 virtual Edge* WaitForCommand(ExitStatus* status, string* output);
331 virtual vector<Edge*> GetActiveEdges();
332 virtual void Abort();
334 BuildConfig MakeConfig() {
336 config.verbosity = BuildConfig::QUIET;
341 VirtualFileSystem fs_;
345 vector<string> commands_ran_;
350 void BuildTest::Dirty(const string& path) {
351 Node* node = GetNode(path);
354 // If it's an input file, mark that we've already stat()ed it and
356 if (!node->in_edge())
360 bool BuildTest::CanRunMore() {
361 // Only run one at a time.
362 return last_command_ == NULL;
365 bool BuildTest::StartCommand(Edge* edge) {
366 assert(!last_command_);
367 commands_ran_.push_back(edge->EvaluateCommand());
368 if (edge->rule().name() == "cat" ||
369 edge->rule().name() == "cat_rsp" ||
370 edge->rule().name() == "cc" ||
371 edge->rule().name() == "touch" ||
372 edge->rule().name() == "touch-interrupt") {
373 for (vector<Node*>::iterator out = edge->outputs_.begin();
374 out != edge->outputs_.end(); ++out) {
375 fs_.Create((*out)->path(), now_, "");
377 } else if (edge->rule().name() == "true" ||
378 edge->rule().name() == "fail" ||
379 edge->rule().name() == "interrupt") {
380 // Don't do anything.
382 printf("unknown command\n");
386 last_command_ = edge;
390 Edge* BuildTest::WaitForCommand(ExitStatus* status, string* /* output */) {
391 if (Edge* edge = last_command_) {
392 if (edge->rule().name() == "interrupt" ||
393 edge->rule().name() == "touch-interrupt") {
394 *status = ExitInterrupted;
398 if (edge->rule().name() == "fail")
399 *status = ExitFailure;
401 *status = ExitSuccess;
402 last_command_ = NULL;
405 *status = ExitFailure;
409 vector<Edge*> BuildTest::GetActiveEdges() {
412 edges.push_back(last_command_);
416 void BuildTest::Abort() {
417 last_command_ = NULL;
420 TEST_F(BuildTest, NoWork) {
422 EXPECT_TRUE(builder_.AlreadyUpToDate());
425 TEST_F(BuildTest, OneStep) {
426 // Given a dirty target with one ready input,
427 // we should rebuild the target.
430 EXPECT_TRUE(builder_.AddTarget("cat1", &err));
432 EXPECT_TRUE(builder_.Build(&err));
435 ASSERT_EQ(1u, commands_ran_.size());
436 EXPECT_EQ("cat in1 > cat1", commands_ran_[0]);
439 TEST_F(BuildTest, OneStep2) {
440 // Given a target with one dirty input,
441 // we should rebuild the target.
444 EXPECT_TRUE(builder_.AddTarget("cat1", &err));
446 EXPECT_TRUE(builder_.Build(&err));
449 ASSERT_EQ(1u, commands_ran_.size());
450 EXPECT_EQ("cat in1 > cat1", commands_ran_[0]);
453 TEST_F(BuildTest, TwoStep) {
455 EXPECT_TRUE(builder_.AddTarget("cat12", &err));
457 EXPECT_TRUE(builder_.Build(&err));
459 ASSERT_EQ(3u, commands_ran_.size());
460 // Depending on how the pointers work out, we could've ran
461 // the first two commands in either order.
462 EXPECT_TRUE((commands_ran_[0] == "cat in1 > cat1" &&
463 commands_ran_[1] == "cat in1 in2 > cat2") ||
464 (commands_ran_[1] == "cat in1 > cat1" &&
465 commands_ran_[0] == "cat in1 in2 > cat2"));
467 EXPECT_EQ("cat cat1 cat2 > cat12", commands_ran_[2]);
471 // Modifying in2 requires rebuilding one intermediate file
472 // and the final file.
473 fs_.Create("in2", now_, "");
475 EXPECT_TRUE(builder_.AddTarget("cat12", &err));
477 EXPECT_TRUE(builder_.Build(&err));
479 ASSERT_EQ(5u, commands_ran_.size());
480 EXPECT_EQ("cat in1 in2 > cat2", commands_ran_[3]);
481 EXPECT_EQ("cat cat1 cat2 > cat12", commands_ran_[4]);
484 TEST_F(BuildTest, TwoOutputs) {
485 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
487 " command = touch $out\n"
488 "build out1 out2: touch in.txt\n"));
490 fs_.Create("in.txt", now_, "");
493 EXPECT_TRUE(builder_.AddTarget("out1", &err));
495 EXPECT_TRUE(builder_.Build(&err));
497 ASSERT_EQ(1u, commands_ran_.size());
498 EXPECT_EQ("touch out1 out2", commands_ran_[0]);
502 // https://github.com/martine/ninja/issues/148
503 TEST_F(BuildTest, MultiOutIn) {
504 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
506 " command = touch $out\n"
507 "build in1 otherfile: touch in\n"
508 "build out: touch in | in1\n"));
510 fs_.Create("in", now_, "");
511 fs_.Create("in1", ++now_, "");
514 EXPECT_TRUE(builder_.AddTarget("out", &err));
516 EXPECT_TRUE(builder_.Build(&err));
520 TEST_F(BuildTest, Chain) {
521 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
525 "build c5: cat c4\n"));
527 fs_.Create("c1", now_, "");
530 EXPECT_TRUE(builder_.AddTarget("c5", &err));
532 EXPECT_TRUE(builder_.Build(&err));
534 ASSERT_EQ(4u, commands_ran_.size());
537 commands_ran_.clear();
539 EXPECT_TRUE(builder_.AddTarget("c5", &err));
541 EXPECT_TRUE(builder_.AlreadyUpToDate());
545 fs_.Create("c3", now_, "");
547 commands_ran_.clear();
549 EXPECT_TRUE(builder_.AddTarget("c5", &err));
551 EXPECT_FALSE(builder_.AlreadyUpToDate());
552 EXPECT_TRUE(builder_.Build(&err));
553 ASSERT_EQ(2u, commands_ran_.size()); // 3->4, 4->5
556 TEST_F(BuildTest, MissingInput) {
557 // Input is referenced by build file, but no rule for it.
560 EXPECT_FALSE(builder_.AddTarget("cat1", &err));
561 EXPECT_EQ("'in1', needed by 'cat1', missing and no known rule to make it",
565 TEST_F(BuildTest, MissingTarget) {
566 // Target is not referenced by build file.
568 EXPECT_FALSE(builder_.AddTarget("meow", &err));
569 EXPECT_EQ("unknown target: 'meow'", err);
572 TEST_F(BuildTest, MakeDirs) {
576 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, "build subdir\\dir2\\file: cat in1\n"));
577 EXPECT_TRUE(builder_.AddTarget("subdir\\dir2\\file", &err));
579 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, "build subdir/dir2/file: cat in1\n"));
580 EXPECT_TRUE(builder_.AddTarget("subdir/dir2/file", &err));
584 now_ = 0; // Make all stat()s return file not found.
585 EXPECT_TRUE(builder_.Build(&err));
587 ASSERT_EQ(2u, fs_.directories_made_.size());
588 EXPECT_EQ("subdir", fs_.directories_made_[0]);
590 EXPECT_EQ("subdir\\dir2", fs_.directories_made_[1]);
592 EXPECT_EQ("subdir/dir2", fs_.directories_made_[1]);
596 TEST_F(BuildTest, DepFileMissing) {
598 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
599 "rule cc\n command = cc $in\n depfile = $out.d\n"
600 "build foo.o: cc foo.c\n"));
601 fs_.Create("foo.c", now_, "");
603 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
605 ASSERT_EQ(1u, fs_.files_read_.size());
606 EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
609 TEST_F(BuildTest, DepFileOK) {
611 int orig_edges = state_.edges_.size();
612 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
613 "rule cc\n command = cc $in\n depfile = $out.d\n"
614 "build foo.o: cc foo.c\n"));
615 Edge* edge = state_.edges_.back();
617 fs_.Create("foo.c", now_, "");
618 GetNode("bar.h")->MarkDirty(); // Mark bar.h as missing.
619 fs_.Create("foo.o.d", now_, "foo.o: blah.h bar.h\n");
620 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
622 ASSERT_EQ(1u, fs_.files_read_.size());
623 EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
625 // Expect three new edges: one generating foo.o, and two more from
626 // loading the depfile.
627 ASSERT_EQ(orig_edges + 3, (int)state_.edges_.size());
628 // Expect our edge to now have three inputs: foo.c and two headers.
629 ASSERT_EQ(3u, edge->inputs_.size());
631 // Expect the command line we generate to only use the original input.
632 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
635 TEST_F(BuildTest, DepFileParseError) {
637 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
638 "rule cc\n command = cc $in\n depfile = $out.d\n"
639 "build foo.o: cc foo.c\n"));
640 fs_.Create("foo.c", now_, "");
641 fs_.Create("foo.o.d", now_, "randomtext\n");
642 EXPECT_FALSE(builder_.AddTarget("foo.o", &err));
643 EXPECT_EQ("expected depfile 'foo.o.d' to mention 'foo.o', got 'randomtext'",
647 TEST_F(BuildTest, OrderOnlyDeps) {
649 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
650 "rule cc\n command = cc $in\n depfile = $out.d\n"
651 "build foo.o: cc foo.c || otherfile\n"));
652 Edge* edge = state_.edges_.back();
654 fs_.Create("foo.c", now_, "");
655 fs_.Create("otherfile", now_, "");
656 fs_.Create("foo.o.d", now_, "foo.o: blah.h bar.h\n");
657 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
660 // One explicit, two implicit, one order only.
661 ASSERT_EQ(4u, edge->inputs_.size());
662 EXPECT_EQ(2, edge->implicit_deps_);
663 EXPECT_EQ(1, edge->order_only_deps_);
664 // Verify the inputs are in the order we expect
665 // (explicit then implicit then orderonly).
666 EXPECT_EQ("foo.c", edge->inputs_[0]->path());
667 EXPECT_EQ("blah.h", edge->inputs_[1]->path());
668 EXPECT_EQ("bar.h", edge->inputs_[2]->path());
669 EXPECT_EQ("otherfile", edge->inputs_[3]->path());
671 // Expect the command line we generate to only use the original input.
672 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
674 // explicit dep dirty, expect a rebuild.
675 EXPECT_TRUE(builder_.Build(&err));
677 ASSERT_EQ(1u, commands_ran_.size());
681 // implicit dep dirty, expect a rebuild.
682 fs_.Create("blah.h", now_, "");
683 fs_.Create("bar.h", now_, "");
684 commands_ran_.clear();
686 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
687 EXPECT_TRUE(builder_.Build(&err));
689 ASSERT_EQ(1u, commands_ran_.size());
693 // order only dep dirty, no rebuild.
694 fs_.Create("otherfile", now_, "");
695 commands_ran_.clear();
697 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
699 EXPECT_TRUE(builder_.AlreadyUpToDate());
701 // implicit dep missing, expect rebuild.
702 fs_.RemoveFile("bar.h");
703 commands_ran_.clear();
705 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
706 EXPECT_TRUE(builder_.Build(&err));
708 ASSERT_EQ(1u, commands_ran_.size());
711 TEST_F(BuildTest, RebuildOrderOnlyDeps) {
713 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
714 "rule cc\n command = cc $in\n"
715 "rule true\n command = true\n"
716 "build oo.h: cc oo.h.in\n"
717 "build foo.o: cc foo.c || oo.h\n"));
719 fs_.Create("foo.c", now_, "");
720 fs_.Create("oo.h.in", now_, "");
722 // foo.o and order-only dep dirty, build both.
723 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
724 EXPECT_TRUE(builder_.Build(&err));
726 ASSERT_EQ(2u, commands_ran_.size());
728 // all clean, no rebuild.
729 commands_ran_.clear();
731 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
733 EXPECT_TRUE(builder_.AlreadyUpToDate());
735 // order-only dep missing, build it only.
736 fs_.RemoveFile("oo.h");
737 commands_ran_.clear();
739 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
740 EXPECT_TRUE(builder_.Build(&err));
742 ASSERT_EQ(1u, commands_ran_.size());
743 ASSERT_EQ("cc oo.h.in", commands_ran_[0]);
747 // order-only dep dirty, build it only.
748 fs_.Create("oo.h.in", now_, "");
749 commands_ran_.clear();
751 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
752 EXPECT_TRUE(builder_.Build(&err));
754 ASSERT_EQ(1u, commands_ran_.size());
755 ASSERT_EQ("cc oo.h.in", commands_ran_[0]);
758 TEST_F(BuildTest, Phony) {
760 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
761 "build out: cat bar.cc\n"
762 "build all: phony out\n"));
763 fs_.Create("bar.cc", now_, "");
765 EXPECT_TRUE(builder_.AddTarget("all", &err));
768 // Only one command to run, because phony runs no command.
769 EXPECT_FALSE(builder_.AlreadyUpToDate());
770 EXPECT_TRUE(builder_.Build(&err));
772 ASSERT_EQ(1u, commands_ran_.size());
775 TEST_F(BuildTest, PhonyNoWork) {
777 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
778 "build out: cat bar.cc\n"
779 "build all: phony out\n"));
780 fs_.Create("bar.cc", now_, "");
781 fs_.Create("out", now_, "");
783 EXPECT_TRUE(builder_.AddTarget("all", &err));
785 EXPECT_TRUE(builder_.AlreadyUpToDate());
788 TEST_F(BuildTest, Fail) {
789 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
792 "build out1: fail\n"));
795 EXPECT_TRUE(builder_.AddTarget("out1", &err));
798 EXPECT_FALSE(builder_.Build(&err));
799 ASSERT_EQ(1u, commands_ran_.size());
800 ASSERT_EQ("subcommand failed", err);
803 TEST_F(BuildTest, SwallowFailures) {
804 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
810 "build all: phony out1 out2 out3\n"));
812 // Swallow two failures, die on the third.
813 config_.failures_allowed = 3;
816 EXPECT_TRUE(builder_.AddTarget("all", &err));
819 EXPECT_FALSE(builder_.Build(&err));
820 ASSERT_EQ(3u, commands_ran_.size());
821 ASSERT_EQ("subcommands failed", err);
824 TEST_F(BuildTest, SwallowFailuresLimit) {
825 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
831 "build final: cat out1 out2 out3\n"));
833 // Swallow ten failures; we should stop before building final.
834 config_.failures_allowed = 11;
837 EXPECT_TRUE(builder_.AddTarget("final", &err));
840 EXPECT_FALSE(builder_.Build(&err));
841 ASSERT_EQ(3u, commands_ran_.size());
842 ASSERT_EQ("cannot make progress due to previous errors", err);
845 struct BuildWithLogTest : public BuildTest {
847 builder_.SetBuildLog(&build_log_);
853 TEST_F(BuildWithLogTest, NotInLogButOnDisk) {
854 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
857 "build out1: cc in\n"));
859 // Create input/output that would be considered up to date when
860 // not considering the command line hash.
861 fs_.Create("in", now_, "");
862 fs_.Create("out1", now_, "");
865 // Because it's not in the log, it should not be up-to-date until
867 EXPECT_TRUE(builder_.AddTarget("out1", &err));
868 EXPECT_FALSE(builder_.AlreadyUpToDate());
870 commands_ran_.clear();
873 EXPECT_TRUE(builder_.AddTarget("out1", &err));
874 EXPECT_TRUE(builder_.Build(&err));
875 EXPECT_TRUE(builder_.AlreadyUpToDate());
878 TEST_F(BuildWithLogTest, RestatTest) {
879 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
886 "build out1: cc in\n"
887 "build out2: true out1\n"
888 "build out3: cat out2\n"));
890 fs_.Create("out1", now_, "");
891 fs_.Create("out2", now_, "");
892 fs_.Create("out3", now_, "");
896 fs_.Create("in", now_, "");
898 // Do a pre-build so that there's commands in the log for the outputs,
899 // otherwise, the lack of an entry in the build log will cause out3 to rebuild
900 // regardless of restat.
902 EXPECT_TRUE(builder_.AddTarget("out3", &err));
904 EXPECT_TRUE(builder_.Build(&err));
906 commands_ran_.clear();
911 fs_.Create("in", now_, "");
912 // "cc" touches out1, so we should build out2. But because "true" does not
913 // touch out2, we should cancel the build of out3.
914 EXPECT_TRUE(builder_.AddTarget("out3", &err));
916 EXPECT_TRUE(builder_.Build(&err));
917 ASSERT_EQ(2u, commands_ran_.size());
919 // If we run again, it should be a no-op, because the build log has recorded
920 // that we've already built out2 with an input timestamp of 2 (from out1).
921 commands_ran_.clear();
923 EXPECT_TRUE(builder_.AddTarget("out3", &err));
925 EXPECT_TRUE(builder_.AlreadyUpToDate());
929 fs_.Create("in", now_, "");
931 // The build log entry should not, however, prevent us from rebuilding out2
933 commands_ran_.clear();
935 EXPECT_TRUE(builder_.AddTarget("out3", &err));
937 EXPECT_TRUE(builder_.Build(&err));
938 ASSERT_EQ(2u, commands_ran_.size());
941 TEST_F(BuildWithLogTest, RestatMissingFile) {
942 // If a restat rule doesn't create its output, and the output didn't
943 // exist before the rule was run, consider that behavior equivalent
944 // to a rule that doesn't modify its existent output file.
946 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
952 "build out1: true in\n"
953 "build out2: cc out1\n"));
955 fs_.Create("in", now_, "");
956 fs_.Create("out2", now_, "");
958 // Do a pre-build so that there's commands in the log for the outputs,
959 // otherwise, the lack of an entry in the build log will cause out2 to rebuild
960 // regardless of restat.
962 EXPECT_TRUE(builder_.AddTarget("out2", &err));
964 EXPECT_TRUE(builder_.Build(&err));
966 commands_ran_.clear();
970 fs_.Create("in", now_, "");
971 fs_.Create("out2", now_, "");
973 // Run a build, expect only the first command to run.
974 // It doesn't touch its output (due to being the "true" command), so
975 // we shouldn't run the dependent build.
976 EXPECT_TRUE(builder_.AddTarget("out2", &err));
978 EXPECT_TRUE(builder_.Build(&err));
979 ASSERT_EQ(1u, commands_ran_.size());
982 // Test scenario, in which an input file is removed, but output isn't changed
983 // https://github.com/martine/ninja/issues/295
984 TEST_F(BuildWithLogTest, RestatMissingInput) {
985 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
988 " depfile = $out.d\n"
992 "build out1: true in\n"
993 "build out2: cc out1\n"));
995 // Create all necessary files
996 fs_.Create("in", now_, "");
998 // The implicit dependencies and the depfile itself
999 // are newer than the output
1000 TimeStamp restat_mtime = ++now_;
1001 fs_.Create("out1.d", now_, "out1: will.be.deleted restat.file\n");
1002 fs_.Create("will.be.deleted", now_, "");
1003 fs_.Create("restat.file", now_, "");
1005 // Run the build, out1 and out2 get built
1007 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1009 EXPECT_TRUE(builder_.Build(&err));
1010 ASSERT_EQ(2u, commands_ran_.size());
1012 // See that an entry in the logfile is created, capturing
1014 BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out1");
1015 ASSERT_TRUE(NULL != log_entry);
1016 ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
1018 // Now remove a file, referenced from depfile, so that target becomes
1019 // dirty, but the output does not change
1020 fs_.RemoveFile("will.be.deleted");
1022 // Trigger the build again - only out1 gets built
1023 commands_ran_.clear();
1025 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1027 EXPECT_TRUE(builder_.Build(&err));
1028 ASSERT_EQ(1u, commands_ran_.size());
1030 // Check that the logfile entry remains correctly set
1031 log_entry = build_log_.LookupByOutput("out1");
1032 ASSERT_TRUE(NULL != log_entry);
1033 ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
1036 struct BuildDryRun : public BuildWithLogTest {
1038 config_.dry_run = true;
1042 TEST_F(BuildDryRun, AllCommandsShown) {
1043 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1050 "build out1: cc in\n"
1051 "build out2: true out1\n"
1052 "build out3: cat out2\n"));
1054 fs_.Create("out1", now_, "");
1055 fs_.Create("out2", now_, "");
1056 fs_.Create("out3", now_, "");
1060 fs_.Create("in", now_, "");
1062 // "cc" touches out1, so we should build out2. But because "true" does not
1063 // touch out2, we should cancel the build of out3.
1065 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1067 EXPECT_TRUE(builder_.Build(&err));
1068 ASSERT_EQ(3u, commands_ran_.size());
1071 // Test that RSP files are created when & where appropriate and deleted after
1072 // successful execution.
1073 TEST_F(BuildTest, RspFileSuccess)
1075 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1077 " command = cat $rspfile > $out\n"
1078 " rspfile = $rspfile\n"
1079 " rspfile_content = $long_command\n"
1080 "build out1: cat in\n"
1081 "build out2: cat_rsp in\n"
1082 " rspfile = out2.rsp\n"
1083 " long_command = Some very long command\n"));
1085 fs_.Create("out1", now_, "");
1086 fs_.Create("out2", now_, "");
1087 fs_.Create("out3", now_, "");
1091 fs_.Create("in", now_, "");
1094 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1096 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1099 size_t files_created = fs_.files_created_.size();
1100 size_t files_removed = fs_.files_removed_.size();
1102 EXPECT_TRUE(builder_.Build(&err));
1103 ASSERT_EQ(2u, commands_ran_.size()); // cat + cat_rsp
1105 // The RSP file was created
1106 ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1107 ASSERT_EQ(1u, fs_.files_created_.count("out2.rsp"));
1109 // The RSP file was removed
1110 ASSERT_EQ(files_removed + 1, fs_.files_removed_.size());
1111 ASSERT_EQ(1u, fs_.files_removed_.count("out2.rsp"));
1114 // Test that RSP file is created but not removed for commands, which fail
1115 TEST_F(BuildTest, RspFileFailure) {
1116 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1119 " rspfile = $rspfile\n"
1120 " rspfile_content = $long_command\n"
1121 "build out: fail in\n"
1122 " rspfile = out.rsp\n"
1123 " long_command = Another very long command\n"));
1125 fs_.Create("out", now_, "");
1127 fs_.Create("in", now_, "");
1130 EXPECT_TRUE(builder_.AddTarget("out", &err));
1133 size_t files_created = fs_.files_created_.size();
1134 size_t files_removed = fs_.files_removed_.size();
1136 EXPECT_FALSE(builder_.Build(&err));
1137 ASSERT_EQ("subcommand failed", err);
1138 ASSERT_EQ(1u, commands_ran_.size());
1140 // The RSP file was created
1141 ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1142 ASSERT_EQ(1u, fs_.files_created_.count("out.rsp"));
1144 // The RSP file was NOT removed
1145 ASSERT_EQ(files_removed, fs_.files_removed_.size());
1146 ASSERT_EQ(0u, fs_.files_removed_.count("out.rsp"));
1148 // The RSP file contains what it should
1149 ASSERT_EQ("Another very long command", fs_.files_["out.rsp"].contents);
1152 // Test that contens of the RSP file behaves like a regular part of
1153 // command line, i.e. triggers a rebuild if changed
1154 TEST_F(BuildWithLogTest, RspFileCmdLineChange) {
1155 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1157 " command = cat $rspfile > $out\n"
1158 " rspfile = $rspfile\n"
1159 " rspfile_content = $long_command\n"
1160 "build out: cat_rsp in\n"
1161 " rspfile = out.rsp\n"
1162 " long_command = Original very long command\n"));
1164 fs_.Create("out", now_, "");
1166 fs_.Create("in", now_, "");
1169 EXPECT_TRUE(builder_.AddTarget("out", &err));
1172 // 1. Build for the 1st time (-> populate log)
1173 EXPECT_TRUE(builder_.Build(&err));
1174 ASSERT_EQ(1u, commands_ran_.size());
1176 // 2. Build again (no change)
1177 commands_ran_.clear();
1179 EXPECT_TRUE(builder_.AddTarget("out", &err));
1181 ASSERT_TRUE(builder_.AlreadyUpToDate());
1183 // 3. Alter the entry in the logfile
1184 // (to simulate a change in the command line between 2 builds)
1185 BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out");
1186 ASSERT_TRUE(NULL != log_entry);
1187 ASSERT_NO_FATAL_FAILURE(AssertHash(
1188 "cat out.rsp > out;rspfile=Original very long command",
1189 log_entry->command_hash));
1190 log_entry->command_hash++; // Change the command hash to something else.
1191 // Now expect the target to be rebuilt
1192 commands_ran_.clear();
1194 EXPECT_TRUE(builder_.AddTarget("out", &err));
1196 EXPECT_TRUE(builder_.Build(&err));
1197 EXPECT_EQ(1u, commands_ran_.size());
1200 TEST_F(BuildTest, InterruptCleanup) {
1201 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1203 " command = interrupt\n"
1204 "rule touch-interrupt\n"
1205 " command = touch-interrupt\n"
1206 "build out1: interrupt in1\n"
1207 "build out2: touch-interrupt in2\n"));
1209 fs_.Create("out1", now_, "");
1210 fs_.Create("out2", now_, "");
1212 fs_.Create("in1", now_, "");
1213 fs_.Create("in2", now_, "");
1215 // An untouched output of an interrupted command should be retained.
1217 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1219 EXPECT_FALSE(builder_.Build(&err));
1220 EXPECT_EQ("interrupted by user", err);
1222 EXPECT_EQ(now_-1, fs_.Stat("out1"));
1225 // A touched output of an interrupted command should be deleted.
1226 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1228 EXPECT_FALSE(builder_.Build(&err));
1229 EXPECT_EQ("interrupted by user", err);
1231 EXPECT_EQ(0, fs_.Stat("out2"));
1234 TEST_F(BuildTest, PhonyWithNoInputs) {
1235 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1236 "build nonexistent: phony\n"
1237 "build out1: cat || nonexistent\n"
1238 "build out2: cat nonexistent\n"));
1239 fs_.Create("out1", now_, "");
1240 fs_.Create("out2", now_, "");
1242 // out1 should be up to date even though its input is dirty, because its
1243 // order-only dependency has nothing to do.
1245 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1247 EXPECT_TRUE(builder_.AlreadyUpToDate());
1249 // out2 should still be out of date though, because its input is dirty.
1251 commands_ran_.clear();
1253 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1255 EXPECT_TRUE(builder_.Build(&err));
1257 ASSERT_EQ(1u, commands_ran_.size());
1260 TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
1261 EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
1262 status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]"));