Revert 264226 "Reduce dependency of TiclInvalidationService on P..."
[chromium-blink-merge.git] / tools / gn / ninja_binary_target_writer_unittest.cc
blob94755c6b2eb4d92dae82c7f229c7755620719ce9
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.
5 #include <sstream>
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"
11 TEST(NinjaBinaryTargetWriter, SourceSet) {
12 TestWithScope setup;
13 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
14 setup.settings()->set_target_os(Settings::WIN);
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 target.OnResolved();
22 // Source set itself.
24 std::ostringstream out;
25 NinjaBinaryTargetWriter writer(&target, setup.toolchain(), out);
26 writer.Run();
28 // TODO(brettw) I think we'll need to worry about backslashes here
29 // depending if we're on actual Windows or Linux pretending to be Windows.
30 const char expected_win[] =
31 "defines =\n"
32 "includes =\n"
33 "cflags =\n"
34 "cflags_c =\n"
35 "cflags_cc =\n"
36 "cflags_objc =\n"
37 "cflags_objcc =\n"
38 "\n"
39 "build obj/foo/bar.input1.obj: cxx ../../foo/input1.cc\n"
40 "build obj/foo/bar.input2.obj: cxx ../../foo/input2.cc\n"
41 "\n"
42 "build obj/foo/bar.stamp: stamp obj/foo/bar.input1.obj obj/foo/bar.input2.obj\n";
43 std::string out_str = out.str();
44 #if defined(OS_WIN)
45 std::replace(out_str.begin(), out_str.end(), '\\', '/');
46 #endif
47 EXPECT_EQ(expected_win, out_str);
50 // A shared library that depends on the source set.
51 Target shlib_target(setup.settings(), Label(SourceDir("//foo/"), "shlib"));
52 shlib_target.set_output_type(Target::SHARED_LIBRARY);
53 shlib_target.deps().push_back(LabelTargetPair(&target));
54 shlib_target.OnResolved();
57 std::ostringstream out;
58 NinjaBinaryTargetWriter writer(&shlib_target, setup.toolchain(), out);
59 writer.Run();
61 // TODO(brettw) I think we'll need to worry about backslashes here
62 // depending if we're on actual Windows or Linux pretending to be Windows.
63 const char expected_win[] =
64 "defines =\n"
65 "includes =\n"
66 "cflags =\n"
67 "cflags_c =\n"
68 "cflags_cc =\n"
69 "cflags_objc =\n"
70 "cflags_objcc =\n"
71 "\n"
72 "\n"
73 "manifests = obj/foo/shlib.intermediate.manifest\n"
74 "ldflags = /MANIFEST /ManifestFile:obj/foo/shlib.intermediate."
75 "manifest\n"
76 "libs =\n"
77 "build shlib.dll shlib.dll.lib: solink obj/foo/bar.input1.obj "
78 "obj/foo/bar.input2.obj\n"
79 " soname = shlib.dll\n"
80 " lib = shlib.dll\n"
81 " dll = shlib.dll\n"
82 " implibflag = /IMPLIB:shlib.dll.lib\n\n";
83 std::string out_str = out.str();
84 #if defined(OS_WIN)
85 std::replace(out_str.begin(), out_str.end(), '\\', '/');
86 #endif
87 EXPECT_EQ(expected_win, out_str);
91 TEST(NinjaBinaryTargetWriter, ProductExtension) {
92 TestWithScope setup;
93 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
94 setup.settings()->set_target_os(Settings::LINUX);
96 // A shared library w/ the product_extension set to a custom value.
97 Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib"));
98 target.set_output_type(Target::SHARED_LIBRARY);
99 target.set_output_extension(std::string("so.6"));
100 target.sources().push_back(SourceFile("//foo/input1.cc"));
101 target.sources().push_back(SourceFile("//foo/input2.cc"));
102 target.OnResolved();
104 std::ostringstream out;
105 NinjaBinaryTargetWriter writer(&target, setup.toolchain(), out);
106 writer.Run();
108 // TODO(brettw) I think we'll need to worry about backslashes here
109 // depending if we're on actual Windows or Linux pretending to be Windows.
110 const char expected[] =
111 "defines =\n"
112 "includes =\n"
113 "cflags =\n"
114 "cflags_c =\n"
115 "cflags_cc =\n"
116 "cflags_objc =\n"
117 "cflags_objcc =\n"
118 "\n"
119 "build obj/foo/shlib.input1.o: cxx ../../foo/input1.cc\n"
120 "build obj/foo/shlib.input2.o: cxx ../../foo/input2.cc\n"
121 "\n"
122 "ldflags =\n"
123 "libs =\n"
124 "build lib/libshlib.so.6: solink obj/foo/shlib.input1.o "
125 "obj/foo/shlib.input2.o\n"
126 " soname = libshlib.so.6\n"
127 " lib = lib/libshlib.so.6\n"
128 "\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, out_str);
137 TEST(NinjaBinaryTargetWriter, EmptyProductExtension) {
138 TestWithScope setup;
139 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
140 setup.settings()->set_target_os(Settings::LINUX);
142 // This test is the same as ProductExtension, except that
143 // we call set_output_extension("") and ensure that we still get the default.
144 Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib"));
145 target.set_output_type(Target::SHARED_LIBRARY);
146 target.set_output_extension(std::string());
147 target.sources().push_back(SourceFile("//foo/input1.cc"));
148 target.sources().push_back(SourceFile("//foo/input2.cc"));
150 std::ostringstream out;
151 NinjaBinaryTargetWriter writer(&target, setup.toolchain(), out);
152 writer.Run();
154 // TODO(brettw) I think we'll need to worry about backslashes here
155 // depending if we're on actual Windows or Linux pretending to be Windows.
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 "\n"
165 "build obj/foo/shlib.input1.o: cxx ../../foo/input1.cc\n"
166 "build obj/foo/shlib.input2.o: cxx ../../foo/input2.cc\n"
167 "\n"
168 "ldflags =\n"
169 "libs =\n"
170 "build lib/libshlib.so: solink obj/foo/shlib.input1.o "
171 "obj/foo/shlib.input2.o\n"
172 " soname = libshlib.so\n"
173 " lib = lib/libshlib.so\n"
174 "\n";
176 std::string out_str = out.str();
177 #if defined(OS_WIN)
178 std::replace(out_str.begin(), out_str.end(), '\\', '/');
179 #endif
180 EXPECT_EQ(expected, out_str);