[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / tools / gn / ninja_binary_target_writer.cc
blob1a6fe5bedc767060096d57c294f91e42ede01e8c
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 <cstring>
8 #include <set>
9 #include <sstream>
11 #include "base/containers/hash_tables.h"
12 #include "base/strings/string_util.h"
13 #include "tools/gn/config_values_extractors.h"
14 #include "tools/gn/deps_iterator.h"
15 #include "tools/gn/err.h"
16 #include "tools/gn/escape.h"
17 #include "tools/gn/filesystem_utils.h"
18 #include "tools/gn/ninja_utils.h"
19 #include "tools/gn/scheduler.h"
20 #include "tools/gn/settings.h"
21 #include "tools/gn/source_file_type.h"
22 #include "tools/gn/string_utils.h"
23 #include "tools/gn/substitution_writer.h"
24 #include "tools/gn/target.h"
26 // Represents a set of tool types. Must be first since it is also shared by
27 // some helper functions in the anonymous namespace below.
28 class NinjaBinaryTargetWriter::SourceFileTypeSet {
29 public:
30 SourceFileTypeSet() {
31 memset(flags_, 0, sizeof(bool) * static_cast<int>(SOURCE_NUMTYPES));
34 void Set(SourceFileType type) {
35 flags_[static_cast<int>(type)] = true;
37 bool Get(SourceFileType type) const {
38 return flags_[static_cast<int>(type)];
41 private:
42 bool flags_[static_cast<int>(SOURCE_NUMTYPES)];
45 namespace {
47 // Returns the proper escape options for writing compiler and linker flags.
48 EscapeOptions GetFlagOptions() {
49 EscapeOptions opts;
50 opts.mode = ESCAPE_NINJA_COMMAND;
52 // Some flag strings are actually multiple flags that expect to be just
53 // added to the command line. We assume that quoting is done by the
54 // buildfiles if it wants such things quoted.
55 opts.inhibit_quoting = true;
57 return opts;
60 struct DefineWriter {
61 DefineWriter() {
62 options.mode = ESCAPE_NINJA_COMMAND;
65 void operator()(const std::string& s, std::ostream& out) const {
66 out << " -D";
67 EscapeStringToStream(out, s, options);
70 EscapeOptions options;
73 struct IncludeWriter {
74 explicit IncludeWriter(PathOutput& path_output) : path_output_(path_output) {
76 ~IncludeWriter() {
79 void operator()(const SourceDir& d, std::ostream& out) const {
80 std::ostringstream path_out;
81 path_output_.WriteDir(path_out, d, PathOutput::DIR_NO_LAST_SLASH);
82 const std::string& path = path_out.str();
83 if (path[0] == '"')
84 out << " \"-I" << path.substr(1);
85 else
86 out << " -I" << path;
89 PathOutput& path_output_;
92 // Computes the set of output files resulting from compiling the given source
93 // file. If the file can be compiled and the tool exists, fills the outputs in
94 // and writes the tool type to computed_tool_type. If the file is not
95 // compilable, returns false.
97 // The target that the source belongs to is passed as an argument. In the case
98 // of linking to source sets, this can be different than the target this class
99 // is currently writing.
101 // The function can succeed with a "NONE" tool type for object files which are
102 // just passed to the output. The output will always be overwritten, not
103 // appended to.
104 bool GetOutputFilesForSource(const Target* target,
105 const SourceFile& source,
106 Toolchain::ToolType* computed_tool_type,
107 std::vector<OutputFile>* outputs) {
108 outputs->clear();
109 *computed_tool_type = Toolchain::TYPE_NONE;
111 SourceFileType file_type = GetSourceFileType(source);
112 if (file_type == SOURCE_UNKNOWN)
113 return false;
114 if (file_type == SOURCE_O) {
115 // Object files just get passed to the output and not compiled.
116 outputs->push_back(
117 OutputFile(target->settings()->build_settings(), source));
118 return true;
121 *computed_tool_type =
122 target->toolchain()->GetToolTypeForSourceType(file_type);
123 if (*computed_tool_type == Toolchain::TYPE_NONE)
124 return false; // No tool for this file (it's a header file or something).
125 const Tool* tool = target->toolchain()->GetTool(*computed_tool_type);
126 if (!tool)
127 return false; // Tool does not apply for this toolchain.file.
129 // Figure out what output(s) this compiler produces.
130 SubstitutionWriter::ApplyListToCompilerAsOutputFile(
131 target, source, tool->outputs(), outputs);
132 return !outputs->empty();
135 // Returns the language-specific suffix for precompiled header files.
136 const char* GetPCHLangSuffixForToolType(Toolchain::ToolType type) {
137 switch (type) {
138 case Toolchain::TYPE_CC:
139 return "c";
140 case Toolchain::TYPE_CXX:
141 return "cc";
142 case Toolchain::TYPE_OBJC:
143 return "m";
144 case Toolchain::TYPE_OBJCXX:
145 return "mm";
146 default:
147 NOTREACHED() << "Not a valid PCH tool type: " << type;
148 return "";
152 std::string GetWindowsPCHObjectExtension(Toolchain::ToolType tool_type) {
153 const char* lang_suffix = GetPCHLangSuffixForToolType(tool_type);
154 std::string result = ".";
155 // For MSVC, annotate the obj files with the language type. For example:
156 // obj/foo/target_name.precompile.o ->
157 // obj/foo/target_name.precompile.cc.o
158 result += lang_suffix;
159 result += ".o";
160 return result;
163 std::string GetGCCPCHOutputExtension(Toolchain::ToolType tool_type) {
164 const char* lang_suffix = GetPCHLangSuffixForToolType(tool_type);
165 std::string result = ".";
166 // For GCC, the output name must have a .gch suffix and be annotated with
167 // the language type. For example:
168 // obj/foo/target_name.header.h ->
169 // obj/foo/target_name.header.h-cc.gch
170 // In order for the compiler to pick it up, the output name (minus the .gch
171 // suffix MUST match whatever is passed to the -include flag).
172 result += "h-";
173 result += lang_suffix;
174 result += ".gch";
175 return result;
178 // Returns the language-specific lang recognized by gcc’s -x flag for
179 // precompiled header files.
180 const char* GetPCHLangForToolType(Toolchain::ToolType type) {
181 switch (type) {
182 case Toolchain::TYPE_CC:
183 return "c-header";
184 case Toolchain::TYPE_CXX:
185 return "c++-header";
186 case Toolchain::TYPE_OBJC:
187 return "objective-c-header";
188 case Toolchain::TYPE_OBJCXX:
189 return "objective-c++-header";
190 default:
191 NOTREACHED() << "Not a valid PCH tool type: " << type;
192 return "";
196 // Fills |outputs| with the object or gch file for the precompiled header of the
197 // given type (flag type and tool type must match).
198 void GetPCHOutputFiles(const Target* target,
199 Toolchain::ToolType tool_type,
200 std::vector<OutputFile>* outputs) {
201 outputs->clear();
203 // Compute the tool. This must use the tool type passed in rather than the
204 // detected file type of the precompiled source file since the same
205 // precompiled source file will be used for separate C/C++ compiles.
206 const Tool* tool = target->toolchain()->GetTool(tool_type);
207 if (!tool)
208 return;
209 SubstitutionWriter::ApplyListToCompilerAsOutputFile(
210 target, target->config_values().precompiled_source(),
211 tool->outputs(), outputs);
213 if (outputs->empty())
214 return;
215 if (outputs->size() > 1)
216 outputs->resize(1); // Only link the first output from the compiler tool.
218 std::string& output_value = (*outputs)[0].value();
219 size_t extension_offset = FindExtensionOffset(output_value);
220 if (extension_offset == std::string::npos) {
221 // No extension found.
222 return;
224 DCHECK(extension_offset >= 1);
225 DCHECK(output_value[extension_offset - 1] == '.');
227 std::string output_extension;
228 Tool::PrecompiledHeaderType header_type = tool->precompiled_header_type();
229 switch (header_type) {
230 case Tool::PCH_MSVC:
231 output_extension = GetWindowsPCHObjectExtension(tool_type);
232 break;
233 case Tool::PCH_GCC:
234 output_extension = GetGCCPCHOutputExtension(tool_type);
235 break;
236 case Tool::PCH_NONE:
237 NOTREACHED() << "No outputs for no PCH type.";
238 break;
240 output_value.replace(extension_offset - 1,
241 std::string::npos,
242 output_extension);
245 // Appends the object files generated by the given source set to the given
246 // output vector.
247 void AddSourceSetObjectFiles(const Target* source_set,
248 UniqueVector<OutputFile>* obj_files) {
249 std::vector<OutputFile> tool_outputs; // Prevent allocation in loop.
250 NinjaBinaryTargetWriter::SourceFileTypeSet used_types;
252 // Compute object files for all sources. Only link the first output from
253 // the tool if there are more than one.
254 for (const auto& source : source_set->sources()) {
255 Toolchain::ToolType tool_type = Toolchain::TYPE_NONE;
256 if (GetOutputFilesForSource(source_set, source, &tool_type, &tool_outputs))
257 obj_files->push_back(tool_outputs[0]);
259 used_types.Set(GetSourceFileType(source));
262 // Add MSVC precompiled header object files. GCC .gch files are not object
263 // files so they are omitted.
264 if (source_set->config_values().has_precompiled_headers()) {
265 if (used_types.Get(SOURCE_C)) {
266 const Tool* tool = source_set->toolchain()->GetTool(Toolchain::TYPE_CC);
267 if (tool && tool->precompiled_header_type() == Tool::PCH_MSVC) {
268 GetPCHOutputFiles(source_set, Toolchain::TYPE_CC, &tool_outputs);
269 obj_files->Append(tool_outputs.begin(), tool_outputs.end());
272 if (used_types.Get(SOURCE_CPP)) {
273 const Tool* tool = source_set->toolchain()->GetTool(Toolchain::TYPE_CXX);
274 if (tool && tool->precompiled_header_type() == Tool::PCH_MSVC) {
275 GetPCHOutputFiles(source_set, Toolchain::TYPE_CXX, &tool_outputs);
276 obj_files->Append(tool_outputs.begin(), tool_outputs.end());
279 if (used_types.Get(SOURCE_M)) {
280 const Tool* tool = source_set->toolchain()->GetTool(Toolchain::TYPE_OBJC);
281 if (tool && tool->precompiled_header_type() == Tool::PCH_MSVC) {
282 GetPCHOutputFiles(source_set, Toolchain::TYPE_OBJC, &tool_outputs);
283 obj_files->Append(tool_outputs.begin(), tool_outputs.end());
286 if (used_types.Get(SOURCE_MM)) {
287 const Tool* tool = source_set->toolchain()->GetTool(
288 Toolchain::TYPE_OBJCXX);
289 if (tool && tool->precompiled_header_type() == Tool::PCH_MSVC) {
290 GetPCHOutputFiles(source_set, Toolchain::TYPE_OBJCXX, &tool_outputs);
291 obj_files->Append(tool_outputs.begin(), tool_outputs.end());
297 } // namespace
299 NinjaBinaryTargetWriter::NinjaBinaryTargetWriter(const Target* target,
300 std::ostream& out)
301 : NinjaTargetWriter(target, out),
302 tool_(target->toolchain()->GetToolForTargetFinalOutput(target)),
303 rule_prefix_(GetNinjaRulePrefixForToolchain(settings_)) {
306 NinjaBinaryTargetWriter::~NinjaBinaryTargetWriter() {
309 void NinjaBinaryTargetWriter::Run() {
310 // Figure out what source types are needed.
311 SourceFileTypeSet used_types;
312 for (const auto& source : target_->sources())
313 used_types.Set(GetSourceFileType(source));
315 WriteCompilerVars(used_types);
317 // The input dependencies will be an order-only dependency. This will cause
318 // Ninja to make sure the inputs are up-to-date before compiling this source,
319 // but changes in the inputs deps won't cause the file to be recompiled.
321 // This is important to prevent changes in unrelated actions that are
322 // upstream of this target from causing everything to be recompiled.
324 // Why can we get away with this rather than using implicit deps ("|", which
325 // will force rebuilds when the inputs change)? For source code, the
326 // computed dependencies of all headers will be computed by the compiler,
327 // which will cause source rebuilds if any "real" upstream dependencies
328 // change.
330 // If a .cc file is generated by an input dependency, Ninja will see the
331 // input to the build rule doesn't exist, and that it is an output from a
332 // previous step, and build the previous step first. This is a "real"
333 // dependency and doesn't need | or || to express.
335 // The only case where this rule matters is for the first build where no .d
336 // files exist, and Ninja doesn't know what that source file depends on. In
337 // this case it's sufficient to ensure that the upstream dependencies are
338 // built first. This is exactly what Ninja's order-only dependencies
339 // expresses.
340 OutputFile order_only_dep =
341 WriteInputDepsStampAndGetDep(std::vector<const Target*>());
343 // For GCC builds, the .gch files are not object files, but still need to be
344 // added as explicit dependencies below. The .gch output files are placed in
345 // |pch_other_files|. This is to prevent linking against them.
346 std::vector<OutputFile> pch_obj_files;
347 std::vector<OutputFile> pch_other_files;
348 WritePCHCommands(used_types, order_only_dep,
349 &pch_obj_files, &pch_other_files);
350 std::vector<OutputFile>* pch_files = !pch_obj_files.empty() ?
351 &pch_obj_files : &pch_other_files;
353 // Treat all pch output files as explicit dependencies of all
354 // compiles. Some notes:
356 // - Only the language-specific one is required for any specific compile, but
357 // that's more difficult to express and the additional logic doesn't buy
358 // much reduced parallelism. Just list them all (there's usually only one
359 // anyway).
361 // - On Windows, the .pch file is the input to the compile, not the
362 // precompiled header's corresponding object file that we're using here.
363 // But Ninja's depslog doesn't support multiple outputs from the
364 // precompiled header compile step (it outputs both the .pch file and a
365 // corresponding .obj file). So we consistently list the .obj file and the
366 // .pch file we really need comes along with it.
368 // - GCC .gch files are not object files, therefore they are not added to the
369 // object file list.
370 std::vector<OutputFile> obj_files;
371 std::vector<SourceFile> other_files;
372 WriteSources(*pch_files, order_only_dep, &obj_files, &other_files);
374 // Link all MSVC pch object files. The vector will be empty on GCC toolchains.
375 obj_files.insert(obj_files.end(), pch_obj_files.begin(), pch_obj_files.end());
376 if (!CheckForDuplicateObjectFiles(obj_files))
377 return;
379 if (target_->output_type() == Target::SOURCE_SET) {
380 WriteSourceSetStamp(obj_files);
381 #ifndef NDEBUG
382 // Verify that the function that separately computes a source set's object
383 // files match the object files just computed.
384 UniqueVector<OutputFile> computed_obj;
385 AddSourceSetObjectFiles(target_, &computed_obj);
386 DCHECK_EQ(obj_files.size(), computed_obj.size());
387 for (const auto& obj : obj_files)
388 DCHECK_NE(static_cast<size_t>(-1), computed_obj.IndexOf(obj));
389 #endif
390 } else {
391 WriteLinkerStuff(obj_files, other_files);
395 void NinjaBinaryTargetWriter::WriteCompilerVars(
396 const SourceFileTypeSet& used_types) {
397 const SubstitutionBits& subst = target_->toolchain()->substitution_bits();
399 // Defines.
400 if (subst.used[SUBSTITUTION_DEFINES]) {
401 out_ << kSubstitutionNinjaNames[SUBSTITUTION_DEFINES] << " =";
402 RecursiveTargetConfigToStream<std::string>(
403 target_, &ConfigValues::defines, DefineWriter(), out_);
404 out_ << std::endl;
407 // Include directories.
408 if (subst.used[SUBSTITUTION_INCLUDE_DIRS]) {
409 out_ << kSubstitutionNinjaNames[SUBSTITUTION_INCLUDE_DIRS] << " =";
410 PathOutput include_path_output(
411 path_output_.current_dir(),
412 settings_->build_settings()->root_path_utf8(),
413 ESCAPE_NINJA_COMMAND);
414 RecursiveTargetConfigToStream<SourceDir>(
415 target_, &ConfigValues::include_dirs,
416 IncludeWriter(include_path_output), out_);
417 out_ << std::endl;
420 bool has_precompiled_headers =
421 target_->config_values().has_precompiled_headers();
423 // Some toolchains pass cflags to the assembler since it's the same command.
424 EscapeOptions opts = GetFlagOptions();
425 if (used_types.Get(SOURCE_C) || used_types.Get(SOURCE_CPP) ||
426 used_types.Get(SOURCE_M) || used_types.Get(SOURCE_MM) ||
427 used_types.Get(SOURCE_S) || used_types.Get(SOURCE_ASM)) {
428 WriteOneFlag(SUBSTITUTION_CFLAGS, false, Toolchain::TYPE_NONE,
429 &ConfigValues::cflags, opts);
431 if (used_types.Get(SOURCE_C) || used_types.Get(SOURCE_S) ||
432 used_types.Get(SOURCE_ASM)) {
433 WriteOneFlag(SUBSTITUTION_CFLAGS_C, has_precompiled_headers,
434 Toolchain::TYPE_CC, &ConfigValues::cflags_c, opts);
436 if (used_types.Get(SOURCE_CPP)) {
437 WriteOneFlag(SUBSTITUTION_CFLAGS_CC, has_precompiled_headers,
438 Toolchain::TYPE_CXX, &ConfigValues::cflags_cc, opts);
440 if (used_types.Get(SOURCE_M)) {
441 WriteOneFlag(SUBSTITUTION_CFLAGS_OBJC, has_precompiled_headers,
442 Toolchain::TYPE_OBJC, &ConfigValues::cflags_objc, opts);
444 if (used_types.Get(SOURCE_MM)) {
445 WriteOneFlag(SUBSTITUTION_CFLAGS_OBJCC, has_precompiled_headers,
446 Toolchain::TYPE_OBJCXX, &ConfigValues::cflags_objcc, opts);
449 WriteSharedVars(subst);
452 void NinjaBinaryTargetWriter::WriteOneFlag(
453 SubstitutionType subst_enum,
454 bool has_precompiled_headers,
455 Toolchain::ToolType tool_type,
456 const std::vector<std::string>& (ConfigValues::* getter)() const,
457 EscapeOptions flag_escape_options) {
458 if (!target_->toolchain()->substitution_bits().used[subst_enum])
459 return;
461 out_ << kSubstitutionNinjaNames[subst_enum] << " =";
463 if (has_precompiled_headers) {
464 const Tool* tool = target_->toolchain()->GetTool(tool_type);
465 if (tool && tool->precompiled_header_type() == Tool::PCH_MSVC) {
466 // Name the .pch file.
467 out_ << " /Fp";
468 path_output_.WriteFile(out_, GetWindowsPCHFile(tool_type));
470 // Enables precompiled headers and names the .h file. It's a string
471 // rather than a file name (so no need to rebase or use path_output_).
472 out_ << " /Yu" << target_->config_values().precompiled_header();
473 RecursiveTargetConfigStringsToStream(target_, getter,
474 flag_escape_options, out_);
475 } else if (tool && tool->precompiled_header_type() == Tool::PCH_GCC) {
476 // The targets to build the .gch files should omit the -include flag
477 // below. To accomplish this, each substitution flag is overwritten in the
478 // target rule and these values are repeated. The -include flag is omitted
479 // in place of the required -x <header lang> flag for .gch targets.
480 RecursiveTargetConfigStringsToStream(target_, getter,
481 flag_escape_options, out_);
483 // Compute the gch file (it will be language-specific).
484 std::vector<OutputFile> outputs;
485 GetPCHOutputFiles(target_, tool_type, &outputs);
486 if (!outputs.empty()) {
487 // Trim the .gch suffix for the -include flag.
488 // e.g. for gch file foo/bar/target.precompiled.h.gch:
489 // -include foo/bar/target.precompiled.h
490 std::string pch_file = outputs[0].value();
491 pch_file.erase(pch_file.length() - 4);
492 out_ << " -include " << pch_file;
496 out_ << std::endl;
499 void NinjaBinaryTargetWriter::WritePCHCommands(
500 const SourceFileTypeSet& used_types,
501 const OutputFile& order_only_dep,
502 std::vector<OutputFile>* object_files,
503 std::vector<OutputFile>* other_files) {
504 if (!target_->config_values().has_precompiled_headers())
505 return;
507 const Tool* tool_c = target_->toolchain()->GetTool(Toolchain::TYPE_CC);
508 if (tool_c &&
509 tool_c->precompiled_header_type() != Tool::PCH_NONE &&
510 used_types.Get(SOURCE_C)) {
511 WritePCHCommand(SUBSTITUTION_CFLAGS_C,
512 Toolchain::TYPE_CC,
513 tool_c->precompiled_header_type(),
514 order_only_dep, object_files, other_files);
516 const Tool* tool_cxx = target_->toolchain()->GetTool(Toolchain::TYPE_CXX);
517 if (tool_cxx &&
518 tool_cxx->precompiled_header_type() != Tool::PCH_NONE &&
519 used_types.Get(SOURCE_CPP)) {
520 WritePCHCommand(SUBSTITUTION_CFLAGS_CC,
521 Toolchain::TYPE_CXX,
522 tool_cxx->precompiled_header_type(),
523 order_only_dep, object_files, other_files);
526 const Tool* tool_objc = target_->toolchain()->GetTool(Toolchain::TYPE_OBJC);
527 if (tool_objc &&
528 tool_objc->precompiled_header_type() == Tool::PCH_GCC &&
529 used_types.Get(SOURCE_M)) {
530 WritePCHCommand(SUBSTITUTION_CFLAGS_OBJC,
531 Toolchain::TYPE_OBJC,
532 tool_objc->precompiled_header_type(),
533 order_only_dep, object_files, other_files);
536 const Tool* tool_objcxx =
537 target_->toolchain()->GetTool(Toolchain::TYPE_OBJCXX);
538 if (tool_objcxx &&
539 tool_objcxx->precompiled_header_type() == Tool::PCH_GCC &&
540 used_types.Get(SOURCE_MM)) {
541 WritePCHCommand(SUBSTITUTION_CFLAGS_OBJCC,
542 Toolchain::TYPE_OBJCXX,
543 tool_objcxx->precompiled_header_type(),
544 order_only_dep, object_files, other_files);
548 void NinjaBinaryTargetWriter::WritePCHCommand(
549 SubstitutionType flag_type,
550 Toolchain::ToolType tool_type,
551 Tool::PrecompiledHeaderType header_type,
552 const OutputFile& order_only_dep,
553 std::vector<OutputFile>* object_files,
554 std::vector<OutputFile>* other_files) {
555 switch (header_type) {
556 case Tool::PCH_MSVC:
557 WriteWindowsPCHCommand(flag_type, tool_type, order_only_dep,
558 object_files);
559 break;
560 case Tool::PCH_GCC:
561 WriteGCCPCHCommand(flag_type, tool_type, order_only_dep,
562 other_files);
563 break;
564 case Tool::PCH_NONE:
565 NOTREACHED() << "Cannot write a PCH command with no PCH header type";
566 break;
570 void NinjaBinaryTargetWriter::WriteGCCPCHCommand(
571 SubstitutionType flag_type,
572 Toolchain::ToolType tool_type,
573 const OutputFile& order_only_dep,
574 std::vector<OutputFile>* gch_files) {
575 // Compute the pch output file (it will be language-specific).
576 std::vector<OutputFile> outputs;
577 GetPCHOutputFiles(target_, tool_type, &outputs);
578 if (outputs.empty())
579 return;
581 gch_files->insert(gch_files->end(), outputs.begin(), outputs.end());
583 // Build line to compile the file.
584 WriteCompilerBuildLine(target_->config_values().precompiled_source(),
585 std::vector<OutputFile>(), order_only_dep, tool_type,
586 outputs);
588 // This build line needs a custom language-specific flags value. Rule-specific
589 // variables are just indented underneath the rule line.
590 out_ << " " << kSubstitutionNinjaNames[flag_type] << " =";
592 // Each substitution flag is overwritten in the target rule to replace the
593 // implicitly generated -include flag with the -x <header lang> flag required
594 // for .gch targets.
595 EscapeOptions opts = GetFlagOptions();
596 if (tool_type == Toolchain::TYPE_CC) {
597 RecursiveTargetConfigStringsToStream(target_,
598 &ConfigValues::cflags_c, opts, out_);
599 } else if (tool_type == Toolchain::TYPE_CXX) {
600 RecursiveTargetConfigStringsToStream(target_,
601 &ConfigValues::cflags_cc, opts, out_);
602 } else if (tool_type == Toolchain::TYPE_OBJC) {
603 RecursiveTargetConfigStringsToStream(target_,
604 &ConfigValues::cflags_objc, opts, out_);
605 } else if (tool_type == Toolchain::TYPE_OBJCXX) {
606 RecursiveTargetConfigStringsToStream(target_,
607 &ConfigValues::cflags_objcc, opts, out_);
610 // Append the command to specify the language of the .gch file.
611 out_ << " -x " << GetPCHLangForToolType(tool_type);
613 // Write two blank lines to help separate the PCH build lines from the
614 // regular source build lines.
615 out_ << std::endl << std::endl;
618 void NinjaBinaryTargetWriter::WriteWindowsPCHCommand(
619 SubstitutionType flag_type,
620 Toolchain::ToolType tool_type,
621 const OutputFile& order_only_dep,
622 std::vector<OutputFile>* object_files) {
623 // Compute the pch output file (it will be language-specific).
624 std::vector<OutputFile> outputs;
625 GetPCHOutputFiles(target_, tool_type, &outputs);
626 if (outputs.empty())
627 return;
629 object_files->insert(object_files->end(), outputs.begin(), outputs.end());
631 // Build line to compile the file.
632 WriteCompilerBuildLine(target_->config_values().precompiled_source(),
633 std::vector<OutputFile>(), order_only_dep, tool_type,
634 outputs);
636 // This build line needs a custom language-specific flags value. Rule-specific
637 // variables are just indented underneath the rule line.
638 out_ << " " << kSubstitutionNinjaNames[flag_type] << " =";
640 // Append the command to generate the .pch file.
641 // This adds the value to the existing flag instead of overwriting it.
642 out_ << " ${" << kSubstitutionNinjaNames[flag_type] << "}";
643 out_ << " /Yc" << target_->config_values().precompiled_header();
645 // Write two blank lines to help separate the PCH build lines from the
646 // regular source build lines.
647 out_ << std::endl << std::endl;
650 void NinjaBinaryTargetWriter::WriteSources(
651 const std::vector<OutputFile>& pch_deps,
652 const OutputFile& order_only_dep,
653 std::vector<OutputFile>* object_files,
654 std::vector<SourceFile>* other_files) {
655 object_files->reserve(object_files->size() + target_->sources().size());
657 std::vector<OutputFile> tool_outputs; // Prevent reallocation in loop.
658 std::vector<OutputFile> deps;
659 for (const auto& source : target_->sources()) {
660 // Clear the vector but maintain the max capacity to prevent reallocations.
661 deps.resize(0);
662 Toolchain::ToolType tool_type = Toolchain::TYPE_NONE;
663 if (!GetOutputFilesForSource(target_, source, &tool_type, &tool_outputs)) {
664 if (GetSourceFileType(source) == SOURCE_DEF)
665 other_files->push_back(source);
666 continue; // No output for this source.
669 if (tool_type != Toolchain::TYPE_NONE) {
670 // Only include PCH deps that correspond to the tool type, for instance,
671 // do not specify target_name.precompile.cc.o (a CXX PCH file) as a dep
672 // for the output of a C tool type.
674 // This makes the assumption that pch_deps only contains pch output files
675 // with the naming scheme specified in GetWindowsPCHObjectExtension or
676 // GetGCCPCHOutputExtension.
677 const Tool* tool = target_->toolchain()->GetTool(tool_type);
678 for (const auto& dep : pch_deps) {
679 const std::string& output_value = dep.value();
680 std::string output_extension;
681 if (tool->precompiled_header_type() == Tool::PCH_MSVC) {
682 output_extension = GetWindowsPCHObjectExtension(tool_type);
683 } else if (tool->precompiled_header_type() == Tool::PCH_GCC) {
684 output_extension = GetGCCPCHOutputExtension(tool_type);
686 if (output_value.compare(output_value.size() - output_extension.size(),
687 output_extension.size(), output_extension) == 0) {
688 deps.push_back(dep);
691 WriteCompilerBuildLine(source, deps, order_only_dep, tool_type,
692 tool_outputs);
695 // It's theoretically possible for a compiler to produce more than one
696 // output, but we'll only link to the first output.
697 object_files->push_back(tool_outputs[0]);
699 out_ << std::endl;
702 void NinjaBinaryTargetWriter::WriteCompilerBuildLine(
703 const SourceFile& source,
704 const std::vector<OutputFile>& extra_deps,
705 const OutputFile& order_only_dep,
706 Toolchain::ToolType tool_type,
707 const std::vector<OutputFile>& outputs) {
708 out_ << "build";
709 path_output_.WriteFiles(out_, outputs);
711 out_ << ": " << rule_prefix_ << Toolchain::ToolTypeToName(tool_type);
712 out_ << " ";
713 path_output_.WriteFile(out_, source);
715 if (!extra_deps.empty()) {
716 out_ << " |";
717 for (const OutputFile& dep : extra_deps) {
718 out_ << " ";
719 path_output_.WriteFile(out_, dep);
723 if (!order_only_dep.value().empty()) {
724 out_ << " || ";
725 path_output_.WriteFile(out_, order_only_dep);
727 out_ << std::endl;
730 void NinjaBinaryTargetWriter::WriteLinkerStuff(
731 const std::vector<OutputFile>& object_files,
732 const std::vector<SourceFile>& other_files) {
733 std::vector<OutputFile> output_files;
734 SubstitutionWriter::ApplyListToLinkerAsOutputFile(
735 target_, tool_, tool_->outputs(), &output_files);
737 out_ << "build";
738 path_output_.WriteFiles(out_, output_files);
740 out_ << ": " << rule_prefix_
741 << Toolchain::ToolTypeToName(
742 target_->toolchain()->GetToolTypeForTargetFinalOutput(target_));
744 UniqueVector<OutputFile> extra_object_files;
745 UniqueVector<const Target*> linkable_deps;
746 UniqueVector<const Target*> non_linkable_deps;
747 GetDeps(&extra_object_files, &linkable_deps, &non_linkable_deps);
749 // Object files.
750 path_output_.WriteFiles(out_, object_files);
751 path_output_.WriteFiles(out_, extra_object_files);
753 // Dependencies.
754 std::vector<OutputFile> implicit_deps;
755 std::vector<OutputFile> solibs;
756 for (const Target* cur : linkable_deps) {
757 // All linkable deps should have a link output file.
758 DCHECK(!cur->link_output_file().value().empty())
759 << "No link output file for "
760 << target_->label().GetUserVisibleName(false);
762 if (cur->dependency_output_file().value() !=
763 cur->link_output_file().value()) {
764 // This is a shared library with separate link and deps files. Save for
765 // later.
766 implicit_deps.push_back(cur->dependency_output_file());
767 solibs.push_back(cur->link_output_file());
768 } else {
769 // Normal case, just link to this target.
770 out_ << " ";
771 path_output_.WriteFile(out_, cur->link_output_file());
775 const SourceFile* optional_def_file = nullptr;
776 if (!other_files.empty()) {
777 for (const SourceFile& src_file : other_files) {
778 if (GetSourceFileType(src_file) == SOURCE_DEF) {
779 optional_def_file = &src_file;
780 implicit_deps.push_back(
781 OutputFile(settings_->build_settings(), src_file));
782 break; // Only one def file is allowed.
787 // Append implicit dependencies collected above.
788 if (!implicit_deps.empty()) {
789 out_ << " |";
790 path_output_.WriteFiles(out_, implicit_deps);
793 // Append data dependencies as order-only dependencies.
795 // This will include data dependencies and input dependencies (like when
796 // this target depends on an action). Having the data dependencies in this
797 // list ensures that the data is available at runtime when the user builds
798 // this target.
800 // The action dependencies are not strictly necessary in this case. They
801 // should also have been collected via the input deps stamp that each source
802 // file has for an order-only dependency, and since this target depends on
803 // the sources, there is already an implicit order-only dependency. However,
804 // it's extra work to separate these out and there's no disadvantage to
805 // listing them again.
806 WriteOrderOnlyDependencies(non_linkable_deps);
808 // End of the link "build" line.
809 out_ << std::endl;
811 // These go in the inner scope of the link line.
812 WriteLinkerFlags(optional_def_file);
814 WriteLibs();
815 WriteOutputExtension();
816 WriteSolibs(solibs);
819 void NinjaBinaryTargetWriter::WriteLinkerFlags(
820 const SourceFile* optional_def_file) {
821 out_ << " ldflags =";
823 // First the ldflags from the target and its config.
824 EscapeOptions flag_options = GetFlagOptions();
825 RecursiveTargetConfigStringsToStream(target_, &ConfigValues::ldflags,
826 flag_options, out_);
828 // Followed by library search paths that have been recursively pushed
829 // through the dependency tree.
830 const OrderedSet<SourceDir> all_lib_dirs = target_->all_lib_dirs();
831 if (!all_lib_dirs.empty()) {
832 // Since we're passing these on the command line to the linker and not
833 // to Ninja, we need to do shell escaping.
834 PathOutput lib_path_output(path_output_.current_dir(),
835 settings_->build_settings()->root_path_utf8(),
836 ESCAPE_NINJA_COMMAND);
837 for (size_t i = 0; i < all_lib_dirs.size(); i++) {
838 out_ << " " << tool_->lib_dir_switch();
839 lib_path_output.WriteDir(out_, all_lib_dirs[i],
840 PathOutput::DIR_NO_LAST_SLASH);
844 if (optional_def_file) {
845 out_ << " /DEF:";
846 path_output_.WriteFile(out_, *optional_def_file);
849 out_ << std::endl;
852 void NinjaBinaryTargetWriter::WriteLibs() {
853 out_ << " libs =";
855 // Libraries that have been recursively pushed through the dependency tree.
856 EscapeOptions lib_escape_opts;
857 lib_escape_opts.mode = ESCAPE_NINJA_COMMAND;
858 const OrderedSet<std::string> all_libs = target_->all_libs();
859 const std::string framework_ending(".framework");
860 for (size_t i = 0; i < all_libs.size(); i++) {
861 if (settings_->IsMac() &&
862 base::EndsWith(all_libs[i], framework_ending,
863 base::CompareCase::INSENSITIVE_ASCII)) {
864 // Special-case libraries ending in ".framework" on Mac. Add the
865 // -framework switch and don't add the extension to the output.
866 out_ << " -framework ";
867 EscapeStringToStream(out_,
868 all_libs[i].substr(0, all_libs[i].size() - framework_ending.size()),
869 lib_escape_opts);
870 } else {
871 out_ << " " << tool_->lib_switch();
872 EscapeStringToStream(out_, all_libs[i], lib_escape_opts);
875 out_ << std::endl;
878 void NinjaBinaryTargetWriter::WriteOutputExtension() {
879 out_ << " output_extension = ";
880 if (target_->output_extension().empty()) {
881 // Use the default from the tool.
882 out_ << tool_->default_output_extension();
883 } else {
884 // Use the one specified in the target. Note that the one in the target
885 // does not include the leading dot, so add that.
886 out_ << "." << target_->output_extension();
888 out_ << std::endl;
891 void NinjaBinaryTargetWriter::WriteSolibs(
892 const std::vector<OutputFile>& solibs) {
893 if (solibs.empty())
894 return;
896 out_ << " solibs =";
897 path_output_.WriteFiles(out_, solibs);
898 out_ << std::endl;
901 void NinjaBinaryTargetWriter::WriteSourceSetStamp(
902 const std::vector<OutputFile>& object_files) {
903 // The stamp rule for source sets is generally not used, since targets that
904 // depend on this will reference the object files directly. However, writing
905 // this rule allows the user to type the name of the target and get a build
906 // which can be convenient for development.
907 UniqueVector<OutputFile> extra_object_files;
908 UniqueVector<const Target*> linkable_deps;
909 UniqueVector<const Target*> non_linkable_deps;
910 GetDeps(&extra_object_files, &linkable_deps, &non_linkable_deps);
912 // The classifier should never put extra object files in a source set:
913 // any source sets that we depend on should appear in our non-linkable
914 // deps instead.
915 DCHECK(extra_object_files.empty());
917 std::vector<OutputFile> order_only_deps;
918 for (const auto& dep : non_linkable_deps)
919 order_only_deps.push_back(dep->dependency_output_file());
921 WriteStampForTarget(object_files, order_only_deps);
924 void NinjaBinaryTargetWriter::GetDeps(
925 UniqueVector<OutputFile>* extra_object_files,
926 UniqueVector<const Target*>* linkable_deps,
927 UniqueVector<const Target*>* non_linkable_deps) const {
928 // Normal public/private deps.
929 for (const auto& pair : target_->GetDeps(Target::DEPS_LINKED)) {
930 ClassifyDependency(pair.ptr, extra_object_files,
931 linkable_deps, non_linkable_deps);
934 // Inherited libraries.
935 for (const auto& inherited_target :
936 target_->inherited_libraries().GetOrdered()) {
937 ClassifyDependency(inherited_target, extra_object_files,
938 linkable_deps, non_linkable_deps);
941 // Data deps.
942 for (const auto& data_dep_pair : target_->data_deps())
943 non_linkable_deps->push_back(data_dep_pair.ptr);
946 void NinjaBinaryTargetWriter::ClassifyDependency(
947 const Target* dep,
948 UniqueVector<OutputFile>* extra_object_files,
949 UniqueVector<const Target*>* linkable_deps,
950 UniqueVector<const Target*>* non_linkable_deps) const {
951 // Only the following types of outputs have libraries linked into them:
952 // EXECUTABLE
953 // SHARED_LIBRARY
954 // _complete_ STATIC_LIBRARY
956 // Child deps of intermediate static libraries get pushed up the
957 // dependency tree until one of these is reached, and source sets
958 // don't link at all.
959 bool can_link_libs = target_->IsFinal();
961 if (dep->output_type() == Target::SOURCE_SET) {
962 // Source sets have their object files linked into final targets
963 // (shared libraries, executables, and complete static
964 // libraries). Intermediate static libraries and other source sets
965 // just forward the dependency, otherwise the files in the source
966 // set can easily get linked more than once which will cause
967 // multiple definition errors.
968 if (can_link_libs)
969 AddSourceSetObjectFiles(dep, extra_object_files);
971 // Add the source set itself as a non-linkable dependency on the current
972 // target. This will make sure that anything the source set's stamp file
973 // depends on (like data deps) are also built before the current target
974 // can be complete. Otherwise, these will be skipped since this target
975 // will depend only on the source set's object files.
976 non_linkable_deps->push_back(dep);
977 } else if (can_link_libs && dep->IsLinkable()) {
978 linkable_deps->push_back(dep);
979 } else {
980 non_linkable_deps->push_back(dep);
984 void NinjaBinaryTargetWriter::WriteOrderOnlyDependencies(
985 const UniqueVector<const Target*>& non_linkable_deps) {
986 if (!non_linkable_deps.empty()) {
987 out_ << " ||";
989 // Non-linkable targets.
990 for (const auto& non_linkable_dep : non_linkable_deps) {
991 out_ << " ";
992 path_output_.WriteFile(out_, non_linkable_dep->dependency_output_file());
997 OutputFile NinjaBinaryTargetWriter::GetWindowsPCHFile(
998 Toolchain::ToolType tool_type) const {
999 // Use "obj/{dir}/{target_name}_{lang}.pch" which ends up
1000 // looking like "obj/chrome/browser/browser_cc.pch"
1001 OutputFile ret = GetTargetOutputDirAsOutputFile(target_);
1002 ret.value().append(target_->label().name());
1003 ret.value().push_back('_');
1004 ret.value().append(GetPCHLangSuffixForToolType(tool_type));
1005 ret.value().append(".pch");
1007 return ret;
1010 bool NinjaBinaryTargetWriter::CheckForDuplicateObjectFiles(
1011 const std::vector<OutputFile>& files) const {
1012 base::hash_set<std::string> set;
1013 for (const auto& file : files) {
1014 if (!set.insert(file.value()).second) {
1015 Err err(
1016 target_->defined_from(),
1017 "Duplicate object file",
1018 "The target " + target_->label().GetUserVisibleName(false) +
1019 "\ngenerates two object files with the same name:\n " +
1020 file.value() + "\n"
1021 "\n"
1022 "It could be you accidentally have a file listed twice in the\n"
1023 "sources. Or, depending on how your toolchain maps sources to\n"
1024 "object files, two source files with the same name in different\n"
1025 "directories could map to the same object file.\n"
1026 "\n"
1027 "In the latter case, either rename one of the files or move one of\n"
1028 "the sources to a separate source_set to avoid them both being in\n"
1029 "the same target.");
1030 g_scheduler->FailWithError(err);
1031 return false;
1034 return true;