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.
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
) {
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 // 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"));
28 std::ostringstream out
;
29 NinjaBinaryTargetWriter
writer(&target
, setup
.toolchain(), out
);
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
[] =
43 "build obj/foo/bar.input1.obj: cxx ../../foo/input1.cc\n"
44 "build obj/foo/bar.input2.obj: cxx ../../foo/input2.cc\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();
50 std::replace(out_str
.begin(), out_str
.end(), '\\', '/');
52 EXPECT_EQ(expected_win
, out_str
);
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();
62 std::ostringstream out
;
63 NinjaBinaryTargetWriter
writer(&shlib_target
, setup
.toolchain(), out
);
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
[] =
78 "manifests = obj/foo/shlib.intermediate.manifest\n"
79 "ldflags = /MANIFEST /ManifestFile:obj/foo/shlib.intermediate."
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"
90 " implibflag = /IMPLIB:shlib.dll.lib\n\n";
91 std::string out_str
= out
.str();
93 std::replace(out_str
.begin(), out_str
.end(), '\\', '/');
95 EXPECT_EQ(expected_win
, out_str
);
98 // A static library that depends on the source set (should not link it).
99 Target
stlib_target(setup
.settings(), Label(SourceDir("//foo/"), "stlib"));
100 stlib_target
.set_output_type(Target::STATIC_LIBRARY
);
101 stlib_target
.deps().push_back(LabelTargetPair(&target
));
102 stlib_target
.OnResolved();
105 std::ostringstream out
;
106 NinjaBinaryTargetWriter
writer(&stlib_target
, setup
.toolchain(), out
);
109 // TODO(brettw) I think we'll need to worry about backslashes here
110 // depending if we're on actual Windows or Linux pretending to be Windows.
111 const char expected_win
[] =
121 "manifests = obj/foo/stlib.intermediate.manifest\n"
122 "ldflags = /MANIFEST /ManifestFile:obj/foo/stlib.intermediate.manifest\n"
124 // There are no sources so there are no params to alink.
125 "build obj/foo/stlib.lib: alink\n\n";
126 std::string out_str
= out
.str();
128 std::replace(out_str
.begin(), out_str
.end(), '\\', '/');
130 EXPECT_EQ(expected_win
, out_str
);
135 TEST(NinjaBinaryTargetWriter
, ProductExtension
) {
137 setup
.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
138 setup
.settings()->set_target_os(Settings::LINUX
);
140 // A shared library w/ the product_extension set to a custom value.
141 Target
target(setup
.settings(), Label(SourceDir("//foo/"), "shlib"));
142 target
.set_output_type(Target::SHARED_LIBRARY
);
143 target
.set_output_extension(std::string("so.6"));
144 target
.sources().push_back(SourceFile("//foo/input1.cc"));
145 target
.sources().push_back(SourceFile("//foo/input2.cc"));
148 std::ostringstream out
;
149 NinjaBinaryTargetWriter
writer(&target
, setup
.toolchain(), out
);
152 // TODO(brettw) I think we'll need to worry about backslashes here
153 // depending if we're on actual Windows or Linux pretending to be Windows.
154 const char expected
[] =
163 "build obj/foo/shlib.input1.o: cxx ../../foo/input1.cc\n"
164 "build obj/foo/shlib.input2.o: cxx ../../foo/input2.cc\n"
168 "build lib/libshlib.so.6: solink obj/foo/shlib.input1.o "
169 "obj/foo/shlib.input2.o\n"
170 " soname = libshlib.so.6\n"
171 " lib = lib/libshlib.so.6\n"
174 std::string out_str
= out
.str();
176 std::replace(out_str
.begin(), out_str
.end(), '\\', '/');
178 EXPECT_EQ(expected
, out_str
);
181 TEST(NinjaBinaryTargetWriter
, EmptyProductExtension
) {
183 setup
.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
184 setup
.settings()->set_target_os(Settings::LINUX
);
186 // This test is the same as ProductExtension, except that
187 // we call set_output_extension("") and ensure that we still get the default.
188 Target
target(setup
.settings(), Label(SourceDir("//foo/"), "shlib"));
189 target
.set_output_type(Target::SHARED_LIBRARY
);
190 target
.set_output_extension(std::string());
191 target
.sources().push_back(SourceFile("//foo/input1.cc"));
192 target
.sources().push_back(SourceFile("//foo/input2.cc"));
194 std::ostringstream out
;
195 NinjaBinaryTargetWriter
writer(&target
, setup
.toolchain(), out
);
198 // TODO(brettw) I think we'll need to worry about backslashes here
199 // depending if we're on actual Windows or Linux pretending to be Windows.
200 const char expected
[] =
209 "build obj/foo/shlib.input1.o: cxx ../../foo/input1.cc\n"
210 "build obj/foo/shlib.input2.o: cxx ../../foo/input2.cc\n"
214 "build lib/libshlib.so: solink obj/foo/shlib.input1.o "
215 "obj/foo/shlib.input2.o\n"
216 " soname = libshlib.so\n"
217 " lib = lib/libshlib.so\n"
220 std::string out_str
= out
.str();
222 std::replace(out_str
.begin(), out_str
.end(), '\\', '/');
224 EXPECT_EQ(expected
, out_str
);