| 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/test_with_scope.h" | 10 #include "tools/gn/test_with_scope.h" |
| 10 | 11 |
| 11 TEST(NinjaBinaryTargetWriter, SourceSet) { | 12 TEST(NinjaBinaryTargetWriter, SourceSet) { |
| 12 TestWithScope setup; | 13 TestWithScope setup; |
| 13 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | 14 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 14 setup.settings()->set_target_os(Settings::WIN); | 15 setup.settings()->set_target_os(Settings::WIN); |
| 15 | 16 |
| 16 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 17 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| 17 target.set_output_type(Target::SOURCE_SET); | 18 target.set_output_type(Target::SOURCE_SET); |
| 18 target.sources().push_back(SourceFile("//foo/input1.cc")); | 19 target.sources().push_back(SourceFile("//foo/input1.cc")); |
| 19 target.sources().push_back(SourceFile("//foo/input2.cc")); | 20 target.sources().push_back(SourceFile("//foo/input2.cc")); |
| 20 // Also test object files, which should be just passed through to the | 21 // Also test object files, which should be just passed through to the |
| 21 // dependents to link. | 22 // dependents to link. |
| 22 target.sources().push_back(SourceFile("//foo/input3.o")); | 23 target.sources().push_back(SourceFile("//foo/input3.o")); |
| 23 target.sources().push_back(SourceFile("//foo/input4.obj")); | 24 target.sources().push_back(SourceFile("//foo/input4.obj")); |
| 25 target.SetToolchain(setup.toolchain()); |
| 24 target.OnResolved(); | 26 target.OnResolved(); |
| 25 | 27 |
| 26 // Source set itself. | 28 // Source set itself. |
| 27 { | 29 { |
| 28 std::ostringstream out; | 30 std::ostringstream out; |
| 29 NinjaBinaryTargetWriter writer(&target, setup.toolchain(), out); | 31 NinjaBinaryTargetWriter writer(&target, out); |
| 30 writer.Run(); | 32 writer.Run(); |
| 31 | 33 |
| 32 const char expected_win[] = | 34 const char expected[] = |
| 33 "defines =\n" | 35 "defines =\n" |
| 34 "includes =\n" | 36 "include_dirs =\n" |
| 35 "cflags =\n" | 37 "cflags =\n" |
| 36 "cflags_c =\n" | 38 "cflags_c =\n" |
| 37 "cflags_cc =\n" | 39 "cflags_cc =\n" |
| 38 "cflags_objc =\n" | 40 "cflags_objc =\n" |
| 39 "cflags_objcc =\n" | 41 "cflags_objcc =\n" |
| 40 "target_name = bar\n" | 42 "root_out_dir = \n" |
| 41 "target_out_dir = obj/foo\n" | 43 "target_out_dir = obj/foo\n" |
| 42 "root_out_dir = \n" | 44 "target_output_name = bar\n" |
| 43 "\n" | 45 "\n" |
| 44 "build obj/foo/bar.input1.obj: cxx ../../foo/input1.cc\n" | 46 "build obj/foo/bar.input1.o: cxx ../../foo/input1.cc\n" |
| 45 "build obj/foo/bar.input2.obj: cxx ../../foo/input2.cc\n" | 47 "build obj/foo/bar.input2.o: cxx ../../foo/input2.cc\n" |
| 46 "\n" | 48 "\n" |
| 47 "build obj/foo/bar.stamp: stamp obj/foo/bar.input1.obj " | 49 "build obj/foo/bar.stamp: stamp obj/foo/bar.input1.o " |
| 48 "obj/foo/bar.input2.obj ../../foo/input3.o ../../foo/input4.obj\n"; | 50 "obj/foo/bar.input2.o ../../foo/input3.o ../../foo/input4.obj\n"; |
| 49 std::string out_str = out.str(); | 51 std::string out_str = out.str(); |
| 50 #if defined(OS_WIN) | 52 EXPECT_EQ(expected, out_str); |
| 51 std::replace(out_str.begin(), out_str.end(), '\\', '/'); | |
| 52 #endif | |
| 53 EXPECT_EQ(expected_win, out_str); | |
| 54 } | 53 } |
| 55 | 54 |
| 56 // A shared library that depends on the source set. | 55 // A shared library that depends on the source set. |
| 57 Target shlib_target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); | 56 Target shlib_target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); |
| 58 shlib_target.set_output_type(Target::SHARED_LIBRARY); | 57 shlib_target.set_output_type(Target::SHARED_LIBRARY); |
| 59 shlib_target.deps().push_back(LabelTargetPair(&target)); | 58 shlib_target.deps().push_back(LabelTargetPair(&target)); |
| 59 shlib_target.SetToolchain(setup.toolchain()); |
| 60 shlib_target.OnResolved(); | 60 shlib_target.OnResolved(); |
| 61 | 61 |
| 62 { | 62 { |
| 63 std::ostringstream out; | 63 std::ostringstream out; |
| 64 NinjaBinaryTargetWriter writer(&shlib_target, setup.toolchain(), out); | 64 NinjaBinaryTargetWriter writer(&shlib_target, out); |
| 65 writer.Run(); | 65 writer.Run(); |
| 66 | 66 |
| 67 const char expected_win[] = | 67 const char expected[] = |
| 68 "defines =\n" | 68 "defines =\n" |
| 69 "includes =\n" | 69 "include_dirs =\n" |
| 70 "cflags =\n" | 70 "cflags =\n" |
| 71 "cflags_c =\n" | 71 "cflags_c =\n" |
| 72 "cflags_cc =\n" | 72 "cflags_cc =\n" |
| 73 "cflags_objc =\n" | 73 "cflags_objc =\n" |
| 74 "cflags_objcc =\n" | 74 "cflags_objcc =\n" |
| 75 "target_name = shlib\n" | 75 "root_out_dir = \n" |
| 76 "target_out_dir = obj/foo\n" | 76 "target_out_dir = obj/foo\n" |
| 77 "root_out_dir = \n" | 77 "target_output_name = libshlib\n" |
| 78 "\n" | 78 "\n" |
| 79 "\n" | 79 "\n" |
| 80 "manifests = obj/foo/shlib.intermediate.manifest\n" | |
| 81 "ldflags = /MANIFEST /ManifestFile:obj/foo/shlib.intermediate." | |
| 82 "manifest\n" | |
| 83 "libs =\n" | |
| 84 // Ordering of the obj files here should come out in the order | 80 // Ordering of the obj files here should come out in the order |
| 85 // specified, with the target's first, followed by the source set's, in | 81 // specified, with the target's first, followed by the source set's, in |
| 86 // order. | 82 // order. |
| 87 "build shlib.dll shlib.dll.lib: solink obj/foo/bar.input1.obj " | 83 "build libshlib.so: solink obj/foo/bar.input1.o " |
| 88 "obj/foo/bar.input2.obj ../../foo/input3.o " | 84 "obj/foo/bar.input2.o ../../foo/input3.o ../../foo/input4.obj\n" |
| 89 "../../foo/input4.obj\n" | 85 " ldflags =\n" |
| 90 " soname = shlib.dll\n" | 86 " libs =\n" |
| 91 " lib = shlib.dll\n" | 87 " output_extension = .so\n"; |
| 92 " dll = shlib.dll\n" | |
| 93 " implibflag = /IMPLIB:shlib.dll.lib\n\n"; | |
| 94 std::string out_str = out.str(); | 88 std::string out_str = out.str(); |
| 95 #if defined(OS_WIN) | 89 EXPECT_EQ(expected, out_str); |
| 96 std::replace(out_str.begin(), out_str.end(), '\\', '/'); | |
| 97 #endif | |
| 98 EXPECT_EQ(expected_win, out_str); | |
| 99 } | 90 } |
| 100 | 91 |
| 101 // A static library that depends on the source set (should not link it). | 92 // A static library that depends on the source set (should not link it). |
| 102 Target stlib_target(setup.settings(), Label(SourceDir("//foo/"), "stlib")); | 93 Target stlib_target(setup.settings(), Label(SourceDir("//foo/"), "stlib")); |
| 103 stlib_target.set_output_type(Target::STATIC_LIBRARY); | 94 stlib_target.set_output_type(Target::STATIC_LIBRARY); |
| 104 stlib_target.deps().push_back(LabelTargetPair(&target)); | 95 stlib_target.deps().push_back(LabelTargetPair(&target)); |
| 96 stlib_target.SetToolchain(setup.toolchain()); |
| 105 stlib_target.OnResolved(); | 97 stlib_target.OnResolved(); |
| 106 | 98 |
| 107 { | 99 { |
| 108 std::ostringstream out; | 100 std::ostringstream out; |
| 109 NinjaBinaryTargetWriter writer(&stlib_target, setup.toolchain(), out); | 101 NinjaBinaryTargetWriter writer(&stlib_target, out); |
| 110 writer.Run(); | 102 writer.Run(); |
| 111 | 103 |
| 112 const char expected_win[] = | 104 const char expected[] = |
| 113 "defines =\n" | 105 "defines =\n" |
| 114 "includes =\n" | 106 "include_dirs =\n" |
| 115 "cflags =\n" | 107 "cflags =\n" |
| 116 "cflags_c =\n" | 108 "cflags_c =\n" |
| 117 "cflags_cc =\n" | 109 "cflags_cc =\n" |
| 118 "cflags_objc =\n" | 110 "cflags_objc =\n" |
| 119 "cflags_objcc =\n" | 111 "cflags_objcc =\n" |
| 120 "target_name = stlib\n" | 112 "root_out_dir = \n" |
| 121 "target_out_dir = obj/foo\n" | 113 "target_out_dir = obj/foo\n" |
| 122 "root_out_dir = \n" | 114 "target_output_name = libstlib\n" |
| 123 "\n" | 115 "\n" |
| 124 "\n" | 116 "\n" |
| 125 "manifests = obj/foo/stlib.intermediate.manifest\n" | 117 // There are no sources so there are no params to alink. (In practice |
| 126 "ldflags = /MANIFEST /ManifestFile:obj/foo/stlib.intermediate.manifest\n
" | 118 // this will probably fail in the archive tool.) |
| 127 "libs =\n" | 119 "build obj/foo/libstlib.a: alink\n" |
| 128 // There are no sources so there are no params to alink. | 120 " ldflags =\n" |
| 129 "build obj/foo/stlib.lib: alink\n\n"; | 121 " libs =\n" |
| 122 " output_extension = \n"; |
| 130 std::string out_str = out.str(); | 123 std::string out_str = out.str(); |
| 131 #if defined(OS_WIN) | 124 EXPECT_EQ(expected, out_str); |
| 132 std::replace(out_str.begin(), out_str.end(), '\\', '/'); | |
| 133 #endif | |
| 134 EXPECT_EQ(expected_win, out_str); | |
| 135 } | 125 } |
| 136 | |
| 137 } | 126 } |
| 138 | 127 |
| 139 TEST(NinjaBinaryTargetWriter, ProductExtension) { | 128 TEST(NinjaBinaryTargetWriter, ProductExtension) { |
| 140 TestWithScope setup; | 129 TestWithScope setup; |
| 141 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | 130 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 142 setup.settings()->set_target_os(Settings::LINUX); | 131 setup.settings()->set_target_os(Settings::LINUX); |
| 143 | 132 |
| 144 // A shared library w/ the product_extension set to a custom value. | 133 // A shared library w/ the product_extension set to a custom value. |
| 145 Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); | 134 Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); |
| 146 target.set_output_type(Target::SHARED_LIBRARY); | 135 target.set_output_type(Target::SHARED_LIBRARY); |
| 147 target.set_output_extension(std::string("so.6")); | 136 target.set_output_extension(std::string("so.6")); |
| 148 target.sources().push_back(SourceFile("//foo/input1.cc")); | 137 target.sources().push_back(SourceFile("//foo/input1.cc")); |
| 149 target.sources().push_back(SourceFile("//foo/input2.cc")); | 138 target.sources().push_back(SourceFile("//foo/input2.cc")); |
| 139 target.SetToolchain(setup.toolchain()); |
| 150 target.OnResolved(); | 140 target.OnResolved(); |
| 151 | 141 |
| 152 std::ostringstream out; | 142 std::ostringstream out; |
| 153 NinjaBinaryTargetWriter writer(&target, setup.toolchain(), out); | 143 NinjaBinaryTargetWriter writer(&target, out); |
| 154 writer.Run(); | 144 writer.Run(); |
| 155 | 145 |
| 156 const char expected[] = | 146 const char expected[] = |
| 157 "defines =\n" | 147 "defines =\n" |
| 158 "includes =\n" | 148 "include_dirs =\n" |
| 159 "cflags =\n" | 149 "cflags =\n" |
| 160 "cflags_c =\n" | 150 "cflags_c =\n" |
| 161 "cflags_cc =\n" | 151 "cflags_cc =\n" |
| 162 "cflags_objc =\n" | 152 "cflags_objc =\n" |
| 163 "cflags_objcc =\n" | 153 "cflags_objcc =\n" |
| 164 "target_name = shlib\n" | 154 "root_out_dir = \n" |
| 165 "target_out_dir = obj/foo\n" | 155 "target_out_dir = obj/foo\n" |
| 166 "root_out_dir = \n" | 156 "target_output_name = libshlib\n" |
| 167 "\n" | 157 "\n" |
| 168 "build obj/foo/shlib.input1.o: cxx ../../foo/input1.cc\n" | 158 "build obj/foo/libshlib.input1.o: cxx ../../foo/input1.cc\n" |
| 169 "build obj/foo/shlib.input2.o: cxx ../../foo/input2.cc\n" | 159 "build obj/foo/libshlib.input2.o: cxx ../../foo/input2.cc\n" |
| 170 "\n" | 160 "\n" |
| 171 "ldflags =\n" | 161 "build libshlib.so.6: solink obj/foo/libshlib.input1.o " |
| 172 "libs =\n" | 162 "obj/foo/libshlib.input2.o\n" |
| 173 "build libshlib.so.6: solink obj/foo/shlib.input1.o " | 163 " ldflags =\n" |
| 174 "obj/foo/shlib.input2.o\n" | 164 " libs =\n" |
| 175 " soname = libshlib.so.6\n" | 165 " output_extension = .so.6\n"; |
| 176 " lib = libshlib.so.6\n" | |
| 177 "\n"; | |
| 178 | 166 |
| 179 std::string out_str = out.str(); | 167 std::string out_str = out.str(); |
| 180 #if defined(OS_WIN) | |
| 181 std::replace(out_str.begin(), out_str.end(), '\\', '/'); | |
| 182 #endif | |
| 183 EXPECT_EQ(expected, out_str); | 168 EXPECT_EQ(expected, out_str); |
| 184 } | 169 } |
| 185 | 170 |
| 186 TEST(NinjaBinaryTargetWriter, EmptyProductExtension) { | 171 TEST(NinjaBinaryTargetWriter, EmptyProductExtension) { |
| 187 TestWithScope setup; | 172 TestWithScope setup; |
| 188 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | 173 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 189 setup.settings()->set_target_os(Settings::LINUX); | 174 setup.settings()->set_target_os(Settings::LINUX); |
| 190 | 175 |
| 191 // This test is the same as ProductExtension, except that | 176 // This test is the same as ProductExtension, except that |
| 192 // we call set_output_extension("") and ensure that we still get the default. | 177 // we call set_output_extension("") and ensure that we still get the default. |
| 193 Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); | 178 Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); |
| 194 target.set_output_type(Target::SHARED_LIBRARY); | 179 target.set_output_type(Target::SHARED_LIBRARY); |
| 195 target.set_output_extension(std::string()); | 180 target.set_output_extension(std::string()); |
| 196 target.sources().push_back(SourceFile("//foo/input1.cc")); | 181 target.sources().push_back(SourceFile("//foo/input1.cc")); |
| 197 target.sources().push_back(SourceFile("//foo/input2.cc")); | 182 target.sources().push_back(SourceFile("//foo/input2.cc")); |
| 198 | 183 |
| 184 target.SetToolchain(setup.toolchain()); |
| 185 target.OnResolved(); |
| 186 |
| 199 std::ostringstream out; | 187 std::ostringstream out; |
| 200 NinjaBinaryTargetWriter writer(&target, setup.toolchain(), out); | 188 NinjaBinaryTargetWriter writer(&target, out); |
| 201 writer.Run(); | 189 writer.Run(); |
| 202 | 190 |
| 203 const char expected[] = | 191 const char expected[] = |
| 204 "defines =\n" | 192 "defines =\n" |
| 205 "includes =\n" | 193 "include_dirs =\n" |
| 206 "cflags =\n" | 194 "cflags =\n" |
| 207 "cflags_c =\n" | 195 "cflags_c =\n" |
| 208 "cflags_cc =\n" | 196 "cflags_cc =\n" |
| 209 "cflags_objc =\n" | 197 "cflags_objc =\n" |
| 210 "cflags_objcc =\n" | 198 "cflags_objcc =\n" |
| 211 "target_name = shlib\n" | 199 "root_out_dir = \n" |
| 212 "target_out_dir = obj/foo\n" | 200 "target_out_dir = obj/foo\n" |
| 213 "root_out_dir = \n" | 201 "target_output_name = libshlib\n" |
| 214 "\n" | 202 "\n" |
| 215 "build obj/foo/shlib.input1.o: cxx ../../foo/input1.cc\n" | 203 "build obj/foo/libshlib.input1.o: cxx ../../foo/input1.cc\n" |
| 216 "build obj/foo/shlib.input2.o: cxx ../../foo/input2.cc\n" | 204 "build obj/foo/libshlib.input2.o: cxx ../../foo/input2.cc\n" |
| 217 "\n" | 205 "\n" |
| 218 "ldflags =\n" | 206 "build libshlib.so: solink obj/foo/libshlib.input1.o " |
| 219 "libs =\n" | 207 "obj/foo/libshlib.input2.o\n" |
| 220 "build libshlib.so: solink obj/foo/shlib.input1.o " | 208 " ldflags =\n" |
| 221 "obj/foo/shlib.input2.o\n" | 209 " libs =\n" |
| 222 " soname = libshlib.so\n" | 210 " output_extension = .so\n"; |
| 223 " lib = libshlib.so\n" | |
| 224 "\n"; | |
| 225 | 211 |
| 226 std::string out_str = out.str(); | 212 std::string out_str = out.str(); |
| 227 #if defined(OS_WIN) | |
| 228 std::replace(out_str.begin(), out_str.end(), '\\', '/'); | |
| 229 #endif | |
| 230 EXPECT_EQ(expected, out_str); | 213 EXPECT_EQ(expected, out_str); |
| 231 } | 214 } |
| OLD | NEW |