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_) {}
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 #if _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