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.
20 struct GraphTest : public StateTestWithBuiltinRules {
21 GraphTest() : scan_(&state_, NULL, NULL, &fs_) {}
23 VirtualFileSystem fs_;
27 TEST_F(GraphTest, MissingImplicit) {
28 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
29 "build out: cat in | implicit\n"));
31 fs_.Create("out", "");
34 EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out"), &err));
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());
43 TEST_F(GraphTest, ModifiedImplicit) {
44 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
45 "build out: cat in | implicit\n"));
47 fs_.Create("out", "");
49 fs_.Create("implicit", "");
52 EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out"), &err));
55 // A modified implicit dep should make the output dirty.
56 EXPECT_TRUE(GetNode("out")->dirty());
59 TEST_F(GraphTest, FunkyMakefilePath) {
60 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
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", "");
69 fs_.Create("implicit.h", "");
72 EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
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());
80 TEST_F(GraphTest, ExplicitImplicit) {
81 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
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", "");
92 fs_.Create("data", "");
95 EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
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());
104 TEST_F(GraphTest, ImplicitOutputParse) {
105 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
106 "build out | out.imp: cat in\n"));
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());
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", "");
123 EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out"), &err));
126 EXPECT_TRUE(GetNode("out")->dirty());
127 EXPECT_TRUE(GetNode("out.imp")->dirty());
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", "");
135 fs_.Create("in", "");
136 fs_.Create("out", "");
139 EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out"), &err));
142 EXPECT_TRUE(GetNode("out")->dirty());
143 EXPECT_TRUE(GetNode("out.imp")->dirty());
146 TEST_F(GraphTest, ImplicitOutputOnlyParse) {
147 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
148 "build | out.imp: cat in\n"));
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());
157 TEST_F(GraphTest, ImplicitOutputOnlyMissing) {
158 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
159 "build | out.imp: cat in\n"));
160 fs_.Create("in", "");
163 EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.imp"), &err));
166 EXPECT_TRUE(GetNode("out.imp")->dirty());
169 TEST_F(GraphTest, ImplicitOutputOnlyOutOfDate) {
170 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
171 "build | out.imp: cat in\n"));
172 fs_.Create("out.imp", "");
174 fs_.Create("in", "");
177 EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.imp"), &err));
180 EXPECT_TRUE(GetNode("out.imp")->dirty());
183 TEST_F(GraphTest, PathWithCurrentDirectory) {
184 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
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", "");
194 EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
197 EXPECT_FALSE(GetNode("out.o")->dirty());
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"));
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));
216 TEST_F(GraphTest, VarInOutPathEscaping) {
217 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
218 "build a$ b: cat no'space with$ space$$ no\"space2\n"));
220 Edge* edge = GetNode("a b")->in_edge();
222 EXPECT_EQ("cat no'space \"with space$\" \"no\\\"space2\" > \"a b\"",
223 edge->EvaluateCommand());
225 EXPECT_EQ("cat 'no'\\''space' 'with space$' 'no\"space2' > 'a b'",
226 edge->EvaluateCommand());
230 // Regression test for https://github.com/ninja-build/ninja/issues/380
231 TEST_F(GraphTest, DepfileWithCanonicalizablePath) {
232 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
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", "");
242 EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
245 EXPECT_FALSE(GetNode("out.o")->dirty());
248 // Regression test for https://github.com/ninja-build/ninja/issues/404
249 TEST_F(GraphTest, DepfileRemoved) {
250 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
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", "");
258 fs_.Create("out.o.d", "out.o: foo.h\n");
259 fs_.Create("out.o", "");
262 EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
264 EXPECT_FALSE(GetNode("out.o")->dirty());
267 fs_.RemoveFile("out.o.d");
268 EXPECT_TRUE(scan_.RecomputeDirty(GetNode("out.o"), &err));
270 EXPECT_TRUE(GetNode("out.o")->dirty());
273 // Check that rule-level variables are in scope for eval.
274 TEST_F(GraphTest, RuleVariablesInScope) {
275 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
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());
284 // Check that build statements can override rule builtins like depfile.
285 TEST_F(GraphTest, DepfileOverride) {
286 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
289 " command = unused\n"
292 Edge* edge = GetNode("out")->in_edge();
293 EXPECT_EQ("y", edge->GetBinding("depfile"));
296 // Check that overridden values show up in expansion of rule-level bindings.
297 TEST_F(GraphTest, DepfileOverrideParent) {
298 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
301 " command = depfile is $depfile\n"
304 Edge* edge = GetNode("out")->in_edge();
305 EXPECT_EQ("depfile is y", edge->GetBinding("command"));
308 // Verify that building a nested phony rule prints "no work to do"
309 TEST_F(GraphTest, NestedPhonyPrintsDone) {
312 "build n2: phony n1\n"
315 EXPECT_TRUE(scan_.RecomputeDirty(GetNode("n2"), &err));
319 EXPECT_TRUE(plan_.AddTarget(GetNode("n2"), &err));
322 EXPECT_EQ(0, plan_.command_edge_count());
323 ASSERT_FALSE(plan_.more_to_do());
326 TEST_F(GraphTest, PhonySelfReferenceError) {
327 ManifestParserOptions parser_opts;
328 parser_opts.phony_cycle_action_ = kPhonyCycleActionError;
330 "build a: phony a\n",
334 EXPECT_FALSE(scan_.RecomputeDirty(GetNode("a"), &err));
335 ASSERT_EQ("dependency cycle: a -> a [-w phonycycle=err]", err);
338 TEST_F(GraphTest, DependencyCycle) {
340 "build out: cat mid\n"
341 "build mid: cat in\n"
342 "build in: cat pre\n"
343 "build pre: cat out\n");
346 EXPECT_FALSE(scan_.RecomputeDirty(GetNode("out"), &err));
347 ASSERT_EQ("dependency cycle: out -> mid -> in -> pre -> out", err);
350 TEST_F(GraphTest, CycleInEdgesButNotInNodes1) {
353 "build a b: cat a\n");
354 EXPECT_FALSE(scan_.RecomputeDirty(GetNode("b"), &err));
355 ASSERT_EQ("dependency cycle: a -> a", err);
358 TEST_F(GraphTest, CycleInEdgesButNotInNodes2) {
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);
366 TEST_F(GraphTest, CycleInEdgesButNotInNodes3) {
368 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
370 "build c: cat a\n"));
371 EXPECT_FALSE(scan_.RecomputeDirty(GetNode("b"), &err));
372 ASSERT_EQ("dependency cycle: a -> c -> a", err);
375 TEST_F(GraphTest, CycleInEdgesButNotInNodes4) {
377 ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
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);
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.
389 TEST_F(GraphTest, CycleWithLengthZeroFromDepfile) {
393 " command = unused\n"
394 "build a b: deprule\n"
396 fs_.Create("dep.d", "a: b\n");
399 EXPECT_FALSE(scan_.RecomputeDirty(GetNode("a"), &err));
400 ASSERT_EQ("dependency cycle: b -> b", err);
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
405 Edge* edge = GetNode("a")->in_edge();
406 EXPECT_EQ(1, edge->inputs_.size());
407 EXPECT_EQ("b", edge->inputs_[0]->path());
410 // Like CycleWithLengthZeroFromDepfile but with a higher cycle length.
411 TEST_F(GraphTest, CycleWithLengthOneFromDepfile) {
415 " command = unused\n"
417 " command = unused\n"
418 "build a b: deprule\n"
421 fs_.Create("dep.d", "a: c\n");
424 EXPECT_FALSE(scan_.RecomputeDirty(GetNode("a"), &err));
425 ASSERT_EQ("dependency cycle: b -> c -> b", err);
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());
435 // Like CycleWithLengthOneFromDepfile but building a node one hop away from
437 TEST_F(GraphTest, CycleWithLengthOneFromDepfileOneHopAway) {
441 " command = unused\n"
443 " command = unused\n"
444 "build a b: deprule\n"
448 fs_.Create("dep.d", "a: c\n");
451 EXPECT_FALSE(scan_.RecomputeDirty(GetNode("d"), &err));
452 ASSERT_EQ("dependency cycle: b -> c -> b", err);
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());
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"));
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");