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 "gn/ninja_binary_target_writer.h"
6
7 #include "gn/test_with_scheduler.h"
8 #include "gn/test_with_scope.h"
9 #include "util/test/test.h"
10
11 using NinjaBinaryTargetWriterTest = TestWithScheduler;
12
TEST_F(NinjaBinaryTargetWriterTest,CSources)13 TEST_F(NinjaBinaryTargetWriterTest, CSources) {
14 Err err;
15 TestWithScope setup;
16
17 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
18 target.set_output_type(Target::SOURCE_SET);
19 target.visibility().SetPublic();
20 target.sources().push_back(SourceFile("//foo/input1.cc"));
21 target.sources().push_back(SourceFile("//foo/input2.cc"));
22 // Also test object files, which should be just passed through to the
23 // dependents to link.
24 target.sources().push_back(SourceFile("//foo/input3.o"));
25 target.sources().push_back(SourceFile("//foo/input4.obj"));
26 target.source_types_used().Set(SourceFile::SOURCE_CPP);
27 target.source_types_used().Set(SourceFile::SOURCE_O);
28 target.SetToolchain(setup.toolchain());
29 ASSERT_TRUE(target.OnResolved(&err));
30
31 std::ostringstream out;
32 NinjaBinaryTargetWriter writer(&target, out);
33 writer.Run();
34
35 const char expected[] =
36 "defines =\n"
37 "include_dirs =\n"
38 "cflags =\n"
39 "cflags_cc =\n"
40 "root_out_dir = .\n"
41 "target_out_dir = obj/foo\n"
42 "target_output_name = bar\n"
43 "\n"
44 "build obj/foo/bar.input1.o: cxx ../../foo/input1.cc\n"
45 "build obj/foo/bar.input2.o: cxx ../../foo/input2.cc\n"
46 "\n"
47 "build obj/foo/bar.stamp: stamp obj/foo/bar.input1.o "
48 "obj/foo/bar.input2.o ../../foo/input3.o ../../foo/input4.obj\n";
49 std::string out_str = out.str();
50 EXPECT_EQ(expected, out_str);
51 }
52
TEST_F(NinjaBinaryTargetWriterTest,NoSourcesSourceSet)53 TEST_F(NinjaBinaryTargetWriterTest, NoSourcesSourceSet) {
54 Err err;
55 TestWithScope setup;
56
57 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
58 target.set_output_type(Target::SOURCE_SET);
59 target.visibility().SetPublic();
60 target.SetToolchain(setup.toolchain());
61 ASSERT_TRUE(target.OnResolved(&err));
62
63 std::ostringstream out;
64 NinjaBinaryTargetWriter writer(&target, out);
65 writer.Run();
66
67 const char expected[] =
68 "defines =\n"
69 "include_dirs =\n"
70 "root_out_dir = .\n"
71 "target_out_dir = obj/foo\n"
72 "target_output_name = bar\n"
73 "\n"
74 "\n"
75 "build obj/foo/bar.stamp: stamp\n";
76 std::string out_str = out.str();
77 EXPECT_EQ(expected, out_str);
78 }
79
TEST_F(NinjaBinaryTargetWriterTest,NoSourcesStaticLib)80 TEST_F(NinjaBinaryTargetWriterTest, NoSourcesStaticLib) {
81 Err err;
82 TestWithScope setup;
83
84 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
85 target.set_output_type(Target::STATIC_LIBRARY);
86 target.visibility().SetPublic();
87 target.SetToolchain(setup.toolchain());
88 ASSERT_TRUE(target.OnResolved(&err));
89
90 std::ostringstream out;
91 NinjaBinaryTargetWriter writer(&target, out);
92 writer.Run();
93
94 const char expected[] =
95 "defines =\n"
96 "include_dirs =\n"
97 "root_out_dir = .\n"
98 "target_out_dir = obj/foo\n"
99 "target_output_name = libbar\n"
100 "\n"
101 "\n"
102 "build obj/foo/libbar.a: alink\n"
103 " arflags =\n"
104 " output_extension = \n"
105 " output_dir = \n";
106 std::string out_str = out.str();
107 EXPECT_EQ(expected, out_str);
108 }
109