Upstream version 7.36.149.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     // TODO(brettw) I think we'll need to worry about backslashes here
33     // depending if we're on actual Windows or Linux pretending to be Windows.
34     const char expected_win[] =
35         "defines =\n"
36         "includes =\n"
37         "cflags =\n"
38         "cflags_c =\n"
39         "cflags_cc =\n"
40         "cflags_objc =\n"
41         "cflags_objcc =\n"
42         "\n"
43         "build obj/foo/bar.input1.obj: cxx ../../foo/input1.cc\n"
44         "build obj/foo/bar.input2.obj: cxx ../../foo/input2.cc\n"
45         "\n"
46         "build obj/foo/bar.stamp: stamp obj/foo/bar.input1.obj "
47             "obj/foo/bar.input2.obj ../../foo/input3.o ../../foo/input4.obj\n";
48     std::string out_str = out.str();
49 #if defined(OS_WIN)
50     std::replace(out_str.begin(), out_str.end(), '\\', '/');
51 #endif
52     EXPECT_EQ(expected_win, out_str);
53   }
54
55   // A shared library that depends on the source set.
56   Target shlib_target(setup.settings(), Label(SourceDir("//foo/"), "shlib"));
57   shlib_target.set_output_type(Target::SHARED_LIBRARY);
58   shlib_target.deps().push_back(LabelTargetPair(&target));
59   shlib_target.OnResolved();
60
61   {
62     std::ostringstream out;
63     NinjaBinaryTargetWriter writer(&shlib_target, setup.toolchain(), out);
64     writer.Run();
65
66     // TODO(brettw) I think we'll need to worry about backslashes here
67     // depending if we're on actual Windows or Linux pretending to be Windows.
68     const char expected_win[] =
69         "defines =\n"
70         "includes =\n"
71         "cflags =\n"
72         "cflags_c =\n"
73         "cflags_cc =\n"
74         "cflags_objc =\n"
75         "cflags_objcc =\n"
76         "\n"
77         "\n"
78         "manifests = obj/foo/shlib.intermediate.manifest\n"
79         "ldflags = /MANIFEST /ManifestFile:obj/foo/shlib.intermediate."
80             "manifest\n"
81         "libs =\n"
82         // Ordering of the obj files here is arbitrary. Currently they're put
83         // in a set and come out sorted.
84         "build shlib.dll shlib.dll.lib: solink ../../foo/input3.o "
85             "../../foo/input4.obj obj/foo/bar.input1.obj "
86             "obj/foo/bar.input2.obj\n"
87         "  soname = shlib.dll\n"
88         "  lib = shlib.dll\n"
89         "  dll = shlib.dll\n"
90         "  implibflag = /IMPLIB:shlib.dll.lib\n\n";
91     std::string out_str = out.str();
92 #if defined(OS_WIN)
93     std::replace(out_str.begin(), out_str.end(), '\\', '/');
94 #endif
95     EXPECT_EQ(expected_win, out_str);
96   }
97 }
98
99 TEST(NinjaBinaryTargetWriter, ProductExtension) {
100   TestWithScope setup;
101   setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
102   setup.settings()->set_target_os(Settings::LINUX);
103
104   // A shared library w/ the product_extension set to a custom value.
105   Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib"));
106   target.set_output_type(Target::SHARED_LIBRARY);
107   target.set_output_extension(std::string("so.6"));
108   target.sources().push_back(SourceFile("//foo/input1.cc"));
109   target.sources().push_back(SourceFile("//foo/input2.cc"));
110   target.OnResolved();
111
112   std::ostringstream out;
113   NinjaBinaryTargetWriter writer(&target, setup.toolchain(), out);
114   writer.Run();
115
116   // TODO(brettw) I think we'll need to worry about backslashes here
117   // depending if we're on actual Windows or Linux pretending to be Windows.
118   const char expected[] =
119       "defines =\n"
120       "includes =\n"
121       "cflags =\n"
122       "cflags_c =\n"
123       "cflags_cc =\n"
124       "cflags_objc =\n"
125       "cflags_objcc =\n"
126       "\n"
127       "build obj/foo/shlib.input1.o: cxx ../../foo/input1.cc\n"
128       "build obj/foo/shlib.input2.o: cxx ../../foo/input2.cc\n"
129       "\n"
130       "ldflags =\n"
131       "libs =\n"
132       "build lib/libshlib.so.6: solink obj/foo/shlib.input1.o "
133           "obj/foo/shlib.input2.o\n"
134       "  soname = libshlib.so.6\n"
135       "  lib = lib/libshlib.so.6\n"
136       "\n";
137
138   std::string out_str = out.str();
139 #if defined(OS_WIN)
140   std::replace(out_str.begin(), out_str.end(), '\\', '/');
141 #endif
142   EXPECT_EQ(expected, out_str);
143 }
144
145 TEST(NinjaBinaryTargetWriter, EmptyProductExtension) {
146   TestWithScope setup;
147   setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
148   setup.settings()->set_target_os(Settings::LINUX);
149
150   // This test is the same as ProductExtension, except that
151   // we call set_output_extension("") and ensure that we still get the default.
152   Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib"));
153   target.set_output_type(Target::SHARED_LIBRARY);
154   target.set_output_extension(std::string());
155   target.sources().push_back(SourceFile("//foo/input1.cc"));
156   target.sources().push_back(SourceFile("//foo/input2.cc"));
157
158   std::ostringstream out;
159   NinjaBinaryTargetWriter writer(&target, setup.toolchain(), out);
160   writer.Run();
161
162   // TODO(brettw) I think we'll need to worry about backslashes here
163   // depending if we're on actual Windows or Linux pretending to be Windows.
164   const char expected[] =
165       "defines =\n"
166       "includes =\n"
167       "cflags =\n"
168       "cflags_c =\n"
169       "cflags_cc =\n"
170       "cflags_objc =\n"
171       "cflags_objcc =\n"
172       "\n"
173       "build obj/foo/shlib.input1.o: cxx ../../foo/input1.cc\n"
174       "build obj/foo/shlib.input2.o: cxx ../../foo/input2.cc\n"
175       "\n"
176       "ldflags =\n"
177       "libs =\n"
178       "build lib/libshlib.so: solink obj/foo/shlib.input1.o "
179           "obj/foo/shlib.input2.o\n"
180       "  soname = libshlib.so\n"
181       "  lib = lib/libshlib.so\n"
182       "\n";
183
184   std::string out_str = out.str();
185 #if defined(OS_WIN)
186   std::replace(out_str.begin(), out_str.end(), '\\', '/');
187 #endif
188   EXPECT_EQ(expected, out_str);
189 }