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()),
182 builder_(&state_, config_, NULL, &fs_),
183 now_(1), last_command_(NULL), status_(config_) {
184 builder_.command_runner_.reset(this);
186 "build cat1: cat in1\n"
187 "build cat2: cat in1 in2\n"
188 "build cat12: cat cat1 cat2\n");
190 fs_.Create("in1", now_, "");
191 fs_.Create("in2", now_, "");
195 builder_.command_runner_.release();
198 // Mark a path dirty.
199 void Dirty(const string& path);
201 // CommandRunner impl
202 virtual bool CanRunMore();
203 virtual bool StartCommand(Edge* edge);
204 virtual Edge* WaitForCommand(ExitStatus* status, string* output);
205 virtual vector<Edge*> GetActiveEdges();
206 virtual void Abort();
208 BuildConfig MakeConfig() {
210 config.verbosity = BuildConfig::QUIET;
215 VirtualFileSystem fs_;
219 vector<string> commands_ran_;
224 void BuildTest::Dirty(const string& path) {
225 Node* node = GetNode(path);
228 // If it's an input file, mark that we've already stat()ed it and
230 if (!node->in_edge())
234 bool BuildTest::CanRunMore() {
235 // Only run one at a time.
236 return last_command_ == NULL;
239 bool BuildTest::StartCommand(Edge* edge) {
240 assert(!last_command_);
241 commands_ran_.push_back(edge->EvaluateCommand());
242 if (edge->rule().name() == "cat" ||
243 edge->rule().name() == "cat_rsp" ||
244 edge->rule().name() == "cc" ||
245 edge->rule().name() == "touch" ||
246 edge->rule().name() == "touch-interrupt") {
247 for (vector<Node*>::iterator out = edge->outputs_.begin();
248 out != edge->outputs_.end(); ++out) {
249 fs_.Create((*out)->path(), now_, "");
251 } else if (edge->rule().name() == "true" ||
252 edge->rule().name() == "fail" ||
253 edge->rule().name() == "interrupt") {
254 // Don't do anything.
256 printf("unknown command\n");
260 last_command_ = edge;
264 Edge* BuildTest::WaitForCommand(ExitStatus* status, string* /* output */) {
265 if (Edge* edge = last_command_) {
266 if (edge->rule().name() == "interrupt" ||
267 edge->rule().name() == "touch-interrupt") {
268 *status = ExitInterrupted;
272 if (edge->rule().name() == "fail")
273 *status = ExitFailure;
275 *status = ExitSuccess;
276 last_command_ = NULL;
279 *status = ExitFailure;
283 vector<Edge*> BuildTest::GetActiveEdges() {
286 edges.push_back(last_command_);
290 void BuildTest::Abort() {
291 last_command_ = NULL;
294 TEST_F(BuildTest, NoWork) {
296 EXPECT_TRUE(builder_.AlreadyUpToDate());
299 TEST_F(BuildTest, OneStep) {
300 // Given a dirty target with one ready input,
301 // we should rebuild the target.
304 EXPECT_TRUE(builder_.AddTarget("cat1", &err));
306 EXPECT_TRUE(builder_.Build(&err));
309 ASSERT_EQ(1u, commands_ran_.size());
310 EXPECT_EQ("cat in1 > cat1", commands_ran_[0]);
313 TEST_F(BuildTest, OneStep2) {
314 // Given a target with one dirty input,
315 // we should rebuild the target.
318 EXPECT_TRUE(builder_.AddTarget("cat1", &err));
320 EXPECT_TRUE(builder_.Build(&err));
323 ASSERT_EQ(1u, commands_ran_.size());
324 EXPECT_EQ("cat in1 > cat1", commands_ran_[0]);
327 TEST_F(BuildTest, TwoStep) {
329 EXPECT_TRUE(builder_.AddTarget("cat12", &err));
331 EXPECT_TRUE(builder_.Build(&err));
333 ASSERT_EQ(3u, commands_ran_.size());
334 // Depending on how the pointers work out, we could've ran
335 // the first two commands in either order.
336 EXPECT_TRUE((commands_ran_[0] == "cat in1 > cat1" &&
337 commands_ran_[1] == "cat in1 in2 > cat2") ||
338 (commands_ran_[1] == "cat in1 > cat1" &&
339 commands_ran_[0] == "cat in1 in2 > cat2"));
341 EXPECT_EQ("cat cat1 cat2 > cat12", commands_ran_[2]);
345 // Modifying in2 requires rebuilding one intermediate file
346 // and the final file.
347 fs_.Create("in2", now_, "");
349 EXPECT_TRUE(builder_.AddTarget("cat12", &err));
351 EXPECT_TRUE(builder_.Build(&err));
353 ASSERT_EQ(5u, commands_ran_.size());
354 EXPECT_EQ("cat in1 in2 > cat2", commands_ran_[3]);
355 EXPECT_EQ("cat cat1 cat2 > cat12", commands_ran_[4]);
358 TEST_F(BuildTest, TwoOutputs) {
359 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
361 " command = touch $out\n"
362 "build out1 out2: touch in.txt\n"));
364 fs_.Create("in.txt", now_, "");
367 EXPECT_TRUE(builder_.AddTarget("out1", &err));
369 EXPECT_TRUE(builder_.Build(&err));
371 ASSERT_EQ(1u, commands_ran_.size());
372 EXPECT_EQ("touch out1 out2", commands_ran_[0]);
376 // https://github.com/martine/ninja/issues/148
377 TEST_F(BuildTest, MultiOutIn) {
378 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
380 " command = touch $out\n"
381 "build in1 otherfile: touch in\n"
382 "build out: touch in | in1\n"));
384 fs_.Create("in", now_, "");
385 fs_.Create("in1", ++now_, "");
388 EXPECT_TRUE(builder_.AddTarget("out", &err));
390 EXPECT_TRUE(builder_.Build(&err));
394 TEST_F(BuildTest, Chain) {
395 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
399 "build c5: cat c4\n"));
401 fs_.Create("c1", now_, "");
404 EXPECT_TRUE(builder_.AddTarget("c5", &err));
406 EXPECT_TRUE(builder_.Build(&err));
408 ASSERT_EQ(4u, commands_ran_.size());
411 commands_ran_.clear();
413 EXPECT_TRUE(builder_.AddTarget("c5", &err));
415 EXPECT_TRUE(builder_.AlreadyUpToDate());
419 fs_.Create("c3", now_, "");
421 commands_ran_.clear();
423 EXPECT_TRUE(builder_.AddTarget("c5", &err));
425 EXPECT_FALSE(builder_.AlreadyUpToDate());
426 EXPECT_TRUE(builder_.Build(&err));
427 ASSERT_EQ(2u, commands_ran_.size()); // 3->4, 4->5
430 TEST_F(BuildTest, MissingInput) {
431 // Input is referenced by build file, but no rule for it.
434 EXPECT_FALSE(builder_.AddTarget("cat1", &err));
435 EXPECT_EQ("'in1', needed by 'cat1', missing and no known rule to make it",
439 TEST_F(BuildTest, MissingTarget) {
440 // Target is not referenced by build file.
442 EXPECT_FALSE(builder_.AddTarget("meow", &err));
443 EXPECT_EQ("unknown target: 'meow'", err);
446 TEST_F(BuildTest, MakeDirs) {
450 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, "build subdir\\dir2\\file: cat in1\n"));
451 EXPECT_TRUE(builder_.AddTarget("subdir\\dir2\\file", &err));
453 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, "build subdir/dir2/file: cat in1\n"));
454 EXPECT_TRUE(builder_.AddTarget("subdir/dir2/file", &err));
458 now_ = 0; // Make all stat()s return file not found.
459 EXPECT_TRUE(builder_.Build(&err));
461 ASSERT_EQ(2u, fs_.directories_made_.size());
462 EXPECT_EQ("subdir", fs_.directories_made_[0]);
464 EXPECT_EQ("subdir\\dir2", fs_.directories_made_[1]);
466 EXPECT_EQ("subdir/dir2", fs_.directories_made_[1]);
470 TEST_F(BuildTest, DepFileMissing) {
472 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
473 "rule cc\n command = cc $in\n depfile = $out.d\n"
474 "build foo.o: cc foo.c\n"));
475 fs_.Create("foo.c", now_, "");
477 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
479 ASSERT_EQ(1u, fs_.files_read_.size());
480 EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
483 TEST_F(BuildTest, DepFileOK) {
485 int orig_edges = state_.edges_.size();
486 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
487 "rule cc\n command = cc $in\n depfile = $out.d\n"
488 "build foo.o: cc foo.c\n"));
489 Edge* edge = state_.edges_.back();
491 fs_.Create("foo.c", now_, "");
492 GetNode("bar.h")->MarkDirty(); // Mark bar.h as missing.
493 fs_.Create("foo.o.d", now_, "foo.o: blah.h bar.h\n");
494 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
496 ASSERT_EQ(1u, fs_.files_read_.size());
497 EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
499 // Expect three new edges: one generating foo.o, and two more from
500 // loading the depfile.
501 ASSERT_EQ(orig_edges + 3, (int)state_.edges_.size());
502 // Expect our edge to now have three inputs: foo.c and two headers.
503 ASSERT_EQ(3u, edge->inputs_.size());
505 // Expect the command line we generate to only use the original input.
506 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
509 TEST_F(BuildTest, DepFileParseError) {
511 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
512 "rule cc\n command = cc $in\n depfile = $out.d\n"
513 "build foo.o: cc foo.c\n"));
514 fs_.Create("foo.c", now_, "");
515 fs_.Create("foo.o.d", now_, "randomtext\n");
516 EXPECT_FALSE(builder_.AddTarget("foo.o", &err));
517 EXPECT_EQ("expected depfile 'foo.o.d' to mention 'foo.o', got 'randomtext'",
521 TEST_F(BuildTest, OrderOnlyDeps) {
523 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
524 "rule cc\n command = cc $in\n depfile = $out.d\n"
525 "build foo.o: cc foo.c || otherfile\n"));
526 Edge* edge = state_.edges_.back();
528 fs_.Create("foo.c", now_, "");
529 fs_.Create("otherfile", now_, "");
530 fs_.Create("foo.o.d", now_, "foo.o: blah.h bar.h\n");
531 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
534 // One explicit, two implicit, one order only.
535 ASSERT_EQ(4u, edge->inputs_.size());
536 EXPECT_EQ(2, edge->implicit_deps_);
537 EXPECT_EQ(1, edge->order_only_deps_);
538 // Verify the inputs are in the order we expect
539 // (explicit then implicit then orderonly).
540 EXPECT_EQ("foo.c", edge->inputs_[0]->path());
541 EXPECT_EQ("blah.h", edge->inputs_[1]->path());
542 EXPECT_EQ("bar.h", edge->inputs_[2]->path());
543 EXPECT_EQ("otherfile", edge->inputs_[3]->path());
545 // Expect the command line we generate to only use the original input.
546 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
548 // explicit dep dirty, expect a rebuild.
549 EXPECT_TRUE(builder_.Build(&err));
551 ASSERT_EQ(1u, commands_ran_.size());
555 // implicit dep dirty, expect a rebuild.
556 fs_.Create("blah.h", now_, "");
557 fs_.Create("bar.h", now_, "");
558 commands_ran_.clear();
560 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
561 EXPECT_TRUE(builder_.Build(&err));
563 ASSERT_EQ(1u, commands_ran_.size());
567 // order only dep dirty, no rebuild.
568 fs_.Create("otherfile", now_, "");
569 commands_ran_.clear();
571 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
573 EXPECT_TRUE(builder_.AlreadyUpToDate());
575 // implicit dep missing, expect rebuild.
576 fs_.RemoveFile("bar.h");
577 commands_ran_.clear();
579 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
580 EXPECT_TRUE(builder_.Build(&err));
582 ASSERT_EQ(1u, commands_ran_.size());
585 TEST_F(BuildTest, RebuildOrderOnlyDeps) {
587 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
588 "rule cc\n command = cc $in\n"
589 "rule true\n command = true\n"
590 "build oo.h: cc oo.h.in\n"
591 "build foo.o: cc foo.c || oo.h\n"));
593 fs_.Create("foo.c", now_, "");
594 fs_.Create("oo.h.in", now_, "");
596 // foo.o and order-only dep dirty, build both.
597 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
598 EXPECT_TRUE(builder_.Build(&err));
600 ASSERT_EQ(2u, commands_ran_.size());
602 // all clean, no rebuild.
603 commands_ran_.clear();
605 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
607 EXPECT_TRUE(builder_.AlreadyUpToDate());
609 // order-only dep missing, build it only.
610 fs_.RemoveFile("oo.h");
611 commands_ran_.clear();
613 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
614 EXPECT_TRUE(builder_.Build(&err));
616 ASSERT_EQ(1u, commands_ran_.size());
617 ASSERT_EQ("cc oo.h.in", commands_ran_[0]);
621 // order-only dep dirty, build it only.
622 fs_.Create("oo.h.in", now_, "");
623 commands_ran_.clear();
625 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
626 EXPECT_TRUE(builder_.Build(&err));
628 ASSERT_EQ(1u, commands_ran_.size());
629 ASSERT_EQ("cc oo.h.in", commands_ran_[0]);
632 TEST_F(BuildTest, Phony) {
634 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
635 "build out: cat bar.cc\n"
636 "build all: phony out\n"));
637 fs_.Create("bar.cc", now_, "");
639 EXPECT_TRUE(builder_.AddTarget("all", &err));
642 // Only one command to run, because phony runs no command.
643 EXPECT_FALSE(builder_.AlreadyUpToDate());
644 EXPECT_TRUE(builder_.Build(&err));
646 ASSERT_EQ(1u, commands_ran_.size());
649 TEST_F(BuildTest, PhonyNoWork) {
651 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
652 "build out: cat bar.cc\n"
653 "build all: phony out\n"));
654 fs_.Create("bar.cc", now_, "");
655 fs_.Create("out", now_, "");
657 EXPECT_TRUE(builder_.AddTarget("all", &err));
659 EXPECT_TRUE(builder_.AlreadyUpToDate());
662 TEST_F(BuildTest, Fail) {
663 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
666 "build out1: fail\n"));
669 EXPECT_TRUE(builder_.AddTarget("out1", &err));
672 EXPECT_FALSE(builder_.Build(&err));
673 ASSERT_EQ(1u, commands_ran_.size());
674 ASSERT_EQ("subcommand failed", err);
677 TEST_F(BuildTest, SwallowFailures) {
678 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
684 "build all: phony out1 out2 out3\n"));
686 // Swallow two failures, die on the third.
687 config_.failures_allowed = 3;
690 EXPECT_TRUE(builder_.AddTarget("all", &err));
693 EXPECT_FALSE(builder_.Build(&err));
694 ASSERT_EQ(3u, commands_ran_.size());
695 ASSERT_EQ("subcommands failed", err);
698 TEST_F(BuildTest, SwallowFailuresLimit) {
699 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
705 "build final: cat out1 out2 out3\n"));
707 // Swallow ten failures; we should stop before building final.
708 config_.failures_allowed = 11;
711 EXPECT_TRUE(builder_.AddTarget("final", &err));
714 EXPECT_FALSE(builder_.Build(&err));
715 ASSERT_EQ(3u, commands_ran_.size());
716 ASSERT_EQ("cannot make progress due to previous errors", err);
719 struct BuildWithLogTest : public BuildTest {
721 builder_.SetBuildLog(&build_log_);
727 TEST_F(BuildWithLogTest, NotInLogButOnDisk) {
728 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
731 "build out1: cc in\n"));
733 // Create input/output that would be considered up to date when
734 // not considering the command line hash.
735 fs_.Create("in", now_, "");
736 fs_.Create("out1", now_, "");
739 // Because it's not in the log, it should not be up-to-date until
741 EXPECT_TRUE(builder_.AddTarget("out1", &err));
742 EXPECT_FALSE(builder_.AlreadyUpToDate());
744 commands_ran_.clear();
747 EXPECT_TRUE(builder_.AddTarget("out1", &err));
748 EXPECT_TRUE(builder_.Build(&err));
749 EXPECT_TRUE(builder_.AlreadyUpToDate());
752 TEST_F(BuildWithLogTest, RestatTest) {
753 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
760 "build out1: cc in\n"
761 "build out2: true out1\n"
762 "build out3: cat out2\n"));
764 fs_.Create("out1", now_, "");
765 fs_.Create("out2", now_, "");
766 fs_.Create("out3", now_, "");
770 fs_.Create("in", now_, "");
772 // Do a pre-build so that there's commands in the log for the outputs,
773 // otherwise, the lack of an entry in the build log will cause out3 to rebuild
774 // regardless of restat.
776 EXPECT_TRUE(builder_.AddTarget("out3", &err));
778 EXPECT_TRUE(builder_.Build(&err));
780 commands_ran_.clear();
785 fs_.Create("in", now_, "");
786 // "cc" touches out1, so we should build out2. But because "true" does not
787 // touch out2, we should cancel the build of out3.
788 EXPECT_TRUE(builder_.AddTarget("out3", &err));
790 EXPECT_TRUE(builder_.Build(&err));
791 ASSERT_EQ(2u, commands_ran_.size());
793 // If we run again, it should be a no-op, because the build log has recorded
794 // that we've already built out2 with an input timestamp of 2 (from out1).
795 commands_ran_.clear();
797 EXPECT_TRUE(builder_.AddTarget("out3", &err));
799 EXPECT_TRUE(builder_.AlreadyUpToDate());
803 fs_.Create("in", now_, "");
805 // The build log entry should not, however, prevent us from rebuilding out2
807 commands_ran_.clear();
809 EXPECT_TRUE(builder_.AddTarget("out3", &err));
811 EXPECT_TRUE(builder_.Build(&err));
812 ASSERT_EQ(2u, commands_ran_.size());
815 TEST_F(BuildWithLogTest, RestatMissingFile) {
816 // If a restat rule doesn't create its output, and the output didn't
817 // exist before the rule was run, consider that behavior equivalent
818 // to a rule that doesn't modify its existent output file.
820 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
826 "build out1: true in\n"
827 "build out2: cc out1\n"));
829 fs_.Create("in", now_, "");
830 fs_.Create("out2", now_, "");
832 // Do a pre-build so that there's commands in the log for the outputs,
833 // otherwise, the lack of an entry in the build log will cause out2 to rebuild
834 // regardless of restat.
836 EXPECT_TRUE(builder_.AddTarget("out2", &err));
838 EXPECT_TRUE(builder_.Build(&err));
840 commands_ran_.clear();
844 fs_.Create("in", now_, "");
845 fs_.Create("out2", now_, "");
847 // Run a build, expect only the first command to run.
848 // It doesn't touch its output (due to being the "true" command), so
849 // we shouldn't run the dependent build.
850 EXPECT_TRUE(builder_.AddTarget("out2", &err));
852 EXPECT_TRUE(builder_.Build(&err));
853 ASSERT_EQ(1u, commands_ran_.size());
856 // Test scenario, in which an input file is removed, but output isn't changed
857 // https://github.com/martine/ninja/issues/295
858 TEST_F(BuildWithLogTest, RestatMissingInput) {
859 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
862 " depfile = $out.d\n"
866 "build out1: true in\n"
867 "build out2: cc out1\n"));
869 // Create all necessary files
870 fs_.Create("in", now_, "");
872 // The implicit dependencies and the depfile itself
873 // are newer than the output
874 TimeStamp restat_mtime = ++now_;
875 fs_.Create("out1.d", now_, "out1: will.be.deleted restat.file\n");
876 fs_.Create("will.be.deleted", now_, "");
877 fs_.Create("restat.file", now_, "");
879 // Run the build, out1 and out2 get built
881 EXPECT_TRUE(builder_.AddTarget("out2", &err));
883 EXPECT_TRUE(builder_.Build(&err));
884 ASSERT_EQ(2u, commands_ran_.size());
886 // See that an entry in the logfile is created, capturing
888 BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out1");
889 ASSERT_TRUE(NULL != log_entry);
890 ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
892 // Now remove a file, referenced from depfile, so that target becomes
893 // dirty, but the output does not change
894 fs_.RemoveFile("will.be.deleted");
896 // Trigger the build again - only out1 gets built
897 commands_ran_.clear();
899 EXPECT_TRUE(builder_.AddTarget("out2", &err));
901 EXPECT_TRUE(builder_.Build(&err));
902 ASSERT_EQ(1u, commands_ran_.size());
904 // Check that the logfile entry remains correctly set
905 log_entry = build_log_.LookupByOutput("out1");
906 ASSERT_TRUE(NULL != log_entry);
907 ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
910 struct BuildDryRun : public BuildWithLogTest {
912 config_.dry_run = true;
916 TEST_F(BuildDryRun, AllCommandsShown) {
917 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
924 "build out1: cc in\n"
925 "build out2: true out1\n"
926 "build out3: cat out2\n"));
928 fs_.Create("out1", now_, "");
929 fs_.Create("out2", now_, "");
930 fs_.Create("out3", now_, "");
934 fs_.Create("in", now_, "");
936 // "cc" touches out1, so we should build out2. But because "true" does not
937 // touch out2, we should cancel the build of out3.
939 EXPECT_TRUE(builder_.AddTarget("out3", &err));
941 EXPECT_TRUE(builder_.Build(&err));
942 ASSERT_EQ(3u, commands_ran_.size());
945 // Test that RSP files are created when & where appropriate and deleted after
946 // successful execution.
947 TEST_F(BuildTest, RspFileSuccess)
949 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
951 " command = cat $rspfile > $out\n"
952 " rspfile = $rspfile\n"
953 " rspfile_content = $long_command\n"
954 "build out1: cat in\n"
955 "build out2: cat_rsp in\n"
956 " rspfile = out2.rsp\n"
957 " long_command = Some very long command\n"));
959 fs_.Create("out1", now_, "");
960 fs_.Create("out2", now_, "");
961 fs_.Create("out3", now_, "");
965 fs_.Create("in", now_, "");
968 EXPECT_TRUE(builder_.AddTarget("out1", &err));
970 EXPECT_TRUE(builder_.AddTarget("out2", &err));
973 size_t files_created = fs_.files_created_.size();
974 size_t files_removed = fs_.files_removed_.size();
976 EXPECT_TRUE(builder_.Build(&err));
977 ASSERT_EQ(2u, commands_ran_.size()); // cat + cat_rsp
979 // The RSP file was created
980 ASSERT_EQ(files_created + 1, fs_.files_created_.size());
981 ASSERT_EQ(1u, fs_.files_created_.count("out2.rsp"));
983 // The RSP file was removed
984 ASSERT_EQ(files_removed + 1, fs_.files_removed_.size());
985 ASSERT_EQ(1u, fs_.files_removed_.count("out2.rsp"));
988 // Test that RSP file is created but not removed for commands, which fail
989 TEST_F(BuildTest, RspFileFailure) {
990 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
993 " rspfile = $rspfile\n"
994 " rspfile_content = $long_command\n"
995 "build out: fail in\n"
996 " rspfile = out.rsp\n"
997 " long_command = Another very long command\n"));
999 fs_.Create("out", now_, "");
1001 fs_.Create("in", now_, "");
1004 EXPECT_TRUE(builder_.AddTarget("out", &err));
1007 size_t files_created = fs_.files_created_.size();
1008 size_t files_removed = fs_.files_removed_.size();
1010 EXPECT_FALSE(builder_.Build(&err));
1011 ASSERT_EQ("subcommand failed", err);
1012 ASSERT_EQ(1u, commands_ran_.size());
1014 // The RSP file was created
1015 ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1016 ASSERT_EQ(1u, fs_.files_created_.count("out.rsp"));
1018 // The RSP file was NOT removed
1019 ASSERT_EQ(files_removed, fs_.files_removed_.size());
1020 ASSERT_EQ(0u, fs_.files_removed_.count("out.rsp"));
1022 // The RSP file contains what it should
1023 ASSERT_EQ("Another very long command", fs_.files_["out.rsp"].contents);
1026 // Test that contens of the RSP file behaves like a regular part of
1027 // command line, i.e. triggers a rebuild if changed
1028 TEST_F(BuildWithLogTest, RspFileCmdLineChange) {
1029 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1031 " command = cat $rspfile > $out\n"
1032 " rspfile = $rspfile\n"
1033 " rspfile_content = $long_command\n"
1034 "build out: cat_rsp in\n"
1035 " rspfile = out.rsp\n"
1036 " long_command = Original very long command\n"));
1038 fs_.Create("out", now_, "");
1040 fs_.Create("in", now_, "");
1043 EXPECT_TRUE(builder_.AddTarget("out", &err));
1046 // 1. Build for the 1st time (-> populate log)
1047 EXPECT_TRUE(builder_.Build(&err));
1048 ASSERT_EQ(1u, commands_ran_.size());
1050 // 2. Build again (no change)
1051 commands_ran_.clear();
1053 EXPECT_TRUE(builder_.AddTarget("out", &err));
1055 ASSERT_TRUE(builder_.AlreadyUpToDate());
1057 // 3. Alter the entry in the logfile
1058 // (to simulate a change in the command line between 2 builds)
1059 BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out");
1060 ASSERT_TRUE(NULL != log_entry);
1061 ASSERT_NO_FATAL_FAILURE(AssertHash(
1062 "cat out.rsp > out;rspfile=Original very long command",
1063 log_entry->command_hash));
1064 log_entry->command_hash++; // Change the command hash to something else.
1065 // Now expect the target to be rebuilt
1066 commands_ran_.clear();
1068 EXPECT_TRUE(builder_.AddTarget("out", &err));
1070 EXPECT_TRUE(builder_.Build(&err));
1071 EXPECT_EQ(1u, commands_ran_.size());
1074 TEST_F(BuildTest, InterruptCleanup) {
1075 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1077 " command = interrupt\n"
1078 "rule touch-interrupt\n"
1079 " command = touch-interrupt\n"
1080 "build out1: interrupt in1\n"
1081 "build out2: touch-interrupt in2\n"));
1083 fs_.Create("out1", now_, "");
1084 fs_.Create("out2", now_, "");
1086 fs_.Create("in1", now_, "");
1087 fs_.Create("in2", now_, "");
1089 // An untouched output of an interrupted command should be retained.
1091 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1093 EXPECT_FALSE(builder_.Build(&err));
1094 EXPECT_EQ("interrupted by user", err);
1096 EXPECT_EQ(now_-1, fs_.Stat("out1"));
1099 // A touched output of an interrupted command should be deleted.
1100 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1102 EXPECT_FALSE(builder_.Build(&err));
1103 EXPECT_EQ("interrupted by user", err);
1105 EXPECT_EQ(0, fs_.Stat("out2"));
1108 TEST_F(BuildTest, PhonyWithNoInputs) {
1109 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1110 "build nonexistent: phony\n"
1111 "build out1: cat || nonexistent\n"
1112 "build out2: cat nonexistent\n"));
1113 fs_.Create("out1", now_, "");
1114 fs_.Create("out2", now_, "");
1116 // out1 should be up to date even though its input is dirty, because its
1117 // order-only dependency has nothing to do.
1119 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1121 EXPECT_TRUE(builder_.AlreadyUpToDate());
1123 // out2 should still be out of date though, because its input is dirty.
1125 commands_ran_.clear();
1127 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1129 EXPECT_TRUE(builder_.Build(&err));
1131 ASSERT_EQ(1u, commands_ran_.size());
1134 TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
1135 EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
1136 status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]"));