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 struct BuildTest : public StateTestWithBuiltinRules,
180 public CommandRunner {
181 BuildTest() : config_(MakeConfig()), builder_(&state_, config_, &fs_),
182 now_(1), last_command_(NULL), status_(config_) {
183 builder_.command_runner_.reset(this);
185 "build cat1: cat in1\n"
186 "build cat2: cat in1 in2\n"
187 "build cat12: cat cat1 cat2\n");
189 fs_.Create("in1", now_, "");
190 fs_.Create("in2", now_, "");
194 builder_.command_runner_.release();
197 // Mark a path dirty.
198 void Dirty(const string& path);
200 // CommandRunner impl
201 virtual bool CanRunMore();
202 virtual bool StartCommand(Edge* edge);
203 virtual Edge* WaitForCommand(ExitStatus* status, string* output);
204 virtual vector<Edge*> GetActiveEdges();
205 virtual void Abort();
207 BuildConfig MakeConfig() {
209 config.verbosity = BuildConfig::QUIET;
214 VirtualFileSystem fs_;
218 vector<string> commands_ran_;
223 void BuildTest::Dirty(const string& path) {
224 Node* node = GetNode(path);
227 // If it's an input file, mark that we've already stat()ed it and
229 if (!node->in_edge())
233 bool BuildTest::CanRunMore() {
234 // Only run one at a time.
235 return last_command_ == NULL;
238 bool BuildTest::StartCommand(Edge* edge) {
239 assert(!last_command_);
240 commands_ran_.push_back(edge->EvaluateCommand());
241 if (edge->rule().name() == "cat" ||
242 edge->rule().name() == "cat_rsp" ||
243 edge->rule().name() == "cc" ||
244 edge->rule().name() == "touch" ||
245 edge->rule().name() == "touch-interrupt") {
246 for (vector<Node*>::iterator out = edge->outputs_.begin();
247 out != edge->outputs_.end(); ++out) {
248 fs_.Create((*out)->path(), now_, "");
250 } else if (edge->rule().name() == "true" ||
251 edge->rule().name() == "fail" ||
252 edge->rule().name() == "interrupt") {
253 // Don't do anything.
255 printf("unknown command\n");
259 last_command_ = edge;
263 Edge* BuildTest::WaitForCommand(ExitStatus* status, string* /* output */) {
264 if (Edge* edge = last_command_) {
265 if (edge->rule().name() == "interrupt" ||
266 edge->rule().name() == "touch-interrupt") {
267 *status = ExitInterrupted;
271 if (edge->rule().name() == "fail")
272 *status = ExitFailure;
274 *status = ExitSuccess;
275 last_command_ = NULL;
278 *status = ExitFailure;
282 vector<Edge*> BuildTest::GetActiveEdges() {
285 edges.push_back(last_command_);
289 void BuildTest::Abort() {
290 last_command_ = NULL;
293 TEST_F(BuildTest, NoWork) {
295 EXPECT_TRUE(builder_.AlreadyUpToDate());
298 TEST_F(BuildTest, OneStep) {
299 // Given a dirty target with one ready input,
300 // we should rebuild the target.
303 EXPECT_TRUE(builder_.AddTarget("cat1", &err));
305 EXPECT_TRUE(builder_.Build(&err));
308 ASSERT_EQ(1u, commands_ran_.size());
309 EXPECT_EQ("cat in1 > cat1", commands_ran_[0]);
312 TEST_F(BuildTest, OneStep2) {
313 // Given a target with one dirty input,
314 // we should rebuild the target.
317 EXPECT_TRUE(builder_.AddTarget("cat1", &err));
319 EXPECT_TRUE(builder_.Build(&err));
322 ASSERT_EQ(1u, commands_ran_.size());
323 EXPECT_EQ("cat in1 > cat1", commands_ran_[0]);
326 TEST_F(BuildTest, TwoStep) {
328 EXPECT_TRUE(builder_.AddTarget("cat12", &err));
330 EXPECT_TRUE(builder_.Build(&err));
332 ASSERT_EQ(3u, commands_ran_.size());
333 // Depending on how the pointers work out, we could've ran
334 // the first two commands in either order.
335 EXPECT_TRUE((commands_ran_[0] == "cat in1 > cat1" &&
336 commands_ran_[1] == "cat in1 in2 > cat2") ||
337 (commands_ran_[1] == "cat in1 > cat1" &&
338 commands_ran_[0] == "cat in1 in2 > cat2"));
340 EXPECT_EQ("cat cat1 cat2 > cat12", commands_ran_[2]);
344 // Modifying in2 requires rebuilding one intermediate file
345 // and the final file.
346 fs_.Create("in2", now_, "");
348 EXPECT_TRUE(builder_.AddTarget("cat12", &err));
350 EXPECT_TRUE(builder_.Build(&err));
352 ASSERT_EQ(5u, commands_ran_.size());
353 EXPECT_EQ("cat in1 in2 > cat2", commands_ran_[3]);
354 EXPECT_EQ("cat cat1 cat2 > cat12", commands_ran_[4]);
357 TEST_F(BuildTest, TwoOutputs) {
358 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
360 " command = touch $out\n"
361 "build out1 out2: touch in.txt\n"));
363 fs_.Create("in.txt", now_, "");
366 EXPECT_TRUE(builder_.AddTarget("out1", &err));
368 EXPECT_TRUE(builder_.Build(&err));
370 ASSERT_EQ(1u, commands_ran_.size());
371 EXPECT_EQ("touch out1 out2", commands_ran_[0]);
375 // https://github.com/martine/ninja/issues/148
376 TEST_F(BuildTest, MultiOutIn) {
377 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
379 " command = touch $out\n"
380 "build in1 otherfile: touch in\n"
381 "build out: touch in | in1\n"));
383 fs_.Create("in", now_, "");
384 fs_.Create("in1", ++now_, "");
387 EXPECT_TRUE(builder_.AddTarget("out", &err));
389 EXPECT_TRUE(builder_.Build(&err));
393 TEST_F(BuildTest, Chain) {
394 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
398 "build c5: cat c4\n"));
400 fs_.Create("c1", now_, "");
403 EXPECT_TRUE(builder_.AddTarget("c5", &err));
405 EXPECT_TRUE(builder_.Build(&err));
407 ASSERT_EQ(4u, commands_ran_.size());
410 commands_ran_.clear();
412 EXPECT_TRUE(builder_.AddTarget("c5", &err));
414 EXPECT_TRUE(builder_.AlreadyUpToDate());
418 fs_.Create("c3", now_, "");
420 commands_ran_.clear();
422 EXPECT_TRUE(builder_.AddTarget("c5", &err));
424 EXPECT_FALSE(builder_.AlreadyUpToDate());
425 EXPECT_TRUE(builder_.Build(&err));
426 ASSERT_EQ(2u, commands_ran_.size()); // 3->4, 4->5
429 TEST_F(BuildTest, MissingInput) {
430 // Input is referenced by build file, but no rule for it.
433 EXPECT_FALSE(builder_.AddTarget("cat1", &err));
434 EXPECT_EQ("'in1', needed by 'cat1', missing and no known rule to make it",
438 TEST_F(BuildTest, MissingTarget) {
439 // Target is not referenced by build file.
441 EXPECT_FALSE(builder_.AddTarget("meow", &err));
442 EXPECT_EQ("unknown target: 'meow'", err);
445 TEST_F(BuildTest, MakeDirs) {
449 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, "build subdir\\dir2\\file: cat in1\n"));
450 EXPECT_TRUE(builder_.AddTarget("subdir\\dir2\\file", &err));
452 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, "build subdir/dir2/file: cat in1\n"));
453 EXPECT_TRUE(builder_.AddTarget("subdir/dir2/file", &err));
457 now_ = 0; // Make all stat()s return file not found.
458 EXPECT_TRUE(builder_.Build(&err));
460 ASSERT_EQ(2u, fs_.directories_made_.size());
461 EXPECT_EQ("subdir", fs_.directories_made_[0]);
463 EXPECT_EQ("subdir\\dir2", fs_.directories_made_[1]);
465 EXPECT_EQ("subdir/dir2", fs_.directories_made_[1]);
469 TEST_F(BuildTest, DepFileMissing) {
471 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
472 "rule cc\n command = cc $in\n depfile = $out.d\n"
473 "build foo.o: cc foo.c\n"));
474 fs_.Create("foo.c", now_, "");
476 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
478 ASSERT_EQ(1u, fs_.files_read_.size());
479 EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
482 TEST_F(BuildTest, DepFileOK) {
484 int orig_edges = state_.edges_.size();
485 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
486 "rule cc\n command = cc $in\n depfile = $out.d\n"
487 "build foo.o: cc foo.c\n"));
488 Edge* edge = state_.edges_.back();
490 fs_.Create("foo.c", now_, "");
491 GetNode("bar.h")->MarkDirty(); // Mark bar.h as missing.
492 fs_.Create("foo.o.d", now_, "foo.o: blah.h bar.h\n");
493 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
495 ASSERT_EQ(1u, fs_.files_read_.size());
496 EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
498 // Expect three new edges: one generating foo.o, and two more from
499 // loading the depfile.
500 ASSERT_EQ(orig_edges + 3, (int)state_.edges_.size());
501 // Expect our edge to now have three inputs: foo.c and two headers.
502 ASSERT_EQ(3u, edge->inputs_.size());
504 // Expect the command line we generate to only use the original input.
505 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
508 TEST_F(BuildTest, DepFileParseError) {
510 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
511 "rule cc\n command = cc $in\n depfile = $out.d\n"
512 "build foo.o: cc foo.c\n"));
513 fs_.Create("foo.c", now_, "");
514 fs_.Create("foo.o.d", now_, "randomtext\n");
515 EXPECT_FALSE(builder_.AddTarget("foo.o", &err));
516 EXPECT_EQ("expected depfile 'foo.o.d' to mention 'foo.o', got 'randomtext'",
520 TEST_F(BuildTest, OrderOnlyDeps) {
522 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
523 "rule cc\n command = cc $in\n depfile = $out.d\n"
524 "build foo.o: cc foo.c || otherfile\n"));
525 Edge* edge = state_.edges_.back();
527 fs_.Create("foo.c", now_, "");
528 fs_.Create("otherfile", now_, "");
529 fs_.Create("foo.o.d", now_, "foo.o: blah.h bar.h\n");
530 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
533 // One explicit, two implicit, one order only.
534 ASSERT_EQ(4u, edge->inputs_.size());
535 EXPECT_EQ(2, edge->implicit_deps_);
536 EXPECT_EQ(1, edge->order_only_deps_);
537 // Verify the inputs are in the order we expect
538 // (explicit then implicit then orderonly).
539 EXPECT_EQ("foo.c", edge->inputs_[0]->path());
540 EXPECT_EQ("blah.h", edge->inputs_[1]->path());
541 EXPECT_EQ("bar.h", edge->inputs_[2]->path());
542 EXPECT_EQ("otherfile", edge->inputs_[3]->path());
544 // Expect the command line we generate to only use the original input.
545 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
547 // explicit dep dirty, expect a rebuild.
548 EXPECT_TRUE(builder_.Build(&err));
550 ASSERT_EQ(1u, commands_ran_.size());
554 // implicit dep dirty, expect a rebuild.
555 fs_.Create("blah.h", now_, "");
556 fs_.Create("bar.h", now_, "");
557 commands_ran_.clear();
559 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
560 EXPECT_TRUE(builder_.Build(&err));
562 ASSERT_EQ(1u, commands_ran_.size());
566 // order only dep dirty, no rebuild.
567 fs_.Create("otherfile", now_, "");
568 commands_ran_.clear();
570 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
572 EXPECT_TRUE(builder_.AlreadyUpToDate());
574 // implicit dep missing, expect rebuild.
575 fs_.RemoveFile("bar.h");
576 commands_ran_.clear();
578 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
579 EXPECT_TRUE(builder_.Build(&err));
581 ASSERT_EQ(1u, commands_ran_.size());
584 TEST_F(BuildTest, RebuildOrderOnlyDeps) {
586 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
587 "rule cc\n command = cc $in\n"
588 "rule true\n command = true\n"
589 "build oo.h: cc oo.h.in\n"
590 "build foo.o: cc foo.c || oo.h\n"));
592 fs_.Create("foo.c", now_, "");
593 fs_.Create("oo.h.in", now_, "");
595 // foo.o and order-only dep dirty, build both.
596 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
597 EXPECT_TRUE(builder_.Build(&err));
599 ASSERT_EQ(2u, commands_ran_.size());
601 // all clean, no rebuild.
602 commands_ran_.clear();
604 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
606 EXPECT_TRUE(builder_.AlreadyUpToDate());
608 // order-only dep missing, build it only.
609 fs_.RemoveFile("oo.h");
610 commands_ran_.clear();
612 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
613 EXPECT_TRUE(builder_.Build(&err));
615 ASSERT_EQ(1u, commands_ran_.size());
616 ASSERT_EQ("cc oo.h.in", commands_ran_[0]);
620 // order-only dep dirty, build it only.
621 fs_.Create("oo.h.in", now_, "");
622 commands_ran_.clear();
624 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
625 EXPECT_TRUE(builder_.Build(&err));
627 ASSERT_EQ(1u, commands_ran_.size());
628 ASSERT_EQ("cc oo.h.in", commands_ran_[0]);
631 TEST_F(BuildTest, Phony) {
633 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
634 "build out: cat bar.cc\n"
635 "build all: phony out\n"));
636 fs_.Create("bar.cc", now_, "");
638 EXPECT_TRUE(builder_.AddTarget("all", &err));
641 // Only one command to run, because phony runs no command.
642 EXPECT_FALSE(builder_.AlreadyUpToDate());
643 EXPECT_TRUE(builder_.Build(&err));
645 ASSERT_EQ(1u, commands_ran_.size());
648 TEST_F(BuildTest, PhonyNoWork) {
650 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
651 "build out: cat bar.cc\n"
652 "build all: phony out\n"));
653 fs_.Create("bar.cc", now_, "");
654 fs_.Create("out", now_, "");
656 EXPECT_TRUE(builder_.AddTarget("all", &err));
658 EXPECT_TRUE(builder_.AlreadyUpToDate());
661 TEST_F(BuildTest, Fail) {
662 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
665 "build out1: fail\n"));
668 EXPECT_TRUE(builder_.AddTarget("out1", &err));
671 EXPECT_FALSE(builder_.Build(&err));
672 ASSERT_EQ(1u, commands_ran_.size());
673 ASSERT_EQ("subcommand failed", err);
676 TEST_F(BuildTest, SwallowFailures) {
677 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
683 "build all: phony out1 out2 out3\n"));
685 // Swallow two failures, die on the third.
686 config_.failures_allowed = 3;
689 EXPECT_TRUE(builder_.AddTarget("all", &err));
692 EXPECT_FALSE(builder_.Build(&err));
693 ASSERT_EQ(3u, commands_ran_.size());
694 ASSERT_EQ("subcommands failed", err);
697 TEST_F(BuildTest, SwallowFailuresLimit) {
698 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
704 "build final: cat out1 out2 out3\n"));
706 // Swallow ten failures; we should stop before building final.
707 config_.failures_allowed = 11;
710 EXPECT_TRUE(builder_.AddTarget("final", &err));
713 EXPECT_FALSE(builder_.Build(&err));
714 ASSERT_EQ(3u, commands_ran_.size());
715 ASSERT_EQ("cannot make progress due to previous errors", err);
718 struct BuildWithLogTest : public BuildTest {
720 state_.build_log_ = builder_.log_ = &build_log_;
726 TEST_F(BuildWithLogTest, NotInLogButOnDisk) {
727 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
730 "build out1: cc in\n"));
732 // Create input/output that would be considered up to date when
733 // not considering the command line hash.
734 fs_.Create("in", now_, "");
735 fs_.Create("out1", now_, "");
738 // Because it's not in the log, it should not be up-to-date until
740 EXPECT_TRUE(builder_.AddTarget("out1", &err));
741 EXPECT_FALSE(builder_.AlreadyUpToDate());
743 commands_ran_.clear();
746 EXPECT_TRUE(builder_.AddTarget("out1", &err));
747 EXPECT_TRUE(builder_.Build(&err));
748 EXPECT_TRUE(builder_.AlreadyUpToDate());
751 TEST_F(BuildWithLogTest, RestatTest) {
752 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
759 "build out1: cc in\n"
760 "build out2: true out1\n"
761 "build out3: cat out2\n"));
763 fs_.Create("out1", now_, "");
764 fs_.Create("out2", now_, "");
765 fs_.Create("out3", now_, "");
769 fs_.Create("in", now_, "");
771 // Do a pre-build so that there's commands in the log for the outputs,
772 // otherwise, the lack of an entry in the build log will cause out3 to rebuild
773 // regardless of restat.
775 EXPECT_TRUE(builder_.AddTarget("out3", &err));
777 EXPECT_TRUE(builder_.Build(&err));
779 commands_ran_.clear();
784 fs_.Create("in", now_, "");
785 // "cc" touches out1, so we should build out2. But because "true" does not
786 // touch out2, we should cancel the build of out3.
787 EXPECT_TRUE(builder_.AddTarget("out3", &err));
789 EXPECT_TRUE(builder_.Build(&err));
790 ASSERT_EQ(2u, commands_ran_.size());
792 // If we run again, it should be a no-op, because the build log has recorded
793 // that we've already built out2 with an input timestamp of 2 (from out1).
794 commands_ran_.clear();
796 EXPECT_TRUE(builder_.AddTarget("out3", &err));
798 EXPECT_TRUE(builder_.AlreadyUpToDate());
802 fs_.Create("in", now_, "");
804 // The build log entry should not, however, prevent us from rebuilding out2
806 commands_ran_.clear();
808 EXPECT_TRUE(builder_.AddTarget("out3", &err));
810 EXPECT_TRUE(builder_.Build(&err));
811 ASSERT_EQ(2u, commands_ran_.size());
814 TEST_F(BuildWithLogTest, RestatMissingFile) {
815 // If a restat rule doesn't create its output, and the output didn't
816 // exist before the rule was run, consider that behavior equivalent
817 // to a rule that doesn't modify its existent output file.
819 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
825 "build out1: true in\n"
826 "build out2: cc out1\n"));
828 fs_.Create("in", now_, "");
829 fs_.Create("out2", now_, "");
831 // Do a pre-build so that there's commands in the log for the outputs,
832 // otherwise, the lack of an entry in the build log will cause out2 to rebuild
833 // regardless of restat.
835 EXPECT_TRUE(builder_.AddTarget("out2", &err));
837 EXPECT_TRUE(builder_.Build(&err));
839 commands_ran_.clear();
843 fs_.Create("in", now_, "");
844 fs_.Create("out2", now_, "");
846 // Run a build, expect only the first command to run.
847 // It doesn't touch its output (due to being the "true" command), so
848 // we shouldn't run the dependent build.
849 EXPECT_TRUE(builder_.AddTarget("out2", &err));
851 EXPECT_TRUE(builder_.Build(&err));
852 ASSERT_EQ(1u, commands_ran_.size());
855 // Test scenario, in which an input file is removed, but output isn't changed
856 // https://github.com/martine/ninja/issues/295
857 TEST_F(BuildWithLogTest, RestatMissingInput) {
858 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
861 " depfile = $out.d\n"
865 "build out1: true in\n"
866 "build out2: cc out1\n"));
868 // Create all necessary files
869 fs_.Create("in", now_, "");
871 // The implicit dependencies and the depfile itself
872 // are newer than the output
873 TimeStamp restat_mtime = ++now_;
874 fs_.Create("out1.d", now_, "out1: will.be.deleted restat.file\n");
875 fs_.Create("will.be.deleted", now_, "");
876 fs_.Create("restat.file", now_, "");
878 // Run the build, out1 and out2 get built
880 EXPECT_TRUE(builder_.AddTarget("out2", &err));
882 EXPECT_TRUE(builder_.Build(&err));
883 ASSERT_EQ(2u, commands_ran_.size());
885 // See that an entry in the logfile is created, capturing
887 BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out1");
888 ASSERT_TRUE(NULL != log_entry);
889 ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
891 // Now remove a file, referenced from depfile, so that target becomes
892 // dirty, but the output does not change
893 fs_.RemoveFile("will.be.deleted");
895 // Trigger the build again - only out1 gets built
896 commands_ran_.clear();
898 EXPECT_TRUE(builder_.AddTarget("out2", &err));
900 EXPECT_TRUE(builder_.Build(&err));
901 ASSERT_EQ(1u, commands_ran_.size());
903 // Check that the logfile entry remains correctly set
904 log_entry = build_log_.LookupByOutput("out1");
905 ASSERT_TRUE(NULL != log_entry);
906 ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
909 struct BuildDryRun : public BuildWithLogTest {
911 config_.dry_run = true;
915 TEST_F(BuildDryRun, AllCommandsShown) {
916 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
923 "build out1: cc in\n"
924 "build out2: true out1\n"
925 "build out3: cat out2\n"));
927 fs_.Create("out1", now_, "");
928 fs_.Create("out2", now_, "");
929 fs_.Create("out3", now_, "");
933 fs_.Create("in", now_, "");
935 // "cc" touches out1, so we should build out2. But because "true" does not
936 // touch out2, we should cancel the build of out3.
938 EXPECT_TRUE(builder_.AddTarget("out3", &err));
940 EXPECT_TRUE(builder_.Build(&err));
941 ASSERT_EQ(3u, commands_ran_.size());
944 // Test that RSP files are created when & where appropriate and deleted after
945 // succesful execution.
946 TEST_F(BuildTest, RspFileSuccess)
948 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
950 " command = cat $rspfile > $out\n"
951 " rspfile = $rspfile\n"
952 " rspfile_content = $long_command\n"
953 "build out1: cat in\n"
954 "build out2: cat_rsp in\n"
955 " rspfile = out2.rsp\n"
956 " long_command = Some very long command\n"));
958 fs_.Create("out1", now_, "");
959 fs_.Create("out2", now_, "");
960 fs_.Create("out3", now_, "");
964 fs_.Create("in", now_, "");
967 EXPECT_TRUE(builder_.AddTarget("out1", &err));
969 EXPECT_TRUE(builder_.AddTarget("out2", &err));
972 size_t files_created = fs_.files_created_.size();
973 size_t files_removed = fs_.files_removed_.size();
975 EXPECT_TRUE(builder_.Build(&err));
976 ASSERT_EQ(2u, commands_ran_.size()); // cat + cat_rsp
978 // The RSP file was created
979 ASSERT_EQ(files_created + 1, fs_.files_created_.size());
980 ASSERT_EQ(1u, fs_.files_created_.count("out2.rsp"));
982 // The RSP file was removed
983 ASSERT_EQ(files_removed + 1, fs_.files_removed_.size());
984 ASSERT_EQ(1u, fs_.files_removed_.count("out2.rsp"));
987 // Test that RSP file is created but not removed for commands, which fail
988 TEST_F(BuildTest, RspFileFailure) {
989 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
992 " rspfile = $rspfile\n"
993 " rspfile_content = $long_command\n"
994 "build out: fail in\n"
995 " rspfile = out.rsp\n"
996 " long_command = Another very long command\n"));
998 fs_.Create("out", now_, "");
1000 fs_.Create("in", now_, "");
1003 EXPECT_TRUE(builder_.AddTarget("out", &err));
1006 size_t files_created = fs_.files_created_.size();
1007 size_t files_removed = fs_.files_removed_.size();
1009 EXPECT_FALSE(builder_.Build(&err));
1010 ASSERT_EQ("subcommand failed", err);
1011 ASSERT_EQ(1u, commands_ran_.size());
1013 // The RSP file was created
1014 ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1015 ASSERT_EQ(1u, fs_.files_created_.count("out.rsp"));
1017 // The RSP file was NOT removed
1018 ASSERT_EQ(files_removed, fs_.files_removed_.size());
1019 ASSERT_EQ(0u, fs_.files_removed_.count("out.rsp"));
1021 // The RSP file contains what it should
1022 ASSERT_EQ("Another very long command", fs_.files_["out.rsp"].contents);
1025 // Test that contens of the RSP file behaves like a regular part of
1026 // command line, i.e. triggers a rebuild if changed
1027 TEST_F(BuildWithLogTest, RspFileCmdLineChange) {
1028 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1030 " command = cat $rspfile > $out\n"
1031 " rspfile = $rspfile\n"
1032 " rspfile_content = $long_command\n"
1033 "build out: cat_rsp in\n"
1034 " rspfile = out.rsp\n"
1035 " long_command = Original very long command\n"));
1037 fs_.Create("out", now_, "");
1039 fs_.Create("in", now_, "");
1042 EXPECT_TRUE(builder_.AddTarget("out", &err));
1045 // 1. Build for the 1st time (-> populate log)
1046 EXPECT_TRUE(builder_.Build(&err));
1047 ASSERT_EQ(1u, commands_ran_.size());
1049 // 2. Build again (no change)
1050 commands_ran_.clear();
1052 EXPECT_TRUE(builder_.AddTarget("out", &err));
1054 ASSERT_TRUE(builder_.AlreadyUpToDate());
1056 // 3. Alter the entry in the logfile
1057 // (to simulate a change in the command line between 2 builds)
1058 BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out");
1059 ASSERT_TRUE(NULL != log_entry);
1060 ASSERT_NO_FATAL_FAILURE(AssertHash(
1061 "cat out.rsp > out;rspfile=Original very long command",
1062 log_entry->command_hash));
1063 log_entry->command_hash++; // Change the command hash to something else.
1064 // Now expect the target to be rebuilt
1065 commands_ran_.clear();
1067 EXPECT_TRUE(builder_.AddTarget("out", &err));
1069 EXPECT_TRUE(builder_.Build(&err));
1070 EXPECT_EQ(1u, commands_ran_.size());
1073 TEST_F(BuildTest, InterruptCleanup) {
1074 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1076 " command = interrupt\n"
1077 "rule touch-interrupt\n"
1078 " command = touch-interrupt\n"
1079 "build out1: interrupt in1\n"
1080 "build out2: touch-interrupt in2\n"));
1082 fs_.Create("out1", now_, "");
1083 fs_.Create("out2", now_, "");
1085 fs_.Create("in1", now_, "");
1086 fs_.Create("in2", now_, "");
1088 // An untouched output of an interrupted command should be retained.
1090 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1092 EXPECT_FALSE(builder_.Build(&err));
1093 EXPECT_EQ("interrupted by user", err);
1095 EXPECT_EQ(now_-1, fs_.Stat("out1"));
1098 // A touched output of an interrupted command should be deleted.
1099 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1101 EXPECT_FALSE(builder_.Build(&err));
1102 EXPECT_EQ("interrupted by user", err);
1104 EXPECT_EQ(0, fs_.Stat("out2"));
1107 TEST_F(BuildTest, PhonyWithNoInputs) {
1108 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1109 "build nonexistent: phony\n"
1110 "build out1: cat || nonexistent\n"
1111 "build out2: cat nonexistent\n"));
1112 fs_.Create("out1", now_, "");
1113 fs_.Create("out2", now_, "");
1115 // out1 should be up to date even though its input is dirty, because its
1116 // order-only dependency has nothing to do.
1118 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1120 EXPECT_TRUE(builder_.AlreadyUpToDate());
1122 // out2 should still be out of date though, because its input is dirty.
1124 commands_ran_.clear();
1126 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1128 EXPECT_TRUE(builder_.Build(&err));
1130 ASSERT_EQ(1u, commands_ran_.size());
1133 TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
1134 EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
1135 status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]"));