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