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"
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"
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();
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();
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
)
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
) {
74 // Only show the toolchain labels (which can be confusing) if something
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;
86 const Target
* generator
= FindTargetThatGeneratesFile(builder
, file
);
88 generator
->settings()->toolchain_label() != default_toolchain
)
89 show_toolchains
= true;
91 const std::string target_str
= targets
.size() > 1 ? "targets" : "target";
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";
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
);
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()) {
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
);
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
);
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"
155 " gn gen <out_dir>\n"
157 " Generates ninja files from the current tree and puts them in the given\n"
158 " output directory.\n"
160 " The output directory can be a source-repo-absolute path name such as:\n"
162 " Or it can be a directory relative to the current directory such as:\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();
177 // Deliberately leaked to avoid expensive process teardown.
178 Setup
* setup
= new Setup();
179 if (!setup
->DoSetup(args
[0], true))
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.
197 // Write the root ninja files.
198 if (!NinjaWriter::RunAndWriteFiles(&setup
->build_settings(),
205 if (!WriteRuntimeDepsFilesIfNecessary(*setup
->builder(), &err
)) {
210 if (!CheckForInvalidGeneratedInputs(setup
))
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
)) +
222 setup
->scheduler().input_file_manager()->GetInputFileCount()) +
224 base::Int64ToString(elapsed_time
.InMilliseconds()) + "ms\n";
231 } // namespace commands