• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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 <sstream>
6 
7 #include "base/command_line.h"
8 #include "base/files/file_util.h"
9 #include "gn/ninja_build_writer.h"
10 #include "gn/pool.h"
11 #include "gn/scheduler.h"
12 #include "gn/switches.h"
13 #include "gn/target.h"
14 #include "gn/test_with_scheduler.h"
15 #include "gn/test_with_scope.h"
16 #include "util/test/test.h"
17 
18 using NinjaBuildWriterTest = TestWithScheduler;
19 
20 class ScopedDotGNFile {
21  public:
ScopedDotGNFile(const base::FilePath & path)22   ScopedDotGNFile(const base::FilePath& path)
23       : path_(path),
24         file_(path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE) {
25     EXPECT_TRUE(file_.IsValid());
26   }
~ScopedDotGNFile()27   ~ScopedDotGNFile() {
28     file_.Close();
29     base::DeleteFile(path_, false);
30   }
31 
32  private:
33   base::FilePath path_;
34   base::File file_;
35 };
36 
TEST_F(NinjaBuildWriterTest,GetSelfInvocationCommandLine)37 TEST_F(NinjaBuildWriterTest, GetSelfInvocationCommandLine) {
38   // TestWithScope sets up a config with a build dir of //out/Debug.
39   TestWithScope setup;
40   base::CommandLine cmd_out(base::CommandLine::NO_PROGRAM);
41 
42   // Setup sets the default root dir to ".".
43   base::FilePath root(FILE_PATH_LITERAL("."));
44   base::FilePath root_realpath = base::MakeAbsoluteFilePath(root);
45 
46   base::FilePath gn(FILE_PATH_LITERAL("testdot.gn"));
47 
48   // The file must exist on disk for MakeAbsoluteFilePath() to work.
49   ScopedDotGNFile dot_gn(gn);
50   base::FilePath gn_realpath = base::MakeAbsoluteFilePath(gn);
51 
52   // Without any parameters the self invocation should pass --root=../..
53   // (from //out/Debug to //).
54   setup.build_settings()->SetRootPath(root_realpath);
55   cmd_out = GetSelfInvocationCommandLine(setup.build_settings());
56   EXPECT_EQ("../..", cmd_out.GetSwitchValueASCII(switches::kRoot));
57   EXPECT_FALSE(cmd_out.HasSwitch(switches::kDotfile));
58 
59   // If --root is . and --dotfile is foo/.gn, then --dotfile also needs
60   // to to become ../../foo/.gn.
61   setup.build_settings()->SetRootPath(root_realpath);
62   setup.build_settings()->set_dotfile_name(gn_realpath);
63   cmd_out = GetSelfInvocationCommandLine(setup.build_settings());
64   EXPECT_EQ("../..", cmd_out.GetSwitchValueASCII(switches::kRoot));
65   EXPECT_EQ("../../testdot.gn",
66             cmd_out.GetSwitchValueASCII(switches::kDotfile));
67 }
68 
TEST_F(NinjaBuildWriterTest,TwoTargets)69 TEST_F(NinjaBuildWriterTest, TwoTargets) {
70   TestWithScope setup;
71   Err err;
72 
73   Target target_foo(setup.settings(), Label(SourceDir("//foo/"), "bar"));
74   target_foo.set_output_type(Target::ACTION);
75   target_foo.action_values().set_script(SourceFile("//foo/script.py"));
76   target_foo.action_values().outputs() = SubstitutionList::MakeForTest(
77       "//out/Debug/out1.out", "//out/Debug/out2.out");
78   target_foo.SetToolchain(setup.toolchain());
79   ASSERT_TRUE(target_foo.OnResolved(&err));
80 
81   Target target_bar(setup.settings(), Label(SourceDir("//bar/"), "bar"));
82   target_bar.set_output_type(Target::ACTION);
83   target_bar.action_values().set_script(SourceFile("//bar/script.py"));
84   target_bar.action_values().outputs() = SubstitutionList::MakeForTest(
85       "//out/Debug/out3.out", "//out/Debug/out4.out");
86   target_bar.SetToolchain(setup.toolchain());
87   ASSERT_TRUE(target_bar.OnResolved(&err));
88 
89   // Make a secondary toolchain that references two pools.
90   Label other_toolchain_label(SourceDir("//other/"), "toolchain");
91   Toolchain other_toolchain(setup.settings(), other_toolchain_label);
92   TestWithScope::SetupToolchain(&other_toolchain);
93 
94   Pool other_regular_pool(
95       setup.settings(),
96       Label(SourceDir("//other/"), "depth_pool", other_toolchain_label.dir(),
97             other_toolchain_label.name()));
98   other_regular_pool.set_depth(42);
99   other_toolchain.GetTool(CTool::kCToolLink)
100       ->set_pool(LabelPtrPair<Pool>(&other_regular_pool));
101 
102   // Make another target that uses its own pool
103 
104   Pool another_regular_pool(
105       setup.settings(),
106       Label(SourceDir("//another/"), "depth_pool", other_toolchain_label.dir(),
107             other_toolchain_label.name()));
108   another_regular_pool.set_depth(7);
109 
110   Target target_baz(setup.settings(), Label(SourceDir("//baz/"), "baz"));
111   target_baz.set_output_type(Target::ACTION);
112   target_baz.action_values().set_script(SourceFile("//baz/script.py"));
113   target_baz.action_values().outputs() = SubstitutionList::MakeForTest(
114       "//out/Debug/out5.out", "//out/Debug/out6.out");
115   target_baz.SetToolchain(&other_toolchain);
116   target_baz.action_values().set_pool(
117       LabelPtrPair<Pool>(&another_regular_pool));
118   ASSERT_TRUE(target_baz.OnResolved(&err));
119 
120   // The console pool must be in the default toolchain.
121   Pool console_pool(setup.settings(), Label(SourceDir("//"), "console",
122                                             setup.toolchain()->label().dir(),
123                                             setup.toolchain()->label().name()));
124   console_pool.set_depth(1);
125   other_toolchain.GetTool(GeneralTool::kGeneralToolStamp)
126       ->set_pool(LabelPtrPair<Pool>(&console_pool));
127 
128   // Settings to go with the other toolchain.
129   Settings other_settings(setup.build_settings(), "toolchain/");
130   other_settings.set_toolchain_label(other_toolchain_label);
131 
132   std::unordered_map<const Settings*, const Toolchain*> used_toolchains;
133   used_toolchains[setup.settings()] = setup.toolchain();
134   used_toolchains[&other_settings] = &other_toolchain;
135 
136   std::vector<const Target*> targets = {&target_foo, &target_bar, &target_baz};
137 
138   std::ostringstream ninja_out;
139   std::ostringstream depfile_out;
140 
141   NinjaBuildWriter writer(setup.build_settings(), used_toolchains, targets,
142                           setup.toolchain(), targets, ninja_out, depfile_out);
143   ASSERT_TRUE(writer.Run(&err));
144 
145   const char expected_rule_gn[] = "rule gn\n";
146   const char expected_build_ninja[] =
147       "build build.ninja: gn\n"
148       "  generator = 1\n"
149       "  depfile = build.ninja.d\n";
150   const char expected_other_pool[] =
151       "pool other_toolchain_another_depth_pool\n"
152       "  depth = 7\n"
153       "\n"
154       "pool other_toolchain_other_depth_pool\n"
155       "  depth = 42\n";
156   const char expected_toolchain[] = "subninja toolchain.ninja\n";
157   const char expected_targets[] =
158       "build bar: phony obj/bar/bar.stamp\n"
159       "build baz: phony obj/baz/baz.stamp\n"
160       "build foo$:bar: phony obj/foo/bar.stamp\n"
161       "build bar$:bar: phony obj/bar/bar.stamp\n"
162       "build baz$:baz: phony obj/baz/baz.stamp\n";
163   const char expected_root_target[] =
164       "build all: phony $\n"
165       "    obj/foo/bar.stamp $\n"
166       "    obj/bar/bar.stamp $\n"
167       "    obj/baz/baz.stamp\n";
168   const char expected_default[] = "default all\n";
169   std::string out_str = ninja_out.str();
170 #define EXPECT_SNIPPET(expected)                       \
171   EXPECT_NE(std::string::npos, out_str.find(expected)) \
172       << "Expected to find: " << expected << "\n"      \
173       << "Within: " << out_str
174   EXPECT_SNIPPET(expected_rule_gn);
175   EXPECT_SNIPPET(expected_build_ninja);
176   EXPECT_SNIPPET(expected_other_pool);
177   EXPECT_SNIPPET(expected_toolchain);
178   EXPECT_SNIPPET(expected_targets);
179   EXPECT_SNIPPET(expected_root_target);
180   EXPECT_SNIPPET(expected_default);
181 #undef EXPECT_SNIPPET
182 
183   // A pool definition for ninja's built-in console pool must not be written.
184   EXPECT_EQ(std::string::npos, out_str.find("pool console"));
185 }
186 
TEST_F(NinjaBuildWriterTest,SpaceInDepfile)187 TEST_F(NinjaBuildWriterTest, SpaceInDepfile) {
188   TestWithScope setup;
189   Err err;
190 
191   // Setup sets the default root dir to ".".
192   base::FilePath root(FILE_PATH_LITERAL("."));
193   base::FilePath root_realpath = base::MakeAbsoluteFilePath(root);
194   setup.build_settings()->SetRootPath(root_realpath);
195 
196   // Cannot use MakeAbsoluteFilePath for non-existed paths
197   base::FilePath dependency =
198       root_realpath.Append(FILE_PATH_LITERAL("path with space/BUILD.gn"));
199   g_scheduler->AddGenDependency(dependency);
200 
201   std::unordered_map<const Settings*, const Toolchain*> used_toolchains;
202   used_toolchains[setup.settings()] = setup.toolchain();
203   std::vector<const Target*> targets;
204   std::ostringstream ninja_out;
205   std::ostringstream depfile_out;
206   NinjaBuildWriter writer(setup.build_settings(), used_toolchains, targets,
207                           setup.toolchain(), targets, ninja_out, depfile_out);
208   ASSERT_TRUE(writer.Run(&err));
209 
210   EXPECT_EQ(depfile_out.str(),
211             "build.ninja: ../../path\\ with\\ space/BUILD.gn");
212 }
213 
TEST_F(NinjaBuildWriterTest,DuplicateOutputs)214 TEST_F(NinjaBuildWriterTest, DuplicateOutputs) {
215   TestWithScope setup;
216   Err err;
217 
218   Target target_foo(setup.settings(), Label(SourceDir("//foo/"), "bar"));
219   target_foo.set_output_type(Target::ACTION);
220   target_foo.action_values().set_script(SourceFile("//foo/script.py"));
221   target_foo.action_values().outputs() = SubstitutionList::MakeForTest(
222       "//out/Debug/out1.out", "//out/Debug/out2.out");
223   target_foo.SetToolchain(setup.toolchain());
224   ASSERT_TRUE(target_foo.OnResolved(&err));
225 
226   Target target_bar(setup.settings(), Label(SourceDir("//bar/"), "bar"));
227   target_bar.set_output_type(Target::ACTION);
228   target_bar.action_values().set_script(SourceFile("//bar/script.py"));
229   target_bar.action_values().outputs() = SubstitutionList::MakeForTest(
230       "//out/Debug/out3.out", "//out/Debug/out2.out");
231   target_bar.SetToolchain(setup.toolchain());
232   ASSERT_TRUE(target_bar.OnResolved(&err));
233 
234   std::unordered_map<const Settings*, const Toolchain*> used_toolchains;
235   used_toolchains[setup.settings()] = setup.toolchain();
236   std::vector<const Target*> targets = {&target_foo, &target_bar};
237   std::ostringstream ninja_out;
238   std::ostringstream depfile_out;
239   NinjaBuildWriter writer(setup.build_settings(), used_toolchains, targets,
240                           setup.toolchain(), targets, ninja_out, depfile_out);
241   ASSERT_FALSE(writer.Run(&err));
242 
243   const char expected_help_test[] =
244       "Two or more targets generate the same output:\n"
245       "  out2.out\n"
246       "\n"
247       "This is can often be fixed by changing one of the target names, or by \n"
248       "setting an output_name on one of them.\n"
249       "\n"
250       "Collisions:\n"
251       "  //foo:bar()\n"
252       "  //bar:bar()\n";
253 
254   EXPECT_EQ(expected_help_test, err.help_text());
255 }
256