[Sync] Add bookmark move tests.
[chromium-blink-merge.git] / tools / gn / command_gen.cc
blobeceb8df09f867c28ed8a45733e8a01b418ee016a
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.
5 #include "base/atomicops.h"
6 #include "base/bind.h"
7 #include "base/command_line.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/timer/elapsed_timer.h"
11 #include "tools/gn/build_settings.h"
12 #include "tools/gn/commands.h"
13 #include "tools/gn/ninja_target_writer.h"
14 #include "tools/gn/ninja_writer.h"
15 #include "tools/gn/runtime_deps.h"
16 #include "tools/gn/scheduler.h"
17 #include "tools/gn/setup.h"
18 #include "tools/gn/standard_out.h"
19 #include "tools/gn/switches.h"
20 #include "tools/gn/target.h"
22 namespace commands {
24 namespace {
26 const char kSwitchCheck[] = "check";
28 // Called on worker thread to write the ninja file.
29 void BackgroundDoWrite(const Target* target) {
30 NinjaTargetWriter::RunAndWriteFile(target);
31 g_scheduler->DecrementWorkCount();
34 // Called on the main thread.
35 void ItemResolvedCallback(base::subtle::Atomic32* write_counter,
36 scoped_refptr<Builder> builder,
37 const BuilderRecord* record) {
38 base::subtle::NoBarrier_AtomicIncrement(write_counter, 1);
40 const Item* item = record->item();
41 const Target* target = item->AsTarget();
42 if (target) {
43 g_scheduler->IncrementWorkCount();
44 g_scheduler->ScheduleWork(base::Bind(&BackgroundDoWrite, target));
48 // Returns a pointer to the target with the given file as an output, or null
49 // if no targets generate the file. This is brute force since this is an
50 // error condition and performance shouldn't matter.
51 const Target* FindTargetThatGeneratesFile(const Builder* builder,
52 const SourceFile& file) {
53 std::vector<const Target*> targets = builder->GetAllResolvedTargets();
54 if (targets.empty())
55 return nullptr;
57 OutputFile output_file(targets[0]->settings()->build_settings(), file);
58 for (const Target* target : targets) {
59 for (const auto& cur_output : target->computed_outputs()) {
60 if (cur_output == output_file)
61 return target;
64 return nullptr;
67 // Prints an error that the given file was present as a source or input in
68 // the given target(s) but was not generated by any of its dependencies.
69 void PrintInvalidGeneratedInput(const Builder* builder,
70 const SourceFile& file,
71 const std::vector<const Target*>& targets) {
72 std::string err;
74 const std::string target_str = targets.size() > 1 ? "targets" : "target";
75 err += "The file:\n";
76 err += " " + file.value() + "\n";
77 err += "is listed as an input or source for the " + target_str + ":\n";
78 for (const Target* target : targets)
79 err += " " + target->label().GetUserVisibleName(false) + "\n";
81 const Target* generator = FindTargetThatGeneratesFile(builder, file);
82 if (generator) {
83 err += "but this file was not generated by any dependencies of the " +
84 target_str + ". The target\nthat generates the file is:\n ";
85 err += generator->label().GetUserVisibleName(false);
86 } else {
87 err += "but no targets in the build generate that file.";
90 Err(Location(), "Input to " + target_str + " not generated by a dependency.",
91 err).PrintToStdout();
94 bool CheckForInvalidGeneratedInputs(Setup* setup) {
95 std::multimap<SourceFile, const Target*> unknown_inputs =
96 g_scheduler->GetUnknownGeneratedInputs();
97 if (unknown_inputs.empty())
98 return true; // No bad files.
100 int errors_found = 0;
101 auto cur = unknown_inputs.begin();
102 while (cur != unknown_inputs.end()) {
103 errors_found++;
104 auto end_of_range = unknown_inputs.upper_bound(cur->first);
106 // Package the values more conveniently for printing.
107 SourceFile bad_input = cur->first;
108 std::vector<const Target*> targets;
109 while (cur != end_of_range)
110 targets.push_back((cur++)->second);
112 PrintInvalidGeneratedInput(setup->builder(), bad_input, targets);
113 OutputString("\n");
116 OutputString(
117 "If you have generated inputs, there needs to be a dependency path "
118 "between the\ntwo targets in addition to just listing the files. For "
119 "indirect dependencies,\nthe intermediate ones must be public_deps. "
120 "data_deps don't count since they're\nonly runtime dependencies. If "
121 "you think a dependency chain exists, it might be\nbecause the chain "
122 "is private. Try \"gn path\" to analyze.\n");
124 if (errors_found > 1) {
125 OutputString(base::StringPrintf("\n%d generated input errors found.\n",
126 errors_found), DECORATION_YELLOW);
128 return false;
131 } // namespace
133 const char kGen[] = "gen";
134 const char kGen_HelpShort[] =
135 "gen: Generate ninja files.";
136 const char kGen_Help[] =
137 "gn gen: Generate ninja files.\n"
138 "\n"
139 " gn gen <out_dir>\n"
140 "\n"
141 " Generates ninja files from the current tree and puts them in the given\n"
142 " output directory.\n"
143 "\n"
144 " The output directory can be a source-repo-absolute path name such as:\n"
145 " //out/foo\n"
146 " Or it can be a directory relative to the current directory such as:\n"
147 " out/foo\n"
148 "\n"
149 " See \"gn help\" for the common command-line switches.\n";
151 int RunGen(const std::vector<std::string>& args) {
152 base::ElapsedTimer timer;
154 if (args.size() != 1) {
155 Err(Location(), "Need exactly one build directory to generate.",
156 "I expected something more like \"gn gen out/foo\"\n"
157 "You can also see \"gn help gen\".").PrintToStdout();
158 return 1;
161 // Deliberately leaked to avoid expensive process teardown.
162 Setup* setup = new Setup();
163 if (!setup->DoSetup(args[0], true))
164 return 1;
166 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kSwitchCheck))
167 setup->set_check_public_headers(true);
169 // Cause the load to also generate the ninja files for each target. We wrap
170 // the writing to maintain a counter.
171 base::subtle::Atomic32 write_counter = 0;
172 setup->builder()->set_resolved_callback(
173 base::Bind(&ItemResolvedCallback, &write_counter,
174 scoped_refptr<Builder>(setup->builder())));
176 // Do the actual load. This will also write out the target ninja files.
177 if (!setup->Run())
178 return 1;
180 Err err;
181 // Write the root ninja files.
182 if (!NinjaWriter::RunAndWriteFiles(&setup->build_settings(),
183 setup->builder(),
184 &err)) {
185 err.PrintToStdout();
186 return 1;
189 if (!WriteRuntimeDepsFilesIfNecessary(*setup->builder(), &err)) {
190 err.PrintToStdout();
191 return 1;
194 if (!CheckForInvalidGeneratedInputs(setup))
195 return 1;
197 base::TimeDelta elapsed_time = timer.Elapsed();
199 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kQuiet)) {
200 OutputString("Done. ", DECORATION_GREEN);
202 std::string stats = "Wrote " +
203 base::IntToString(static_cast<int>(write_counter)) +
204 " targets from " +
205 base::IntToString(
206 setup->scheduler().input_file_manager()->GetInputFileCount()) +
207 " files in " +
208 base::Int64ToString(elapsed_time.InMilliseconds()) + "ms\n";
209 OutputString(stats);
212 return 0;
215 } // namespace commands