• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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       "  source_file_part = input1.cc\n"
46       "  source_name_part = input1\n"
47       "build obj/foo/bar.input2.o: cxx ../../foo/input2.cc\n"
48       "  source_file_part = input2.cc\n"
49       "  source_name_part = input2\n"
50       "\n"
51       "build obj/foo/bar.stamp: stamp obj/foo/bar.input1.o "
52       "obj/foo/bar.input2.o ../../foo/input3.o ../../foo/input4.obj\n";
53   std::string out_str = out.str();
54   EXPECT_EQ(expected, out_str);
55 }
56 
TEST_F(NinjaBinaryTargetWriterTest,NoSourcesSourceSet)57 TEST_F(NinjaBinaryTargetWriterTest, NoSourcesSourceSet) {
58   Err err;
59   TestWithScope setup;
60 
61   Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
62   target.set_output_type(Target::SOURCE_SET);
63   target.visibility().SetPublic();
64   target.SetToolchain(setup.toolchain());
65   ASSERT_TRUE(target.OnResolved(&err));
66 
67   std::ostringstream out;
68   NinjaBinaryTargetWriter writer(&target, out);
69   writer.Run();
70 
71   const char expected[] =
72       "defines =\n"
73       "include_dirs =\n"
74       "root_out_dir = .\n"
75       "target_out_dir = obj/foo\n"
76       "target_output_name = bar\n"
77       "\n"
78       "\n"
79       "build obj/foo/bar.stamp: stamp\n";
80   std::string out_str = out.str();
81   EXPECT_EQ(expected, out_str);
82 }
83 
TEST_F(NinjaBinaryTargetWriterTest,NoSourcesStaticLib)84 TEST_F(NinjaBinaryTargetWriterTest, NoSourcesStaticLib) {
85   Err err;
86   TestWithScope setup;
87 
88   Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
89   target.set_output_type(Target::STATIC_LIBRARY);
90   target.visibility().SetPublic();
91   target.SetToolchain(setup.toolchain());
92   ASSERT_TRUE(target.OnResolved(&err));
93 
94   std::ostringstream out;
95   NinjaBinaryTargetWriter writer(&target, out);
96   writer.Run();
97 
98   const char expected[] =
99       "defines =\n"
100       "include_dirs =\n"
101       "root_out_dir = .\n"
102       "target_out_dir = obj/foo\n"
103       "target_output_name = libbar\n"
104       "\n"
105       "\n"
106       "build obj/foo/libbar.a: alink\n"
107       "  arflags =\n"
108       "  output_extension = \n"
109       "  output_dir = \n";
110   std::string out_str = out.str();
111   EXPECT_EQ(expected, out_str);
112 }
113 
TEST_F(NinjaBinaryTargetWriterTest,Inputs)114 TEST_F(NinjaBinaryTargetWriterTest, Inputs) {
115   Err err;
116   TestWithScope setup;
117 
118   {
119     Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
120     target.set_output_type(Target::SOURCE_SET);
121     target.visibility().SetPublic();
122     target.sources().push_back(SourceFile("//foo/source1.cc"));
123     target.config_values().inputs().push_back(SourceFile("//foo/input1"));
124     target.config_values().inputs().push_back(SourceFile("//foo/input2"));
125     target.source_types_used().Set(SourceFile::SOURCE_CPP);
126     target.SetToolchain(setup.toolchain());
127     ASSERT_TRUE(target.OnResolved(&err));
128 
129     std::ostringstream out;
130     NinjaBinaryTargetWriter writer(&target, out);
131     writer.Run();
132 
133     const char expected[] =
134         "defines =\n"
135         "include_dirs =\n"
136         "cflags =\n"
137         "cflags_cc =\n"
138         "root_out_dir = .\n"
139         "target_out_dir = obj/foo\n"
140         "target_output_name = bar\n"
141         "\n"
142         "build obj/foo/bar.source1.o: cxx ../../foo/source1.cc | "
143         "../../foo/input1 ../../foo/input2\n"
144         "  source_file_part = source1.cc\n"
145         "  source_name_part = source1\n"
146         "\n"
147         "build obj/foo/bar.stamp: stamp obj/foo/bar.source1.o\n";
148     std::string out_str = out.str();
149     EXPECT_EQ(expected, out_str) << expected << "\n" << out_str;
150   }
151 
152   {
153     Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
154     target.set_output_type(Target::SOURCE_SET);
155     target.visibility().SetPublic();
156     target.sources().push_back(SourceFile("//foo/source1.cc"));
157     target.sources().push_back(SourceFile("//foo/source2.cc"));
158     target.config_values().inputs().push_back(SourceFile("//foo/input1"));
159     target.config_values().inputs().push_back(SourceFile("//foo/input2"));
160     target.source_types_used().Set(SourceFile::SOURCE_CPP);
161     target.SetToolchain(setup.toolchain());
162     ASSERT_TRUE(target.OnResolved(&err));
163 
164     std::ostringstream out;
165     NinjaBinaryTargetWriter writer(&target, out);
166     writer.Run();
167 
168     const char expected[] =
169         "defines =\n"
170         "include_dirs =\n"
171         "cflags =\n"
172         "cflags_cc =\n"
173         "root_out_dir = .\n"
174         "target_out_dir = obj/foo\n"
175         "target_output_name = bar\n"
176         "\n"
177         "build obj/foo/bar.inputs.stamp: stamp "
178         "../../foo/input1 ../../foo/input2\n"
179         "build obj/foo/bar.source1.o: cxx ../../foo/source1.cc | "
180         "obj/foo/bar.inputs.stamp\n"
181         "  source_file_part = source1.cc\n"
182         "  source_name_part = source1\n"
183         "build obj/foo/bar.source2.o: cxx ../../foo/source2.cc | "
184         "obj/foo/bar.inputs.stamp\n"
185         "  source_file_part = source2.cc\n"
186         "  source_name_part = source2\n"
187         "\n"
188         "build obj/foo/bar.stamp: stamp obj/foo/bar.source1.o "
189         "obj/foo/bar.source2.o\n";
190     std::string out_str = out.str();
191     EXPECT_EQ(expected, out_str) << expected << "\n" << out_str;
192   }
193 }
194