[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / tools / gn / command_gen.cc
bloba8221a6d078283ce05310a18f6ea0763e8ca6725
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 // Only show the toolchain labels (which can be confusing) if something
75 // isn't the default.
76 bool show_toolchains = false;
77 const Label& default_toolchain =
78 targets[0]->settings()->default_toolchain_label();
79 for (const Target* target : targets) {
80 if (target->settings()->toolchain_label() != default_toolchain) {
81 show_toolchains = true;
82 break;
86 const Target* generator = FindTargetThatGeneratesFile(builder, file);
87 if (generator &&
88 generator->settings()->toolchain_label() != default_toolchain)
89 show_toolchains = true;
91 const std::string target_str = targets.size() > 1 ? "targets" : "target";
92 err += "The file:\n";
93 err += " " + file.value() + "\n";
94 err += "is listed as an input or source for the " + target_str + ":\n";
95 for (const Target* target : targets)
96 err += " " + target->label().GetUserVisibleName(show_toolchains) + "\n";
98 if (generator) {
99 err += "but this file was not generated by any dependencies of the " +
100 target_str + ". The target\nthat generates the file is:\n ";
101 err += generator->label().GetUserVisibleName(show_toolchains);
102 } else {
103 err += "but no targets in the build generate that file.";
106 Err(Location(), "Input to " + target_str + " not generated by a dependency.",
107 err).PrintToStdout();
110 bool CheckForInvalidGeneratedInputs(Setup* setup) {
111 std::multimap<SourceFile, const Target*> unknown_inputs =
112 g_scheduler->GetUnknownGeneratedInputs();
113 if (unknown_inputs.empty())
114 return true; // No bad files.
116 int errors_found = 0;
117 auto cur = unknown_inputs.begin();
118 while (cur != unknown_inputs.end()) {
119 errors_found++;
120 auto end_of_range = unknown_inputs.upper_bound(cur->first);
122 // Package the values more conveniently for printing.
123 SourceFile bad_input = cur->first;
124 std::vector<const Target*> targets;
125 while (cur != end_of_range)
126 targets.push_back((cur++)->second);
128 PrintInvalidGeneratedInput(setup->builder(), bad_input, targets);
129 OutputString("\n");
132 OutputString(
133 "If you have generated inputs, there needs to be a dependency path "
134 "between the\ntwo targets in addition to just listing the files. For "
135 "indirect dependencies,\nthe intermediate ones must be public_deps. "
136 "data_deps don't count since they're\nonly runtime dependencies. If "
137 "you think a dependency chain exists, it might be\nbecause the chain "
138 "is private. Try \"gn path\" to analyze.\n");
140 if (errors_found > 1) {
141 OutputString(base::StringPrintf("\n%d generated input errors found.\n",
142 errors_found), DECORATION_YELLOW);
144 return false;
147 } // namespace
149 const char kGen[] = "gen";
150 const char kGen_HelpShort[] =
151 "gen: Generate ninja files.";
152 const char kGen_Help[] =
153 "gn gen: Generate ninja files.\n"
154 "\n"
155 " gn gen <out_dir>\n"
156 "\n"
157 " Generates ninja files from the current tree and puts them in the given\n"
158 " output directory.\n"
159 "\n"
160 " The output directory can be a source-repo-absolute path name such as:\n"
161 " //out/foo\n"
162 " Or it can be a directory relative to the current directory such as:\n"
163 " out/foo\n"
164 "\n"
165 " See \"gn help switches\" for the common command-line switches.\n";
167 int RunGen(const std::vector<std::string>& args) {
168 base::ElapsedTimer timer;
170 if (args.size() != 1) {
171 Err(Location(), "Need exactly one build directory to generate.",
172 "I expected something more like \"gn gen out/foo\"\n"
173 "You can also see \"gn help gen\".").PrintToStdout();
174 return 1;
177 // Deliberately leaked to avoid expensive process teardown.
178 Setup* setup = new Setup();
179 if (!setup->DoSetup(args[0], true))
180 return 1;
182 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kSwitchCheck))
183 setup->set_check_public_headers(true);
185 // Cause the load to also generate the ninja files for each target. We wrap
186 // the writing to maintain a counter.
187 base::subtle::Atomic32 write_counter = 0;
188 setup->builder()->set_resolved_callback(
189 base::Bind(&ItemResolvedCallback, &write_counter,
190 scoped_refptr<Builder>(setup->builder())));
192 // Do the actual load. This will also write out the target ninja files.
193 if (!setup->Run())
194 return 1;
196 Err err;
197 // Write the root ninja files.
198 if (!NinjaWriter::RunAndWriteFiles(&setup->build_settings(),
199 setup->builder(),
200 &err)) {
201 err.PrintToStdout();
202 return 1;
205 if (!WriteRuntimeDepsFilesIfNecessary(*setup->builder(), &err)) {
206 err.PrintToStdout();
207 return 1;
210 if (!CheckForInvalidGeneratedInputs(setup))
211 return 1;
213 base::TimeDelta elapsed_time = timer.Elapsed();
215 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kQuiet)) {
216 OutputString("Done. ", DECORATION_GREEN);
218 std::string stats = "Wrote " +
219 base::IntToString(static_cast<int>(write_counter)) +
220 " targets from " +
221 base::IntToString(
222 setup->scheduler().input_file_manager()->GetInputFileCount()) +
223 " files in " +
224 base::Int64ToString(elapsed_time.InMilliseconds()) + "ms\n";
225 OutputString(stats);
228 return 0;
231 } // namespace commands