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