OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <sstream> |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "tools/gn/ninja_build_writer.h" |
| 9 #include "tools/gn/scheduler.h" |
| 10 #include "tools/gn/target.h" |
| 11 #include "tools/gn/test_with_scope.h" |
| 12 |
| 13 TEST(NinjaBuildWriter, TwoTargets) { |
| 14 Scheduler scheduler; |
| 15 TestWithScope setup; |
| 16 Err err; |
| 17 |
| 18 Target target_foo(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| 19 target_foo.set_output_type(Target::ACTION); |
| 20 target_foo.action_values().set_script(SourceFile("//foo/script.py")); |
| 21 target_foo.action_values().outputs() = SubstitutionList::MakeForTest( |
| 22 "//out/Debug/out1.out", "//out/Debug/out2.out"); |
| 23 target_foo.SetToolchain(setup.toolchain()); |
| 24 ASSERT_TRUE(target_foo.OnResolved(&err)); |
| 25 |
| 26 Target target_bar(setup.settings(), Label(SourceDir("//bar/"), "bar")); |
| 27 target_bar.set_output_type(Target::ACTION); |
| 28 target_bar.action_values().set_script(SourceFile("//bar/script.py")); |
| 29 target_bar.action_values().outputs() = SubstitutionList::MakeForTest( |
| 30 "//out/Debug/out3.out", "//out/Debug/out4.out"); |
| 31 target_bar.SetToolchain(setup.toolchain()); |
| 32 ASSERT_TRUE(target_bar.OnResolved(&err)); |
| 33 |
| 34 std::ostringstream ninja_out; |
| 35 std::ostringstream depfile_out; |
| 36 std::vector<const Settings*> all_settings = {setup.settings()}; |
| 37 std::vector<const Target*> targets = {&target_foo, &target_bar}; |
| 38 NinjaBuildWriter writer(setup.build_settings(), all_settings, |
| 39 setup.toolchain(), targets, ninja_out, depfile_out); |
| 40 ASSERT_TRUE(writer.Run(&err)); |
| 41 |
| 42 const char expected_rule_gn[] = "rule gn\n"; |
| 43 const char expected_build_ninja[] = |
| 44 "build build.ninja: gn\n" |
| 45 " generator = 1\n" |
| 46 " depfile = build.ninja.d\n" |
| 47 "\n"; |
| 48 const char expected_link_pool[] = |
| 49 "pool link_pool\n" |
| 50 " depth = 0\n" |
| 51 "\n"; |
| 52 const char expected_toolchain[] = |
| 53 "subninja toolchain.ninja\n" |
| 54 "\n"; |
| 55 const char expected_targets[] = |
| 56 "build foo$:bar: phony obj/foo/bar.stamp\n" |
| 57 "build bar$:bar: phony obj/bar/bar.stamp\n" |
| 58 "build bar: phony obj/bar/bar.stamp\n" |
| 59 "\n"; |
| 60 const char expected_root_target[] = |
| 61 "build all: phony obj/foo/bar.stamp $\n" |
| 62 " obj/bar/bar.stamp\n" |
| 63 "default all\n"; |
| 64 std::string out_str = ninja_out.str(); |
| 65 #define EXPECT_SNIPPET(expected) \ |
| 66 EXPECT_NE(std::string::npos, out_str.find(expected)) << \ |
| 67 "Expected to find: " << expected << std::endl << \ |
| 68 "Within: " << out_str |
| 69 EXPECT_SNIPPET(expected_rule_gn); |
| 70 EXPECT_SNIPPET(expected_build_ninja); |
| 71 EXPECT_SNIPPET(expected_link_pool); |
| 72 EXPECT_SNIPPET(expected_toolchain); |
| 73 EXPECT_SNIPPET(expected_targets); |
| 74 EXPECT_SNIPPET(expected_root_target); |
| 75 #undef EXPECT_SNIPPET |
| 76 } |
| 77 |
| 78 TEST(NinjaBuildWriter, DuplicateOutputs) { |
| 79 Scheduler scheduler; |
| 80 TestWithScope setup; |
| 81 Err err; |
| 82 |
| 83 Target target_foo(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| 84 target_foo.set_output_type(Target::ACTION); |
| 85 target_foo.action_values().set_script(SourceFile("//foo/script.py")); |
| 86 target_foo.action_values().outputs() = SubstitutionList::MakeForTest( |
| 87 "//out/Debug/out1.out", "//out/Debug/out2.out"); |
| 88 target_foo.SetToolchain(setup.toolchain()); |
| 89 ASSERT_TRUE(target_foo.OnResolved(&err)); |
| 90 |
| 91 Target target_bar(setup.settings(), Label(SourceDir("//bar/"), "bar")); |
| 92 target_bar.set_output_type(Target::ACTION); |
| 93 target_bar.action_values().set_script(SourceFile("//bar/script.py")); |
| 94 target_bar.action_values().outputs() = SubstitutionList::MakeForTest( |
| 95 "//out/Debug/out3.out", "//out/Debug/out2.out"); |
| 96 target_bar.SetToolchain(setup.toolchain()); |
| 97 ASSERT_TRUE(target_bar.OnResolved(&err)); |
| 98 |
| 99 std::ostringstream ninja_out; |
| 100 std::ostringstream depfile_out; |
| 101 std::vector<const Settings*> all_settings = { setup.settings() }; |
| 102 std::vector<const Target*> targets = { &target_foo, &target_bar }; |
| 103 NinjaBuildWriter writer(setup.build_settings(), all_settings, |
| 104 setup.toolchain(), targets, ninja_out, depfile_out); |
| 105 ASSERT_FALSE(writer.Run(&err)); |
| 106 |
| 107 const char expected_help_test[] = |
| 108 "Two or more targets generate the same output:\n" |
| 109 " out2.out\n" |
| 110 "\n" |
| 111 "This is can often be fixed by changing one of the target names, or by \n" |
| 112 "setting an output_name on one of them.\n" |
| 113 "\n" |
| 114 "Collisions:\n" |
| 115 " //foo:bar\n" |
| 116 " //bar:bar\n"; |
| 117 |
| 118 EXPECT_EQ(expected_help_test, err.help_text()); |
| 119 } |
OLD | NEW |