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"));
24 std::ostringstream out
;
25 NinjaBinaryTargetWriter
writer(&target
, setup
.toolchain(), out
);
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
[] =
39 "build obj/foo/bar.input1.obj: cxx ../../foo/input1.cc\n"
40 "build obj/foo/bar.input2.obj: cxx ../../foo/input2.cc\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();
45 std::replace(out_str
.begin(), out_str
.end(), '\\', '/');
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
);
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
[] =
73 "manifests = obj/foo/shlib.intermediate.manifest\n"
74 "ldflags = /MANIFEST /ManifestFile:obj/foo/shlib.intermediate."
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"
82 " implibflag = /IMPLIB:shlib.dll.lib\n\n";
83 std::string out_str
= out
.str();
85 std::replace(out_str
.begin(), out_str
.end(), '\\', '/');
87 EXPECT_EQ(expected_win
, out_str
);
91 TEST(NinjaBinaryTargetWriter
, ProductExtension
) {
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"));
104 std::ostringstream out
;
105 NinjaBinaryTargetWriter
writer(&target
, setup
.toolchain(), out
);
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
[] =
119 "build obj/foo/shlib.input1.o: cxx ../../foo/input1.cc\n"
120 "build obj/foo/shlib.input2.o: cxx ../../foo/input2.cc\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"
130 std::string out_str
= out
.str();
132 std::replace(out_str
.begin(), out_str
.end(), '\\', '/');
134 EXPECT_EQ(expected
, out_str
);
137 TEST(NinjaBinaryTargetWriter
, EmptyProductExtension
) {
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
);
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
[] =
165 "build obj/foo/shlib.input1.o: cxx ../../foo/input1.cc\n"
166 "build obj/foo/shlib.input2.o: cxx ../../foo/input2.cc\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"
176 std::string out_str
= out
.str();
178 std::replace(out_str
.begin(), out_str
.end(), '\\', '/');
180 EXPECT_EQ(expected
, out_str
);