1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14 
15 #include "graph.h"
16 #include "build.h"
17 
18 #include "test.h"
19 
20 using namespace std;
21 
22 struct GraphTest : public StateTestWithBuiltinRules {
GraphTestGraphTest23   GraphTest() : scan_(&state_, NULL, NULL, &fs_, NULL) {}
24 
25   VirtualFileSystem fs_;
26   DependencyScan scan_;
27 };
28 
TEST_F(GraphTest,MissingImplicit)29 TEST_F(GraphTest, MissingImplicit) {
30   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
31 "build out: cat in | implicit\n"));
32   fs_.Create("in", "");
33   fs_.Create("out", "");
34 
35   string err;
36   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out"), &err));
37   ASSERT_EQ("", err);
38 
39   // A missing implicit dep *should* make the output dirty.
40   // (In fact, a build will fail.)
41   // This is a change from prior semantics of ninja.
42   EXPECT_TRUE(GetNode("out")->dirty());
43 }
44 
TEST_F(GraphTest,ModifiedImplicit)45 TEST_F(GraphTest, ModifiedImplicit) {
46   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
47 "build out: cat in | implicit\n"));
48   fs_.Create("in", "");
49   fs_.Create("out", "");
50   fs_.Tick();
51   fs_.Create("implicit", "");
52 
53   string err;
54   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out"), &err));
55   ASSERT_EQ("", err);
56 
57   // A modified implicit dep should make the output dirty.
58   EXPECT_TRUE(GetNode("out")->dirty());
59 }
60 
TEST_F(GraphTest,FunkyMakefilePath)61 TEST_F(GraphTest, FunkyMakefilePath) {
62   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
63 "rule catdep\n"
64 "  depfile = $out.d\n"
65 "  command = cat $in > $out\n"
66 "build out.o: catdep foo.cc\n"));
67   fs_.Create("foo.cc",  "");
68   fs_.Create("out.o.d", "out.o: ./foo/../implicit.h\n");
69   fs_.Create("out.o", "");
70   fs_.Tick();
71   fs_.Create("implicit.h", "");
72 
73   string err;
74   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
75   ASSERT_EQ("", err);
76 
77   // implicit.h has changed, though our depfile refers to it with a
78   // non-canonical path; we should still find it.
79   EXPECT_TRUE(GetNode("out.o")->dirty());
80 }
81 
TEST_F(GraphTest,ExplicitImplicit)82 TEST_F(GraphTest, ExplicitImplicit) {
83   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
84 "rule catdep\n"
85 "  depfile = $out.d\n"
86 "  command = cat $in > $out\n"
87 "build implicit.h: cat data\n"
88 "build out.o: catdep foo.cc || implicit.h\n"));
89   fs_.Create("implicit.h", "");
90   fs_.Create("foo.cc", "");
91   fs_.Create("out.o.d", "out.o: implicit.h\n");
92   fs_.Create("out.o", "");
93   fs_.Tick();
94   fs_.Create("data", "");
95 
96   string err;
97   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
98   ASSERT_EQ("", err);
99 
100   // We have both an implicit and an explicit dep on implicit.h.
101   // The implicit dep should "win" (in the sense that it should cause
102   // the output to be dirty).
103   EXPECT_TRUE(GetNode("out.o")->dirty());
104 }
105 
TEST_F(GraphTest,ImplicitOutputParse)106 TEST_F(GraphTest, ImplicitOutputParse) {
107   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
108 "build out | out.imp: cat in\n"));
109 
110   Edge* edge = GetNode("out")->in_edge();
111   EXPECT_EQ(2, edge->outputs_.size());
112   EXPECT_EQ("out", edge->outputs_[0]->path());
113   EXPECT_EQ("out.imp", edge->outputs_[1]->path());
114   EXPECT_EQ(1, edge->implicit_outs_);
115   EXPECT_EQ(edge, GetNode("out.imp")->in_edge());
116 }
117 
TEST_F(GraphTest,ImplicitOutputMissing)118 TEST_F(GraphTest, ImplicitOutputMissing) {
119   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
120 "build out | out.imp: cat in\n"));
121   fs_.Create("in", "");
122   fs_.Create("out", "");
123 
124   string err;
125   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out"), &err));
126   ASSERT_EQ("", err);
127 
128   EXPECT_TRUE(GetNode("out")->dirty());
129   EXPECT_TRUE(GetNode("out.imp")->dirty());
130 }
131 
TEST_F(GraphTest,ImplicitOutputOutOfDate)132 TEST_F(GraphTest, ImplicitOutputOutOfDate) {
133   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
134 "build out | out.imp: cat in\n"));
135   fs_.Create("out.imp", "");
136   fs_.Tick();
137   fs_.Create("in", "");
138   fs_.Create("out", "");
139 
140   string err;
141   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out"), &err));
142   ASSERT_EQ("", err);
143 
144   EXPECT_TRUE(GetNode("out")->dirty());
145   EXPECT_TRUE(GetNode("out.imp")->dirty());
146 }
147 
TEST_F(GraphTest,ImplicitOutputOnlyParse)148 TEST_F(GraphTest, ImplicitOutputOnlyParse) {
149   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
150 "build | out.imp: cat in\n"));
151 
152   Edge* edge = GetNode("out.imp")->in_edge();
153   EXPECT_EQ(1, edge->outputs_.size());
154   EXPECT_EQ("out.imp", edge->outputs_[0]->path());
155   EXPECT_EQ(1, edge->implicit_outs_);
156   EXPECT_EQ(edge, GetNode("out.imp")->in_edge());
157 }
158 
TEST_F(GraphTest,ImplicitOutputOnlyMissing)159 TEST_F(GraphTest, ImplicitOutputOnlyMissing) {
160   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
161 "build | out.imp: cat in\n"));
162   fs_.Create("in", "");
163 
164   string err;
165   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.imp"), &err));
166   ASSERT_EQ("", err);
167 
168   EXPECT_TRUE(GetNode("out.imp")->dirty());
169 }
170 
TEST_F(GraphTest,ImplicitOutputOnlyOutOfDate)171 TEST_F(GraphTest, ImplicitOutputOnlyOutOfDate) {
172   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
173 "build | out.imp: cat in\n"));
174   fs_.Create("out.imp", "");
175   fs_.Tick();
176   fs_.Create("in", "");
177 
178   string err;
179   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.imp"), &err));
180   ASSERT_EQ("", err);
181 
182   EXPECT_TRUE(GetNode("out.imp")->dirty());
183 }
184 
TEST_F(GraphTest,PathWithCurrentDirectory)185 TEST_F(GraphTest, PathWithCurrentDirectory) {
186   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
187 "rule catdep\n"
188 "  depfile = $out.d\n"
189 "  command = cat $in > $out\n"
190 "build ./out.o: catdep ./foo.cc\n"));
191   fs_.Create("foo.cc", "");
192   fs_.Create("out.o.d", "out.o: foo.cc\n");
193   fs_.Create("out.o", "");
194 
195   string err;
196   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
197   ASSERT_EQ("", err);
198 
199   EXPECT_FALSE(GetNode("out.o")->dirty());
200 }
201 
TEST_F(GraphTest,RootNodes)202 TEST_F(GraphTest, RootNodes) {
203   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
204 "build out1: cat in1\n"
205 "build mid1: cat in1\n"
206 "build out2: cat mid1\n"
207 "build out3 out4: cat mid1\n"));
208 
209   string err;
210   vector<Node*> root_nodes = state_.RootNodes(&err);
211   EXPECT_EQ(4u, root_nodes.size());
212   for (size_t i = 0; i < root_nodes.size(); ++i) {
213     string name = root_nodes[i]->path();
214     EXPECT_EQ("out", name.substr(0, 3));
215   }
216 }
217 
TEST_F(GraphTest,VarInOutPathEscaping)218 TEST_F(GraphTest, VarInOutPathEscaping) {
219   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
220 "build a$ b: cat no'space with$ space$$ no\"space2\n"));
221 
222   Edge* edge = GetNode("a b")->in_edge();
223 #ifdef _WIN32
224   EXPECT_EQ("cat no'space \"with space$\" \"no\\\"space2\" > \"a b\"",
225       edge->EvaluateCommand());
226 #else
227   EXPECT_EQ("cat 'no'\\''space' 'with space$' 'no\"space2' > 'a b'",
228       edge->EvaluateCommand());
229 #endif
230 }
231 
232 // Regression test for https://github.com/ninja-build/ninja/issues/380
TEST_F(GraphTest,DepfileWithCanonicalizablePath)233 TEST_F(GraphTest, DepfileWithCanonicalizablePath) {
234   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
235 "rule catdep\n"
236 "  depfile = $out.d\n"
237 "  command = cat $in > $out\n"
238 "build ./out.o: catdep ./foo.cc\n"));
239   fs_.Create("foo.cc", "");
240   fs_.Create("out.o.d", "out.o: bar/../foo.cc\n");
241   fs_.Create("out.o", "");
242 
243   string err;
244   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
245   ASSERT_EQ("", err);
246 
247   EXPECT_FALSE(GetNode("out.o")->dirty());
248 }
249 
250 // Regression test for https://github.com/ninja-build/ninja/issues/404
TEST_F(GraphTest,DepfileRemoved)251 TEST_F(GraphTest, DepfileRemoved) {
252   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
253 "rule catdep\n"
254 "  depfile = $out.d\n"
255 "  command = cat $in > $out\n"
256 "build ./out.o: catdep ./foo.cc\n"));
257   fs_.Create("foo.h", "");
258   fs_.Create("foo.cc", "");
259   fs_.Tick();
260   fs_.Create("out.o.d", "out.o: foo.h\n");
261   fs_.Create("out.o", "");
262 
263   string err;
264   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
265   ASSERT_EQ("", err);
266   EXPECT_FALSE(GetNode("out.o")->dirty());
267 
268   state_.Reset();
269   fs_.RemoveFile("out.o.d");
270   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
271   ASSERT_EQ("", err);
272   EXPECT_TRUE(GetNode("out.o")->dirty());
273 }
274 
275 // Check that rule-level variables are in scope for eval.
TEST_F(GraphTest,RuleVariablesInScope)276 TEST_F(GraphTest, RuleVariablesInScope) {
277   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
278 "rule r\n"
279 "  depfile = x\n"
280 "  command = depfile is $depfile\n"
281 "build out: r in\n"));
282   Edge* edge = GetNode("out")->in_edge();
283   EXPECT_EQ("depfile is x", edge->EvaluateCommand());
284 }
285 
286 // Check that build statements can override rule builtins like depfile.
TEST_F(GraphTest,DepfileOverride)287 TEST_F(GraphTest, DepfileOverride) {
288   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
289 "rule r\n"
290 "  depfile = x\n"
291 "  command = unused\n"
292 "build out: r in\n"
293 "  depfile = y\n"));
294   Edge* edge = GetNode("out")->in_edge();
295   EXPECT_EQ("y", edge->GetBinding("depfile"));
296 }
297 
298 // Check that overridden values show up in expansion of rule-level bindings.
TEST_F(GraphTest,DepfileOverrideParent)299 TEST_F(GraphTest, DepfileOverrideParent) {
300   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
301 "rule r\n"
302 "  depfile = x\n"
303 "  command = depfile is $depfile\n"
304 "build out: r in\n"
305 "  depfile = y\n"));
306   Edge* edge = GetNode("out")->in_edge();
307   EXPECT_EQ("depfile is y", edge->GetBinding("command"));
308 }
309 
310 // Verify that building a nested phony rule prints "no work to do"
TEST_F(GraphTest,NestedPhonyPrintsDone)311 TEST_F(GraphTest, NestedPhonyPrintsDone) {
312   AssertParse(&state_,
313 "build n1: phony \n"
314 "build n2: phony n1\n"
315   );
316   string err;
317   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("n2"), &err));
318   ASSERT_EQ("", err);
319 
320   Plan plan_;
321   EXPECT_TRUE(plan_.AddTarget(GetNode("n2"), &err));
322   ASSERT_EQ("", err);
323 
324   EXPECT_EQ(0, plan_.command_edge_count());
325   ASSERT_FALSE(plan_.more_to_do());
326 }
327 
TEST_F(GraphTest,PhonySelfReferenceError)328 TEST_F(GraphTest, PhonySelfReferenceError) {
329   ManifestParserOptions parser_opts;
330   parser_opts.phony_cycle_action_ = kPhonyCycleActionError;
331   AssertParse(&state_,
332 "build a: phony a\n",
333   parser_opts);
334 
335   string err;
336   EXPECT_FALSE(scan_.RecomputeDirty(GetNode("a"), &err));
337   ASSERT_EQ("dependency cycle: a -> a [-w phonycycle=err]", err);
338 }
339 
TEST_F(GraphTest,DependencyCycle)340 TEST_F(GraphTest, DependencyCycle) {
341   AssertParse(&state_,
342 "build out: cat mid\n"
343 "build mid: cat in\n"
344 "build in: cat pre\n"
345 "build pre: cat out\n");
346 
347   string err;
348   EXPECT_FALSE(scan_.RecomputeDirty(GetNode("out"), &err));
349   ASSERT_EQ("dependency cycle: out -> mid -> in -> pre -> out", err);
350 }
351 
TEST_F(GraphTest,CycleInEdgesButNotInNodes1)352 TEST_F(GraphTest, CycleInEdgesButNotInNodes1) {
353   string err;
354   AssertParse(&state_,
355 "build a b: cat a\n");
356   EXPECT_FALSE(scan_.RecomputeDirty(GetNode("b"), &err));
357   ASSERT_EQ("dependency cycle: a -> a", err);
358 }
359 
TEST_F(GraphTest,CycleInEdgesButNotInNodes2)360 TEST_F(GraphTest, CycleInEdgesButNotInNodes2) {
361   string err;
362   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
363 "build b a: cat a\n"));
364   EXPECT_FALSE(scan_.RecomputeDirty(GetNode("b"), &err));
365   ASSERT_EQ("dependency cycle: a -> a", err);
366 }
367 
TEST_F(GraphTest,CycleInEdgesButNotInNodes3)368 TEST_F(GraphTest, CycleInEdgesButNotInNodes3) {
369   string err;
370   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
371 "build a b: cat c\n"
372 "build c: cat a\n"));
373   EXPECT_FALSE(scan_.RecomputeDirty(GetNode("b"), &err));
374   ASSERT_EQ("dependency cycle: a -> c -> a", err);
375 }
376 
TEST_F(GraphTest,CycleInEdgesButNotInNodes4)377 TEST_F(GraphTest, CycleInEdgesButNotInNodes4) {
378   string err;
379   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
380 "build d: cat c\n"
381 "build c: cat b\n"
382 "build b: cat a\n"
383 "build a e: cat d\n"
384 "build f: cat e\n"));
385   EXPECT_FALSE(scan_.RecomputeDirty(GetNode("f"), &err));
386   ASSERT_EQ("dependency cycle: a -> d -> c -> b -> a", err);
387 }
388 
389 // Verify that cycles in graphs with multiple outputs are handled correctly
390 // in RecomputeDirty() and don't cause deps to be loaded multiple times.
TEST_F(GraphTest,CycleWithLengthZeroFromDepfile)391 TEST_F(GraphTest, CycleWithLengthZeroFromDepfile) {
392   AssertParse(&state_,
393 "rule deprule\n"
394 "   depfile = dep.d\n"
395 "   command = unused\n"
396 "build a b: deprule\n"
397   );
398   fs_.Create("dep.d", "a: b\n");
399 
400   string err;
401   EXPECT_FALSE(scan_.RecomputeDirty(GetNode("a"), &err));
402   ASSERT_EQ("dependency cycle: b -> b", err);
403 
404   // Despite the depfile causing edge to be a cycle (it has outputs a and b,
405   // but the depfile also adds b as an input), the deps should have been loaded
406   // only once:
407   Edge* edge = GetNode("a")->in_edge();
408   EXPECT_EQ(1, edge->inputs_.size());
409   EXPECT_EQ("b", edge->inputs_[0]->path());
410 }
411 
412 // Like CycleWithLengthZeroFromDepfile but with a higher cycle length.
TEST_F(GraphTest,CycleWithLengthOneFromDepfile)413 TEST_F(GraphTest, CycleWithLengthOneFromDepfile) {
414   AssertParse(&state_,
415 "rule deprule\n"
416 "   depfile = dep.d\n"
417 "   command = unused\n"
418 "rule r\n"
419 "   command = unused\n"
420 "build a b: deprule\n"
421 "build c: r b\n"
422   );
423   fs_.Create("dep.d", "a: c\n");
424 
425   string err;
426   EXPECT_FALSE(scan_.RecomputeDirty(GetNode("a"), &err));
427   ASSERT_EQ("dependency cycle: b -> c -> b", err);
428 
429   // Despite the depfile causing edge to be a cycle (|edge| has outputs a and b,
430   // but c's in_edge has b as input but the depfile also adds |edge| as
431   // output)), the deps should have been loaded only once:
432   Edge* edge = GetNode("a")->in_edge();
433   EXPECT_EQ(1, edge->inputs_.size());
434   EXPECT_EQ("c", edge->inputs_[0]->path());
435 }
436 
437 // Like CycleWithLengthOneFromDepfile but building a node one hop away from
438 // the cycle.
TEST_F(GraphTest,CycleWithLengthOneFromDepfileOneHopAway)439 TEST_F(GraphTest, CycleWithLengthOneFromDepfileOneHopAway) {
440   AssertParse(&state_,
441 "rule deprule\n"
442 "   depfile = dep.d\n"
443 "   command = unused\n"
444 "rule r\n"
445 "   command = unused\n"
446 "build a b: deprule\n"
447 "build c: r b\n"
448 "build d: r a\n"
449   );
450   fs_.Create("dep.d", "a: c\n");
451 
452   string err;
453   EXPECT_FALSE(scan_.RecomputeDirty(GetNode("d"), &err));
454   ASSERT_EQ("dependency cycle: b -> c -> b", err);
455 
456   // Despite the depfile causing edge to be a cycle (|edge| has outputs a and b,
457   // but c's in_edge has b as input but the depfile also adds |edge| as
458   // output)), the deps should have been loaded only once:
459   Edge* edge = GetNode("a")->in_edge();
460   EXPECT_EQ(1, edge->inputs_.size());
461   EXPECT_EQ("c", edge->inputs_[0]->path());
462 }
463 
464 #ifdef _WIN32
TEST_F(GraphTest,Decanonicalize)465 TEST_F(GraphTest, Decanonicalize) {
466   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
467 "build out\\out1: cat src\\in1\n"
468 "build out\\out2/out3\\out4: cat mid1\n"
469 "build out3 out4\\foo: cat mid1\n"));
470 
471   string err;
472   vector<Node*> root_nodes = state_.RootNodes(&err);
473   EXPECT_EQ(4u, root_nodes.size());
474   EXPECT_EQ(root_nodes[0]->path(), "out/out1");
475   EXPECT_EQ(root_nodes[1]->path(), "out/out2/out3/out4");
476   EXPECT_EQ(root_nodes[2]->path(), "out3");
477   EXPECT_EQ(root_nodes[3]->path(), "out4/foo");
478   EXPECT_EQ(root_nodes[0]->PathDecanonicalized(), "out\\out1");
479   EXPECT_EQ(root_nodes[1]->PathDecanonicalized(), "out\\out2/out3\\out4");
480   EXPECT_EQ(root_nodes[2]->PathDecanonicalized(), "out3");
481   EXPECT_EQ(root_nodes[3]->PathDecanonicalized(), "out4\\foo");
482 }
483 #endif
484 
TEST_F(GraphTest,DyndepLoadTrivial)485 TEST_F(GraphTest, DyndepLoadTrivial) {
486   AssertParse(&state_,
487 "rule r\n"
488 "  command = unused\n"
489 "build out: r in || dd\n"
490 "  dyndep = dd\n"
491   );
492   fs_.Create("dd",
493 "ninja_dyndep_version = 1\n"
494 "build out: dyndep\n"
495   );
496 
497   string err;
498   ASSERT_TRUE(GetNode("dd")->dyndep_pending());
499   EXPECT_TRUE(scan_.LoadDyndeps(GetNode("dd"), &err));
500   EXPECT_EQ("", err);
501   EXPECT_FALSE(GetNode("dd")->dyndep_pending());
502 
503   Edge* edge = GetNode("out")->in_edge();
504   ASSERT_EQ(1u, edge->outputs_.size());
505   EXPECT_EQ("out", edge->outputs_[0]->path());
506   ASSERT_EQ(2u, edge->inputs_.size());
507   EXPECT_EQ("in", edge->inputs_[0]->path());
508   EXPECT_EQ("dd", edge->inputs_[1]->path());
509   EXPECT_EQ(0u, edge->implicit_deps_);
510   EXPECT_EQ(1u, edge->order_only_deps_);
511   EXPECT_FALSE(edge->GetBindingBool("restat"));
512 }
513 
TEST_F(GraphTest,DyndepLoadMissingFile)514 TEST_F(GraphTest, DyndepLoadMissingFile) {
515   AssertParse(&state_,
516 "rule r\n"
517 "  command = unused\n"
518 "build out: r in || dd\n"
519 "  dyndep = dd\n"
520   );
521 
522   string err;
523   ASSERT_TRUE(GetNode("dd")->dyndep_pending());
524   EXPECT_FALSE(scan_.LoadDyndeps(GetNode("dd"), &err));
525   EXPECT_EQ("loading 'dd': No such file or directory", err);
526 }
527 
TEST_F(GraphTest,DyndepLoadMissingEntry)528 TEST_F(GraphTest, DyndepLoadMissingEntry) {
529   AssertParse(&state_,
530 "rule r\n"
531 "  command = unused\n"
532 "build out: r in || dd\n"
533 "  dyndep = dd\n"
534   );
535   fs_.Create("dd",
536 "ninja_dyndep_version = 1\n"
537   );
538 
539   string err;
540   ASSERT_TRUE(GetNode("dd")->dyndep_pending());
541   EXPECT_FALSE(scan_.LoadDyndeps(GetNode("dd"), &err));
542   EXPECT_EQ("'out' not mentioned in its dyndep file 'dd'", err);
543 }
544 
TEST_F(GraphTest,DyndepLoadExtraEntry)545 TEST_F(GraphTest, DyndepLoadExtraEntry) {
546   AssertParse(&state_,
547 "rule r\n"
548 "  command = unused\n"
549 "build out: r in || dd\n"
550 "  dyndep = dd\n"
551 "build out2: r in || dd\n"
552   );
553   fs_.Create("dd",
554 "ninja_dyndep_version = 1\n"
555 "build out: dyndep\n"
556 "build out2: dyndep\n"
557   );
558 
559   string err;
560   ASSERT_TRUE(GetNode("dd")->dyndep_pending());
561   EXPECT_FALSE(scan_.LoadDyndeps(GetNode("dd"), &err));
562   EXPECT_EQ("dyndep file 'dd' mentions output 'out2' whose build statement "
563             "does not have a dyndep binding for the file", err);
564 }
565 
TEST_F(GraphTest,DyndepLoadOutputWithMultipleRules1)566 TEST_F(GraphTest, DyndepLoadOutputWithMultipleRules1) {
567   AssertParse(&state_,
568 "rule r\n"
569 "  command = unused\n"
570 "build out1 | out-twice.imp: r in1\n"
571 "build out2: r in2 || dd\n"
572 "  dyndep = dd\n"
573   );
574   fs_.Create("dd",
575 "ninja_dyndep_version = 1\n"
576 "build out2 | out-twice.imp: dyndep\n"
577   );
578 
579   string err;
580   ASSERT_TRUE(GetNode("dd")->dyndep_pending());
581   EXPECT_FALSE(scan_.LoadDyndeps(GetNode("dd"), &err));
582   EXPECT_EQ("multiple rules generate out-twice.imp", err);
583 }
584 
TEST_F(GraphTest,DyndepLoadOutputWithMultipleRules2)585 TEST_F(GraphTest, DyndepLoadOutputWithMultipleRules2) {
586   AssertParse(&state_,
587 "rule r\n"
588 "  command = unused\n"
589 "build out1: r in1 || dd1\n"
590 "  dyndep = dd1\n"
591 "build out2: r in2 || dd2\n"
592 "  dyndep = dd2\n"
593   );
594   fs_.Create("dd1",
595 "ninja_dyndep_version = 1\n"
596 "build out1 | out-twice.imp: dyndep\n"
597   );
598   fs_.Create("dd2",
599 "ninja_dyndep_version = 1\n"
600 "build out2 | out-twice.imp: dyndep\n"
601   );
602 
603   string err;
604   ASSERT_TRUE(GetNode("dd1")->dyndep_pending());
605   EXPECT_TRUE(scan_.LoadDyndeps(GetNode("dd1"), &err));
606   EXPECT_EQ("", err);
607   ASSERT_TRUE(GetNode("dd2")->dyndep_pending());
608   EXPECT_FALSE(scan_.LoadDyndeps(GetNode("dd2"), &err));
609   EXPECT_EQ("multiple rules generate out-twice.imp", err);
610 }
611 
TEST_F(GraphTest,DyndepLoadMultiple)612 TEST_F(GraphTest, DyndepLoadMultiple) {
613   AssertParse(&state_,
614 "rule r\n"
615 "  command = unused\n"
616 "build out1: r in1 || dd\n"
617 "  dyndep = dd\n"
618 "build out2: r in2 || dd\n"
619 "  dyndep = dd\n"
620 "build outNot: r in3 || dd\n"
621   );
622   fs_.Create("dd",
623 "ninja_dyndep_version = 1\n"
624 "build out1 | out1imp: dyndep | in1imp\n"
625 "build out2: dyndep | in2imp\n"
626 "  restat = 1\n"
627   );
628 
629   string err;
630   ASSERT_TRUE(GetNode("dd")->dyndep_pending());
631   EXPECT_TRUE(scan_.LoadDyndeps(GetNode("dd"), &err));
632   EXPECT_EQ("", err);
633   EXPECT_FALSE(GetNode("dd")->dyndep_pending());
634 
635   Edge* edge1 = GetNode("out1")->in_edge();
636   ASSERT_EQ(2u, edge1->outputs_.size());
637   EXPECT_EQ("out1", edge1->outputs_[0]->path());
638   EXPECT_EQ("out1imp", edge1->outputs_[1]->path());
639   EXPECT_EQ(1u, edge1->implicit_outs_);
640   ASSERT_EQ(3u, edge1->inputs_.size());
641   EXPECT_EQ("in1", edge1->inputs_[0]->path());
642   EXPECT_EQ("in1imp", edge1->inputs_[1]->path());
643   EXPECT_EQ("dd", edge1->inputs_[2]->path());
644   EXPECT_EQ(1u, edge1->implicit_deps_);
645   EXPECT_EQ(1u, edge1->order_only_deps_);
646   EXPECT_FALSE(edge1->GetBindingBool("restat"));
647   EXPECT_EQ(edge1, GetNode("out1imp")->in_edge());
648   Node* in1imp = GetNode("in1imp");
649   ASSERT_EQ(1u, in1imp->out_edges().size());
650   EXPECT_EQ(edge1, in1imp->out_edges()[0]);
651 
652   Edge* edge2 = GetNode("out2")->in_edge();
653   ASSERT_EQ(1u, edge2->outputs_.size());
654   EXPECT_EQ("out2", edge2->outputs_[0]->path());
655   EXPECT_EQ(0u, edge2->implicit_outs_);
656   ASSERT_EQ(3u, edge2->inputs_.size());
657   EXPECT_EQ("in2", edge2->inputs_[0]->path());
658   EXPECT_EQ("in2imp", edge2->inputs_[1]->path());
659   EXPECT_EQ("dd", edge2->inputs_[2]->path());
660   EXPECT_EQ(1u, edge2->implicit_deps_);
661   EXPECT_EQ(1u, edge2->order_only_deps_);
662   EXPECT_TRUE(edge2->GetBindingBool("restat"));
663   Node* in2imp = GetNode("in2imp");
664   ASSERT_EQ(1u, in2imp->out_edges().size());
665   EXPECT_EQ(edge2, in2imp->out_edges()[0]);
666 }
667 
TEST_F(GraphTest,DyndepFileMissing)668 TEST_F(GraphTest, DyndepFileMissing) {
669   AssertParse(&state_,
670 "rule r\n"
671 "  command = unused\n"
672 "build out: r || dd\n"
673 "  dyndep = dd\n"
674   );
675 
676   string err;
677   EXPECT_FALSE(scan_.RecomputeDirty(GetNode("out"), &err));
678   ASSERT_EQ("loading 'dd': No such file or directory", err);
679 }
680 
TEST_F(GraphTest,DyndepFileError)681 TEST_F(GraphTest, DyndepFileError) {
682   AssertParse(&state_,
683 "rule r\n"
684 "  command = unused\n"
685 "build out: r || dd\n"
686 "  dyndep = dd\n"
687   );
688   fs_.Create("dd",
689 "ninja_dyndep_version = 1\n"
690   );
691 
692   string err;
693   EXPECT_FALSE(scan_.RecomputeDirty(GetNode("out"), &err));
694   ASSERT_EQ("'out' not mentioned in its dyndep file 'dd'", err);
695 }
696 
TEST_F(GraphTest,DyndepImplicitInputNewer)697 TEST_F(GraphTest, DyndepImplicitInputNewer) {
698   AssertParse(&state_,
699 "rule r\n"
700 "  command = unused\n"
701 "build out: r || dd\n"
702 "  dyndep = dd\n"
703   );
704   fs_.Create("dd",
705 "ninja_dyndep_version = 1\n"
706 "build out: dyndep | in\n"
707   );
708   fs_.Create("out", "");
709   fs_.Tick();
710   fs_.Create("in", "");
711 
712   string err;
713   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out"), &err));
714   ASSERT_EQ("", err);
715 
716   EXPECT_FALSE(GetNode("in")->dirty());
717   EXPECT_FALSE(GetNode("dd")->dirty());
718 
719   // "out" is dirty due to dyndep-specified implicit input
720   EXPECT_TRUE(GetNode("out")->dirty());
721 }
722 
TEST_F(GraphTest,DyndepFileReady)723 TEST_F(GraphTest, DyndepFileReady) {
724   AssertParse(&state_,
725 "rule r\n"
726 "  command = unused\n"
727 "build dd: r dd-in\n"
728 "build out: r || dd\n"
729 "  dyndep = dd\n"
730   );
731   fs_.Create("dd-in", "");
732   fs_.Create("dd",
733 "ninja_dyndep_version = 1\n"
734 "build out: dyndep | in\n"
735   );
736   fs_.Create("out", "");
737   fs_.Tick();
738   fs_.Create("in", "");
739 
740   string err;
741   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out"), &err));
742   ASSERT_EQ("", err);
743 
744   EXPECT_FALSE(GetNode("in")->dirty());
745   EXPECT_FALSE(GetNode("dd")->dirty());
746   EXPECT_TRUE(GetNode("dd")->in_edge()->outputs_ready());
747 
748   // "out" is dirty due to dyndep-specified implicit input
749   EXPECT_TRUE(GetNode("out")->dirty());
750 }
751 
TEST_F(GraphTest,DyndepFileNotClean)752 TEST_F(GraphTest, DyndepFileNotClean) {
753   AssertParse(&state_,
754 "rule r\n"
755 "  command = unused\n"
756 "build dd: r dd-in\n"
757 "build out: r || dd\n"
758 "  dyndep = dd\n"
759   );
760   fs_.Create("dd", "this-should-not-be-loaded");
761   fs_.Tick();
762   fs_.Create("dd-in", "");
763   fs_.Create("out", "");
764 
765   string err;
766   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out"), &err));
767   ASSERT_EQ("", err);
768 
769   EXPECT_TRUE(GetNode("dd")->dirty());
770   EXPECT_FALSE(GetNode("dd")->in_edge()->outputs_ready());
771 
772   // "out" is clean but not ready since "dd" is not ready
773   EXPECT_FALSE(GetNode("out")->dirty());
774   EXPECT_FALSE(GetNode("out")->in_edge()->outputs_ready());
775 }
776 
TEST_F(GraphTest,DyndepFileNotReady)777 TEST_F(GraphTest, DyndepFileNotReady) {
778   AssertParse(&state_,
779 "rule r\n"
780 "  command = unused\n"
781 "build tmp: r\n"
782 "build dd: r dd-in || tmp\n"
783 "build out: r || dd\n"
784 "  dyndep = dd\n"
785   );
786   fs_.Create("dd", "this-should-not-be-loaded");
787   fs_.Create("dd-in", "");
788   fs_.Tick();
789   fs_.Create("out", "");
790 
791   string err;
792   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out"), &err));
793   ASSERT_EQ("", err);
794 
795   EXPECT_FALSE(GetNode("dd")->dirty());
796   EXPECT_FALSE(GetNode("dd")->in_edge()->outputs_ready());
797   EXPECT_FALSE(GetNode("out")->dirty());
798   EXPECT_FALSE(GetNode("out")->in_edge()->outputs_ready());
799 }
800 
TEST_F(GraphTest,DyndepFileSecondNotReady)801 TEST_F(GraphTest, DyndepFileSecondNotReady) {
802   AssertParse(&state_,
803 "rule r\n"
804 "  command = unused\n"
805 "build dd1: r dd1-in\n"
806 "build dd2-in: r || dd1\n"
807 "  dyndep = dd1\n"
808 "build dd2: r dd2-in\n"
809 "build out: r || dd2\n"
810 "  dyndep = dd2\n"
811   );
812   fs_.Create("dd1", "");
813   fs_.Create("dd2", "");
814   fs_.Create("dd2-in", "");
815   fs_.Tick();
816   fs_.Create("dd1-in", "");
817   fs_.Create("out", "");
818 
819   string err;
820   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out"), &err));
821   ASSERT_EQ("", err);
822 
823   EXPECT_TRUE(GetNode("dd1")->dirty());
824   EXPECT_FALSE(GetNode("dd1")->in_edge()->outputs_ready());
825   EXPECT_FALSE(GetNode("dd2")->dirty());
826   EXPECT_FALSE(GetNode("dd2")->in_edge()->outputs_ready());
827   EXPECT_FALSE(GetNode("out")->dirty());
828   EXPECT_FALSE(GetNode("out")->in_edge()->outputs_ready());
829 }
830 
TEST_F(GraphTest,DyndepFileCircular)831 TEST_F(GraphTest, DyndepFileCircular) {
832   AssertParse(&state_,
833 "rule r\n"
834 "  command = unused\n"
835 "build out: r in || dd\n"
836 "  depfile = out.d\n"
837 "  dyndep = dd\n"
838 "build in: r circ\n"
839   );
840   fs_.Create("out.d", "out: inimp\n");
841   fs_.Create("dd",
842 "ninja_dyndep_version = 1\n"
843 "build out | circ: dyndep\n"
844   );
845   fs_.Create("out", "");
846 
847   Edge* edge = GetNode("out")->in_edge();
848   string err;
849   EXPECT_FALSE(scan_.RecomputeDirty(GetNode("out"), &err));
850   EXPECT_EQ("dependency cycle: circ -> in -> circ", err);
851 
852   // Verify that "out.d" was loaded exactly once despite
853   // circular reference discovered from dyndep file.
854   ASSERT_EQ(3u, edge->inputs_.size());
855   EXPECT_EQ("in", edge->inputs_[0]->path());
856   EXPECT_EQ("inimp", edge->inputs_[1]->path());
857   EXPECT_EQ("dd", edge->inputs_[2]->path());
858   EXPECT_EQ(1u, edge->implicit_deps_);
859   EXPECT_EQ(1u, edge->order_only_deps_);
860 }
861