1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
19 #include "build_log.h"
24 /// Fixture for tests involving Plan.
25 // Though Plan doesn't use State, it's useful to have one around
26 // to create Nodes and Edges.
27 struct PlanTest : public StateTestWithBuiltinRules {
30 /// Because FindWork does not return Edges in any sort of predictable order,
31 // provide a means to get available Edges in order and in a format which is
32 // easy to write tests around.
33 void FindWorkSorted(deque<Edge*>* ret, int count) {
34 struct CompareEdgesByOutput {
35 static bool cmp(const Edge* a, const Edge* b) {
36 return a->outputs_[0]->path() < b->outputs_[0]->path();
40 for (int i = 0; i < count; ++i) {
41 ASSERT_TRUE(plan_.more_to_do());
42 Edge* edge = plan_.FindWork();
46 ASSERT_FALSE(plan_.FindWork());
47 sort(ret->begin(), ret->end(), CompareEdgesByOutput::cmp);
50 void TestPoolWithDepthOne(const char *test_case);
53 TEST_F(PlanTest, Basic) {
55 "build out: cat mid\n"
56 "build mid: cat in\n");
57 GetNode("mid")->MarkDirty();
58 GetNode("out")->MarkDirty();
60 EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
62 ASSERT_TRUE(plan_.more_to_do());
64 Edge* edge = plan_.FindWork();
66 ASSERT_EQ("in", edge->inputs_[0]->path());
67 ASSERT_EQ("mid", edge->outputs_[0]->path());
69 ASSERT_FALSE(plan_.FindWork());
71 plan_.EdgeFinished(edge);
73 edge = plan_.FindWork();
75 ASSERT_EQ("mid", edge->inputs_[0]->path());
76 ASSERT_EQ("out", edge->outputs_[0]->path());
78 plan_.EdgeFinished(edge);
80 ASSERT_FALSE(plan_.more_to_do());
81 edge = plan_.FindWork();
85 // Test that two outputs from one rule can be handled as inputs to the next.
86 TEST_F(PlanTest, DoubleOutputDirect) {
88 "build out: cat mid1 mid2\n"
89 "build mid1 mid2: cat in\n");
90 GetNode("mid1")->MarkDirty();
91 GetNode("mid2")->MarkDirty();
92 GetNode("out")->MarkDirty();
95 EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
97 ASSERT_TRUE(plan_.more_to_do());
100 edge = plan_.FindWork();
101 ASSERT_TRUE(edge); // cat in
102 plan_.EdgeFinished(edge);
104 edge = plan_.FindWork();
105 ASSERT_TRUE(edge); // cat mid1 mid2
106 plan_.EdgeFinished(edge);
108 edge = plan_.FindWork();
109 ASSERT_FALSE(edge); // done
112 // Test that two outputs from one rule can eventually be routed to another.
113 TEST_F(PlanTest, DoubleOutputIndirect) {
115 "build out: cat b1 b2\n"
118 "build a1 a2: cat in\n");
119 GetNode("a1")->MarkDirty();
120 GetNode("a2")->MarkDirty();
121 GetNode("b1")->MarkDirty();
122 GetNode("b2")->MarkDirty();
123 GetNode("out")->MarkDirty();
125 EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
127 ASSERT_TRUE(plan_.more_to_do());
130 edge = plan_.FindWork();
131 ASSERT_TRUE(edge); // cat in
132 plan_.EdgeFinished(edge);
134 edge = plan_.FindWork();
135 ASSERT_TRUE(edge); // cat a1
136 plan_.EdgeFinished(edge);
138 edge = plan_.FindWork();
139 ASSERT_TRUE(edge); // cat a2
140 plan_.EdgeFinished(edge);
142 edge = plan_.FindWork();
143 ASSERT_TRUE(edge); // cat b1 b2
144 plan_.EdgeFinished(edge);
146 edge = plan_.FindWork();
147 ASSERT_FALSE(edge); // done
150 // Test that two edges from one output can both execute.
151 TEST_F(PlanTest, DoubleDependent) {
153 "build out: cat a1 a2\n"
154 "build a1: cat mid\n"
155 "build a2: cat mid\n"
156 "build mid: cat in\n");
157 GetNode("mid")->MarkDirty();
158 GetNode("a1")->MarkDirty();
159 GetNode("a2")->MarkDirty();
160 GetNode("out")->MarkDirty();
163 EXPECT_TRUE(plan_.AddTarget(GetNode("out"), &err));
165 ASSERT_TRUE(plan_.more_to_do());
168 edge = plan_.FindWork();
169 ASSERT_TRUE(edge); // cat in
170 plan_.EdgeFinished(edge);
172 edge = plan_.FindWork();
173 ASSERT_TRUE(edge); // cat mid
174 plan_.EdgeFinished(edge);
176 edge = plan_.FindWork();
177 ASSERT_TRUE(edge); // cat mid
178 plan_.EdgeFinished(edge);
180 edge = plan_.FindWork();
181 ASSERT_TRUE(edge); // cat a1 a2
182 plan_.EdgeFinished(edge);
184 edge = plan_.FindWork();
185 ASSERT_FALSE(edge); // done
188 TEST_F(PlanTest, DependencyCycle) {
190 "build out: cat mid\n"
191 "build mid: cat in\n"
192 "build in: cat pre\n"
193 "build pre: cat out\n");
194 GetNode("out")->MarkDirty();
195 GetNode("mid")->MarkDirty();
196 GetNode("in")->MarkDirty();
197 GetNode("pre")->MarkDirty();
200 EXPECT_FALSE(plan_.AddTarget(GetNode("out"), &err));
201 ASSERT_EQ("dependency cycle: out -> mid -> in -> pre -> out", err);
204 void PlanTest::TestPoolWithDepthOne(const char* test_case) {
205 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, test_case));
206 GetNode("out1")->MarkDirty();
207 GetNode("out2")->MarkDirty();
209 EXPECT_TRUE(plan_.AddTarget(GetNode("out1"), &err));
211 EXPECT_TRUE(plan_.AddTarget(GetNode("out2"), &err));
213 ASSERT_TRUE(plan_.more_to_do());
215 Edge* edge = plan_.FindWork();
217 ASSERT_EQ("in", edge->inputs_[0]->path());
218 ASSERT_EQ("out1", edge->outputs_[0]->path());
220 // This will be false since poolcat is serialized
221 ASSERT_FALSE(plan_.FindWork());
223 plan_.EdgeFinished(edge);
225 edge = plan_.FindWork();
227 ASSERT_EQ("in", edge->inputs_[0]->path());
228 ASSERT_EQ("out2", edge->outputs_[0]->path());
230 ASSERT_FALSE(plan_.FindWork());
232 plan_.EdgeFinished(edge);
234 ASSERT_FALSE(plan_.more_to_do());
235 edge = plan_.FindWork();
239 TEST_F(PlanTest, PoolWithDepthOne) {
240 TestPoolWithDepthOne(
244 " command = cat $in > $out\n"
246 "build out1: poolcat in\n"
247 "build out2: poolcat in\n");
250 TEST_F(PlanTest, ConsolePool) {
251 TestPoolWithDepthOne(
253 " command = cat $in > $out\n"
255 "build out1: poolcat in\n"
256 "build out2: poolcat in\n");
259 TEST_F(PlanTest, PoolsWithDepthTwo) {
260 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
266 " command = cat $in > $out\n"
269 " command = cat $in > $out\n"
271 "build out1: foocat in\n"
272 "build out2: foocat in\n"
273 "build out3: foocat in\n"
274 "build outb1: bazcat in\n"
275 "build outb2: bazcat in\n"
276 "build outb3: bazcat in\n"
278 "build allTheThings: cat out1 out2 out3 outb1 outb2 outb3\n"
280 // Mark all the out* nodes dirty
281 for (int i = 0; i < 3; ++i) {
282 GetNode("out" + string(1, '1' + static_cast<char>(i)))->MarkDirty();
283 GetNode("outb" + string(1, '1' + static_cast<char>(i)))->MarkDirty();
285 GetNode("allTheThings")->MarkDirty();
288 EXPECT_TRUE(plan_.AddTarget(GetNode("allTheThings"), &err));
292 FindWorkSorted(&edges, 5);
294 for (int i = 0; i < 4; ++i) {
295 Edge *edge = edges[i];
296 ASSERT_EQ("in", edge->inputs_[0]->path());
297 string base_name(i < 2 ? "out" : "outb");
298 ASSERT_EQ(base_name + string(1, '1' + (i % 2)), edge->outputs_[0]->path());
301 // outb3 is exempt because it has an empty pool
302 Edge* edge = edges[4];
304 ASSERT_EQ("in", edge->inputs_[0]->path());
305 ASSERT_EQ("outb3", edge->outputs_[0]->path());
308 plan_.EdgeFinished(edges.front());
311 // out3 should be available
312 Edge* out3 = plan_.FindWork();
314 ASSERT_EQ("in", out3->inputs_[0]->path());
315 ASSERT_EQ("out3", out3->outputs_[0]->path());
317 ASSERT_FALSE(plan_.FindWork());
319 plan_.EdgeFinished(out3);
321 ASSERT_FALSE(plan_.FindWork());
323 for (deque<Edge*>::iterator it = edges.begin(); it != edges.end(); ++it) {
324 plan_.EdgeFinished(*it);
327 Edge* last = plan_.FindWork();
329 ASSERT_EQ("allTheThings", last->outputs_[0]->path());
331 plan_.EdgeFinished(last);
333 ASSERT_FALSE(plan_.more_to_do());
334 ASSERT_FALSE(plan_.FindWork());
337 TEST_F(PlanTest, PoolWithRedundantEdges) {
338 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
342 " command = touch foo.cpp\n"
344 " command = touch bar.cpp\n"
346 " command = echo $out > $out\n"
347 "build foo.cpp.obj: echo foo.cpp || foo.cpp\n"
349 "build bar.cpp.obj: echo bar.cpp || bar.cpp\n"
351 "build libfoo.a: echo foo.cpp.obj bar.cpp.obj\n"
352 "build foo.cpp: gen_foo\n"
353 "build bar.cpp: gen_bar\n"
354 "build all: phony libfoo.a\n"));
355 GetNode("foo.cpp")->MarkDirty();
356 GetNode("foo.cpp.obj")->MarkDirty();
357 GetNode("bar.cpp")->MarkDirty();
358 GetNode("bar.cpp.obj")->MarkDirty();
359 GetNode("libfoo.a")->MarkDirty();
360 GetNode("all")->MarkDirty();
362 EXPECT_TRUE(plan_.AddTarget(GetNode("all"), &err));
364 ASSERT_TRUE(plan_.more_to_do());
368 deque<Edge*> initial_edges;
369 FindWorkSorted(&initial_edges, 2);
371 edge = initial_edges[1]; // Foo first
372 ASSERT_EQ("foo.cpp", edge->outputs_[0]->path());
373 plan_.EdgeFinished(edge);
375 edge = plan_.FindWork();
377 ASSERT_FALSE(plan_.FindWork());
378 ASSERT_EQ("foo.cpp", edge->inputs_[0]->path());
379 ASSERT_EQ("foo.cpp", edge->inputs_[1]->path());
380 ASSERT_EQ("foo.cpp.obj", edge->outputs_[0]->path());
381 plan_.EdgeFinished(edge);
383 edge = initial_edges[0]; // Now for bar
384 ASSERT_EQ("bar.cpp", edge->outputs_[0]->path());
385 plan_.EdgeFinished(edge);
387 edge = plan_.FindWork();
389 ASSERT_FALSE(plan_.FindWork());
390 ASSERT_EQ("bar.cpp", edge->inputs_[0]->path());
391 ASSERT_EQ("bar.cpp", edge->inputs_[1]->path());
392 ASSERT_EQ("bar.cpp.obj", edge->outputs_[0]->path());
393 plan_.EdgeFinished(edge);
395 edge = plan_.FindWork();
397 ASSERT_FALSE(plan_.FindWork());
398 ASSERT_EQ("foo.cpp.obj", edge->inputs_[0]->path());
399 ASSERT_EQ("bar.cpp.obj", edge->inputs_[1]->path());
400 ASSERT_EQ("libfoo.a", edge->outputs_[0]->path());
401 plan_.EdgeFinished(edge);
403 edge = plan_.FindWork();
405 ASSERT_FALSE(plan_.FindWork());
406 ASSERT_EQ("libfoo.a", edge->inputs_[0]->path());
407 ASSERT_EQ("all", edge->outputs_[0]->path());
408 plan_.EdgeFinished(edge);
410 edge = plan_.FindWork();
412 ASSERT_FALSE(plan_.more_to_do());
415 /// Fake implementation of CommandRunner, useful for tests.
416 struct FakeCommandRunner : public CommandRunner {
417 explicit FakeCommandRunner(VirtualFileSystem* fs) :
418 last_command_(NULL), fs_(fs) {}
420 // CommandRunner impl
421 virtual bool CanRunMore();
422 virtual bool StartCommand(Edge* edge);
423 virtual bool WaitForCommand(Result* result);
424 virtual vector<Edge*> GetActiveEdges();
425 virtual void Abort();
427 vector<string> commands_ran_;
429 VirtualFileSystem* fs_;
432 struct BuildTest : public StateTestWithBuiltinRules, public BuildLogUser {
433 BuildTest() : config_(MakeConfig()), command_runner_(&fs_),
434 builder_(&state_, config_, NULL, NULL, &fs_),
438 virtual void SetUp() {
439 StateTestWithBuiltinRules::SetUp();
441 builder_.command_runner_.reset(&command_runner_);
443 "build cat1: cat in1\n"
444 "build cat2: cat in1 in2\n"
445 "build cat12: cat cat1 cat2\n");
447 fs_.Create("in1", "");
448 fs_.Create("in2", "");
452 builder_.command_runner_.release();
455 virtual bool IsPathDead(StringPiece s) const { return false; }
457 /// Rebuild target in the 'working tree' (fs_).
458 /// State of command_runner_ and logs contents (if specified) ARE MODIFIED.
459 /// Handy to check for NOOP builds, and higher-level rebuild tests.
460 void RebuildTarget(const string& target, const char* manifest,
461 const char* log_path = NULL,
462 const char* deps_path = NULL);
464 // Mark a path dirty.
465 void Dirty(const string& path);
467 BuildConfig MakeConfig() {
469 config.verbosity = BuildConfig::QUIET;
474 FakeCommandRunner command_runner_;
475 VirtualFileSystem fs_;
481 void BuildTest::RebuildTarget(const string& target, const char* manifest,
482 const char* log_path, const char* deps_path) {
484 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
485 AssertParse(&state, manifest);
488 BuildLog build_log, *pbuild_log = NULL;
490 ASSERT_TRUE(build_log.Load(log_path, &err));
491 ASSERT_TRUE(build_log.OpenForWrite(log_path, *this, &err));
493 pbuild_log = &build_log;
496 DepsLog deps_log, *pdeps_log = NULL;
498 ASSERT_TRUE(deps_log.Load(deps_path, &state, &err));
499 ASSERT_TRUE(deps_log.OpenForWrite(deps_path, &err));
501 pdeps_log = &deps_log;
504 Builder builder(&state, config_, pbuild_log, pdeps_log, &fs_);
505 EXPECT_TRUE(builder.AddTarget(target, &err));
507 command_runner_.commands_ran_.clear();
508 builder.command_runner_.reset(&command_runner_);
509 if (!builder.AlreadyUpToDate()) {
510 bool build_res = builder.Build(&err);
511 EXPECT_TRUE(build_res);
513 builder.command_runner_.release();
516 bool FakeCommandRunner::CanRunMore() {
517 // Only run one at a time.
518 return last_command_ == NULL;
521 bool FakeCommandRunner::StartCommand(Edge* edge) {
522 assert(!last_command_);
523 commands_ran_.push_back(edge->EvaluateCommand());
524 if (edge->rule().name() == "cat" ||
525 edge->rule().name() == "cat_rsp" ||
526 edge->rule().name() == "cat_rsp_out" ||
527 edge->rule().name() == "cc" ||
528 edge->rule().name() == "touch" ||
529 edge->rule().name() == "touch-interrupt") {
530 for (vector<Node*>::iterator out = edge->outputs_.begin();
531 out != edge->outputs_.end(); ++out) {
532 fs_->Create((*out)->path(), "");
534 } else if (edge->rule().name() == "true" ||
535 edge->rule().name() == "fail" ||
536 edge->rule().name() == "interrupt" ||
537 edge->rule().name() == "console") {
538 // Don't do anything.
540 printf("unknown command\n");
544 last_command_ = edge;
548 bool FakeCommandRunner::WaitForCommand(Result* result) {
552 Edge* edge = last_command_;
555 if (edge->rule().name() == "interrupt" ||
556 edge->rule().name() == "touch-interrupt") {
557 result->status = ExitInterrupted;
561 if (edge->rule().name() == "console") {
562 if (edge->use_console())
563 result->status = ExitSuccess;
565 result->status = ExitFailure;
566 last_command_ = NULL;
570 if (edge->rule().name() == "fail")
571 result->status = ExitFailure;
573 result->status = ExitSuccess;
574 last_command_ = NULL;
578 vector<Edge*> FakeCommandRunner::GetActiveEdges() {
581 edges.push_back(last_command_);
585 void FakeCommandRunner::Abort() {
586 last_command_ = NULL;
589 void BuildTest::Dirty(const string& path) {
590 Node* node = GetNode(path);
593 // If it's an input file, mark that we've already stat()ed it and
595 if (!node->in_edge())
599 TEST_F(BuildTest, NoWork) {
601 EXPECT_TRUE(builder_.AlreadyUpToDate());
604 TEST_F(BuildTest, OneStep) {
605 // Given a dirty target with one ready input,
606 // we should rebuild the target.
609 EXPECT_TRUE(builder_.AddTarget("cat1", &err));
611 EXPECT_TRUE(builder_.Build(&err));
614 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
615 EXPECT_EQ("cat in1 > cat1", command_runner_.commands_ran_[0]);
618 TEST_F(BuildTest, OneStep2) {
619 // Given a target with one dirty input,
620 // we should rebuild the target.
623 EXPECT_TRUE(builder_.AddTarget("cat1", &err));
625 EXPECT_TRUE(builder_.Build(&err));
628 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
629 EXPECT_EQ("cat in1 > cat1", command_runner_.commands_ran_[0]);
632 TEST_F(BuildTest, TwoStep) {
634 EXPECT_TRUE(builder_.AddTarget("cat12", &err));
636 EXPECT_TRUE(builder_.Build(&err));
638 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
639 // Depending on how the pointers work out, we could've ran
640 // the first two commands in either order.
641 EXPECT_TRUE((command_runner_.commands_ran_[0] == "cat in1 > cat1" &&
642 command_runner_.commands_ran_[1] == "cat in1 in2 > cat2") ||
643 (command_runner_.commands_ran_[1] == "cat in1 > cat1" &&
644 command_runner_.commands_ran_[0] == "cat in1 in2 > cat2"));
646 EXPECT_EQ("cat cat1 cat2 > cat12", command_runner_.commands_ran_[2]);
650 // Modifying in2 requires rebuilding one intermediate file
651 // and the final file.
652 fs_.Create("in2", "");
654 EXPECT_TRUE(builder_.AddTarget("cat12", &err));
656 EXPECT_TRUE(builder_.Build(&err));
658 ASSERT_EQ(5u, command_runner_.commands_ran_.size());
659 EXPECT_EQ("cat in1 in2 > cat2", command_runner_.commands_ran_[3]);
660 EXPECT_EQ("cat cat1 cat2 > cat12", command_runner_.commands_ran_[4]);
663 TEST_F(BuildTest, TwoOutputs) {
664 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
666 " command = touch $out\n"
667 "build out1 out2: touch in.txt\n"));
669 fs_.Create("in.txt", "");
672 EXPECT_TRUE(builder_.AddTarget("out1", &err));
674 EXPECT_TRUE(builder_.Build(&err));
676 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
677 EXPECT_EQ("touch out1 out2", command_runner_.commands_ran_[0]);
681 // https://github.com/martine/ninja/issues/148
682 TEST_F(BuildTest, MultiOutIn) {
683 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
685 " command = touch $out\n"
686 "build in1 otherfile: touch in\n"
687 "build out: touch in | in1\n"));
689 fs_.Create("in", "");
691 fs_.Create("in1", "");
694 EXPECT_TRUE(builder_.AddTarget("out", &err));
696 EXPECT_TRUE(builder_.Build(&err));
700 TEST_F(BuildTest, Chain) {
701 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
705 "build c5: cat c4\n"));
707 fs_.Create("c1", "");
710 EXPECT_TRUE(builder_.AddTarget("c5", &err));
712 EXPECT_TRUE(builder_.Build(&err));
714 ASSERT_EQ(4u, command_runner_.commands_ran_.size());
717 command_runner_.commands_ran_.clear();
719 EXPECT_TRUE(builder_.AddTarget("c5", &err));
721 EXPECT_TRUE(builder_.AlreadyUpToDate());
725 fs_.Create("c3", "");
727 command_runner_.commands_ran_.clear();
729 EXPECT_TRUE(builder_.AddTarget("c5", &err));
731 EXPECT_FALSE(builder_.AlreadyUpToDate());
732 EXPECT_TRUE(builder_.Build(&err));
733 ASSERT_EQ(2u, command_runner_.commands_ran_.size()); // 3->4, 4->5
736 TEST_F(BuildTest, MissingInput) {
737 // Input is referenced by build file, but no rule for it.
740 EXPECT_FALSE(builder_.AddTarget("cat1", &err));
741 EXPECT_EQ("'in1', needed by 'cat1', missing and no known rule to make it",
745 TEST_F(BuildTest, MissingTarget) {
746 // Target is not referenced by build file.
748 EXPECT_FALSE(builder_.AddTarget("meow", &err));
749 EXPECT_EQ("unknown target: 'meow'", err);
752 TEST_F(BuildTest, MakeDirs) {
756 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
757 "build subdir\\dir2\\file: cat in1\n"));
759 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
760 "build subdir/dir2/file: cat in1\n"));
762 EXPECT_TRUE(builder_.AddTarget("subdir/dir2/file", &err));
765 EXPECT_TRUE(builder_.Build(&err));
767 ASSERT_EQ(2u, fs_.directories_made_.size());
768 EXPECT_EQ("subdir", fs_.directories_made_[0]);
769 EXPECT_EQ("subdir/dir2", fs_.directories_made_[1]);
772 TEST_F(BuildTest, DepFileMissing) {
774 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
775 "rule cc\n command = cc $in\n depfile = $out.d\n"
776 "build fo$ o.o: cc foo.c\n"));
777 fs_.Create("foo.c", "");
779 EXPECT_TRUE(builder_.AddTarget("fo o.o", &err));
781 ASSERT_EQ(1u, fs_.files_read_.size());
782 EXPECT_EQ("fo o.o.d", fs_.files_read_[0]);
785 TEST_F(BuildTest, DepFileOK) {
787 int orig_edges = state_.edges_.size();
788 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
789 "rule cc\n command = cc $in\n depfile = $out.d\n"
790 "build foo.o: cc foo.c\n"));
791 Edge* edge = state_.edges_.back();
793 fs_.Create("foo.c", "");
794 GetNode("bar.h")->MarkDirty(); // Mark bar.h as missing.
795 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
796 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
798 ASSERT_EQ(1u, fs_.files_read_.size());
799 EXPECT_EQ("foo.o.d", fs_.files_read_[0]);
801 // Expect three new edges: one generating foo.o, and two more from
802 // loading the depfile.
803 ASSERT_EQ(orig_edges + 3, (int)state_.edges_.size());
804 // Expect our edge to now have three inputs: foo.c and two headers.
805 ASSERT_EQ(3u, edge->inputs_.size());
807 // Expect the command line we generate to only use the original input.
808 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
811 TEST_F(BuildTest, DepFileParseError) {
813 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
814 "rule cc\n command = cc $in\n depfile = $out.d\n"
815 "build foo.o: cc foo.c\n"));
816 fs_.Create("foo.c", "");
817 fs_.Create("foo.o.d", "randomtext\n");
818 EXPECT_FALSE(builder_.AddTarget("foo.o", &err));
819 EXPECT_EQ("expected depfile 'foo.o.d' to mention 'foo.o', got 'randomtext'",
823 TEST_F(BuildTest, OrderOnlyDeps) {
825 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
826 "rule cc\n command = cc $in\n depfile = $out.d\n"
827 "build foo.o: cc foo.c || otherfile\n"));
828 Edge* edge = state_.edges_.back();
830 fs_.Create("foo.c", "");
831 fs_.Create("otherfile", "");
832 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
833 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
836 // One explicit, two implicit, one order only.
837 ASSERT_EQ(4u, edge->inputs_.size());
838 EXPECT_EQ(2, edge->implicit_deps_);
839 EXPECT_EQ(1, edge->order_only_deps_);
840 // Verify the inputs are in the order we expect
841 // (explicit then implicit then orderonly).
842 EXPECT_EQ("foo.c", edge->inputs_[0]->path());
843 EXPECT_EQ("blah.h", edge->inputs_[1]->path());
844 EXPECT_EQ("bar.h", edge->inputs_[2]->path());
845 EXPECT_EQ("otherfile", edge->inputs_[3]->path());
847 // Expect the command line we generate to only use the original input.
848 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
850 // explicit dep dirty, expect a rebuild.
851 EXPECT_TRUE(builder_.Build(&err));
853 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
857 // Recreate the depfile, as it should have been deleted by the build.
858 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
860 // implicit dep dirty, expect a rebuild.
861 fs_.Create("blah.h", "");
862 fs_.Create("bar.h", "");
863 command_runner_.commands_ran_.clear();
865 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
866 EXPECT_TRUE(builder_.Build(&err));
868 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
872 // Recreate the depfile, as it should have been deleted by the build.
873 fs_.Create("foo.o.d", "foo.o: blah.h bar.h\n");
875 // order only dep dirty, no rebuild.
876 fs_.Create("otherfile", "");
877 command_runner_.commands_ran_.clear();
879 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
881 EXPECT_TRUE(builder_.AlreadyUpToDate());
883 // implicit dep missing, expect rebuild.
884 fs_.RemoveFile("bar.h");
885 command_runner_.commands_ran_.clear();
887 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
888 EXPECT_TRUE(builder_.Build(&err));
890 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
893 TEST_F(BuildTest, RebuildOrderOnlyDeps) {
895 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
896 "rule cc\n command = cc $in\n"
897 "rule true\n command = true\n"
898 "build oo.h: cc oo.h.in\n"
899 "build foo.o: cc foo.c || oo.h\n"));
901 fs_.Create("foo.c", "");
902 fs_.Create("oo.h.in", "");
904 // foo.o and order-only dep dirty, build both.
905 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
906 EXPECT_TRUE(builder_.Build(&err));
908 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
910 // all clean, no rebuild.
911 command_runner_.commands_ran_.clear();
913 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
915 EXPECT_TRUE(builder_.AlreadyUpToDate());
917 // order-only dep missing, build it only.
918 fs_.RemoveFile("oo.h");
919 command_runner_.commands_ran_.clear();
921 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
922 EXPECT_TRUE(builder_.Build(&err));
924 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
925 ASSERT_EQ("cc oo.h.in", command_runner_.commands_ran_[0]);
929 // order-only dep dirty, build it only.
930 fs_.Create("oo.h.in", "");
931 command_runner_.commands_ran_.clear();
933 EXPECT_TRUE(builder_.AddTarget("foo.o", &err));
934 EXPECT_TRUE(builder_.Build(&err));
936 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
937 ASSERT_EQ("cc oo.h.in", command_runner_.commands_ran_[0]);
941 TEST_F(BuildTest, DepFileCanonicalize) {
943 int orig_edges = state_.edges_.size();
944 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
945 "rule cc\n command = cc $in\n depfile = $out.d\n"
946 "build gen/stuff\\things/foo.o: cc x\\y/z\\foo.c\n"));
947 Edge* edge = state_.edges_.back();
949 fs_.Create("x/y/z/foo.c", "");
950 GetNode("bar.h")->MarkDirty(); // Mark bar.h as missing.
951 // Note, different slashes from manifest.
952 fs_.Create("gen/stuff\\things/foo.o.d",
953 "gen\\stuff\\things\\foo.o: blah.h bar.h\n");
954 EXPECT_TRUE(builder_.AddTarget("gen/stuff/things/foo.o", &err));
956 ASSERT_EQ(1u, fs_.files_read_.size());
957 // The depfile path does not get Canonicalize as it seems unnecessary.
958 EXPECT_EQ("gen/stuff\\things/foo.o.d", fs_.files_read_[0]);
960 // Expect three new edges: one generating foo.o, and two more from
961 // loading the depfile.
962 ASSERT_EQ(orig_edges + 3, (int)state_.edges_.size());
963 // Expect our edge to now have three inputs: foo.c and two headers.
964 ASSERT_EQ(3u, edge->inputs_.size());
966 // Expect the command line we generate to only use the original input, and
967 // using the slashes from the manifest.
968 ASSERT_EQ("cc x\\y/z\\foo.c", edge->EvaluateCommand());
972 TEST_F(BuildTest, Phony) {
974 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
975 "build out: cat bar.cc\n"
976 "build all: phony out\n"));
977 fs_.Create("bar.cc", "");
979 EXPECT_TRUE(builder_.AddTarget("all", &err));
982 // Only one command to run, because phony runs no command.
983 EXPECT_FALSE(builder_.AlreadyUpToDate());
984 EXPECT_TRUE(builder_.Build(&err));
986 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
989 TEST_F(BuildTest, PhonyNoWork) {
991 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
992 "build out: cat bar.cc\n"
993 "build all: phony out\n"));
994 fs_.Create("bar.cc", "");
995 fs_.Create("out", "");
997 EXPECT_TRUE(builder_.AddTarget("all", &err));
999 EXPECT_TRUE(builder_.AlreadyUpToDate());
1002 TEST_F(BuildTest, Fail) {
1003 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1006 "build out1: fail\n"));
1009 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1012 EXPECT_FALSE(builder_.Build(&err));
1013 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1014 ASSERT_EQ("subcommand failed", err);
1017 TEST_F(BuildTest, SwallowFailures) {
1018 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1021 "build out1: fail\n"
1022 "build out2: fail\n"
1023 "build out3: fail\n"
1024 "build all: phony out1 out2 out3\n"));
1026 // Swallow two failures, die on the third.
1027 config_.failures_allowed = 3;
1030 EXPECT_TRUE(builder_.AddTarget("all", &err));
1033 EXPECT_FALSE(builder_.Build(&err));
1034 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1035 ASSERT_EQ("subcommands failed", err);
1038 TEST_F(BuildTest, SwallowFailuresLimit) {
1039 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1042 "build out1: fail\n"
1043 "build out2: fail\n"
1044 "build out3: fail\n"
1045 "build final: cat out1 out2 out3\n"));
1047 // Swallow ten failures; we should stop before building final.
1048 config_.failures_allowed = 11;
1051 EXPECT_TRUE(builder_.AddTarget("final", &err));
1054 EXPECT_FALSE(builder_.Build(&err));
1055 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1056 ASSERT_EQ("cannot make progress due to previous errors", err);
1059 struct BuildWithLogTest : public BuildTest {
1060 BuildWithLogTest() {
1061 builder_.SetBuildLog(&build_log_);
1064 BuildLog build_log_;
1067 TEST_F(BuildWithLogTest, NotInLogButOnDisk) {
1068 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1071 "build out1: cc in\n"));
1073 // Create input/output that would be considered up to date when
1074 // not considering the command line hash.
1075 fs_.Create("in", "");
1076 fs_.Create("out1", "");
1079 // Because it's not in the log, it should not be up-to-date until
1081 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1082 EXPECT_FALSE(builder_.AlreadyUpToDate());
1084 command_runner_.commands_ran_.clear();
1087 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1088 EXPECT_TRUE(builder_.Build(&err));
1089 EXPECT_TRUE(builder_.AlreadyUpToDate());
1092 TEST_F(BuildWithLogTest, RestatTest) {
1093 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1100 "build out1: cc in\n"
1101 "build out2: true out1\n"
1102 "build out3: cat out2\n"));
1104 fs_.Create("out1", "");
1105 fs_.Create("out2", "");
1106 fs_.Create("out3", "");
1110 fs_.Create("in", "");
1112 // Do a pre-build so that there's commands in the log for the outputs,
1113 // otherwise, the lack of an entry in the build log will cause out3 to rebuild
1114 // regardless of restat.
1116 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1118 EXPECT_TRUE(builder_.Build(&err));
1120 EXPECT_EQ("[3/3]", builder_.status_->FormatProgressStatus("[%s/%t]"));
1121 command_runner_.commands_ran_.clear();
1126 fs_.Create("in", "");
1127 // "cc" touches out1, so we should build out2. But because "true" does not
1128 // touch out2, we should cancel the build of out3.
1129 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1131 EXPECT_TRUE(builder_.Build(&err));
1132 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1134 // If we run again, it should be a no-op, because the build log has recorded
1135 // that we've already built out2 with an input timestamp of 2 (from out1).
1136 command_runner_.commands_ran_.clear();
1138 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1140 EXPECT_TRUE(builder_.AlreadyUpToDate());
1144 fs_.Create("in", "");
1146 // The build log entry should not, however, prevent us from rebuilding out2
1148 command_runner_.commands_ran_.clear();
1150 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1152 EXPECT_TRUE(builder_.Build(&err));
1153 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1156 TEST_F(BuildWithLogTest, RestatMissingFile) {
1157 // If a restat rule doesn't create its output, and the output didn't
1158 // exist before the rule was run, consider that behavior equivalent
1159 // to a rule that doesn't modify its existent output file.
1161 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1167 "build out1: true in\n"
1168 "build out2: cc out1\n"));
1170 fs_.Create("in", "");
1171 fs_.Create("out2", "");
1173 // Do a pre-build so that there's commands in the log for the outputs,
1174 // otherwise, the lack of an entry in the build log will cause out2 to rebuild
1175 // regardless of restat.
1177 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1179 EXPECT_TRUE(builder_.Build(&err));
1181 command_runner_.commands_ran_.clear();
1185 fs_.Create("in", "");
1186 fs_.Create("out2", "");
1188 // Run a build, expect only the first command to run.
1189 // It doesn't touch its output (due to being the "true" command), so
1190 // we shouldn't run the dependent build.
1191 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1193 EXPECT_TRUE(builder_.Build(&err));
1194 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1197 TEST_F(BuildWithLogTest, RestatSingleDependentOutputDirty) {
1198 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1203 " command = touch\n"
1204 "build out1: true in\n"
1205 "build out2 out3: touch out1\n"
1206 "build out4: touch out2\n"
1209 // Create the necessary files
1210 fs_.Create("in", "");
1213 EXPECT_TRUE(builder_.AddTarget("out4", &err));
1215 EXPECT_TRUE(builder_.Build(&err));
1217 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1220 fs_.Create("in", "");
1221 fs_.RemoveFile("out3");
1223 // Since "in" is missing, out1 will be built. Since "out3" is missing,
1224 // out2 and out3 will be built even though "in" is not touched when built.
1225 // Then, since out2 is rebuilt, out4 should be rebuilt -- the restat on the
1226 // "true" rule should not lead to the "touch" edge writing out2 and out3 being
1228 command_runner_.commands_ran_.clear();
1230 EXPECT_TRUE(builder_.AddTarget("out4", &err));
1232 EXPECT_TRUE(builder_.Build(&err));
1234 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1237 // Test scenario, in which an input file is removed, but output isn't changed
1238 // https://github.com/martine/ninja/issues/295
1239 TEST_F(BuildWithLogTest, RestatMissingInput) {
1240 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1243 " depfile = $out.d\n"
1247 "build out1: true in\n"
1248 "build out2: cc out1\n"));
1250 // Create all necessary files
1251 fs_.Create("in", "");
1253 // The implicit dependencies and the depfile itself
1254 // are newer than the output
1255 TimeStamp restat_mtime = fs_.Tick();
1256 fs_.Create("out1.d", "out1: will.be.deleted restat.file\n");
1257 fs_.Create("will.be.deleted", "");
1258 fs_.Create("restat.file", "");
1260 // Run the build, out1 and out2 get built
1262 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1264 EXPECT_TRUE(builder_.Build(&err));
1265 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1267 // See that an entry in the logfile is created, capturing
1269 BuildLog::LogEntry* log_entry = build_log_.LookupByOutput("out1");
1270 ASSERT_TRUE(NULL != log_entry);
1271 ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
1273 // Now remove a file, referenced from depfile, so that target becomes
1274 // dirty, but the output does not change
1275 fs_.RemoveFile("will.be.deleted");
1277 // Trigger the build again - only out1 gets built
1278 command_runner_.commands_ran_.clear();
1280 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1282 EXPECT_TRUE(builder_.Build(&err));
1283 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1285 // Check that the logfile entry remains correctly set
1286 log_entry = build_log_.LookupByOutput("out1");
1287 ASSERT_TRUE(NULL != log_entry);
1288 ASSERT_EQ(restat_mtime, log_entry->restat_mtime);
1291 struct BuildDryRun : public BuildWithLogTest {
1293 config_.dry_run = true;
1297 TEST_F(BuildDryRun, AllCommandsShown) {
1298 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1305 "build out1: cc in\n"
1306 "build out2: true out1\n"
1307 "build out3: cat out2\n"));
1309 fs_.Create("out1", "");
1310 fs_.Create("out2", "");
1311 fs_.Create("out3", "");
1315 fs_.Create("in", "");
1317 // "cc" touches out1, so we should build out2. But because "true" does not
1318 // touch out2, we should cancel the build of out3.
1320 EXPECT_TRUE(builder_.AddTarget("out3", &err));
1322 EXPECT_TRUE(builder_.Build(&err));
1323 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1326 // Test that RSP files are created when & where appropriate and deleted after
1327 // successful execution.
1328 TEST_F(BuildTest, RspFileSuccess)
1330 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1332 " command = cat $rspfile > $out\n"
1333 " rspfile = $rspfile\n"
1334 " rspfile_content = $long_command\n"
1335 "rule cat_rsp_out\n"
1336 " command = cat $rspfile > $out\n"
1337 " rspfile = $out.rsp\n"
1338 " rspfile_content = $long_command\n"
1339 "build out1: cat in\n"
1340 "build out2: cat_rsp in\n"
1341 " rspfile = out 2.rsp\n"
1342 " long_command = Some very long command\n"
1343 "build out$ 3: cat_rsp_out in\n"
1344 " long_command = Some very long command\n"));
1346 fs_.Create("out1", "");
1347 fs_.Create("out2", "");
1348 fs_.Create("out 3", "");
1352 fs_.Create("in", "");
1355 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1357 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1359 EXPECT_TRUE(builder_.AddTarget("out 3", &err));
1362 size_t files_created = fs_.files_created_.size();
1363 size_t files_removed = fs_.files_removed_.size();
1365 EXPECT_TRUE(builder_.Build(&err));
1366 ASSERT_EQ(3u, command_runner_.commands_ran_.size());
1368 // The RSP files were created
1369 ASSERT_EQ(files_created + 2, fs_.files_created_.size());
1370 ASSERT_EQ(1u, fs_.files_created_.count("out 2.rsp"));
1371 ASSERT_EQ(1u, fs_.files_created_.count("out 3.rsp"));
1373 // The RSP files were removed
1374 ASSERT_EQ(files_removed + 2, fs_.files_removed_.size());
1375 ASSERT_EQ(1u, fs_.files_removed_.count("out 2.rsp"));
1376 ASSERT_EQ(1u, fs_.files_removed_.count("out 3.rsp"));
1379 // Test that RSP file is created but not removed for commands, which fail
1380 TEST_F(BuildTest, RspFileFailure) {
1381 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1384 " rspfile = $rspfile\n"
1385 " rspfile_content = $long_command\n"
1386 "build out: fail in\n"
1387 " rspfile = out.rsp\n"
1388 " long_command = Another very long command\n"));
1390 fs_.Create("out", "");
1392 fs_.Create("in", "");
1395 EXPECT_TRUE(builder_.AddTarget("out", &err));
1398 size_t files_created = fs_.files_created_.size();
1399 size_t files_removed = fs_.files_removed_.size();
1401 EXPECT_FALSE(builder_.Build(&err));
1402 ASSERT_EQ("subcommand failed", err);
1403 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1405 // The RSP file was created
1406 ASSERT_EQ(files_created + 1, fs_.files_created_.size());
1407 ASSERT_EQ(1u, fs_.files_created_.count("out.rsp"));
1409 // The RSP file was NOT removed
1410 ASSERT_EQ(files_removed, fs_.files_removed_.size());
1411 ASSERT_EQ(0u, fs_.files_removed_.count("out.rsp"));
1413 // The RSP file contains what it should
1414 ASSERT_EQ("Another very long command", fs_.files_["out.rsp"].contents);
1417 // Test that contens of the RSP file behaves like a regular part of
1418 // command line, i.e. triggers a rebuild if changed
1419 TEST_F(BuildWithLogTest, RspFileCmdLineChange) {
1420 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1422 " command = cat $rspfile > $out\n"
1423 " rspfile = $rspfile\n"
1424 " rspfile_content = $long_command\n"
1425 "build out: cat_rsp in\n"
1426 " rspfile = out.rsp\n"
1427 " long_command = Original very long command\n"));
1429 fs_.Create("out", "");
1431 fs_.Create("in", "");
1434 EXPECT_TRUE(builder_.AddTarget("out", &err));
1437 // 1. Build for the 1st time (-> populate log)
1438 EXPECT_TRUE(builder_.Build(&err));
1439 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1441 // 2. Build again (no change)
1442 command_runner_.commands_ran_.clear();
1444 EXPECT_TRUE(builder_.AddTarget("out", &err));
1446 ASSERT_TRUE(builder_.AlreadyUpToDate());
1448 // 3. Alter the entry in the logfile
1449 // (to simulate a change in the command line between 2 builds)
1450 BuildLog::LogEntry* log_entry = build_log_.LookupByOutput("out");
1451 ASSERT_TRUE(NULL != log_entry);
1452 ASSERT_NO_FATAL_FAILURE(AssertHash(
1453 "cat out.rsp > out;rspfile=Original very long command",
1454 log_entry->command_hash));
1455 log_entry->command_hash++; // Change the command hash to something else.
1456 // Now expect the target to be rebuilt
1457 command_runner_.commands_ran_.clear();
1459 EXPECT_TRUE(builder_.AddTarget("out", &err));
1461 EXPECT_TRUE(builder_.Build(&err));
1462 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1465 TEST_F(BuildTest, InterruptCleanup) {
1466 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1468 " command = interrupt\n"
1469 "rule touch-interrupt\n"
1470 " command = touch-interrupt\n"
1471 "build out1: interrupt in1\n"
1472 "build out2: touch-interrupt in2\n"));
1474 fs_.Create("out1", "");
1475 fs_.Create("out2", "");
1477 fs_.Create("in1", "");
1478 fs_.Create("in2", "");
1480 // An untouched output of an interrupted command should be retained.
1482 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1484 EXPECT_FALSE(builder_.Build(&err));
1485 EXPECT_EQ("interrupted by user", err);
1487 EXPECT_GT(fs_.Stat("out1"), 0);
1490 // A touched output of an interrupted command should be deleted.
1491 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1493 EXPECT_FALSE(builder_.Build(&err));
1494 EXPECT_EQ("interrupted by user", err);
1496 EXPECT_EQ(0, fs_.Stat("out2"));
1499 TEST_F(BuildTest, PhonyWithNoInputs) {
1500 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1501 "build nonexistent: phony\n"
1502 "build out1: cat || nonexistent\n"
1503 "build out2: cat nonexistent\n"));
1504 fs_.Create("out1", "");
1505 fs_.Create("out2", "");
1507 // out1 should be up to date even though its input is dirty, because its
1508 // order-only dependency has nothing to do.
1510 EXPECT_TRUE(builder_.AddTarget("out1", &err));
1512 EXPECT_TRUE(builder_.AlreadyUpToDate());
1514 // out2 should still be out of date though, because its input is dirty.
1516 command_runner_.commands_ran_.clear();
1518 EXPECT_TRUE(builder_.AddTarget("out2", &err));
1520 EXPECT_TRUE(builder_.Build(&err));
1522 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1525 TEST_F(BuildTest, DepsGccWithEmptyDepfileErrorsOut) {
1526 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1530 "build out: cc\n"));
1534 EXPECT_TRUE(builder_.AddTarget("out", &err));
1536 EXPECT_FALSE(builder_.AlreadyUpToDate());
1538 EXPECT_FALSE(builder_.Build(&err));
1539 ASSERT_EQ("subcommand failed", err);
1540 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1543 TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
1544 EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
1545 status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]"));
1548 TEST_F(BuildTest, FailedDepsParse) {
1549 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1550 "build bad_deps.o: cat in1\n"
1552 " depfile = in1.d\n"));
1555 EXPECT_TRUE(builder_.AddTarget("bad_deps.o", &err));
1558 // These deps will fail to parse, as they should only have one
1559 // path to the left of the colon.
1560 fs_.Create("in1.d", "AAA BBB");
1562 EXPECT_FALSE(builder_.Build(&err));
1563 EXPECT_EQ("subcommand failed", err);
1566 /// Tests of builds involving deps logs necessarily must span
1567 /// multiple builds. We reuse methods on BuildTest but not the
1568 /// builder_ it sets up, because we want pristine objects for
1570 struct BuildWithDepsLogTest : public BuildTest {
1571 BuildWithDepsLogTest() {}
1573 virtual void SetUp() {
1576 temp_dir_.CreateAndEnter("BuildWithDepsLogTest");
1579 virtual void TearDown() {
1580 temp_dir_.Cleanup();
1583 ScopedTempDir temp_dir_;
1585 /// Shadow parent class builder_ so we don't accidentally use it.
1589 /// Run a straightforwad build where the deps log is used.
1590 TEST_F(BuildWithDepsLogTest, Straightforward) {
1592 // Note: in1 was created by the superclass SetUp().
1593 const char* manifest =
1594 "build out: cat in1\n"
1596 " depfile = in1.d\n";
1599 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1600 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1602 // Run the build once, everything should be ok.
1604 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1607 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1608 builder.command_runner_.reset(&command_runner_);
1609 EXPECT_TRUE(builder.AddTarget("out", &err));
1611 fs_.Create("in1.d", "out: in2");
1612 EXPECT_TRUE(builder.Build(&err));
1615 // The deps file should have been removed.
1616 EXPECT_EQ(0, fs_.Stat("in1.d"));
1617 // Recreate it for the next step.
1618 fs_.Create("in1.d", "out: in2");
1620 builder.command_runner_.release();
1625 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1626 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1628 // Touch the file only mentioned in the deps.
1630 fs_.Create("in2", "");
1632 // Run the build again.
1634 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1635 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1637 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1638 builder.command_runner_.reset(&command_runner_);
1639 command_runner_.commands_ran_.clear();
1640 EXPECT_TRUE(builder.AddTarget("out", &err));
1642 EXPECT_TRUE(builder.Build(&err));
1645 // We should have rebuilt the output due to in2 being
1647 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1649 builder.command_runner_.release();
1653 /// Verify that obsolete dependency info causes a rebuild.
1654 /// 1) Run a successful build where everything has time t, record deps.
1655 /// 2) Move input/output to time t+1 -- despite files in alignment,
1656 /// should still need to rebuild due to deps at older time.
1657 TEST_F(BuildWithDepsLogTest, ObsoleteDeps) {
1659 // Note: in1 was created by the superclass SetUp().
1660 const char* manifest =
1661 "build out: cat in1\n"
1663 " depfile = in1.d\n";
1665 // Run an ordinary build that gathers dependencies.
1666 fs_.Create("in1", "");
1667 fs_.Create("in1.d", "out: ");
1670 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1671 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1673 // Run the build once, everything should be ok.
1675 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1678 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1679 builder.command_runner_.reset(&command_runner_);
1680 EXPECT_TRUE(builder.AddTarget("out", &err));
1682 EXPECT_TRUE(builder.Build(&err));
1686 builder.command_runner_.release();
1689 // Push all files one tick forward so that only the deps are out
1692 fs_.Create("in1", "");
1693 fs_.Create("out", "");
1695 // The deps file should have been removed, so no need to timestamp it.
1696 EXPECT_EQ(0, fs_.Stat("in1.d"));
1700 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1701 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1704 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1705 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1707 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1708 builder.command_runner_.reset(&command_runner_);
1709 command_runner_.commands_ran_.clear();
1710 EXPECT_TRUE(builder.AddTarget("out", &err));
1713 // Recreate the deps file here because the build expects them to exist.
1714 fs_.Create("in1.d", "out: ");
1716 EXPECT_TRUE(builder.Build(&err));
1719 // We should have rebuilt the output due to the deps being
1721 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1723 builder.command_runner_.release();
1727 TEST_F(BuildWithDepsLogTest, DepsIgnoredInDryRun) {
1728 const char* manifest =
1729 "build out: cat in1\n"
1731 " depfile = in1.d\n";
1733 fs_.Create("out", "");
1735 fs_.Create("in1", "");
1738 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1739 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1741 // The deps log is NULL in dry runs.
1742 config_.dry_run = true;
1743 Builder builder(&state, config_, NULL, NULL, &fs_);
1744 builder.command_runner_.reset(&command_runner_);
1745 command_runner_.commands_ran_.clear();
1748 EXPECT_TRUE(builder.AddTarget("out", &err));
1750 EXPECT_TRUE(builder.Build(&err));
1751 ASSERT_EQ(1u, command_runner_.commands_ran_.size());
1753 builder.command_runner_.release();
1756 /// Check that a restat rule generating a header cancels compilations correctly.
1757 TEST_F(BuildTest, RestatDepfileDependency) {
1758 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
1760 " command = true\n" // Would be "write if out-of-date" in reality.
1762 "build header.h: true header.in\n"
1763 "build out: cat in1\n"
1764 " depfile = in1.d\n"));
1766 fs_.Create("header.h", "");
1767 fs_.Create("in1.d", "out: header.h");
1769 fs_.Create("header.in", "");
1772 EXPECT_TRUE(builder_.AddTarget("out", &err));
1774 EXPECT_TRUE(builder_.Build(&err));
1778 /// Check that a restat rule generating a header cancels compilations correctly,
1780 TEST_F(BuildWithDepsLogTest, RestatDepfileDependencyDepsLog) {
1782 // Note: in1 was created by the superclass SetUp().
1783 const char* manifest =
1785 " command = true\n" // Would be "write if out-of-date" in reality.
1787 "build header.h: true header.in\n"
1788 "build out: cat in1\n"
1790 " depfile = in1.d\n";
1793 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1794 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1796 // Run the build once, everything should be ok.
1798 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1801 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1802 builder.command_runner_.reset(&command_runner_);
1803 EXPECT_TRUE(builder.AddTarget("out", &err));
1805 fs_.Create("in1.d", "out: header.h");
1806 EXPECT_TRUE(builder.Build(&err));
1810 builder.command_runner_.release();
1815 ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
1816 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1818 // Touch the input of the restat rule.
1820 fs_.Create("header.in", "");
1822 // Run the build again.
1824 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1825 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1827 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1828 builder.command_runner_.reset(&command_runner_);
1829 command_runner_.commands_ran_.clear();
1830 EXPECT_TRUE(builder.AddTarget("out", &err));
1832 EXPECT_TRUE(builder.Build(&err));
1835 // Rule "true" should have run again, but the build of "out" should have
1836 // been cancelled due to restat propagating through the depfile header.
1837 EXPECT_EQ(1u, command_runner_.commands_ran_.size());
1839 builder.command_runner_.release();
1843 TEST_F(BuildWithDepsLogTest, DepFileOKDepsLog) {
1845 const char* manifest =
1846 "rule cc\n command = cc $in\n depfile = $out.d\n deps = gcc\n"
1847 "build fo$ o.o: cc foo.c\n";
1849 fs_.Create("foo.c", "");
1853 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1855 // Run the build once, everything should be ok.
1857 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1860 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1861 builder.command_runner_.reset(&command_runner_);
1862 EXPECT_TRUE(builder.AddTarget("fo o.o", &err));
1864 fs_.Create("fo o.o.d", "fo\\ o.o: blah.h bar.h\n");
1865 EXPECT_TRUE(builder.Build(&err));
1869 builder.command_runner_.release();
1874 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1877 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1878 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1881 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1882 builder.command_runner_.reset(&command_runner_);
1884 Edge* edge = state.edges_.back();
1886 state.GetNode("bar.h", 0)->MarkDirty(); // Mark bar.h as missing.
1887 EXPECT_TRUE(builder.AddTarget("fo o.o", &err));
1890 // Expect three new edges: one generating fo o.o, and two more from
1891 // loading the depfile.
1892 ASSERT_EQ(3u, state.edges_.size());
1893 // Expect our edge to now have three inputs: foo.c and two headers.
1894 ASSERT_EQ(3u, edge->inputs_.size());
1896 // Expect the command line we generate to only use the original input.
1897 ASSERT_EQ("cc foo.c", edge->EvaluateCommand());
1900 builder.command_runner_.release();
1905 TEST_F(BuildWithDepsLogTest, DepFileDepsLogCanonicalize) {
1907 const char* manifest =
1908 "rule cc\n command = cc $in\n depfile = $out.d\n deps = gcc\n"
1909 "build a/b\\c\\d/e/fo$ o.o: cc x\\y/z\\foo.c\n";
1911 fs_.Create("x/y/z/foo.c", "");
1915 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1917 // Run the build once, everything should be ok.
1919 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1922 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1923 builder.command_runner_.reset(&command_runner_);
1924 EXPECT_TRUE(builder.AddTarget("a/b/c/d/e/fo o.o", &err));
1926 // Note, different slashes from manifest.
1927 fs_.Create("a/b\\c\\d/e/fo o.o.d",
1928 "a\\b\\c\\d\\e\\fo\\ o.o: blah.h bar.h\n");
1929 EXPECT_TRUE(builder.Build(&err));
1933 builder.command_runner_.release();
1938 ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
1941 ASSERT_TRUE(deps_log.Load("ninja_deps", &state, &err));
1942 ASSERT_TRUE(deps_log.OpenForWrite("ninja_deps", &err));
1945 Builder builder(&state, config_, NULL, &deps_log, &fs_);
1946 builder.command_runner_.reset(&command_runner_);
1948 Edge* edge = state.edges_.back();
1950 state.GetNode("bar.h", 0)->MarkDirty(); // Mark bar.h as missing.
1951 EXPECT_TRUE(builder.AddTarget("a/b/c/d/e/fo o.o", &err));
1954 // Expect three new edges: one generating fo o.o, and two more from
1955 // loading the depfile.
1956 ASSERT_EQ(3u, state.edges_.size());
1957 // Expect our edge to now have three inputs: foo.c and two headers.
1958 ASSERT_EQ(3u, edge->inputs_.size());
1960 // Expect the command line we generate to only use the original input.
1961 // Note, slashes from manifest, not .d.
1962 ASSERT_EQ("cc x\\y/z\\foo.c", edge->EvaluateCommand());
1965 builder.command_runner_.release();
1970 /// Check that a restat rule doesn't clear an edge if the depfile is missing.
1971 /// Follows from: https://github.com/martine/ninja/issues/603
1972 TEST_F(BuildTest, RestatMissingDepfile) {
1973 const char* manifest =
1975 " command = true\n" // Would be "write if out-of-date" in reality.
1977 "build header.h: true header.in\n"
1978 "build out: cat header.h\n"
1979 " depfile = out.d\n";
1981 fs_.Create("header.h", "");
1983 fs_.Create("out", "");
1984 fs_.Create("header.in", "");
1986 // Normally, only 'header.h' would be rebuilt, as
1987 // its rule doesn't touch the output and has 'restat=1' set.
1988 // But we are also missing the depfile for 'out',
1989 // which should force its command to run anyway!
1990 RebuildTarget("out", manifest);
1991 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
1994 /// Check that a restat rule doesn't clear an edge if the deps are missing.
1995 /// https://github.com/martine/ninja/issues/603
1996 TEST_F(BuildWithDepsLogTest, RestatMissingDepfileDepslog) {
1998 const char* manifest =
2000 " command = true\n" // Would be "write if out-of-date" in reality.
2002 "build header.h: true header.in\n"
2003 "build out: cat header.h\n"
2005 " depfile = out.d\n";
2007 // Build once to populate ninja deps logs from out.d
2008 fs_.Create("header.in", "");
2009 fs_.Create("out.d", "out: header.h");
2010 fs_.Create("header.h", "");
2012 RebuildTarget("out", manifest, "build_log", "ninja_deps");
2013 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
2015 // Sanity: this rebuild should be NOOP
2016 RebuildTarget("out", manifest, "build_log", "ninja_deps");
2017 ASSERT_EQ(0u, command_runner_.commands_ran_.size());
2019 // Touch 'header.in', blank dependencies log (create a different one).
2020 // Building header.h triggers 'restat' outputs cleanup.
2021 // Validate that out is rebuilt netherless, as deps are missing.
2023 fs_.Create("header.in", "");
2025 // (switch to a new blank deps_log "ninja_deps2")
2026 RebuildTarget("out", manifest, "build_log", "ninja_deps2");
2027 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
2029 // Sanity: this build should be NOOP
2030 RebuildTarget("out", manifest, "build_log", "ninja_deps2");
2031 ASSERT_EQ(0u, command_runner_.commands_ran_.size());
2033 // Check that invalidating deps by target timestamp also works here
2034 // Repeat the test but touch target instead of blanking the log.
2036 fs_.Create("header.in", "");
2037 fs_.Create("out", "");
2038 RebuildTarget("out", manifest, "build_log", "ninja_deps2");
2039 ASSERT_EQ(2u, command_runner_.commands_ran_.size());
2041 // And this build should be NOOP again
2042 RebuildTarget("out", manifest, "build_log", "ninja_deps2");
2043 ASSERT_EQ(0u, command_runner_.commands_ran_.size());
2046 TEST_F(BuildTest, Console) {
2047 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
2049 " command = console\n"
2051 "build cons: console in.txt\n"));
2053 fs_.Create("in.txt", "");
2056 EXPECT_TRUE(builder_.AddTarget("cons", &err));
2058 EXPECT_TRUE(builder_.Build(&err));
2060 ASSERT_EQ(1u, command_runner_.commands_ran_.size());