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_), now_(1),
182 last_command_(NULL), status_(config_) {
183 builder_.disk_interface_ = &fs_;
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;
218 VirtualFileSystem fs_;
220 vector<string> commands_ran_;
225 void BuildTest::Dirty(const string& path) {
226 Node* node = GetNode(path);
229 // If it's an input file, mark that we've already stat()ed it and
231 if (!node->in_edge())
235 bool BuildTest::CanRunMore() {
236 // Only run one at a time.
237 return last_command_ == NULL;
240 bool BuildTest::StartCommand(Edge* edge) {
241 assert(!last_command_);
242 commands_ran_.push_back(edge->EvaluateCommand());
243 if (edge->rule().name() == "cat" ||
244 edge->rule().name() == "cat_rsp" ||
245 edge->rule().name() == "cc" ||
246 edge->rule().name() == "touch" ||
247 edge->rule().name() == "touch-interrupt") {
248 for (vector<Node*>::iterator out = edge->outputs_.begin();
249 out != edge->outputs_.end(); ++out) {
250 fs_.Create((*out)->path(), now_, "");
252 } else if (edge->rule().name() == "true" ||
253 edge->rule().name() == "fail" ||
254 edge->rule().name() == "interrupt") {
255 // Don't do anything.
257 printf("unknown command\n");
261 last_command_ = edge;
265 Edge* BuildTest::WaitForCommand(ExitStatus* status, string* /* output */) {
266 if (Edge* edge = last_command_) {
267 if (edge->rule().name() == "interrupt" ||
268 edge->rule().name() == "touch-interrupt") {
269 *status = ExitInterrupted;
273 if (edge->rule().name() == "fail")
274 *status = ExitFailure;
276 *status = ExitSuccess;
277 last_command_ = NULL;
280 *status = ExitFailure;
284 vector<Edge*> BuildTest::GetActiveEdges() {
287 edges.push_back(last_command_);
291 void BuildTest::Abort() {
292 last_command_ = NULL;
295 TEST_F(BuildTest, NoWork) {
297 EXPECT_TRUE(builder_.AlreadyUpToDate());
300 TEST_F(BuildTest, OneStep) {
301 // Given a dirty target with one ready input,
302 // we should rebuild the target.
305 EXPECT_TRUE(builder_.AddTarget("cat1", &err));
307 EXPECT_TRUE(builder_.Build(&err));
310 ASSERT_EQ(1u, commands_ran_.size());
311 EXPECT_EQ("cat in1 > cat1", commands_ran_[0]);
314 TEST_F(BuildTest, OneStep2) {
315 // Given a target with one dirty input,
316 // we should rebuild the target.
319 EXPECT_TRUE(builder_.AddTarget("cat1", &err));
321 EXPECT_TRUE(builder_.Build(&err));
324 ASSERT_EQ(1u, commands_ran_.size());
325 EXPECT_EQ("cat in1 > cat1", commands_ran_[0]);
328 TEST_F(BuildTest, TwoStep) {
330 EXPECT_TRUE(builder_.AddTarget("cat12", &err));
332 EXPECT_TRUE(builder_.Build(&err));
334 ASSERT_EQ(3u, commands_ran_.size());
335 // Depending on how the pointers work out, we could've ran
336 // the first two commands in either order.
337 EXPECT_TRUE((commands_ran_[0] == "cat in1 > cat1" &&
338 commands_ran_[1] == "cat in1 in2 > cat2") ||
339 (commands_ran_[1] == "cat in1 > cat1" &&
340 commands_ran_[0] == "cat in1 in2 > cat2"));
342 EXPECT_EQ("cat cat1 cat2 > cat12", commands_ran_[2]);
346 // Modifying in2 requires rebuilding one intermediate file
347 // and the final file.
348 fs_.Create("in2", now_, "");
350 EXPECT_TRUE(builder_.AddTarget("cat12", &err));
352 EXPECT_TRUE(builder_.Build(&err));
354 ASSERT_EQ(5u, commands_ran_.size());
355 EXPECT_EQ("cat in1 in2 > cat2", commands_ran_[3]);
356 EXPECT_EQ("cat cat1 cat2 > cat12", commands_ran_[4]);
359 TEST_F(BuildTest, TwoOutputs) {
360 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
362 " command = touch $out\n"
363 "build out1 out2: touch in.txt\n"));
365 fs_.Create("in.txt", now_, "");
368 EXPECT_TRUE(builder_.AddTarget("out1", &err));
370 EXPECT_TRUE(builder_.Build(&err));
372 ASSERT_EQ(1u, commands_ran_.size());
373 EXPECT_EQ("touch out1 out2", commands_ran_[0]);
377 // https://github.com/martine/ninja/issues/148
378 TEST_F(BuildTest, MultiOutIn) {
379 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
381 " command = touch $out\n"
382 "build in1 otherfile: touch in\n"
383 "build out: touch in | in1\n"));
385 fs_.Create("in", now_, "");
386 fs_.Create("in1", ++now_, "");
389 EXPECT_TRUE(builder_.AddTarget("out", &err));
391 EXPECT_TRUE(builder_.Build(&err));
395 TEST_F(BuildTest, Chain) {
396 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
400 "build c5: cat c4\n"));
402 fs_.Create("c1", now_, "");
405 EXPECT_TRUE(builder_.AddTarget("c5", &err));
407 EXPECT_TRUE(builder_.Build(&err));
409 ASSERT_EQ(4u, commands_ran_.size());
412 commands_ran_.clear();
414 EXPECT_TRUE(builder_.AddTarget("c5", &err));
416 EXPECT_TRUE(builder_.AlreadyUpToDate());
420 fs_.Create("c3", now_, "");
422 commands_ran_.clear();
424 EXPECT_TRUE(builder_.AddTarget("c5", &err));
426 EXPECT_FALSE(builder_.AlreadyUpToDate());
427 EXPECT_TRUE(builder_.Build(&err));
428 ASSERT_EQ(2u, commands_ran_.size()); // 3->4, 4->5
431 TEST_F(BuildTest, MissingInput) {
432 // Input is referenced by build file, but no rule for it.
435 EXPECT_FALSE(builder_.AddTarget("cat1", &err));
436 EXPECT_EQ("'in1', needed by 'cat1', missing and no known rule to make it",
440 TEST_F(BuildTest, MissingTarget) {
441 // Target is not referenced by build file.
443 EXPECT_FALSE(builder_.AddTarget("meow", &err));
444 EXPECT_EQ("unknown target: 'meow'", err);
447 TEST_F(BuildTest, MakeDirs) {
451 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, "build subdir\\dir2\\file: cat in1\n"));
452 EXPECT_TRUE(builder_.AddTarget("subdir\\dir2\\file", &err));
454 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, "build subdir/dir2/file: cat in1\n"));
455 EXPECT_TRUE(builder_.AddTarget("subdir/dir2/file", &err));
459 now_ = 0; // Make all stat()s return file not found.
460 EXPECT_TRUE(builder_.Build(&err));
462 ASSERT_EQ(2u, fs_.directories_made_.size());
463 EXPECT_EQ("subdir", fs_.directories_made_[0]);
465 EXPECT_EQ("subdir\\dir2", fs_.directories_made_[1]);
467 EXPECT_EQ("subdir/dir2", fs_.directories_made_[1]);
471 TEST_F(BuildTest, DepFileMissing) {
473 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
474 "rule cc\n command = cc $in\n depfile = $out.d\n"
475 "build foo.o: cc foo.c\n"));
476 fs_.Create("foo.c", now_, "");
478 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
480 ASSERT_EQ(1u, fs_.files_read_.size());
481 EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
484 TEST_F(BuildTest, DepFileOK) {
486 int orig_edges = state_.edges_.size();
487 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
488 "rule cc\n command = cc $in\n depfile = $out.d\n"
489 "build foo.o: cc foo.c\n"));
490 Edge* edge = state_.edges_.back();
492 fs_.Create("foo.c", now_, "");
493 GetNode("bar.h")->MarkDirty(); // Mark bar.h as missing.
494 fs_.Create("foo.o.d", now_, "foo.o: blah.h bar.h\n");
495 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
497 ASSERT_EQ(1u, fs_.files_read_.size());
498 EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
500 // Expect three new edges: one generating foo.o, and two more from
501 // loading the depfile.
502 ASSERT_EQ(orig_edges + 3, (int)state_.edges_.size());
503 // Expect our edge to now have three inputs: foo.c and two headers.
504 ASSERT_EQ(3u, edge->inputs_.size());
506 // Expect the command line we generate to only use the original input.
507 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
510 TEST_F(BuildTest, DepFileParseError) {
512 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
513 "rule cc\n command = cc $in\n depfile = $out.d\n"
514 "build foo.o: cc foo.c\n"));
515 fs_.Create("foo.c", now_, "");
516 fs_.Create("foo.o.d", now_, "randomtext\n");
517 EXPECT_FALSE(builder_.AddTarget("foo.o", &err));
518 EXPECT_EQ("expected depfile 'foo.o.d' to mention 'foo.o', got 'randomtext'",
522 TEST_F(BuildTest, OrderOnlyDeps) {
524 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
525 "rule cc\n command = cc $in\n depfile = $out.d\n"
526 "build foo.o: cc foo.c || otherfile\n"));
527 Edge* edge = state_.edges_.back();
529 fs_.Create("foo.c", now_, "");
530 fs_.Create("otherfile", now_, "");
531 fs_.Create("foo.o.d", now_, "foo.o: blah.h bar.h\n");
532 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
535 // One explicit, two implicit, one order only.
536 ASSERT_EQ(4u, edge->inputs_.size());
537 EXPECT_EQ(2, edge->implicit_deps_);
538 EXPECT_EQ(1, edge->order_only_deps_);
539 // Verify the inputs are in the order we expect
540 // (explicit then implicit then orderonly).
541 EXPECT_EQ("foo.c", edge->inputs_[0]->path());
542 EXPECT_EQ("blah.h", edge->inputs_[1]->path());
543 EXPECT_EQ("bar.h", edge->inputs_[2]->path());
544 EXPECT_EQ("otherfile", edge->inputs_[3]->path());
546 // Expect the command line we generate to only use the original input.
547 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
549 // explicit dep dirty, expect a rebuild.
550 EXPECT_TRUE(builder_.Build(&err));
552 ASSERT_EQ(1u, commands_ran_.size());
556 // implicit dep dirty, expect a rebuild.
557 fs_.Create("blah.h", now_, "");
558 fs_.Create("bar.h", now_, "");
559 commands_ran_.clear();
561 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
562 EXPECT_TRUE(builder_.Build(&err));
564 ASSERT_EQ(1u, commands_ran_.size());
568 // order only dep dirty, no rebuild.
569 fs_.Create("otherfile", now_, "");
570 commands_ran_.clear();
572 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
574 EXPECT_TRUE(builder_.AlreadyUpToDate());
576 // implicit dep missing, expect rebuild.
577 fs_.RemoveFile("bar.h");
578 commands_ran_.clear();
580 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
581 EXPECT_TRUE(builder_.Build(&err));
583 ASSERT_EQ(1u, commands_ran_.size());
586 TEST_F(BuildTest, RebuildOrderOnlyDeps) {
588 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
589 "rule cc\n command = cc $in\n"
590 "rule true\n command = true\n"
591 "build oo.h: cc oo.h.in\n"
592 "build foo.o: cc foo.c || oo.h\n"));
594 fs_.Create("foo.c", now_, "");
595 fs_.Create("oo.h.in", now_, "");
597 // foo.o and order-only dep dirty, build both.
598 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
599 EXPECT_TRUE(builder_.Build(&err));
601 ASSERT_EQ(2u, commands_ran_.size());
603 // all clean, no rebuild.
604 commands_ran_.clear();
606 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
608 EXPECT_TRUE(builder_.AlreadyUpToDate());
610 // order-only dep missing, build it only.
611 fs_.RemoveFile("oo.h");
612 commands_ran_.clear();
614 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
615 EXPECT_TRUE(builder_.Build(&err));
617 ASSERT_EQ(1u, commands_ran_.size());
618 ASSERT_EQ("cc oo.h.in", commands_ran_[0]);
622 // order-only dep dirty, build it only.
623 fs_.Create("oo.h.in", now_, "");
624 commands_ran_.clear();
626 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
627 EXPECT_TRUE(builder_.Build(&err));
629 ASSERT_EQ(1u, commands_ran_.size());
630 ASSERT_EQ("cc oo.h.in", commands_ran_[0]);
633 TEST_F(BuildTest, Phony) {
635 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
636 "build out: cat bar.cc\n"
637 "build all: phony out\n"));
638 fs_.Create("bar.cc", now_, "");
640 EXPECT_TRUE(builder_.AddTarget("all", &err));
643 // Only one command to run, because phony runs no command.
644 EXPECT_FALSE(builder_.AlreadyUpToDate());
645 EXPECT_TRUE(builder_.Build(&err));
647 ASSERT_EQ(1u, commands_ran_.size());
650 TEST_F(BuildTest, PhonyNoWork) {
652 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
653 "build out: cat bar.cc\n"
654 "build all: phony out\n"));
655 fs_.Create("bar.cc", now_, "");
656 fs_.Create("out", now_, "");
658 EXPECT_TRUE(builder_.AddTarget("all", &err));
660 EXPECT_TRUE(builder_.AlreadyUpToDate());
663 TEST_F(BuildTest, Fail) {
664 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
667 "build out1: fail\n"));
670 EXPECT_TRUE(builder_.AddTarget("out1", &err));
673 EXPECT_FALSE(builder_.Build(&err));
674 ASSERT_EQ(1u, commands_ran_.size());
675 ASSERT_EQ("subcommand failed", err);
678 TEST_F(BuildTest, SwallowFailures) {
679 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
685 "build all: phony out1 out2 out3\n"));
687 // Swallow two failures, die on the third.
688 config_.failures_allowed = 3;
691 EXPECT_TRUE(builder_.AddTarget("all", &err));
694 EXPECT_FALSE(builder_.Build(&err));
695 ASSERT_EQ(3u, commands_ran_.size());
696 ASSERT_EQ("subcommands failed", err);
699 TEST_F(BuildTest, SwallowFailuresLimit) {
700 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
706 "build final: cat out1 out2 out3\n"));
708 // Swallow ten failures; we should stop before building final.
709 config_.failures_allowed = 11;
712 EXPECT_TRUE(builder_.AddTarget("final", &err));
715 EXPECT_FALSE(builder_.Build(&err));
716 ASSERT_EQ(3u, commands_ran_.size());
717 ASSERT_EQ("cannot make progress due to previous errors", err);
720 struct BuildWithLogTest : public BuildTest {
722 state_.build_log_ = builder_.log_ = &build_log_;
728 TEST_F(BuildWithLogTest, RestatTest) {
729 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
736 "build out1: cc in\n"
737 "build out2: true out1\n"
738 "build out3: cat out2\n"));
740 fs_.Create("out1", now_, "");
741 fs_.Create("out2", now_, "");
742 fs_.Create("out3", now_, "");
746 fs_.Create("in", now_, "");
748 // "cc" touches out1, so we should build out2. But because "true" does not
749 // touch out2, we should cancel the build of out3.
751 EXPECT_TRUE(builder_.AddTarget("out3", &err));
753 EXPECT_TRUE(builder_.Build(&err));
754 ASSERT_EQ(2u, commands_ran_.size());
756 // If we run again, it should be a no-op, because the build log has recorded
757 // that we've already built out2 with an input timestamp of 2 (from out1).
758 commands_ran_.clear();
760 EXPECT_TRUE(builder_.AddTarget("out3", &err));
762 EXPECT_TRUE(builder_.AlreadyUpToDate());
766 fs_.Create("in", now_, "");
768 // The build log entry should not, however, prevent us from rebuilding out2
770 commands_ran_.clear();
772 EXPECT_TRUE(builder_.AddTarget("out3", &err));
774 EXPECT_TRUE(builder_.Build(&err));
775 ASSERT_EQ(2u, commands_ran_.size());
778 TEST_F(BuildWithLogTest, RestatMissingFile) {
779 // If a restat rule doesn't create its output, and the output didn't
780 // exist before the rule was run, consider that behavior equivalent
781 // to a rule that doesn't modify its existent output file.
783 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
789 "build out1: true in\n"
790 "build out2: cc out1\n"));
792 fs_.Create("in", now_, "");
793 fs_.Create("out2", now_, "");
795 // Run a build, expect only the first command to run.
796 // It doesn't touch its output (due to being the "true" command), so
797 // we shouldn't run the dependent build.
799 EXPECT_TRUE(builder_.AddTarget("out2", &err));
801 EXPECT_TRUE(builder_.Build(&err));
802 ASSERT_EQ(1u, commands_ran_.size());
805 struct BuildDryRun : public BuildWithLogTest {
807 config_.dry_run = true;
811 TEST_F(BuildDryRun, AllCommandsShown) {
812 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
819 "build out1: cc in\n"
820 "build out2: true out1\n"
821 "build out3: cat out2\n"));
823 fs_.Create("out1", now_, "");
824 fs_.Create("out2", now_, "");
825 fs_.Create("out3", now_, "");
829 fs_.Create("in", now_, "");
831 // "cc" touches out1, so we should build out2. But because "true" does not
832 // touch out2, we should cancel the build of out3.
834 EXPECT_TRUE(builder_.AddTarget("out3", &err));
836 EXPECT_TRUE(builder_.Build(&err));
837 ASSERT_EQ(3u, commands_ran_.size());
840 // Test that RSP files are created when & where appropriate and deleted after
841 // succesful execution.
842 TEST_F(BuildTest, RspFileSuccess)
844 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
846 " command = cat $rspfile > $out\n"
847 " rspfile = $rspfile\n"
848 " rspfile_content = $long_command\n"
849 "build out1: cat in\n"
850 "build out2: cat_rsp in\n"
851 " rspfile = out2.rsp\n"
852 " long_command = Some very long command\n"));
854 fs_.Create("out1", now_, "");
855 fs_.Create("out2", now_, "");
856 fs_.Create("out3", now_, "");
860 fs_.Create("in", now_, "");
863 EXPECT_TRUE(builder_.AddTarget("out1", &err));
865 EXPECT_TRUE(builder_.AddTarget("out2", &err));
868 size_t files_created = fs_.files_created_.size();
869 size_t files_removed = fs_.files_removed_.size();
871 EXPECT_TRUE(builder_.Build(&err));
872 ASSERT_EQ(2u, commands_ran_.size()); // cat + cat_rsp
874 // The RSP file was created
875 ASSERT_EQ(files_created + 1, fs_.files_created_.size());
876 ASSERT_EQ(1u, fs_.files_created_.count("out2.rsp"));
878 // The RSP file was removed
879 ASSERT_EQ(files_removed + 1, fs_.files_removed_.size());
880 ASSERT_EQ(1u, fs_.files_removed_.count("out2.rsp"));
883 // Test that RSP file is created but not removed for commands, which fail
884 TEST_F(BuildTest, RspFileFailure) {
885 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
888 " rspfile = $rspfile\n"
889 " rspfile_content = $long_command\n"
890 "build out: fail in\n"
891 " rspfile = out.rsp\n"
892 " long_command = Another very long command\n"));
894 fs_.Create("out", now_, "");
896 fs_.Create("in", now_, "");
899 EXPECT_TRUE(builder_.AddTarget("out", &err));
902 size_t files_created = fs_.files_created_.size();
903 size_t files_removed = fs_.files_removed_.size();
905 EXPECT_FALSE(builder_.Build(&err));
906 ASSERT_EQ("subcommand failed", err);
907 ASSERT_EQ(1u, commands_ran_.size());
909 // The RSP file was created
910 ASSERT_EQ(files_created + 1, fs_.files_created_.size());
911 ASSERT_EQ(1u, fs_.files_created_.count("out.rsp"));
913 // The RSP file was NOT removed
914 ASSERT_EQ(files_removed, fs_.files_removed_.size());
915 ASSERT_EQ(0u, fs_.files_removed_.count("out.rsp"));
917 // The RSP file contains what it should
918 ASSERT_EQ("Another very long command", fs_.files_["out.rsp"].contents);
921 // Test that contens of the RSP file behaves like a regular part of
922 // command line, i.e. triggers a rebuild if changed
923 TEST_F(BuildWithLogTest, RspFileCmdLineChange) {
924 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
926 " command = cat $rspfile > $out\n"
927 " rspfile = $rspfile\n"
928 " rspfile_content = $long_command\n"
929 "build out: cat_rsp in\n"
930 " rspfile = out.rsp\n"
931 " long_command = Original very long command\n"));
933 fs_.Create("out", now_, "");
935 fs_.Create("in", now_, "");
938 EXPECT_TRUE(builder_.AddTarget("out", &err));
941 // 1. Build for the 1st time (-> populate log)
942 EXPECT_TRUE(builder_.Build(&err));
943 ASSERT_EQ(1u, commands_ran_.size());
945 // 2. Build again (no change)
946 commands_ran_.clear();
948 EXPECT_TRUE(builder_.AddTarget("out", &err));
950 ASSERT_TRUE(builder_.AlreadyUpToDate());
952 // 3. Alter the entry in the logfile
953 // (to simulate a change in the command line between 2 builds)
954 BuildLog::LogEntry * log_entry = build_log_.LookupByOutput("out");
955 ASSERT_TRUE(NULL != log_entry);
956 ASSERT_NO_FATAL_FAILURE(AssertHash(
957 "cat out.rsp > out;rspfile=Original very long command",
958 log_entry->command_hash));
959 log_entry->command_hash++; // Change the command hash to something else.
960 // Now expect the target to be rebuilt
961 commands_ran_.clear();
963 EXPECT_TRUE(builder_.AddTarget("out", &err));
965 EXPECT_TRUE(builder_.Build(&err));
966 EXPECT_EQ(1u, commands_ran_.size());
969 TEST_F(BuildTest, InterruptCleanup) {
970 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
972 " command = interrupt\n"
973 "rule touch-interrupt\n"
974 " command = touch-interrupt\n"
975 "build out1: interrupt in1\n"
976 "build out2: touch-interrupt in2\n"));
978 fs_.Create("out1", now_, "");
979 fs_.Create("out2", now_, "");
981 fs_.Create("in1", now_, "");
982 fs_.Create("in2", now_, "");
984 // An untouched output of an interrupted command should be retained.
986 EXPECT_TRUE(builder_.AddTarget("out1", &err));
988 EXPECT_FALSE(builder_.Build(&err));
989 EXPECT_EQ("interrupted by user", err);
991 EXPECT_EQ(now_-1, fs_.Stat("out1"));
994 // A touched output of an interrupted command should be deleted.
995 EXPECT_TRUE(builder_.AddTarget("out2", &err));
997 EXPECT_FALSE(builder_.Build(&err));
998 EXPECT_EQ("interrupted by user", err);
1000 EXPECT_EQ(0, fs_.Stat("out2"));
1003 TEST_F(BuildTest, PhonyWithNoInputs) {
1004 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1005 "build nonexistent: phony\n"
1006 "build out1: cat || nonexistent\n"
1007 "build out2: cat nonexistent\n"));
1008 fs_.Create("out1", now_, "");
1009 fs_.Create("out2", now_, "");
1011 // out1 should be up to date even though its input is dirty, because its
1012 // order-only dependency has nothing to do.
1014 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1016 EXPECT_TRUE(builder_.AlreadyUpToDate());
1018 // out2 should still be out of date though, because its input is dirty.
1020 commands_ran_.clear();
1022 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1024 EXPECT_TRUE(builder_.Build(&err));
1026 ASSERT_EQ(1u, commands_ran_.size());
1029 TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
1030 EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
1031 status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]"));