| 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 <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_binary_target_writer.h" | 8 #include "tools/gn/ninja_binary_target_writer.h" |
| 9 #include "tools/gn/target.h" | 9 #include "tools/gn/target.h" |
| 10 #include "tools/gn/test_with_scope.h" | 10 #include "tools/gn/test_with_scope.h" |
| 11 | 11 |
| 12 TEST(NinjaBinaryTargetWriter, SourceSet) { | 12 TEST(NinjaBinaryTargetWriter, SourceSet) { |
| 13 TestWithScope setup; | 13 TestWithScope setup; |
| 14 Err err; |
| 15 |
| 14 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | 16 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 15 setup.settings()->set_target_os(Settings::WIN); | 17 setup.settings()->set_target_os(Settings::WIN); |
| 16 | 18 |
| 17 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 19 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| 18 target.set_output_type(Target::SOURCE_SET); | 20 target.set_output_type(Target::SOURCE_SET); |
| 19 target.sources().push_back(SourceFile("//foo/input1.cc")); | 21 target.sources().push_back(SourceFile("//foo/input1.cc")); |
| 20 target.sources().push_back(SourceFile("//foo/input2.cc")); | 22 target.sources().push_back(SourceFile("//foo/input2.cc")); |
| 21 // Also test object files, which should be just passed through to the | 23 // Also test object files, which should be just passed through to the |
| 22 // dependents to link. | 24 // dependents to link. |
| 23 target.sources().push_back(SourceFile("//foo/input3.o")); | 25 target.sources().push_back(SourceFile("//foo/input3.o")); |
| 24 target.sources().push_back(SourceFile("//foo/input4.obj")); | 26 target.sources().push_back(SourceFile("//foo/input4.obj")); |
| 25 target.SetToolchain(setup.toolchain()); | 27 target.SetToolchain(setup.toolchain()); |
| 26 target.OnResolved(); | 28 ASSERT_TRUE(target.OnResolved(&err)); |
| 27 | 29 |
| 28 // Source set itself. | 30 // Source set itself. |
| 29 { | 31 { |
| 30 std::ostringstream out; | 32 std::ostringstream out; |
| 31 NinjaBinaryTargetWriter writer(&target, out); | 33 NinjaBinaryTargetWriter writer(&target, out); |
| 32 writer.Run(); | 34 writer.Run(); |
| 33 | 35 |
| 34 const char expected[] = | 36 const char expected[] = |
| 35 "defines =\n" | 37 "defines =\n" |
| 36 "include_dirs =\n" | 38 "include_dirs =\n" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 50 "obj/foo/bar.input2.o ../../foo/input3.o ../../foo/input4.obj\n"; | 52 "obj/foo/bar.input2.o ../../foo/input3.o ../../foo/input4.obj\n"; |
| 51 std::string out_str = out.str(); | 53 std::string out_str = out.str(); |
| 52 EXPECT_EQ(expected, out_str); | 54 EXPECT_EQ(expected, out_str); |
| 53 } | 55 } |
| 54 | 56 |
| 55 // A shared library that depends on the source set. | 57 // A shared library that depends on the source set. |
| 56 Target shlib_target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); | 58 Target shlib_target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); |
| 57 shlib_target.set_output_type(Target::SHARED_LIBRARY); | 59 shlib_target.set_output_type(Target::SHARED_LIBRARY); |
| 58 shlib_target.deps().push_back(LabelTargetPair(&target)); | 60 shlib_target.deps().push_back(LabelTargetPair(&target)); |
| 59 shlib_target.SetToolchain(setup.toolchain()); | 61 shlib_target.SetToolchain(setup.toolchain()); |
| 60 shlib_target.OnResolved(); | 62 ASSERT_TRUE(shlib_target.OnResolved(&err)); |
| 61 | 63 |
| 62 { | 64 { |
| 63 std::ostringstream out; | 65 std::ostringstream out; |
| 64 NinjaBinaryTargetWriter writer(&shlib_target, out); | 66 NinjaBinaryTargetWriter writer(&shlib_target, out); |
| 65 writer.Run(); | 67 writer.Run(); |
| 66 | 68 |
| 67 const char expected[] = | 69 const char expected[] = |
| 68 "defines =\n" | 70 "defines =\n" |
| 69 "include_dirs =\n" | 71 "include_dirs =\n" |
| 70 "cflags =\n" | 72 "cflags =\n" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 87 " output_extension = .so\n"; | 89 " output_extension = .so\n"; |
| 88 std::string out_str = out.str(); | 90 std::string out_str = out.str(); |
| 89 EXPECT_EQ(expected, out_str); | 91 EXPECT_EQ(expected, out_str); |
| 90 } | 92 } |
| 91 | 93 |
| 92 // A static library that depends on the source set (should not link it). | 94 // A static library that depends on the source set (should not link it). |
| 93 Target stlib_target(setup.settings(), Label(SourceDir("//foo/"), "stlib")); | 95 Target stlib_target(setup.settings(), Label(SourceDir("//foo/"), "stlib")); |
| 94 stlib_target.set_output_type(Target::STATIC_LIBRARY); | 96 stlib_target.set_output_type(Target::STATIC_LIBRARY); |
| 95 stlib_target.deps().push_back(LabelTargetPair(&target)); | 97 stlib_target.deps().push_back(LabelTargetPair(&target)); |
| 96 stlib_target.SetToolchain(setup.toolchain()); | 98 stlib_target.SetToolchain(setup.toolchain()); |
| 97 stlib_target.OnResolved(); | 99 ASSERT_TRUE(stlib_target.OnResolved(&err)); |
| 98 | 100 |
| 99 { | 101 { |
| 100 std::ostringstream out; | 102 std::ostringstream out; |
| 101 NinjaBinaryTargetWriter writer(&stlib_target, out); | 103 NinjaBinaryTargetWriter writer(&stlib_target, out); |
| 102 writer.Run(); | 104 writer.Run(); |
| 103 | 105 |
| 104 const char expected[] = | 106 const char expected[] = |
| 105 "defines =\n" | 107 "defines =\n" |
| 106 "include_dirs =\n" | 108 "include_dirs =\n" |
| 107 "cflags =\n" | 109 "cflags =\n" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 122 " output_extension = \n"; | 124 " output_extension = \n"; |
| 123 std::string out_str = out.str(); | 125 std::string out_str = out.str(); |
| 124 EXPECT_EQ(expected, out_str); | 126 EXPECT_EQ(expected, out_str); |
| 125 } | 127 } |
| 126 } | 128 } |
| 127 | 129 |
| 128 // This tests that output extension overrides apply, and input dependencies | 130 // This tests that output extension overrides apply, and input dependencies |
| 129 // are applied. | 131 // are applied. |
| 130 TEST(NinjaBinaryTargetWriter, ProductExtensionAndInputDeps) { | 132 TEST(NinjaBinaryTargetWriter, ProductExtensionAndInputDeps) { |
| 131 TestWithScope setup; | 133 TestWithScope setup; |
| 134 Err err; |
| 135 |
| 132 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | 136 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 133 setup.settings()->set_target_os(Settings::LINUX); | 137 setup.settings()->set_target_os(Settings::LINUX); |
| 134 | 138 |
| 135 // An action for our library to depend on. | 139 // An action for our library to depend on. |
| 136 Target action(setup.settings(), Label(SourceDir("//foo/"), "action")); | 140 Target action(setup.settings(), Label(SourceDir("//foo/"), "action")); |
| 137 action.set_output_type(Target::ACTION_FOREACH); | 141 action.set_output_type(Target::ACTION_FOREACH); |
| 138 action.SetToolchain(setup.toolchain()); | 142 action.SetToolchain(setup.toolchain()); |
| 139 action.OnResolved(); | 143 ASSERT_TRUE(action.OnResolved(&err)); |
| 140 | 144 |
| 141 // A shared library w/ the product_extension set to a custom value. | 145 // A shared library w/ the product_extension set to a custom value. |
| 142 Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); | 146 Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); |
| 143 target.set_output_type(Target::SHARED_LIBRARY); | 147 target.set_output_type(Target::SHARED_LIBRARY); |
| 144 target.set_output_extension(std::string("so.6")); | 148 target.set_output_extension(std::string("so.6")); |
| 145 target.sources().push_back(SourceFile("//foo/input1.cc")); | 149 target.sources().push_back(SourceFile("//foo/input1.cc")); |
| 146 target.sources().push_back(SourceFile("//foo/input2.cc")); | 150 target.sources().push_back(SourceFile("//foo/input2.cc")); |
| 147 target.deps().push_back(LabelTargetPair(&action)); | 151 target.deps().push_back(LabelTargetPair(&action)); |
| 148 target.SetToolchain(setup.toolchain()); | 152 target.SetToolchain(setup.toolchain()); |
| 149 target.OnResolved(); | 153 ASSERT_TRUE(target.OnResolved(&err)); |
| 150 | 154 |
| 151 std::ostringstream out; | 155 std::ostringstream out; |
| 152 NinjaBinaryTargetWriter writer(&target, out); | 156 NinjaBinaryTargetWriter writer(&target, out); |
| 153 writer.Run(); | 157 writer.Run(); |
| 154 | 158 |
| 155 const char expected[] = | 159 const char expected[] = |
| 156 "defines =\n" | 160 "defines =\n" |
| 157 "include_dirs =\n" | 161 "include_dirs =\n" |
| 158 "cflags =\n" | 162 "cflags =\n" |
| 159 "cflags_c =\n" | 163 "cflags_c =\n" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 178 " ldflags =\n" | 182 " ldflags =\n" |
| 179 " libs =\n" | 183 " libs =\n" |
| 180 " output_extension = .so.6\n"; | 184 " output_extension = .so.6\n"; |
| 181 | 185 |
| 182 std::string out_str = out.str(); | 186 std::string out_str = out.str(); |
| 183 EXPECT_EQ(expected, out_str); | 187 EXPECT_EQ(expected, out_str); |
| 184 } | 188 } |
| 185 | 189 |
| 186 TEST(NinjaBinaryTargetWriter, EmptyProductExtension) { | 190 TEST(NinjaBinaryTargetWriter, EmptyProductExtension) { |
| 187 TestWithScope setup; | 191 TestWithScope setup; |
| 192 Err err; |
| 193 |
| 188 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | 194 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 189 setup.settings()->set_target_os(Settings::LINUX); | 195 setup.settings()->set_target_os(Settings::LINUX); |
| 190 | 196 |
| 191 // This test is the same as ProductExtension, except that | 197 // This test is the same as ProductExtension, except that |
| 192 // we call set_output_extension("") and ensure that we still get the default. | 198 // we call set_output_extension("") and ensure that we still get the default. |
| 193 Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); | 199 Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); |
| 194 target.set_output_type(Target::SHARED_LIBRARY); | 200 target.set_output_type(Target::SHARED_LIBRARY); |
| 195 target.set_output_extension(std::string()); | 201 target.set_output_extension(std::string()); |
| 196 target.sources().push_back(SourceFile("//foo/input1.cc")); | 202 target.sources().push_back(SourceFile("//foo/input1.cc")); |
| 197 target.sources().push_back(SourceFile("//foo/input2.cc")); | 203 target.sources().push_back(SourceFile("//foo/input2.cc")); |
| 198 | 204 |
| 199 target.SetToolchain(setup.toolchain()); | 205 target.SetToolchain(setup.toolchain()); |
| 200 target.OnResolved(); | 206 ASSERT_TRUE(target.OnResolved(&err)); |
| 201 | 207 |
| 202 std::ostringstream out; | 208 std::ostringstream out; |
| 203 NinjaBinaryTargetWriter writer(&target, out); | 209 NinjaBinaryTargetWriter writer(&target, out); |
| 204 writer.Run(); | 210 writer.Run(); |
| 205 | 211 |
| 206 const char expected[] = | 212 const char expected[] = |
| 207 "defines =\n" | 213 "defines =\n" |
| 208 "include_dirs =\n" | 214 "include_dirs =\n" |
| 209 "cflags =\n" | 215 "cflags =\n" |
| 210 "cflags_c =\n" | 216 "cflags_c =\n" |
| 211 "cflags_cc =\n" | 217 "cflags_cc =\n" |
| 212 "cflags_objc =\n" | 218 "cflags_objc =\n" |
| 213 "cflags_objcc =\n" | 219 "cflags_objcc =\n" |
| 214 "root_out_dir = .\n" | 220 "root_out_dir = .\n" |
| 215 "target_out_dir = obj/foo\n" | 221 "target_out_dir = obj/foo\n" |
| 216 "target_output_name = libshlib\n" | 222 "target_output_name = libshlib\n" |
| 217 "\n" | 223 "\n" |
| 218 "build obj/foo/libshlib.input1.o: cxx ../../foo/input1.cc\n" | 224 "build obj/foo/libshlib.input1.o: cxx ../../foo/input1.cc\n" |
| 219 "build obj/foo/libshlib.input2.o: cxx ../../foo/input2.cc\n" | 225 "build obj/foo/libshlib.input2.o: cxx ../../foo/input2.cc\n" |
| 220 "\n" | 226 "\n" |
| 221 "build ./libshlib.so: solink obj/foo/libshlib.input1.o " | 227 "build ./libshlib.so: solink obj/foo/libshlib.input1.o " |
| 222 "obj/foo/libshlib.input2.o\n" | 228 "obj/foo/libshlib.input2.o\n" |
| 223 " ldflags =\n" | 229 " ldflags =\n" |
| 224 " libs =\n" | 230 " libs =\n" |
| 225 " output_extension = .so\n"; | 231 " output_extension = .so\n"; |
| 226 | 232 |
| 227 std::string out_str = out.str(); | 233 std::string out_str = out.str(); |
| 228 EXPECT_EQ(expected, out_str); | 234 EXPECT_EQ(expected, out_str); |
| 229 } | 235 } |
| OLD | NEW |