Warn about unused result in LoginDatabase.
[chromium-blink-merge.git] / tools / gn / ninja_binary_target_writer.cc
blob16cc320446a22ee392d4e1ab559d8617d08b538f
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 "tools/gn/ninja_binary_target_writer.h"
7 #include <set>
8 #include <sstream>
10 #include "base/strings/string_util.h"
11 #include "tools/gn/config_values_extractors.h"
12 #include "tools/gn/deps_iterator.h"
13 #include "tools/gn/err.h"
14 #include "tools/gn/escape.h"
15 #include "tools/gn/ninja_utils.h"
16 #include "tools/gn/settings.h"
17 #include "tools/gn/string_utils.h"
18 #include "tools/gn/substitution_writer.h"
19 #include "tools/gn/target.h"
21 namespace {
23 // Returns the proper escape options for writing compiler and linker flags.
24 EscapeOptions GetFlagOptions() {
25 EscapeOptions opts;
26 opts.mode = ESCAPE_NINJA_COMMAND;
28 // Some flag strings are actually multiple flags that expect to be just
29 // added to the command line. We assume that quoting is done by the
30 // buildfiles if it wants such things quoted.
31 opts.inhibit_quoting = true;
33 return opts;
36 struct DefineWriter {
37 DefineWriter() {
38 options.mode = ESCAPE_NINJA_COMMAND;
41 void operator()(const std::string& s, std::ostream& out) const {
42 out << " -D";
43 EscapeStringToStream(out, s, options);
46 EscapeOptions options;
49 struct IncludeWriter {
50 IncludeWriter(PathOutput& path_output) : path_output_(path_output) {
52 ~IncludeWriter() {
55 void operator()(const SourceDir& d, std::ostream& out) const {
56 std::ostringstream path_out;
57 path_output_.WriteDir(path_out, d, PathOutput::DIR_NO_LAST_SLASH);
58 const std::string& path = path_out.str();
59 if (path[0] == '"')
60 out << " \"-I" << path.substr(1);
61 else
62 out << " -I" << path;
65 PathOutput& path_output_;
68 } // namespace
70 NinjaBinaryTargetWriter::NinjaBinaryTargetWriter(const Target* target,
71 std::ostream& out)
72 : NinjaTargetWriter(target, out),
73 tool_(target->toolchain()->GetToolForTargetFinalOutput(target)) {
76 NinjaBinaryTargetWriter::~NinjaBinaryTargetWriter() {
79 void NinjaBinaryTargetWriter::Run() {
80 WriteCompilerVars();
82 std::vector<OutputFile> obj_files;
83 WriteSources(&obj_files);
85 if (target_->output_type() == Target::SOURCE_SET)
86 WriteSourceSetStamp(obj_files);
87 else
88 WriteLinkerStuff(obj_files);
91 void NinjaBinaryTargetWriter::WriteCompilerVars() {
92 const SubstitutionBits& subst = target_->toolchain()->substitution_bits();
94 // Defines.
95 if (subst.used[SUBSTITUTION_DEFINES]) {
96 out_ << kSubstitutionNinjaNames[SUBSTITUTION_DEFINES] << " =";
97 RecursiveTargetConfigToStream<std::string>(
98 target_, &ConfigValues::defines, DefineWriter(), out_);
99 out_ << std::endl;
102 // Include directories.
103 if (subst.used[SUBSTITUTION_INCLUDE_DIRS]) {
104 out_ << kSubstitutionNinjaNames[SUBSTITUTION_INCLUDE_DIRS] << " =";
105 PathOutput include_path_output(
106 path_output_.current_dir(),
107 settings_->build_settings()->root_path_utf8(),
108 ESCAPE_NINJA_COMMAND);
109 RecursiveTargetConfigToStream<SourceDir>(
110 target_, &ConfigValues::include_dirs,
111 IncludeWriter(include_path_output), out_);
112 out_ << std::endl;
115 // C flags and friends.
116 EscapeOptions flag_escape_options = GetFlagOptions();
117 #define WRITE_FLAGS(name, subst_enum) \
118 if (subst.used[subst_enum]) { \
119 out_ << kSubstitutionNinjaNames[subst_enum] << " ="; \
120 RecursiveTargetConfigStringsToStream(target_, &ConfigValues::name, \
121 flag_escape_options, out_); \
122 out_ << std::endl; \
125 WRITE_FLAGS(cflags, SUBSTITUTION_CFLAGS)
126 WRITE_FLAGS(cflags_c, SUBSTITUTION_CFLAGS_C)
127 WRITE_FLAGS(cflags_cc, SUBSTITUTION_CFLAGS_CC)
128 WRITE_FLAGS(cflags_objc, SUBSTITUTION_CFLAGS_OBJC)
129 WRITE_FLAGS(cflags_objcc, SUBSTITUTION_CFLAGS_OBJCC)
131 #undef WRITE_FLAGS
133 WriteSharedVars(subst);
136 void NinjaBinaryTargetWriter::WriteSources(
137 std::vector<OutputFile>* object_files) {
138 object_files->reserve(target_->sources().size());
140 OutputFile input_dep =
141 WriteInputDepsStampAndGetDep(std::vector<const Target*>());
143 std::string rule_prefix = GetNinjaRulePrefixForToolchain(settings_);
145 std::vector<OutputFile> tool_outputs; // Prevent reallocation in loop.
146 for (const auto& source : target_->sources()) {
147 Toolchain::ToolType tool_type = Toolchain::TYPE_NONE;
148 if (!GetOutputFilesForSource(target_, source,
149 &tool_type, &tool_outputs))
150 continue; // No output for this source.
152 if (tool_type != Toolchain::TYPE_NONE) {
153 out_ << "build";
154 path_output_.WriteFiles(out_, tool_outputs);
156 out_ << ": " << rule_prefix << Toolchain::ToolTypeToName(tool_type);
157 out_ << " ";
158 path_output_.WriteFile(out_, source);
159 if (!input_dep.value().empty()) {
160 // Write out the input dependencies as an order-only dependency. This
161 // will cause Ninja to make sure the inputs are up-to-date before
162 // compiling this source, but changes in the inputs deps won't cause
163 // the file to be recompiled.
165 // This is important to prevent changes in unrelated actions that
166 // are upstream of this target from causing everything to be recompiled.
168 // Why can we get away with this rather than using implicit deps ("|",
169 // which will force rebuilds when the inputs change)? For source code,
170 // the computed dependencies of all headers will be computed by the
171 // compiler, which will cause source rebuilds if any "real" upstream
172 // dependencies change.
174 // If a .cc file is generated by an input dependency, Ninja will see
175 // the input to the build rule doesn't exist, and that it is an output
176 // from a previous step, and build the previous step first. This is a
177 // "real" dependency and doesn't need | or || to express.
179 // The only case where this rule matters is for the first build where
180 // no .d files exist, and Ninja doesn't know what that source file
181 // depends on. In this case it's sufficient to ensure that the upstream
182 // dependencies are built first. This is exactly what Ninja's order-
183 // only dependencies expresses.
184 out_ << " || ";
185 path_output_.WriteFile(out_, input_dep);
187 out_ << std::endl;
190 // It's theoretically possible for a compiler to produce more than one
191 // output, but we'll only link to the first output.
192 object_files->push_back(tool_outputs[0]);
194 out_ << std::endl;
197 void NinjaBinaryTargetWriter::WriteLinkerStuff(
198 const std::vector<OutputFile>& object_files) {
199 std::vector<OutputFile> output_files;
200 SubstitutionWriter::ApplyListToLinkerAsOutputFile(
201 target_, tool_, tool_->outputs(), &output_files);
203 out_ << "build";
204 path_output_.WriteFiles(out_, output_files);
206 out_ << ": "
207 << GetNinjaRulePrefixForToolchain(settings_)
208 << Toolchain::ToolTypeToName(
209 target_->toolchain()->GetToolTypeForTargetFinalOutput(target_));
211 UniqueVector<OutputFile> extra_object_files;
212 UniqueVector<const Target*> linkable_deps;
213 UniqueVector<const Target*> non_linkable_deps;
214 GetDeps(&extra_object_files, &linkable_deps, &non_linkable_deps);
216 // Object files.
217 for (const auto& obj : object_files) {
218 out_ << " ";
219 path_output_.WriteFile(out_, obj);
221 for (const auto& obj : extra_object_files) {
222 out_ << " ";
223 path_output_.WriteFile(out_, obj);
226 std::vector<OutputFile> implicit_deps;
227 std::vector<OutputFile> solibs;
229 for (const Target* cur : linkable_deps) {
230 // All linkable deps should have a link output file.
231 DCHECK(!cur->link_output_file().value().empty())
232 << "No link output file for "
233 << target_->label().GetUserVisibleName(false);
235 if (cur->dependency_output_file().value() !=
236 cur->link_output_file().value()) {
237 // This is a shared library with separate link and deps files. Save for
238 // later.
239 implicit_deps.push_back(cur->dependency_output_file());
240 solibs.push_back(cur->link_output_file());
241 } else {
242 // Normal case, just link to this target.
243 out_ << " ";
244 path_output_.WriteFile(out_, cur->link_output_file());
248 // Append implicit dependencies collected above.
249 if (!implicit_deps.empty()) {
250 out_ << " |";
251 path_output_.WriteFiles(out_, implicit_deps);
254 // Append data dependencies as order-only dependencies.
256 // This will include data dependencies and input dependencies (like when
257 // this target depends on an action). Having the data dependencies in this
258 // list ensures that the data is available at runtime when the user builds
259 // this target.
261 // The action dependencies are not strictly necessary in this case. They
262 // should also have been collected via the input deps stamp that each source
263 // file has for an order-only dependency, and since this target depends on
264 // the sources, there is already an implicit order-only dependency. However,
265 // it's extra work to separate these out and there's no disadvantage to
266 // listing them again.
267 WriteOrderOnlyDependencies(non_linkable_deps);
269 // End of the link "build" line.
270 out_ << std::endl;
272 // These go in the inner scope of the link line.
273 WriteLinkerFlags();
274 WriteLibs();
275 WriteOutputExtension();
276 WriteSolibs(solibs);
279 void NinjaBinaryTargetWriter::WriteLinkerFlags() {
280 out_ << " ldflags =";
282 // First the ldflags from the target and its config.
283 EscapeOptions flag_options = GetFlagOptions();
284 RecursiveTargetConfigStringsToStream(target_, &ConfigValues::ldflags,
285 flag_options, out_);
287 // Followed by library search paths that have been recursively pushed
288 // through the dependency tree.
289 const OrderedSet<SourceDir> all_lib_dirs = target_->all_lib_dirs();
290 if (!all_lib_dirs.empty()) {
291 // Since we're passing these on the command line to the linker and not
292 // to Ninja, we need to do shell escaping.
293 PathOutput lib_path_output(path_output_.current_dir(),
294 settings_->build_settings()->root_path_utf8(),
295 ESCAPE_NINJA_COMMAND);
296 for (size_t i = 0; i < all_lib_dirs.size(); i++) {
297 out_ << " " << tool_->lib_dir_switch();
298 lib_path_output.WriteDir(out_, all_lib_dirs[i],
299 PathOutput::DIR_NO_LAST_SLASH);
302 out_ << std::endl;
305 void NinjaBinaryTargetWriter::WriteLibs() {
306 out_ << " libs =";
308 // Libraries that have been recursively pushed through the dependency tree.
309 EscapeOptions lib_escape_opts;
310 lib_escape_opts.mode = ESCAPE_NINJA_COMMAND;
311 const OrderedSet<std::string> all_libs = target_->all_libs();
312 const std::string framework_ending(".framework");
313 for (size_t i = 0; i < all_libs.size(); i++) {
314 if (settings_->IsMac() && EndsWith(all_libs[i], framework_ending, false)) {
315 // Special-case libraries ending in ".framework" on Mac. Add the
316 // -framework switch and don't add the extension to the output.
317 out_ << " -framework ";
318 EscapeStringToStream(out_,
319 all_libs[i].substr(0, all_libs[i].size() - framework_ending.size()),
320 lib_escape_opts);
321 } else {
322 out_ << " " << tool_->lib_switch();
323 EscapeStringToStream(out_, all_libs[i], lib_escape_opts);
326 out_ << std::endl;
329 void NinjaBinaryTargetWriter::WriteOutputExtension() {
330 out_ << " output_extension = ";
331 if (target_->output_extension().empty()) {
332 // Use the default from the tool.
333 out_ << tool_->default_output_extension();
334 } else {
335 // Use the one specified in the target. Note that the one in the target
336 // does not include the leading dot, so add that.
337 out_ << "." << target_->output_extension();
339 out_ << std::endl;
342 void NinjaBinaryTargetWriter::WriteSolibs(
343 const std::vector<OutputFile>& solibs) {
344 if (solibs.empty())
345 return;
347 out_ << " solibs =";
348 path_output_.WriteFiles(out_, solibs);
349 out_ << std::endl;
352 void NinjaBinaryTargetWriter::WriteSourceSetStamp(
353 const std::vector<OutputFile>& object_files) {
354 // The stamp rule for source sets is generally not used, since targets that
355 // depend on this will reference the object files directly. However, writing
356 // this rule allows the user to type the name of the target and get a build
357 // which can be convenient for development.
358 UniqueVector<OutputFile> extra_object_files;
359 UniqueVector<const Target*> linkable_deps;
360 UniqueVector<const Target*> non_linkable_deps;
361 GetDeps(&extra_object_files, &linkable_deps, &non_linkable_deps);
363 // The classifier should never put extra object files in a source set:
364 // any source sets that we depend on should appear in our non-linkable
365 // deps instead.
366 DCHECK(extra_object_files.empty());
368 std::vector<OutputFile> order_only_deps;
369 for (const auto& dep : non_linkable_deps)
370 order_only_deps.push_back(dep->dependency_output_file());
372 WriteStampForTarget(object_files, order_only_deps);
375 void NinjaBinaryTargetWriter::GetDeps(
376 UniqueVector<OutputFile>* extra_object_files,
377 UniqueVector<const Target*>* linkable_deps,
378 UniqueVector<const Target*>* non_linkable_deps) const {
379 // Normal public/private deps.
380 for (const auto& pair : target_->GetDeps(Target::DEPS_LINKED)) {
381 ClassifyDependency(pair.ptr, extra_object_files,
382 linkable_deps, non_linkable_deps);
385 // Inherited libraries.
386 for (const auto& inherited_target : target_->inherited_libraries()) {
387 ClassifyDependency(inherited_target, extra_object_files,
388 linkable_deps, non_linkable_deps);
391 // Data deps.
392 for (const auto& data_dep_pair : target_->data_deps())
393 non_linkable_deps->push_back(data_dep_pair.ptr);
396 void NinjaBinaryTargetWriter::ClassifyDependency(
397 const Target* dep,
398 UniqueVector<OutputFile>* extra_object_files,
399 UniqueVector<const Target*>* linkable_deps,
400 UniqueVector<const Target*>* non_linkable_deps) const {
401 // Only the following types of outputs have libraries linked into them:
402 // EXECUTABLE
403 // SHARED_LIBRARY
404 // _complete_ STATIC_LIBRARY
406 // Child deps of intermediate static libraries get pushed up the
407 // dependency tree until one of these is reached, and source sets
408 // don't link at all.
409 bool can_link_libs = target_->IsFinal();
411 if (dep->output_type() == Target::SOURCE_SET) {
412 // Source sets have their object files linked into final targets
413 // (shared libraries, executables, and complete static
414 // libraries). Intermediate static libraries and other source sets
415 // just forward the dependency, otherwise the files in the source
416 // set can easily get linked more than once which will cause
417 // multiple definition errors.
418 if (can_link_libs) {
419 // Linking in a source set to an executable, shared library, or
420 // complete static library, so copy its object files.
421 std::vector<OutputFile> tool_outputs; // Prevent allocation in loop.
422 for (const auto& source : dep->sources()) {
423 Toolchain::ToolType tool_type = Toolchain::TYPE_NONE;
424 if (GetOutputFilesForSource(dep, source, &tool_type, &tool_outputs)) {
425 // Only link the first output if there are more than one.
426 extra_object_files->push_back(tool_outputs[0]);
430 } else if (can_link_libs && dep->IsLinkable()) {
431 linkable_deps->push_back(dep);
432 } else {
433 non_linkable_deps->push_back(dep);
437 void NinjaBinaryTargetWriter::WriteOrderOnlyDependencies(
438 const UniqueVector<const Target*>& non_linkable_deps) {
439 const std::vector<SourceFile>& data = target_->data();
440 if (!non_linkable_deps.empty() || !data.empty()) {
441 out_ << " ||";
443 // Non-linkable targets.
444 for (size_t i = 0; i < non_linkable_deps.size(); i++) {
445 out_ << " ";
446 path_output_.WriteFile(
447 out_, non_linkable_deps[i]->dependency_output_file());
452 bool NinjaBinaryTargetWriter::GetOutputFilesForSource(
453 const Target* target,
454 const SourceFile& source,
455 Toolchain::ToolType* computed_tool_type,
456 std::vector<OutputFile>* outputs) const {
457 outputs->clear();
458 *computed_tool_type = Toolchain::TYPE_NONE;
460 SourceFileType file_type = GetSourceFileType(source);
461 if (file_type == SOURCE_UNKNOWN)
462 return false;
463 if (file_type == SOURCE_O) {
464 // Object files just get passed to the output and not compiled.
465 outputs->push_back(OutputFile(settings_->build_settings(), source));
466 return true;
469 *computed_tool_type =
470 target->toolchain()->GetToolTypeForSourceType(file_type);
471 if (*computed_tool_type == Toolchain::TYPE_NONE)
472 return false; // No tool for this file (it's a header file or something).
473 const Tool* tool = target->toolchain()->GetTool(*computed_tool_type);
474 if (!tool)
475 return false; // Tool does not apply for this toolchain.file.
477 // Figure out what output(s) this compiler produces.
478 SubstitutionWriter::ApplyListToCompilerAsOutputFile(
479 target, source, tool->outputs(), outputs);
480 return !outputs->empty();