media: Update MediaCodecBridge to use the new log.
[chromium-blink-merge.git] / tools / gn / ninja_binary_target_writer.cc
blob88a6eb2d546ac6e025a52df9f1d322091a27b864
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 explicit 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 :
387 target_->inherited_libraries().GetOrdered()) {
388 ClassifyDependency(inherited_target, extra_object_files,
389 linkable_deps, non_linkable_deps);
392 // Data deps.
393 for (const auto& data_dep_pair : target_->data_deps())
394 non_linkable_deps->push_back(data_dep_pair.ptr);
397 void NinjaBinaryTargetWriter::ClassifyDependency(
398 const Target* dep,
399 UniqueVector<OutputFile>* extra_object_files,
400 UniqueVector<const Target*>* linkable_deps,
401 UniqueVector<const Target*>* non_linkable_deps) const {
402 // Only the following types of outputs have libraries linked into them:
403 // EXECUTABLE
404 // SHARED_LIBRARY
405 // _complete_ STATIC_LIBRARY
407 // Child deps of intermediate static libraries get pushed up the
408 // dependency tree until one of these is reached, and source sets
409 // don't link at all.
410 bool can_link_libs = target_->IsFinal();
412 if (dep->output_type() == Target::SOURCE_SET) {
413 // Source sets have their object files linked into final targets
414 // (shared libraries, executables, and complete static
415 // libraries). Intermediate static libraries and other source sets
416 // just forward the dependency, otherwise the files in the source
417 // set can easily get linked more than once which will cause
418 // multiple definition errors.
419 if (can_link_libs) {
420 // Linking in a source set to an executable, shared library, or
421 // complete static library, so copy its object files.
422 std::vector<OutputFile> tool_outputs; // Prevent allocation in loop.
423 for (const auto& source : dep->sources()) {
424 Toolchain::ToolType tool_type = Toolchain::TYPE_NONE;
425 if (GetOutputFilesForSource(dep, source, &tool_type, &tool_outputs)) {
426 // Only link the first output if there are more than one.
427 extra_object_files->push_back(tool_outputs[0]);
432 // Add the source set itself as a non-linkable dependency on the current
433 // target. This will make sure that anything the source set's stamp file
434 // depends on (like data deps) are also built before the current target
435 // can be complete. Otherwise, these will be skipped since this target
436 // will depend only on the source set's object files.
437 non_linkable_deps->push_back(dep);
438 } else if (can_link_libs && dep->IsLinkable()) {
439 linkable_deps->push_back(dep);
440 } else {
441 non_linkable_deps->push_back(dep);
445 void NinjaBinaryTargetWriter::WriteOrderOnlyDependencies(
446 const UniqueVector<const Target*>& non_linkable_deps) {
447 if (!non_linkable_deps.empty()) {
448 out_ << " ||";
450 // Non-linkable targets.
451 for (const auto& non_linkable_dep : non_linkable_deps) {
452 out_ << " ";
453 path_output_.WriteFile(out_, non_linkable_dep->dependency_output_file());
458 bool NinjaBinaryTargetWriter::GetOutputFilesForSource(
459 const Target* target,
460 const SourceFile& source,
461 Toolchain::ToolType* computed_tool_type,
462 std::vector<OutputFile>* outputs) const {
463 outputs->clear();
464 *computed_tool_type = Toolchain::TYPE_NONE;
466 SourceFileType file_type = GetSourceFileType(source);
467 if (file_type == SOURCE_UNKNOWN)
468 return false;
469 if (file_type == SOURCE_O) {
470 // Object files just get passed to the output and not compiled.
471 outputs->push_back(OutputFile(settings_->build_settings(), source));
472 return true;
475 *computed_tool_type =
476 target->toolchain()->GetToolTypeForSourceType(file_type);
477 if (*computed_tool_type == Toolchain::TYPE_NONE)
478 return false; // No tool for this file (it's a header file or something).
479 const Tool* tool = target->toolchain()->GetTool(*computed_tool_type);
480 if (!tool)
481 return false; // Tool does not apply for this toolchain.file.
483 // Figure out what output(s) this compiler produces.
484 SubstitutionWriter::ApplyListToCompilerAsOutputFile(
485 target, source, tool->outputs(), outputs);
486 return !outputs->empty();