| 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 "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "tools/gn/ninja_action_target_writer.h" | 9 #include "tools/gn/ninja_action_target_writer.h" |
| 10 #include "tools/gn/substitution_list.h" | 10 #include "tools/gn/substitution_list.h" |
| 11 #include "tools/gn/target.h" | 11 #include "tools/gn/target.h" |
| 12 #include "tools/gn/test_with_scope.h" | 12 #include "tools/gn/test_with_scope.h" |
| 13 | 13 |
| 14 TEST(NinjaActionTargetWriter, WriteOutputFilesForBuildLine) { | 14 TEST(NinjaActionTargetWriter, WriteOutputFilesForBuildLine) { |
| 15 TestWithScope setup; | 15 TestWithScope setup; |
| 16 Err err; |
| 17 |
| 16 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | 18 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 17 | 19 |
| 18 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 20 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| 19 target.set_output_type(Target::ACTION_FOREACH); | 21 target.set_output_type(Target::ACTION_FOREACH); |
| 20 target.action_values().outputs() = SubstitutionList::MakeForTest( | 22 target.action_values().outputs() = SubstitutionList::MakeForTest( |
| 21 "//out/Debug/gen/a b{{source_name_part}}.h", | 23 "//out/Debug/gen/a b{{source_name_part}}.h", |
| 22 "//out/Debug/gen/{{source_name_part}}.cc"); | 24 "//out/Debug/gen/{{source_name_part}}.cc"); |
| 23 | 25 |
| 24 target.SetToolchain(setup.toolchain()); | 26 target.SetToolchain(setup.toolchain()); |
| 25 target.OnResolved(); | 27 ASSERT_TRUE(target.OnResolved(&err)); |
| 26 | 28 |
| 27 std::ostringstream out; | 29 std::ostringstream out; |
| 28 NinjaActionTargetWriter writer(&target, out); | 30 NinjaActionTargetWriter writer(&target, out); |
| 29 | 31 |
| 30 SourceFile source("//foo/bar.in"); | 32 SourceFile source("//foo/bar.in"); |
| 31 std::vector<OutputFile> output_files; | 33 std::vector<OutputFile> output_files; |
| 32 writer.WriteOutputFilesForBuildLine(source, &output_files); | 34 writer.WriteOutputFilesForBuildLine(source, &output_files); |
| 33 | 35 |
| 34 EXPECT_EQ(" gen/a$ bbar.h gen/bar.cc", out.str()); | 36 EXPECT_EQ(" gen/a$ bbar.h gen/bar.cc", out.str()); |
| 35 } | 37 } |
| 36 | 38 |
| 37 // Tests an action with no sources. | 39 // Tests an action with no sources. |
| 38 TEST(NinjaActionTargetWriter, ActionNoSources) { | 40 TEST(NinjaActionTargetWriter, ActionNoSources) { |
| 39 TestWithScope setup; | 41 TestWithScope setup; |
| 42 Err err; |
| 43 |
| 40 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | 44 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 41 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 45 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| 42 target.set_output_type(Target::ACTION); | 46 target.set_output_type(Target::ACTION); |
| 43 | 47 |
| 44 target.action_values().set_script(SourceFile("//foo/script.py")); | 48 target.action_values().set_script(SourceFile("//foo/script.py")); |
| 45 target.inputs().push_back(SourceFile("//foo/included.txt")); | 49 target.inputs().push_back(SourceFile("//foo/included.txt")); |
| 46 | 50 |
| 47 target.action_values().outputs() = | 51 target.action_values().outputs() = |
| 48 SubstitutionList::MakeForTest("//out/Debug/foo.out"); | 52 SubstitutionList::MakeForTest("//out/Debug/foo.out"); |
| 49 | 53 |
| 50 target.SetToolchain(setup.toolchain()); | 54 target.SetToolchain(setup.toolchain()); |
| 51 target.OnResolved(); | 55 ASSERT_TRUE(target.OnResolved(&err)); |
| 52 | 56 |
| 53 setup.settings()->set_target_os(Settings::LINUX); | 57 setup.settings()->set_target_os(Settings::LINUX); |
| 54 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( | 58 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( |
| 55 "/usr/bin/python"))); | 59 "/usr/bin/python"))); |
| 56 | 60 |
| 57 std::ostringstream out; | 61 std::ostringstream out; |
| 58 NinjaActionTargetWriter writer(&target, out); | 62 NinjaActionTargetWriter writer(&target, out); |
| 59 writer.Run(); | 63 writer.Run(); |
| 60 | 64 |
| 61 const char expected[] = | 65 const char expected[] = |
| 62 "rule __foo_bar___rule\n" | 66 "rule __foo_bar___rule\n" |
| 63 " command = /usr/bin/python ../../foo/script.py\n" | 67 " command = /usr/bin/python ../../foo/script.py\n" |
| 64 " description = ACTION //foo:bar()\n" | 68 " description = ACTION //foo:bar()\n" |
| 65 " restat = 1\n" | 69 " restat = 1\n" |
| 66 "build obj/foo/bar.inputdeps.stamp: stamp ../../foo/script.py " | 70 "build obj/foo/bar.inputdeps.stamp: stamp ../../foo/script.py " |
| 67 "../../foo/included.txt\n" | 71 "../../foo/included.txt\n" |
| 68 "\n" | 72 "\n" |
| 69 "build foo.out: __foo_bar___rule | obj/foo/bar.inputdeps.stamp\n" | 73 "build foo.out: __foo_bar___rule | obj/foo/bar.inputdeps.stamp\n" |
| 70 "\n" | 74 "\n" |
| 71 "build obj/foo/bar.stamp: stamp foo.out\n"; | 75 "build obj/foo/bar.stamp: stamp foo.out\n"; |
| 72 EXPECT_EQ(expected, out.str()); | 76 EXPECT_EQ(expected, out.str()); |
| 73 } | 77 } |
| 74 | 78 |
| 75 // Makes sure that we write sources as input dependencies for actions with | 79 // Makes sure that we write sources as input dependencies for actions with |
| 76 // both sources and inputs (ACTION_FOREACH treats the sources differently). | 80 // both sources and inputs (ACTION_FOREACH treats the sources differently). |
| 77 TEST(NinjaActionTargetWriter, ActionWithSources) { | 81 TEST(NinjaActionTargetWriter, ActionWithSources) { |
| 78 TestWithScope setup; | 82 TestWithScope setup; |
| 83 Err err; |
| 84 |
| 79 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | 85 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 80 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 86 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| 81 target.set_output_type(Target::ACTION); | 87 target.set_output_type(Target::ACTION); |
| 82 | 88 |
| 83 target.action_values().set_script(SourceFile("//foo/script.py")); | 89 target.action_values().set_script(SourceFile("//foo/script.py")); |
| 84 | 90 |
| 85 target.sources().push_back(SourceFile("//foo/source.txt")); | 91 target.sources().push_back(SourceFile("//foo/source.txt")); |
| 86 target.inputs().push_back(SourceFile("//foo/included.txt")); | 92 target.inputs().push_back(SourceFile("//foo/included.txt")); |
| 87 | 93 |
| 88 target.action_values().outputs() = | 94 target.action_values().outputs() = |
| 89 SubstitutionList::MakeForTest("//out/Debug/foo.out"); | 95 SubstitutionList::MakeForTest("//out/Debug/foo.out"); |
| 90 | 96 |
| 91 target.SetToolchain(setup.toolchain()); | 97 target.SetToolchain(setup.toolchain()); |
| 92 target.OnResolved(); | 98 ASSERT_TRUE(target.OnResolved(&err)); |
| 93 | 99 |
| 94 // Posix. | 100 // Posix. |
| 95 { | 101 { |
| 96 setup.settings()->set_target_os(Settings::LINUX); | 102 setup.settings()->set_target_os(Settings::LINUX); |
| 97 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( | 103 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( |
| 98 "/usr/bin/python"))); | 104 "/usr/bin/python"))); |
| 99 | 105 |
| 100 std::ostringstream out; | 106 std::ostringstream out; |
| 101 NinjaActionTargetWriter writer(&target, out); | 107 NinjaActionTargetWriter writer(&target, out); |
| 102 writer.Run(); | 108 writer.Run(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 "\n" | 145 "\n" |
| 140 "build foo.out: __foo_bar___rule | obj/foo/bar.inputdeps.stamp\n" | 146 "build foo.out: __foo_bar___rule | obj/foo/bar.inputdeps.stamp\n" |
| 141 "\n" | 147 "\n" |
| 142 "build obj/foo/bar.stamp: stamp foo.out\n"; | 148 "build obj/foo/bar.stamp: stamp foo.out\n"; |
| 143 EXPECT_EQ(expected_win, out.str()); | 149 EXPECT_EQ(expected_win, out.str()); |
| 144 } | 150 } |
| 145 } | 151 } |
| 146 | 152 |
| 147 TEST(NinjaActionTargetWriter, ForEach) { | 153 TEST(NinjaActionTargetWriter, ForEach) { |
| 148 TestWithScope setup; | 154 TestWithScope setup; |
| 155 Err err; |
| 156 |
| 149 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | 157 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 150 | 158 |
| 151 // Some dependencies that the action can depend on. Use actions for these | 159 // Some dependencies that the action can depend on. Use actions for these |
| 152 // so they have a nice platform-independent stamp file that can appear in the | 160 // so they have a nice platform-independent stamp file that can appear in the |
| 153 // output (rather than having to worry about how the current platform names | 161 // output (rather than having to worry about how the current platform names |
| 154 // binaries). | 162 // binaries). |
| 155 Target dep(setup.settings(), Label(SourceDir("//foo/"), "dep")); | 163 Target dep(setup.settings(), Label(SourceDir("//foo/"), "dep")); |
| 156 dep.set_output_type(Target::ACTION); | 164 dep.set_output_type(Target::ACTION); |
| 157 dep.SetToolchain(setup.toolchain()); | 165 dep.SetToolchain(setup.toolchain()); |
| 158 dep.OnResolved(); | 166 ASSERT_TRUE(dep.OnResolved(&err)); |
| 159 | 167 |
| 160 Target datadep(setup.settings(), Label(SourceDir("//foo/"), "datadep")); | 168 Target datadep(setup.settings(), Label(SourceDir("//foo/"), "datadep")); |
| 161 datadep.set_output_type(Target::ACTION); | 169 datadep.set_output_type(Target::ACTION); |
| 162 datadep.SetToolchain(setup.toolchain()); | 170 datadep.SetToolchain(setup.toolchain()); |
| 163 datadep.OnResolved(); | 171 ASSERT_TRUE(datadep.OnResolved(&err)); |
| 164 | 172 |
| 165 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 173 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| 166 target.set_output_type(Target::ACTION_FOREACH); | 174 target.set_output_type(Target::ACTION_FOREACH); |
| 167 target.deps().push_back(LabelTargetPair(&dep)); | 175 target.deps().push_back(LabelTargetPair(&dep)); |
| 168 target.datadeps().push_back(LabelTargetPair(&datadep)); | 176 target.datadeps().push_back(LabelTargetPair(&datadep)); |
| 169 | 177 |
| 170 target.sources().push_back(SourceFile("//foo/input1.txt")); | 178 target.sources().push_back(SourceFile("//foo/input1.txt")); |
| 171 target.sources().push_back(SourceFile("//foo/input2.txt")); | 179 target.sources().push_back(SourceFile("//foo/input2.txt")); |
| 172 | 180 |
| 173 target.action_values().set_script(SourceFile("//foo/script.py")); | 181 target.action_values().set_script(SourceFile("//foo/script.py")); |
| 174 | 182 |
| 175 target.action_values().args() = SubstitutionList::MakeForTest( | 183 target.action_values().args() = SubstitutionList::MakeForTest( |
| 176 "-i", | 184 "-i", |
| 177 "{{source}}", | 185 "{{source}}", |
| 178 "--out=foo bar{{source_name_part}}.o"); | 186 "--out=foo bar{{source_name_part}}.o"); |
| 179 target.action_values().outputs() = SubstitutionList::MakeForTest( | 187 target.action_values().outputs() = SubstitutionList::MakeForTest( |
| 180 "//out/Debug/{{source_name_part}}.out"); | 188 "//out/Debug/{{source_name_part}}.out"); |
| 181 | 189 |
| 182 target.inputs().push_back(SourceFile("//foo/included.txt")); | 190 target.inputs().push_back(SourceFile("//foo/included.txt")); |
| 183 | 191 |
| 184 target.SetToolchain(setup.toolchain()); | 192 target.SetToolchain(setup.toolchain()); |
| 185 target.OnResolved(); | 193 ASSERT_TRUE(target.OnResolved(&err)); |
| 186 | 194 |
| 187 // Posix. | 195 // Posix. |
| 188 { | 196 { |
| 189 setup.settings()->set_target_os(Settings::LINUX); | 197 setup.settings()->set_target_os(Settings::LINUX); |
| 190 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( | 198 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( |
| 191 "/usr/bin/python"))); | 199 "/usr/bin/python"))); |
| 192 | 200 |
| 193 std::ostringstream out; | 201 std::ostringstream out; |
| 194 NinjaActionTargetWriter writer(&target, out); | 202 NinjaActionTargetWriter writer(&target, out); |
| 195 writer.Run(); | 203 writer.Run(); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 " source_name_part = input2\n" | 269 " source_name_part = input2\n" |
| 262 "\n" | 270 "\n" |
| 263 "build obj/foo/bar.stamp: " | 271 "build obj/foo/bar.stamp: " |
| 264 "stamp input1.out input2.out || obj/foo/datadep.stamp\n"; | 272 "stamp input1.out input2.out || obj/foo/datadep.stamp\n"; |
| 265 EXPECT_EQ(expected_win, out.str()); | 273 EXPECT_EQ(expected_win, out.str()); |
| 266 } | 274 } |
| 267 } | 275 } |
| 268 | 276 |
| 269 TEST(NinjaActionTargetWriter, ForEachWithDepfile) { | 277 TEST(NinjaActionTargetWriter, ForEachWithDepfile) { |
| 270 TestWithScope setup; | 278 TestWithScope setup; |
| 279 Err err; |
| 280 |
| 271 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | 281 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 272 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 282 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| 273 target.set_output_type(Target::ACTION_FOREACH); | 283 target.set_output_type(Target::ACTION_FOREACH); |
| 274 | 284 |
| 275 target.sources().push_back(SourceFile("//foo/input1.txt")); | 285 target.sources().push_back(SourceFile("//foo/input1.txt")); |
| 276 target.sources().push_back(SourceFile("//foo/input2.txt")); | 286 target.sources().push_back(SourceFile("//foo/input2.txt")); |
| 277 | 287 |
| 278 target.action_values().set_script(SourceFile("//foo/script.py")); | 288 target.action_values().set_script(SourceFile("//foo/script.py")); |
| 279 | 289 |
| 280 target.SetToolchain(setup.toolchain()); | 290 target.SetToolchain(setup.toolchain()); |
| 281 target.OnResolved(); | 291 ASSERT_TRUE(target.OnResolved(&err)); |
| 282 | 292 |
| 283 SubstitutionPattern depfile; | 293 SubstitutionPattern depfile; |
| 284 Err err; | |
| 285 ASSERT_TRUE( | 294 ASSERT_TRUE( |
| 286 depfile.Parse("//out/Debug/gen/{{source_name_part}}.d", NULL, &err)); | 295 depfile.Parse("//out/Debug/gen/{{source_name_part}}.d", NULL, &err)); |
| 287 target.action_values().set_depfile(depfile); | 296 target.action_values().set_depfile(depfile); |
| 288 | 297 |
| 289 target.action_values().args() = SubstitutionList::MakeForTest( | 298 target.action_values().args() = SubstitutionList::MakeForTest( |
| 290 "-i", | 299 "-i", |
| 291 "{{source}}", | 300 "{{source}}", |
| 292 "--out=foo bar{{source_name_part}}.o"); | 301 "--out=foo bar{{source_name_part}}.o"); |
| 293 target.action_values().outputs() = SubstitutionList::MakeForTest( | 302 target.action_values().outputs() = SubstitutionList::MakeForTest( |
| 294 "//out/Debug/{{source_name_part}}.out"); | 303 "//out/Debug/{{source_name_part}}.out"); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 "build input2.out: __foo_bar___rule ../../foo/input2.txt" | 374 "build input2.out: __foo_bar___rule ../../foo/input2.txt" |
| 366 " | obj/foo/bar.inputdeps.stamp\n" | 375 " | obj/foo/bar.inputdeps.stamp\n" |
| 367 " unique_name = 1\n" | 376 " unique_name = 1\n" |
| 368 " source_name_part = input2\n" | 377 " source_name_part = input2\n" |
| 369 " depfile = gen/input2.d\n" | 378 " depfile = gen/input2.d\n" |
| 370 "\n" | 379 "\n" |
| 371 "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; | 380 "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; |
| 372 EXPECT_EQ(expected_win, out.str()); | 381 EXPECT_EQ(expected_win, out.str()); |
| 373 } | 382 } |
| 374 } | 383 } |
| OLD | NEW |