| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "tools/gn/ninja_binary_target_writer.h" | 5 #include "tools/gn/ninja_binary_target_writer.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <cstring> | 10 #include <cstring> |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 const std::string& path = path_out.str(); | 85 const std::string& path = path_out.str(); |
| 86 if (path[0] == '"') | 86 if (path[0] == '"') |
| 87 out << " \"-I" << path.substr(1); | 87 out << " \"-I" << path.substr(1); |
| 88 else | 88 else |
| 89 out << " -I" << path; | 89 out << " -I" << path; |
| 90 } | 90 } |
| 91 | 91 |
| 92 PathOutput& path_output_; | 92 PathOutput& path_output_; |
| 93 }; | 93 }; |
| 94 | 94 |
| 95 // Computes the set of output files resulting from compiling the given source | |
| 96 // file. If the file can be compiled and the tool exists, fills the outputs in | |
| 97 // and writes the tool type to computed_tool_type. If the file is not | |
| 98 // compilable, returns false. | |
| 99 // | |
| 100 // The target that the source belongs to is passed as an argument. In the case | |
| 101 // of linking to source sets, this can be different than the target this class | |
| 102 // is currently writing. | |
| 103 // | |
| 104 // The function can succeed with a "NONE" tool type for object files which are | |
| 105 // just passed to the output. The output will always be overwritten, not | |
| 106 // appended to. | |
| 107 bool GetOutputFilesForSource(const Target* target, | |
| 108 const SourceFile& source, | |
| 109 Toolchain::ToolType* computed_tool_type, | |
| 110 std::vector<OutputFile>* outputs) { | |
| 111 outputs->clear(); | |
| 112 *computed_tool_type = Toolchain::TYPE_NONE; | |
| 113 | |
| 114 SourceFileType file_type = GetSourceFileType(source); | |
| 115 if (file_type == SOURCE_UNKNOWN) | |
| 116 return false; | |
| 117 if (file_type == SOURCE_O) { | |
| 118 // Object files just get passed to the output and not compiled. | |
| 119 outputs->push_back( | |
| 120 OutputFile(target->settings()->build_settings(), source)); | |
| 121 return true; | |
| 122 } | |
| 123 | |
| 124 *computed_tool_type = | |
| 125 target->toolchain()->GetToolTypeForSourceType(file_type); | |
| 126 if (*computed_tool_type == Toolchain::TYPE_NONE) | |
| 127 return false; // No tool for this file (it's a header file or something). | |
| 128 const Tool* tool = target->toolchain()->GetTool(*computed_tool_type); | |
| 129 if (!tool) | |
| 130 return false; // Tool does not apply for this toolchain.file. | |
| 131 | |
| 132 // Figure out what output(s) this compiler produces. | |
| 133 SubstitutionWriter::ApplyListToCompilerAsOutputFile( | |
| 134 target, source, tool->outputs(), outputs); | |
| 135 return !outputs->empty(); | |
| 136 } | |
| 137 | |
| 138 // Returns the language-specific suffix for precompiled header files. | 95 // Returns the language-specific suffix for precompiled header files. |
| 139 const char* GetPCHLangSuffixForToolType(Toolchain::ToolType type) { | 96 const char* GetPCHLangSuffixForToolType(Toolchain::ToolType type) { |
| 140 switch (type) { | 97 switch (type) { |
| 141 case Toolchain::TYPE_CC: | 98 case Toolchain::TYPE_CC: |
| 142 return "c"; | 99 return "c"; |
| 143 case Toolchain::TYPE_CXX: | 100 case Toolchain::TYPE_CXX: |
| 144 return "cc"; | 101 return "cc"; |
| 145 case Toolchain::TYPE_OBJC: | 102 case Toolchain::TYPE_OBJC: |
| 146 return "m"; | 103 return "m"; |
| 147 case Toolchain::TYPE_OBJCXX: | 104 case Toolchain::TYPE_OBJCXX: |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 // output vector. | 206 // output vector. |
| 250 void AddSourceSetObjectFiles(const Target* source_set, | 207 void AddSourceSetObjectFiles(const Target* source_set, |
| 251 UniqueVector<OutputFile>* obj_files) { | 208 UniqueVector<OutputFile>* obj_files) { |
| 252 std::vector<OutputFile> tool_outputs; // Prevent allocation in loop. | 209 std::vector<OutputFile> tool_outputs; // Prevent allocation in loop. |
| 253 NinjaBinaryTargetWriter::SourceFileTypeSet used_types; | 210 NinjaBinaryTargetWriter::SourceFileTypeSet used_types; |
| 254 | 211 |
| 255 // Compute object files for all sources. Only link the first output from | 212 // Compute object files for all sources. Only link the first output from |
| 256 // the tool if there are more than one. | 213 // the tool if there are more than one. |
| 257 for (const auto& source : source_set->sources()) { | 214 for (const auto& source : source_set->sources()) { |
| 258 Toolchain::ToolType tool_type = Toolchain::TYPE_NONE; | 215 Toolchain::ToolType tool_type = Toolchain::TYPE_NONE; |
| 259 if (GetOutputFilesForSource(source_set, source, &tool_type, &tool_outputs)) | 216 if (source_set->GetOutputFilesForSource(source, &tool_type, &tool_outputs)) |
| 260 obj_files->push_back(tool_outputs[0]); | 217 obj_files->push_back(tool_outputs[0]); |
| 261 | 218 |
| 262 used_types.Set(GetSourceFileType(source)); | 219 used_types.Set(GetSourceFileType(source)); |
| 263 } | 220 } |
| 264 | 221 |
| 265 // Add MSVC precompiled header object files. GCC .gch files are not object | 222 // Add MSVC precompiled header object files. GCC .gch files are not object |
| 266 // files so they are omitted. | 223 // files so they are omitted. |
| 267 if (source_set->config_values().has_precompiled_headers()) { | 224 if (source_set->config_values().has_precompiled_headers()) { |
| 268 if (used_types.Get(SOURCE_C)) { | 225 if (used_types.Get(SOURCE_C)) { |
| 269 const Tool* tool = source_set->toolchain()->GetTool(Toolchain::TYPE_CC); | 226 const Tool* tool = source_set->toolchain()->GetTool(Toolchain::TYPE_CC); |
| (...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 655 std::vector<OutputFile>* object_files, | 612 std::vector<OutputFile>* object_files, |
| 656 std::vector<SourceFile>* other_files) { | 613 std::vector<SourceFile>* other_files) { |
| 657 object_files->reserve(object_files->size() + target_->sources().size()); | 614 object_files->reserve(object_files->size() + target_->sources().size()); |
| 658 | 615 |
| 659 std::vector<OutputFile> tool_outputs; // Prevent reallocation in loop. | 616 std::vector<OutputFile> tool_outputs; // Prevent reallocation in loop. |
| 660 std::vector<OutputFile> deps; | 617 std::vector<OutputFile> deps; |
| 661 for (const auto& source : target_->sources()) { | 618 for (const auto& source : target_->sources()) { |
| 662 // Clear the vector but maintain the max capacity to prevent reallocations. | 619 // Clear the vector but maintain the max capacity to prevent reallocations. |
| 663 deps.resize(0); | 620 deps.resize(0); |
| 664 Toolchain::ToolType tool_type = Toolchain::TYPE_NONE; | 621 Toolchain::ToolType tool_type = Toolchain::TYPE_NONE; |
| 665 if (!GetOutputFilesForSource(target_, source, &tool_type, &tool_outputs)) { | 622 if (!target_->GetOutputFilesForSource(source, &tool_type, &tool_outputs)) { |
| 666 if (GetSourceFileType(source) == SOURCE_DEF) | 623 if (GetSourceFileType(source) == SOURCE_DEF) |
| 667 other_files->push_back(source); | 624 other_files->push_back(source); |
| 668 continue; // No output for this source. | 625 continue; // No output for this source. |
| 669 } | 626 } |
| 670 | 627 |
| 671 if (tool_type != Toolchain::TYPE_NONE) { | 628 if (tool_type != Toolchain::TYPE_NONE) { |
| 672 // Only include PCH deps that correspond to the tool type, for instance, | 629 // Only include PCH deps that correspond to the tool type, for instance, |
| 673 // do not specify target_name.precompile.cc.o (a CXX PCH file) as a dep | 630 // do not specify target_name.precompile.cc.o (a CXX PCH file) as a dep |
| 674 // for the output of a C tool type. | 631 // for the output of a C tool type. |
| 675 // | 632 // |
| (...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1044 "\n" | 1001 "\n" |
| 1045 "In the latter case, either rename one of the files or move one of\n" | 1002 "In the latter case, either rename one of the files or move one of\n" |
| 1046 "the sources to a separate source_set to avoid them both being in\n" | 1003 "the sources to a separate source_set to avoid them both being in\n" |
| 1047 "the same target."); | 1004 "the same target."); |
| 1048 g_scheduler->FailWithError(err); | 1005 g_scheduler->FailWithError(err); |
| 1049 return false; | 1006 return false; |
| 1050 } | 1007 } |
| 1051 } | 1008 } |
| 1052 return true; | 1009 return true; |
| 1053 } | 1010 } |
| OLD | NEW |