| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <sstream> | 5 #include <sstream> |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "tools/gn/ninja_build_writer.h" | 8 #include "tools/gn/ninja_build_writer.h" |
| 9 #include "tools/gn/pool.h" | 9 #include "tools/gn/pool.h" |
| 10 #include "tools/gn/scheduler.h" | 10 #include "tools/gn/scheduler.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 ASSERT_TRUE(target_foo.OnResolved(&err)); | 25 ASSERT_TRUE(target_foo.OnResolved(&err)); |
| 26 | 26 |
| 27 Target target_bar(setup.settings(), Label(SourceDir("//bar/"), "bar")); | 27 Target target_bar(setup.settings(), Label(SourceDir("//bar/"), "bar")); |
| 28 target_bar.set_output_type(Target::ACTION); | 28 target_bar.set_output_type(Target::ACTION); |
| 29 target_bar.action_values().set_script(SourceFile("//bar/script.py")); | 29 target_bar.action_values().set_script(SourceFile("//bar/script.py")); |
| 30 target_bar.action_values().outputs() = SubstitutionList::MakeForTest( | 30 target_bar.action_values().outputs() = SubstitutionList::MakeForTest( |
| 31 "//out/Debug/out3.out", "//out/Debug/out4.out"); | 31 "//out/Debug/out3.out", "//out/Debug/out4.out"); |
| 32 target_bar.SetToolchain(setup.toolchain()); | 32 target_bar.SetToolchain(setup.toolchain()); |
| 33 ASSERT_TRUE(target_bar.OnResolved(&err)); | 33 ASSERT_TRUE(target_bar.OnResolved(&err)); |
| 34 | 34 |
| 35 Pool swiming_pool(setup.settings(), | 35 // Make a secondary toolchain that references a pool. |
| 36 Label(SourceDir("//swiming/"), "pool", | 36 Label other_toolchain_label(SourceDir("//other/"), "toolchain"); |
| 37 SourceDir("//other/"), "toolchain")); | 37 Pool other_pool(setup.settings(), |
| 38 swiming_pool.set_depth(42); | 38 Label(SourceDir("//other/"), "pool", |
| 39 other_toolchain_label.dir(), |
| 40 other_toolchain_label.name())); |
| 41 other_pool.set_depth(42); |
| 42 Toolchain other_toolchain(setup.settings(), other_toolchain_label); |
| 43 TestWithScope::SetupToolchain(&other_toolchain); |
| 44 other_toolchain.GetTool(Toolchain::TYPE_LINK)->set_pool( |
| 45 LabelPtrPair<Pool>(&other_pool)); |
| 46 |
| 47 // Settings to go with the other toolchain. |
| 48 Settings other_settings(setup.build_settings(), "toolchain/"); |
| 49 other_settings.set_toolchain_label(other_toolchain_label); |
| 50 |
| 51 std::map<const Settings*, const Toolchain*> used_toolchains; |
| 52 used_toolchains[setup.settings()] = setup.toolchain(); |
| 53 used_toolchains[&other_settings] = &other_toolchain; |
| 54 |
| 55 std::vector<const Target*> targets = { &target_foo, &target_bar }; |
| 39 | 56 |
| 40 std::ostringstream ninja_out; | 57 std::ostringstream ninja_out; |
| 41 std::ostringstream depfile_out; | 58 std::ostringstream depfile_out; |
| 42 std::vector<const Settings*> all_settings = {setup.settings()}; | 59 |
| 43 std::vector<const Target*> targets = {&target_foo, &target_bar}; | 60 NinjaBuildWriter writer(setup.build_settings(), used_toolchains, |
| 44 std::vector<const Pool*> all_pools = {&swiming_pool}; | 61 setup.toolchain(), targets, ninja_out, depfile_out); |
| 45 NinjaBuildWriter writer(setup.build_settings(), all_settings, | |
| 46 setup.toolchain(), targets, all_pools, ninja_out, | |
| 47 depfile_out); | |
| 48 ASSERT_TRUE(writer.Run(&err)); | 62 ASSERT_TRUE(writer.Run(&err)); |
| 49 | 63 |
| 50 const char expected_rule_gn[] = "rule gn\n"; | 64 const char expected_rule_gn[] = "rule gn\n"; |
| 51 const char expected_build_ninja[] = | 65 const char expected_build_ninja[] = |
| 52 "build build.ninja: gn\n" | 66 "build build.ninja: gn\n" |
| 53 " generator = 1\n" | 67 " generator = 1\n" |
| 54 " depfile = build.ninja.d\n" | 68 " depfile = build.ninja.d\n"; |
| 55 "\n"; | |
| 56 const char expected_link_pool[] = | 69 const char expected_link_pool[] = |
| 57 "pool link_pool\n" | 70 "pool link_pool\n" |
| 58 " depth = 0\n" | 71 " depth = 0\n"; |
| 59 "\n" | 72 const char expected_other_pool[] = |
| 60 "pool other_toolchain_swiming_pool\n" | 73 "pool other_toolchain_other_pool\n" |
| 61 " depth = 42\n" | 74 " depth = 42\n"; |
| 62 "\n"; | |
| 63 const char expected_toolchain[] = | 75 const char expected_toolchain[] = |
| 64 "subninja toolchain.ninja\n" | 76 "subninja toolchain.ninja\n"; |
| 65 "\n"; | |
| 66 const char expected_targets[] = | 77 const char expected_targets[] = |
| 67 "build bar: phony obj/bar/bar.stamp\n" | 78 "build bar: phony obj/bar/bar.stamp\n" |
| 68 "build foo$:bar: phony obj/foo/bar.stamp\n" | 79 "build foo$:bar: phony obj/foo/bar.stamp\n" |
| 69 "build bar$:bar: phony obj/bar/bar.stamp\n" | 80 "build bar$:bar: phony obj/bar/bar.stamp\n"; |
| 70 "\n"; | |
| 71 const char expected_root_target[] = | 81 const char expected_root_target[] = |
| 72 "build all: phony $\n" | 82 "build all: phony $\n" |
| 73 " obj/foo/bar.stamp $\n" | 83 " obj/foo/bar.stamp $\n" |
| 74 " obj/bar/bar.stamp\n" | 84 " obj/bar/bar.stamp\n"; |
| 75 "\n" | 85 const char expected_default[] = |
| 76 "default all\n"; | 86 "default all\n"; |
| 77 std::string out_str = ninja_out.str(); | 87 std::string out_str = ninja_out.str(); |
| 78 #define EXPECT_SNIPPET(expected) \ | 88 #define EXPECT_SNIPPET(expected) \ |
| 79 EXPECT_NE(std::string::npos, out_str.find(expected)) << \ | 89 EXPECT_NE(std::string::npos, out_str.find(expected)) << \ |
| 80 "Expected to find: " << expected << std::endl << \ | 90 "Expected to find: " << expected << std::endl << \ |
| 81 "Within: " << out_str | 91 "Within: " << out_str |
| 82 EXPECT_SNIPPET(expected_rule_gn); | 92 EXPECT_SNIPPET(expected_rule_gn); |
| 83 EXPECT_SNIPPET(expected_build_ninja); | 93 EXPECT_SNIPPET(expected_build_ninja); |
| 84 EXPECT_SNIPPET(expected_link_pool); | 94 EXPECT_SNIPPET(expected_link_pool); |
| 95 EXPECT_SNIPPET(expected_other_pool); |
| 85 EXPECT_SNIPPET(expected_toolchain); | 96 EXPECT_SNIPPET(expected_toolchain); |
| 86 EXPECT_SNIPPET(expected_targets); | 97 EXPECT_SNIPPET(expected_targets); |
| 87 EXPECT_SNIPPET(expected_root_target); | 98 EXPECT_SNIPPET(expected_root_target); |
| 99 EXPECT_SNIPPET(expected_default); |
| 88 #undef EXPECT_SNIPPET | 100 #undef EXPECT_SNIPPET |
| 89 } | 101 } |
| 90 | 102 |
| 91 TEST(NinjaBuildWriter, DuplicateOutputs) { | 103 TEST(NinjaBuildWriter, DuplicateOutputs) { |
| 92 Scheduler scheduler; | 104 Scheduler scheduler; |
| 93 TestWithScope setup; | 105 TestWithScope setup; |
| 94 Err err; | 106 Err err; |
| 95 | 107 |
| 96 Target target_foo(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 108 Target target_foo(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| 97 target_foo.set_output_type(Target::ACTION); | 109 target_foo.set_output_type(Target::ACTION); |
| 98 target_foo.action_values().set_script(SourceFile("//foo/script.py")); | 110 target_foo.action_values().set_script(SourceFile("//foo/script.py")); |
| 99 target_foo.action_values().outputs() = SubstitutionList::MakeForTest( | 111 target_foo.action_values().outputs() = SubstitutionList::MakeForTest( |
| 100 "//out/Debug/out1.out", "//out/Debug/out2.out"); | 112 "//out/Debug/out1.out", "//out/Debug/out2.out"); |
| 101 target_foo.SetToolchain(setup.toolchain()); | 113 target_foo.SetToolchain(setup.toolchain()); |
| 102 ASSERT_TRUE(target_foo.OnResolved(&err)); | 114 ASSERT_TRUE(target_foo.OnResolved(&err)); |
| 103 | 115 |
| 104 Target target_bar(setup.settings(), Label(SourceDir("//bar/"), "bar")); | 116 Target target_bar(setup.settings(), Label(SourceDir("//bar/"), "bar")); |
| 105 target_bar.set_output_type(Target::ACTION); | 117 target_bar.set_output_type(Target::ACTION); |
| 106 target_bar.action_values().set_script(SourceFile("//bar/script.py")); | 118 target_bar.action_values().set_script(SourceFile("//bar/script.py")); |
| 107 target_bar.action_values().outputs() = SubstitutionList::MakeForTest( | 119 target_bar.action_values().outputs() = SubstitutionList::MakeForTest( |
| 108 "//out/Debug/out3.out", "//out/Debug/out2.out"); | 120 "//out/Debug/out3.out", "//out/Debug/out2.out"); |
| 109 target_bar.SetToolchain(setup.toolchain()); | 121 target_bar.SetToolchain(setup.toolchain()); |
| 110 ASSERT_TRUE(target_bar.OnResolved(&err)); | 122 ASSERT_TRUE(target_bar.OnResolved(&err)); |
| 111 | 123 |
| 124 std::map<const Settings*, const Toolchain*> used_toolchains; |
| 125 used_toolchains[setup.settings()] = setup.toolchain(); |
| 126 std::vector<const Target*> targets = { &target_foo, &target_bar }; |
| 112 std::ostringstream ninja_out; | 127 std::ostringstream ninja_out; |
| 113 std::ostringstream depfile_out; | 128 std::ostringstream depfile_out; |
| 114 std::vector<const Settings*> all_settings = { setup.settings() }; | 129 NinjaBuildWriter writer(setup.build_settings(), used_toolchains, |
| 115 std::vector<const Target*> targets = { &target_foo, &target_bar }; | 130 setup.toolchain(), targets, ninja_out, depfile_out); |
| 116 std::vector<const Pool*> all_pools; | |
| 117 NinjaBuildWriter writer(setup.build_settings(), all_settings, | |
| 118 setup.toolchain(), targets, all_pools, ninja_out, | |
| 119 depfile_out); | |
| 120 ASSERT_FALSE(writer.Run(&err)); | 131 ASSERT_FALSE(writer.Run(&err)); |
| 121 | 132 |
| 122 const char expected_help_test[] = | 133 const char expected_help_test[] = |
| 123 "Two or more targets generate the same output:\n" | 134 "Two or more targets generate the same output:\n" |
| 124 " out2.out\n" | 135 " out2.out\n" |
| 125 "\n" | 136 "\n" |
| 126 "This is can often be fixed by changing one of the target names, or by \n" | 137 "This is can often be fixed by changing one of the target names, or by \n" |
| 127 "setting an output_name on one of them.\n" | 138 "setting an output_name on one of them.\n" |
| 128 "\n" | 139 "\n" |
| 129 "Collisions:\n" | 140 "Collisions:\n" |
| 130 " //foo:bar\n" | 141 " //foo:bar\n" |
| 131 " //bar:bar\n"; | 142 " //bar:bar\n"; |
| 132 | 143 |
| 133 EXPECT_EQ(expected_help_test, err.help_text()); | 144 EXPECT_EQ(expected_help_test, err.help_text()); |
| 134 } | 145 } |
| OLD | NEW |