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);
99 TEST(NinjaBinaryTargetWriter, ProductExtension) {
101 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
102 setup.settings()->set_target_os(Settings::LINUX);
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"));
112 std::ostringstream out;
113 NinjaBinaryTargetWriter writer(&target, setup.toolchain(), out);
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[] =
127 "build obj/foo/shlib.input1.o: cxx ../../foo/input1.cc\n"
128 "build obj/foo/shlib.input2.o: cxx ../../foo/input2.cc\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"
138 std::string out_str = out.str();
140 std::replace(out_str.begin(), out_str.end(), '\\', '/');
142 EXPECT_EQ(expected, out_str);
145 TEST(NinjaBinaryTargetWriter, EmptyProductExtension) {
147 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
148 setup.settings()->set_target_os(Settings::LINUX);
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"));
158 std::ostringstream out;
159 NinjaBinaryTargetWriter writer(&target, setup.toolchain(), out);
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[] =
173 "build obj/foo/shlib.input1.o: cxx ../../foo/input1.cc\n"
174 "build obj/foo/shlib.input2.o: cxx ../../foo/input2.cc\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"
184 std::string out_str = out.str();
186 std::replace(out_str.begin(), out_str.end(), '\\', '/');
188 EXPECT_EQ(expected, out_str);