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 Err err; |
17 TestWithScope setup; | 17 TestWithScope setup; |
18 | 18 |
19 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 19 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"), {}); |
20 target.set_output_type(Target::ACTION_FOREACH); | 20 target.set_output_type(Target::ACTION_FOREACH); |
21 target.action_values().outputs() = SubstitutionList::MakeForTest( | 21 target.action_values().outputs() = SubstitutionList::MakeForTest( |
22 "//out/Debug/gen/a b{{source_name_part}}.h", | 22 "//out/Debug/gen/a b{{source_name_part}}.h", |
23 "//out/Debug/gen/{{source_name_part}}.cc"); | 23 "//out/Debug/gen/{{source_name_part}}.cc"); |
24 | 24 |
25 target.SetToolchain(setup.toolchain()); | 25 target.SetToolchain(setup.toolchain()); |
26 ASSERT_TRUE(target.OnResolved(&err)); | 26 ASSERT_TRUE(target.OnResolved(&err)); |
27 | 27 |
28 std::ostringstream out; | 28 std::ostringstream out; |
29 NinjaActionTargetWriter writer(&target, out); | 29 NinjaActionTargetWriter writer(&target, out); |
30 | 30 |
31 SourceFile source("//foo/bar.in"); | 31 SourceFile source("//foo/bar.in"); |
32 std::vector<OutputFile> output_files; | 32 std::vector<OutputFile> output_files; |
33 writer.WriteOutputFilesForBuildLine(source, &output_files); | 33 writer.WriteOutputFilesForBuildLine(source, &output_files); |
34 | 34 |
35 EXPECT_EQ(" gen/a$ bbar.h gen/bar.cc", out.str()); | 35 EXPECT_EQ(" gen/a$ bbar.h gen/bar.cc", out.str()); |
36 } | 36 } |
37 | 37 |
38 // Tests an action with no sources. | 38 // Tests an action with no sources. |
39 TEST(NinjaActionTargetWriter, ActionNoSources) { | 39 TEST(NinjaActionTargetWriter, ActionNoSources) { |
40 Err err; | 40 Err err; |
41 TestWithScope setup; | 41 TestWithScope setup; |
42 | 42 |
43 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 43 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"), {}); |
44 target.set_output_type(Target::ACTION); | 44 target.set_output_type(Target::ACTION); |
45 | 45 |
46 target.action_values().set_script(SourceFile("//foo/script.py")); | 46 target.action_values().set_script(SourceFile("//foo/script.py")); |
47 target.inputs().push_back(SourceFile("//foo/included.txt")); | 47 target.inputs().push_back(SourceFile("//foo/included.txt")); |
48 | 48 |
49 target.action_values().outputs() = | 49 target.action_values().outputs() = |
50 SubstitutionList::MakeForTest("//out/Debug/foo.out"); | 50 SubstitutionList::MakeForTest("//out/Debug/foo.out"); |
51 | 51 |
52 target.SetToolchain(setup.toolchain()); | 52 target.SetToolchain(setup.toolchain()); |
53 ASSERT_TRUE(target.OnResolved(&err)); | 53 ASSERT_TRUE(target.OnResolved(&err)); |
(...skipping 18 matching lines...) Expand all Loading... |
72 "build obj/foo/bar.stamp: stamp foo.out\n"; | 72 "build obj/foo/bar.stamp: stamp foo.out\n"; |
73 EXPECT_EQ(expected, out.str()); | 73 EXPECT_EQ(expected, out.str()); |
74 } | 74 } |
75 | 75 |
76 | 76 |
77 // Tests an action with no sources and console = true | 77 // Tests an action with no sources and console = true |
78 TEST(NinjaActionTargetWriter, ActionNoSourcesConsole) { | 78 TEST(NinjaActionTargetWriter, ActionNoSourcesConsole) { |
79 Err err; | 79 Err err; |
80 TestWithScope setup; | 80 TestWithScope setup; |
81 | 81 |
82 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 82 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"), {}); |
83 target.set_output_type(Target::ACTION); | 83 target.set_output_type(Target::ACTION); |
84 | 84 |
85 target.action_values().set_script(SourceFile("//foo/script.py")); | 85 target.action_values().set_script(SourceFile("//foo/script.py")); |
86 target.inputs().push_back(SourceFile("//foo/included.txt")); | 86 target.inputs().push_back(SourceFile("//foo/included.txt")); |
87 | 87 |
88 target.action_values().outputs() = | 88 target.action_values().outputs() = |
89 SubstitutionList::MakeForTest("//out/Debug/foo.out"); | 89 SubstitutionList::MakeForTest("//out/Debug/foo.out"); |
90 target.action_values().set_console(true); | 90 target.action_values().set_console(true); |
91 | 91 |
92 target.SetToolchain(setup.toolchain()); | 92 target.SetToolchain(setup.toolchain()); |
(...skipping 20 matching lines...) Expand all Loading... |
113 "build obj/foo/bar.stamp: stamp foo.out\n"; | 113 "build obj/foo/bar.stamp: stamp foo.out\n"; |
114 EXPECT_EQ(expected, out.str()); | 114 EXPECT_EQ(expected, out.str()); |
115 } | 115 } |
116 | 116 |
117 // 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 |
118 // both sources and inputs (ACTION_FOREACH treats the sources differently). | 118 // both sources and inputs (ACTION_FOREACH treats the sources differently). |
119 TEST(NinjaActionTargetWriter, ActionWithSources) { | 119 TEST(NinjaActionTargetWriter, ActionWithSources) { |
120 Err err; | 120 Err err; |
121 TestWithScope setup; | 121 TestWithScope setup; |
122 | 122 |
123 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 123 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"), {}); |
124 target.set_output_type(Target::ACTION); | 124 target.set_output_type(Target::ACTION); |
125 | 125 |
126 target.action_values().set_script(SourceFile("//foo/script.py")); | 126 target.action_values().set_script(SourceFile("//foo/script.py")); |
127 | 127 |
128 target.sources().push_back(SourceFile("//foo/source.txt")); | 128 target.sources().push_back(SourceFile("//foo/source.txt")); |
129 target.inputs().push_back(SourceFile("//foo/included.txt")); | 129 target.inputs().push_back(SourceFile("//foo/included.txt")); |
130 | 130 |
131 target.action_values().outputs() = | 131 target.action_values().outputs() = |
132 SubstitutionList::MakeForTest("//out/Debug/foo.out"); | 132 SubstitutionList::MakeForTest("//out/Debug/foo.out"); |
133 | 133 |
(...skipping 22 matching lines...) Expand all Loading... |
156 } | 156 } |
157 | 157 |
158 TEST(NinjaActionTargetWriter, ForEach) { | 158 TEST(NinjaActionTargetWriter, ForEach) { |
159 Err err; | 159 Err err; |
160 TestWithScope setup; | 160 TestWithScope setup; |
161 | 161 |
162 // 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 |
163 // 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 |
164 // 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 |
165 // binaries). | 165 // binaries). |
166 Target dep(setup.settings(), Label(SourceDir("//foo/"), "dep")); | 166 Target dep(setup.settings(), Label(SourceDir("//foo/"), "dep"), {}); |
167 dep.set_output_type(Target::ACTION); | 167 dep.set_output_type(Target::ACTION); |
168 dep.visibility().SetPublic(); | 168 dep.visibility().SetPublic(); |
169 dep.SetToolchain(setup.toolchain()); | 169 dep.SetToolchain(setup.toolchain()); |
170 ASSERT_TRUE(dep.OnResolved(&err)); | 170 ASSERT_TRUE(dep.OnResolved(&err)); |
171 | 171 |
172 Target datadep(setup.settings(), Label(SourceDir("//foo/"), "datadep")); | 172 Target datadep(setup.settings(), Label(SourceDir("//foo/"), "datadep"), {}); |
173 datadep.set_output_type(Target::ACTION); | 173 datadep.set_output_type(Target::ACTION); |
174 datadep.visibility().SetPublic(); | 174 datadep.visibility().SetPublic(); |
175 datadep.SetToolchain(setup.toolchain()); | 175 datadep.SetToolchain(setup.toolchain()); |
176 ASSERT_TRUE(datadep.OnResolved(&err)); | 176 ASSERT_TRUE(datadep.OnResolved(&err)); |
177 | 177 |
178 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 178 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"), {}); |
179 target.set_output_type(Target::ACTION_FOREACH); | 179 target.set_output_type(Target::ACTION_FOREACH); |
180 target.private_deps().push_back(LabelTargetPair(&dep)); | 180 target.private_deps().push_back(LabelTargetPair(&dep)); |
181 target.data_deps().push_back(LabelTargetPair(&datadep)); | 181 target.data_deps().push_back(LabelTargetPair(&datadep)); |
182 | 182 |
183 target.sources().push_back(SourceFile("//foo/input1.txt")); | 183 target.sources().push_back(SourceFile("//foo/input1.txt")); |
184 target.sources().push_back(SourceFile("//foo/input2.txt")); | 184 target.sources().push_back(SourceFile("//foo/input2.txt")); |
185 | 185 |
186 target.action_values().set_script(SourceFile("//foo/script.py")); | 186 target.action_values().set_script(SourceFile("//foo/script.py")); |
187 | 187 |
188 target.action_values().args() = SubstitutionList::MakeForTest( | 188 target.action_values().args() = SubstitutionList::MakeForTest( |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 #if defined(OS_WIN) | 232 #if defined(OS_WIN) |
233 std::replace(out_str.begin(), out_str.end(), '\\', '/'); | 233 std::replace(out_str.begin(), out_str.end(), '\\', '/'); |
234 #endif | 234 #endif |
235 EXPECT_EQ(expected_linux, out_str); | 235 EXPECT_EQ(expected_linux, out_str); |
236 } | 236 } |
237 | 237 |
238 TEST(NinjaActionTargetWriter, ForEachWithDepfile) { | 238 TEST(NinjaActionTargetWriter, ForEachWithDepfile) { |
239 Err err; | 239 Err err; |
240 TestWithScope setup; | 240 TestWithScope setup; |
241 | 241 |
242 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 242 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"), {}); |
243 target.set_output_type(Target::ACTION_FOREACH); | 243 target.set_output_type(Target::ACTION_FOREACH); |
244 | 244 |
245 target.sources().push_back(SourceFile("//foo/input1.txt")); | 245 target.sources().push_back(SourceFile("//foo/input1.txt")); |
246 target.sources().push_back(SourceFile("//foo/input2.txt")); | 246 target.sources().push_back(SourceFile("//foo/input2.txt")); |
247 | 247 |
248 target.action_values().set_script(SourceFile("//foo/script.py")); | 248 target.action_values().set_script(SourceFile("//foo/script.py")); |
249 | 249 |
250 target.SetToolchain(setup.toolchain()); | 250 target.SetToolchain(setup.toolchain()); |
251 ASSERT_TRUE(target.OnResolved(&err)); | 251 ASSERT_TRUE(target.OnResolved(&err)); |
252 | 252 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 " depfile = gen/input2.d\n" | 294 " depfile = gen/input2.d\n" |
295 "\n" | 295 "\n" |
296 "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; | 296 "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; |
297 EXPECT_EQ(expected_linux, out.str()); | 297 EXPECT_EQ(expected_linux, out.str()); |
298 } | 298 } |
299 | 299 |
300 TEST(NinjaActionTargetWriter, ForEachWithResponseFile) { | 300 TEST(NinjaActionTargetWriter, ForEachWithResponseFile) { |
301 Err err; | 301 Err err; |
302 TestWithScope setup; | 302 TestWithScope setup; |
303 | 303 |
304 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 304 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"), {}); |
305 target.set_output_type(Target::ACTION_FOREACH); | 305 target.set_output_type(Target::ACTION_FOREACH); |
306 | 306 |
307 target.sources().push_back(SourceFile("//foo/input1.txt")); | 307 target.sources().push_back(SourceFile("//foo/input1.txt")); |
308 target.action_values().set_script(SourceFile("//foo/script.py")); | 308 target.action_values().set_script(SourceFile("//foo/script.py")); |
309 | 309 |
310 target.SetToolchain(setup.toolchain()); | 310 target.SetToolchain(setup.toolchain()); |
311 ASSERT_TRUE(target.OnResolved(&err)); | 311 ASSERT_TRUE(target.OnResolved(&err)); |
312 | 312 |
313 // 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 |
314 // response file contents. | 314 // response file contents. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 // Necessary for the rspfile defined in the rule. | 346 // Necessary for the rspfile defined in the rule. |
347 " unique_name = 0\n" | 347 " unique_name = 0\n" |
348 // Substitution for the args. | 348 // Substitution for the args. |
349 " source_file_part = input1.txt\n" | 349 " source_file_part = input1.txt\n" |
350 // Substitution for the rspfile contents. | 350 // Substitution for the rspfile contents. |
351 " source_name_part = input1\n" | 351 " source_name_part = input1\n" |
352 "\n" | 352 "\n" |
353 "build obj/foo/bar.stamp: stamp input1.out\n"; | 353 "build obj/foo/bar.stamp: stamp input1.out\n"; |
354 EXPECT_EQ(expected_linux, out.str()); | 354 EXPECT_EQ(expected_linux, out.str()); |
355 } | 355 } |
OLD | NEW |