OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 #include <sstream> | 6 #include <sstream> |
7 | 7 |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "tools/gn/ninja_action_target_writer.h" | 10 #include "tools/gn/ninja_action_target_writer.h" |
11 #include "tools/gn/substitution_list.h" | 11 #include "tools/gn/substitution_list.h" |
12 #include "tools/gn/target.h" | 12 #include "tools/gn/target.h" |
13 #include "tools/gn/test_with_scope.h" | 13 #include "tools/gn/test_with_scope.h" |
14 | 14 |
15 TEST(NinjaActionTargetWriter, WriteOutputFilesForBuildLine) { | 15 TEST(NinjaActionTargetWriter, WriteOutputFilesForBuildLine) { |
| 16 Err err; |
16 TestWithScope setup; | 17 TestWithScope setup; |
17 Err err; | |
18 | |
19 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | |
20 | 18 |
21 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 19 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
22 target.set_output_type(Target::ACTION_FOREACH); | 20 target.set_output_type(Target::ACTION_FOREACH); |
23 target.action_values().outputs() = SubstitutionList::MakeForTest( | 21 target.action_values().outputs() = SubstitutionList::MakeForTest( |
24 "//out/Debug/gen/a b{{source_name_part}}.h", | 22 "//out/Debug/gen/a b{{source_name_part}}.h", |
25 "//out/Debug/gen/{{source_name_part}}.cc"); | 23 "//out/Debug/gen/{{source_name_part}}.cc"); |
26 | 24 |
27 target.SetToolchain(setup.toolchain()); | 25 target.SetToolchain(setup.toolchain()); |
28 ASSERT_TRUE(target.OnResolved(&err)); | 26 ASSERT_TRUE(target.OnResolved(&err)); |
29 | 27 |
30 std::ostringstream out; | 28 std::ostringstream out; |
31 NinjaActionTargetWriter writer(&target, out); | 29 NinjaActionTargetWriter writer(&target, out); |
32 | 30 |
33 SourceFile source("//foo/bar.in"); | 31 SourceFile source("//foo/bar.in"); |
34 std::vector<OutputFile> output_files; | 32 std::vector<OutputFile> output_files; |
35 writer.WriteOutputFilesForBuildLine(source, &output_files); | 33 writer.WriteOutputFilesForBuildLine(source, &output_files); |
36 | 34 |
37 EXPECT_EQ(" gen/a$ bbar.h gen/bar.cc", out.str()); | 35 EXPECT_EQ(" gen/a$ bbar.h gen/bar.cc", out.str()); |
38 } | 36 } |
39 | 37 |
40 // Tests an action with no sources. | 38 // Tests an action with no sources. |
41 TEST(NinjaActionTargetWriter, ActionNoSources) { | 39 TEST(NinjaActionTargetWriter, ActionNoSources) { |
| 40 Err err; |
42 TestWithScope setup; | 41 TestWithScope setup; |
43 Err err; | |
44 | 42 |
45 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | |
46 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 43 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
47 target.set_output_type(Target::ACTION); | 44 target.set_output_type(Target::ACTION); |
48 | 45 |
49 target.action_values().set_script(SourceFile("//foo/script.py")); | 46 target.action_values().set_script(SourceFile("//foo/script.py")); |
50 target.inputs().push_back(SourceFile("//foo/included.txt")); | 47 target.inputs().push_back(SourceFile("//foo/included.txt")); |
51 | 48 |
52 target.action_values().outputs() = | 49 target.action_values().outputs() = |
53 SubstitutionList::MakeForTest("//out/Debug/foo.out"); | 50 SubstitutionList::MakeForTest("//out/Debug/foo.out"); |
54 | 51 |
55 target.SetToolchain(setup.toolchain()); | 52 target.SetToolchain(setup.toolchain()); |
(...skipping 16 matching lines...) Expand all Loading... |
72 "\n" | 69 "\n" |
73 "build foo.out: __foo_bar___rule | obj/foo/bar.inputdeps.stamp\n" | 70 "build foo.out: __foo_bar___rule | obj/foo/bar.inputdeps.stamp\n" |
74 "\n" | 71 "\n" |
75 "build obj/foo/bar.stamp: stamp foo.out\n"; | 72 "build obj/foo/bar.stamp: stamp foo.out\n"; |
76 EXPECT_EQ(expected, out.str()); | 73 EXPECT_EQ(expected, out.str()); |
77 } | 74 } |
78 | 75 |
79 | 76 |
80 // Tests an action with no sources and console = true | 77 // Tests an action with no sources and console = true |
81 TEST(NinjaActionTargetWriter, ActionNoSourcesConsole) { | 78 TEST(NinjaActionTargetWriter, ActionNoSourcesConsole) { |
| 79 Err err; |
82 TestWithScope setup; | 80 TestWithScope setup; |
83 Err err; | |
84 | 81 |
85 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | |
86 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 82 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
87 target.set_output_type(Target::ACTION); | 83 target.set_output_type(Target::ACTION); |
88 | 84 |
89 target.action_values().set_script(SourceFile("//foo/script.py")); | 85 target.action_values().set_script(SourceFile("//foo/script.py")); |
90 target.inputs().push_back(SourceFile("//foo/included.txt")); | 86 target.inputs().push_back(SourceFile("//foo/included.txt")); |
91 | 87 |
92 target.action_values().outputs() = | 88 target.action_values().outputs() = |
93 SubstitutionList::MakeForTest("//out/Debug/foo.out"); | 89 SubstitutionList::MakeForTest("//out/Debug/foo.out"); |
94 target.action_values().set_console(true); | 90 target.action_values().set_console(true); |
95 | 91 |
(...skipping 18 matching lines...) Expand all Loading... |
114 "build foo.out: __foo_bar___rule | obj/foo/bar.inputdeps.stamp\n" | 110 "build foo.out: __foo_bar___rule | obj/foo/bar.inputdeps.stamp\n" |
115 " pool = console\n" | 111 " pool = console\n" |
116 "\n" | 112 "\n" |
117 "build obj/foo/bar.stamp: stamp foo.out\n"; | 113 "build obj/foo/bar.stamp: stamp foo.out\n"; |
118 EXPECT_EQ(expected, out.str()); | 114 EXPECT_EQ(expected, out.str()); |
119 } | 115 } |
120 | 116 |
121 // Makes sure that we write sources as input dependencies for actions with | 117 // Makes sure that we write sources as input dependencies for actions with |
122 // both sources and inputs (ACTION_FOREACH treats the sources differently). | 118 // both sources and inputs (ACTION_FOREACH treats the sources differently). |
123 TEST(NinjaActionTargetWriter, ActionWithSources) { | 119 TEST(NinjaActionTargetWriter, ActionWithSources) { |
| 120 Err err; |
124 TestWithScope setup; | 121 TestWithScope setup; |
125 Err err; | |
126 | 122 |
127 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | |
128 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 123 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
129 target.set_output_type(Target::ACTION); | 124 target.set_output_type(Target::ACTION); |
130 | 125 |
131 target.action_values().set_script(SourceFile("//foo/script.py")); | 126 target.action_values().set_script(SourceFile("//foo/script.py")); |
132 | 127 |
133 target.sources().push_back(SourceFile("//foo/source.txt")); | 128 target.sources().push_back(SourceFile("//foo/source.txt")); |
134 target.inputs().push_back(SourceFile("//foo/included.txt")); | 129 target.inputs().push_back(SourceFile("//foo/included.txt")); |
135 | 130 |
136 target.action_values().outputs() = | 131 target.action_values().outputs() = |
137 SubstitutionList::MakeForTest("//out/Debug/foo.out"); | 132 SubstitutionList::MakeForTest("//out/Debug/foo.out"); |
(...skipping 16 matching lines...) Expand all Loading... |
154 "build obj/foo/bar.inputdeps.stamp: stamp ../../foo/script.py " | 149 "build obj/foo/bar.inputdeps.stamp: stamp ../../foo/script.py " |
155 "../../foo/included.txt ../../foo/source.txt\n" | 150 "../../foo/included.txt ../../foo/source.txt\n" |
156 "\n" | 151 "\n" |
157 "build foo.out: __foo_bar___rule | obj/foo/bar.inputdeps.stamp\n" | 152 "build foo.out: __foo_bar___rule | obj/foo/bar.inputdeps.stamp\n" |
158 "\n" | 153 "\n" |
159 "build obj/foo/bar.stamp: stamp foo.out\n"; | 154 "build obj/foo/bar.stamp: stamp foo.out\n"; |
160 EXPECT_EQ(expected_linux, out.str()); | 155 EXPECT_EQ(expected_linux, out.str()); |
161 } | 156 } |
162 | 157 |
163 TEST(NinjaActionTargetWriter, ForEach) { | 158 TEST(NinjaActionTargetWriter, ForEach) { |
| 159 Err err; |
164 TestWithScope setup; | 160 TestWithScope setup; |
165 Err err; | |
166 | |
167 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | |
168 | 161 |
169 // Some dependencies that the action can depend on. Use actions for these | 162 // Some dependencies that the action can depend on. Use actions for these |
170 // so they have a nice platform-independent stamp file that can appear in the | 163 // so they have a nice platform-independent stamp file that can appear in the |
171 // output (rather than having to worry about how the current platform names | 164 // output (rather than having to worry about how the current platform names |
172 // binaries). | 165 // binaries). |
173 Target dep(setup.settings(), Label(SourceDir("//foo/"), "dep")); | 166 Target dep(setup.settings(), Label(SourceDir("//foo/"), "dep")); |
174 dep.set_output_type(Target::ACTION); | 167 dep.set_output_type(Target::ACTION); |
175 dep.visibility().SetPublic(); | 168 dep.visibility().SetPublic(); |
176 dep.SetToolchain(setup.toolchain()); | 169 dep.SetToolchain(setup.toolchain()); |
177 ASSERT_TRUE(dep.OnResolved(&err)); | 170 ASSERT_TRUE(dep.OnResolved(&err)); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 "stamp input1.out input2.out || obj/foo/datadep.stamp\n"; | 229 "stamp input1.out input2.out || obj/foo/datadep.stamp\n"; |
237 | 230 |
238 std::string out_str = out.str(); | 231 std::string out_str = out.str(); |
239 #if defined(OS_WIN) | 232 #if defined(OS_WIN) |
240 std::replace(out_str.begin(), out_str.end(), '\\', '/'); | 233 std::replace(out_str.begin(), out_str.end(), '\\', '/'); |
241 #endif | 234 #endif |
242 EXPECT_EQ(expected_linux, out_str); | 235 EXPECT_EQ(expected_linux, out_str); |
243 } | 236 } |
244 | 237 |
245 TEST(NinjaActionTargetWriter, ForEachWithDepfile) { | 238 TEST(NinjaActionTargetWriter, ForEachWithDepfile) { |
| 239 Err err; |
246 TestWithScope setup; | 240 TestWithScope setup; |
247 Err err; | |
248 | 241 |
249 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | |
250 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 242 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
251 target.set_output_type(Target::ACTION_FOREACH); | 243 target.set_output_type(Target::ACTION_FOREACH); |
252 | 244 |
253 target.sources().push_back(SourceFile("//foo/input1.txt")); | 245 target.sources().push_back(SourceFile("//foo/input1.txt")); |
254 target.sources().push_back(SourceFile("//foo/input2.txt")); | 246 target.sources().push_back(SourceFile("//foo/input2.txt")); |
255 | 247 |
256 target.action_values().set_script(SourceFile("//foo/script.py")); | 248 target.action_values().set_script(SourceFile("//foo/script.py")); |
257 | 249 |
258 target.SetToolchain(setup.toolchain()); | 250 target.SetToolchain(setup.toolchain()); |
259 ASSERT_TRUE(target.OnResolved(&err)); | 251 ASSERT_TRUE(target.OnResolved(&err)); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 "build input2.out: __foo_bar___rule ../../foo/input2.txt" | 291 "build input2.out: __foo_bar___rule ../../foo/input2.txt" |
300 " | obj/foo/bar.inputdeps.stamp\n" | 292 " | obj/foo/bar.inputdeps.stamp\n" |
301 " source_name_part = input2\n" | 293 " source_name_part = input2\n" |
302 " depfile = gen/input2.d\n" | 294 " depfile = gen/input2.d\n" |
303 "\n" | 295 "\n" |
304 "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; | 296 "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; |
305 EXPECT_EQ(expected_linux, out.str()); | 297 EXPECT_EQ(expected_linux, out.str()); |
306 } | 298 } |
307 | 299 |
308 TEST(NinjaActionTargetWriter, ForEachWithResponseFile) { | 300 TEST(NinjaActionTargetWriter, ForEachWithResponseFile) { |
| 301 Err err; |
309 TestWithScope setup; | 302 TestWithScope setup; |
310 Err err; | |
311 | 303 |
312 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | |
313 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 304 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
314 target.set_output_type(Target::ACTION_FOREACH); | 305 target.set_output_type(Target::ACTION_FOREACH); |
315 | 306 |
316 target.sources().push_back(SourceFile("//foo/input1.txt")); | 307 target.sources().push_back(SourceFile("//foo/input1.txt")); |
317 target.action_values().set_script(SourceFile("//foo/script.py")); | 308 target.action_values().set_script(SourceFile("//foo/script.py")); |
318 | 309 |
319 target.SetToolchain(setup.toolchain()); | 310 target.SetToolchain(setup.toolchain()); |
320 ASSERT_TRUE(target.OnResolved(&err)); | 311 ASSERT_TRUE(target.OnResolved(&err)); |
321 | 312 |
322 // Make sure we get interesting substitutions for both the args and the | 313 // Make sure we get interesting substitutions for both the args and the |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 // Necessary for the rspfile defined in the rule. | 346 // Necessary for the rspfile defined in the rule. |
356 " unique_name = 0\n" | 347 " unique_name = 0\n" |
357 // Substitution for the args. | 348 // Substitution for the args. |
358 " source_file_part = input1.txt\n" | 349 " source_file_part = input1.txt\n" |
359 // Substitution for the rspfile contents. | 350 // Substitution for the rspfile contents. |
360 " source_name_part = input1\n" | 351 " source_name_part = input1\n" |
361 "\n" | 352 "\n" |
362 "build obj/foo/bar.stamp: stamp input1.out\n"; | 353 "build obj/foo/bar.stamp: stamp input1.out\n"; |
363 EXPECT_EQ(expected_linux, out.str()); | 354 EXPECT_EQ(expected_linux, out.str()); |
364 } | 355 } |
OLD | NEW |