Merge pull request #763 from drbo/master
[platform/upstream/ninja.git] / src / graph_test.cc
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 {
21   GraphTest() : scan_(&state_, NULL, NULL, &fs_) {}
22
23   VirtualFileSystem fs_;
24   DependencyScan scan_;
25 };
26
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   Edge* edge = GetNode("out")->in_edge();
34   string err;
35   EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
36   ASSERT_EQ("", err);
37
38   // A missing implicit dep *should* make the output dirty.
39   // (In fact, a build will fail.)
40   // This is a change from prior semantics of ninja.
41   EXPECT_TRUE(GetNode("out")->dirty());
42 }
43
44 TEST_F(GraphTest, ModifiedImplicit) {
45   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
46 "build out: cat in | implicit\n"));
47   fs_.Create("in", "");
48   fs_.Create("out", "");
49   fs_.Tick();
50   fs_.Create("implicit", "");
51
52   Edge* edge = GetNode("out")->in_edge();
53   string err;
54   EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
55   ASSERT_EQ("", err);
56
57   // A modified implicit dep should make the output dirty.
58   EXPECT_TRUE(GetNode("out")->dirty());
59 }
60
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   Edge* edge = GetNode("out.o")->in_edge();
74   string err;
75   EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
76   ASSERT_EQ("", err);
77
78   // implicit.h has changed, though our depfile refers to it with a
79   // non-canonical path; we should still find it.
80   EXPECT_TRUE(GetNode("out.o")->dirty());
81 }
82
83 TEST_F(GraphTest, ExplicitImplicit) {
84   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
85 "rule catdep\n"
86 "  depfile = $out.d\n"
87 "  command = cat $in > $out\n"
88 "build implicit.h: cat data\n"
89 "build out.o: catdep foo.cc || implicit.h\n"));
90   fs_.Create("implicit.h", "");
91   fs_.Create("foo.cc", "");
92   fs_.Create("out.o.d", "out.o: implicit.h\n");
93   fs_.Create("out.o", "");
94   fs_.Tick();
95   fs_.Create("data", "");
96
97   Edge* edge = GetNode("out.o")->in_edge();
98   string err;
99   EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
100   ASSERT_EQ("", err);
101
102   // We have both an implicit and an explicit dep on implicit.h.
103   // The implicit dep should "win" (in the sense that it should cause
104   // the output to be dirty).
105   EXPECT_TRUE(GetNode("out.o")->dirty());
106 }
107
108 TEST_F(GraphTest, PathWithCurrentDirectory) {
109   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
110 "rule catdep\n"
111 "  depfile = $out.d\n"
112 "  command = cat $in > $out\n"
113 "build ./out.o: catdep ./foo.cc\n"));
114   fs_.Create("foo.cc", "");
115   fs_.Create("out.o.d", "out.o: foo.cc\n");
116   fs_.Create("out.o", "");
117
118   Edge* edge = GetNode("out.o")->in_edge();
119   string err;
120   EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
121   ASSERT_EQ("", err);
122
123   EXPECT_FALSE(GetNode("out.o")->dirty());
124 }
125
126 TEST_F(GraphTest, RootNodes) {
127   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
128 "build out1: cat in1\n"
129 "build mid1: cat in1\n"
130 "build out2: cat mid1\n"
131 "build out3 out4: cat mid1\n"));
132
133   string err;
134   vector<Node*> root_nodes = state_.RootNodes(&err);
135   EXPECT_EQ(4u, root_nodes.size());
136   for (size_t i = 0; i < root_nodes.size(); ++i) {
137     string name = root_nodes[i]->path();
138     EXPECT_EQ("out", name.substr(0, 3));
139   }
140 }
141
142 TEST_F(GraphTest, VarInOutPathEscaping) {
143   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
144 "build a$ b: cat no'space with$ space$$ no\"space2\n"));
145
146   Edge* edge = GetNode("a b")->in_edge();
147 #if _WIN32
148   EXPECT_EQ("cat no'space \"with space$\" \"no\\\"space2\" > \"a b\"",
149       edge->EvaluateCommand());
150 #else
151   EXPECT_EQ("cat 'no'\\''space' 'with space$' 'no\"space2' > 'a b'",
152       edge->EvaluateCommand());
153 #endif
154 }
155
156 // Regression test for https://github.com/ninja-build/ninja/issues/380
157 TEST_F(GraphTest, DepfileWithCanonicalizablePath) {
158   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
159 "rule catdep\n"
160 "  depfile = $out.d\n"
161 "  command = cat $in > $out\n"
162 "build ./out.o: catdep ./foo.cc\n"));
163   fs_.Create("foo.cc", "");
164   fs_.Create("out.o.d", "out.o: bar/../foo.cc\n");
165   fs_.Create("out.o", "");
166
167   Edge* edge = GetNode("out.o")->in_edge();
168   string err;
169   EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
170   ASSERT_EQ("", err);
171
172   EXPECT_FALSE(GetNode("out.o")->dirty());
173 }
174
175 // Regression test for https://github.com/ninja-build/ninja/issues/404
176 TEST_F(GraphTest, DepfileRemoved) {
177   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
178 "rule catdep\n"
179 "  depfile = $out.d\n"
180 "  command = cat $in > $out\n"
181 "build ./out.o: catdep ./foo.cc\n"));
182   fs_.Create("foo.h", "");
183   fs_.Create("foo.cc", "");
184   fs_.Tick();
185   fs_.Create("out.o.d", "out.o: foo.h\n");
186   fs_.Create("out.o", "");
187
188   Edge* edge = GetNode("out.o")->in_edge();
189   string err;
190   EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
191   ASSERT_EQ("", err);
192   EXPECT_FALSE(GetNode("out.o")->dirty());
193
194   state_.Reset();
195   fs_.RemoveFile("out.o.d");
196   EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
197   ASSERT_EQ("", err);
198   EXPECT_TRUE(GetNode("out.o")->dirty());
199 }
200
201 // Check that rule-level variables are in scope for eval.
202 TEST_F(GraphTest, RuleVariablesInScope) {
203   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
204 "rule r\n"
205 "  depfile = x\n"
206 "  command = depfile is $depfile\n"
207 "build out: r in\n"));
208   Edge* edge = GetNode("out")->in_edge();
209   EXPECT_EQ("depfile is x", edge->EvaluateCommand());
210 }
211
212 // Check that build statements can override rule builtins like depfile.
213 TEST_F(GraphTest, DepfileOverride) {
214   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
215 "rule r\n"
216 "  depfile = x\n"
217 "  command = unused\n"
218 "build out: r in\n"
219 "  depfile = y\n"));
220   Edge* edge = GetNode("out")->in_edge();
221   EXPECT_EQ("y", edge->GetBinding("depfile"));
222 }
223
224 // Check that overridden values show up in expansion of rule-level bindings.
225 TEST_F(GraphTest, DepfileOverrideParent) {
226   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
227 "rule r\n"
228 "  depfile = x\n"
229 "  command = depfile is $depfile\n"
230 "build out: r in\n"
231 "  depfile = y\n"));
232   Edge* edge = GetNode("out")->in_edge();
233   EXPECT_EQ("depfile is y", edge->GetBinding("command"));
234 }
235
236 // Verify that building a nested phony rule prints "no work to do"
237 TEST_F(GraphTest, NestedPhonyPrintsDone) {
238   AssertParse(&state_,
239 "build n1: phony \n"
240 "build n2: phony n1\n"
241   );
242   string err;
243   Edge* edge = GetNode("n2")->in_edge();
244   EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
245   ASSERT_EQ("", err);
246
247   Plan plan_;
248   EXPECT_TRUE(plan_.AddTarget(GetNode("n2"), &err));
249   ASSERT_EQ("", err);
250
251   EXPECT_EQ(0, plan_.command_edge_count());
252   ASSERT_FALSE(plan_.more_to_do());
253 }
254
255 // Verify that cycles in graphs with multiple outputs are handled correctly
256 // in RecomputeDirty() and don't cause deps to be loaded multiple times.
257 TEST_F(GraphTest, CycleWithLengthZeroFromDepfile) {
258   AssertParse(&state_,
259 "rule deprule\n"
260 "   depfile = dep.d\n"
261 "   command = unused\n"
262 "build a b: deprule\n"
263   );
264   fs_.Create("dep.d", "a: b\n");
265
266   string err;
267   Edge* edge = GetNode("a")->in_edge();
268   EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
269   ASSERT_EQ("", err);
270
271   // Despite the depfile causing edge to be a cycle (it has outputs a and b,
272   // but the depfile also adds b as an input), the deps should have been loaded
273   // only once:
274   EXPECT_EQ(1, edge->inputs_.size());
275   EXPECT_EQ("b", edge->inputs_[0]->path());
276 }
277
278 // Like CycleWithLengthZeroFromDepfile but with a higher cycle length.
279 TEST_F(GraphTest, CycleWithLengthOneFromDepfile) {
280   AssertParse(&state_,
281 "rule deprule\n"
282 "   depfile = dep.d\n"
283 "   command = unused\n"
284 "rule r\n"
285 "   command = unused\n"
286 "build a b: deprule\n"
287 "build c: r b\n"
288   );
289   fs_.Create("dep.d", "a: c\n");
290
291   string err;
292   Edge* edge = GetNode("a")->in_edge();
293   EXPECT_TRUE(scan_.RecomputeDirty(edge, &err));
294   ASSERT_EQ("", err);
295
296   // Despite the depfile causing edge to be a cycle (|edge| has outputs a and b,
297   // but c's in_edge has b as input but the depfile also adds |edge| as
298   // output)), the deps should have been loaded only once:
299   EXPECT_EQ(1, edge->inputs_.size());
300   EXPECT_EQ("c", edge->inputs_[0]->path());
301 }
302
303 // Like CycleWithLengthOneFromDepfile but building a node one hop away from
304 // the cycle.
305 TEST_F(GraphTest, CycleWithLengthOneFromDepfileOneHopAway) {
306   AssertParse(&state_,
307 "rule deprule\n"
308 "   depfile = dep.d\n"
309 "   command = unused\n"
310 "rule r\n"
311 "   command = unused\n"
312 "build a b: deprule\n"
313 "build c: r b\n"
314 "build d: r a\n"
315   );
316   fs_.Create("dep.d", "a: c\n");
317
318   string err;
319   EXPECT_TRUE(scan_.RecomputeDirty(GetNode("d")->in_edge(), &err));
320   ASSERT_EQ("", err);
321
322   // Despite the depfile causing edge to be a cycle (|edge| has outputs a and b,
323   // but c's in_edge has b as input but the depfile also adds |edge| as
324   // output)), the deps should have been loaded only once:
325   Edge* edge = GetNode("a")->in_edge();
326   EXPECT_EQ(1, edge->inputs_.size());
327   EXPECT_EQ("c", edge->inputs_[0]->path());
328 }
329
330 #ifdef _WIN32
331 TEST_F(GraphTest, Decanonicalize) {
332   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
333 "build out\\out1: cat src\\in1\n"
334 "build out\\out2/out3\\out4: cat mid1\n"
335 "build out3 out4\\foo: cat mid1\n"));
336
337   string err;
338   vector<Node*> root_nodes = state_.RootNodes(&err);
339   EXPECT_EQ(4u, root_nodes.size());
340   EXPECT_EQ(root_nodes[0]->path(), "out/out1");
341   EXPECT_EQ(root_nodes[1]->path(), "out/out2/out3/out4");
342   EXPECT_EQ(root_nodes[2]->path(), "out3");
343   EXPECT_EQ(root_nodes[3]->path(), "out4/foo");
344   EXPECT_EQ(root_nodes[0]->PathDecanonicalized(), "out\\out1");
345   EXPECT_EQ(root_nodes[1]->PathDecanonicalized(), "out\\out2/out3\\out4");
346   EXPECT_EQ(root_nodes[2]->PathDecanonicalized(), "out3");
347   EXPECT_EQ(root_nodes[3]->PathDecanonicalized(), "out4\\foo");
348 }
349 #endif