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