| 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_toolchain_writer.h" | 5 #include "tools/gn/ninja_toolchain_writer.h" |
| 6 | 6 |
| 7 #include <fstream> | 7 #include <fstream> |
| 8 | 8 |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/strings/stringize_macros.h" | 10 #include "base/strings/stringize_macros.h" |
| 11 #include "tools/gn/build_settings.h" | 11 #include "tools/gn/build_settings.h" |
| 12 #include "tools/gn/filesystem_utils.h" | 12 #include "tools/gn/filesystem_utils.h" |
| 13 #include "tools/gn/ninja_utils.h" | 13 #include "tools/gn/ninja_utils.h" |
| 14 #include "tools/gn/pool.h" | 14 #include "tools/gn/pool.h" |
| 15 #include "tools/gn/settings.h" | 15 #include "tools/gn/settings.h" |
| 16 #include "tools/gn/substitution_writer.h" | 16 #include "tools/gn/substitution_writer.h" |
| 17 #include "tools/gn/target.h" | 17 #include "tools/gn/target.h" |
| 18 #include "tools/gn/toolchain.h" | 18 #include "tools/gn/toolchain.h" |
| 19 #include "tools/gn/trace.h" | 19 #include "tools/gn/trace.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 const char kIndent[] = " "; | 23 const char kIndent[] = " "; |
| 24 | 24 |
| 25 } // namespace | 25 } // namespace |
| 26 | 26 |
| 27 NinjaToolchainWriter::NinjaToolchainWriter( | 27 NinjaToolchainWriter::NinjaToolchainWriter( |
| 28 const Settings* settings, | 28 const Settings* settings, |
| 29 const Toolchain* toolchain, | 29 const Toolchain* toolchain, |
| 30 const std::vector<const Target*>& targets, | |
| 31 std::ostream& out) | 30 std::ostream& out) |
| 32 : settings_(settings), | 31 : settings_(settings), |
| 33 toolchain_(toolchain), | 32 toolchain_(toolchain), |
| 34 targets_(targets), | |
| 35 out_(out), | 33 out_(out), |
| 36 path_output_(settings_->build_settings()->build_dir(), | 34 path_output_(settings_->build_settings()->build_dir(), |
| 37 settings_->build_settings()->root_path_utf8(), | 35 settings_->build_settings()->root_path_utf8(), |
| 38 ESCAPE_NINJA) { | 36 ESCAPE_NINJA) { |
| 39 } | 37 } |
| 40 | 38 |
| 41 NinjaToolchainWriter::~NinjaToolchainWriter() { | 39 NinjaToolchainWriter::~NinjaToolchainWriter() { |
| 42 } | 40 } |
| 43 | 41 |
| 44 void NinjaToolchainWriter::Run() { | 42 void NinjaToolchainWriter::Run( |
| 45 WriteRules(); | 43 const std::vector<NinjaWriter::TargetRulePair>& rules) { |
| 46 WriteSubninjas(); | 44 std::string rule_prefix = GetNinjaRulePrefixForToolchain(settings_); |
| 45 |
| 46 for (int i = Toolchain::TYPE_NONE + 1; i < Toolchain::TYPE_NUMTYPES; i++) { |
| 47 Toolchain::ToolType tool_type = static_cast<Toolchain::ToolType>(i); |
| 48 const Tool* tool = toolchain_->GetTool(tool_type); |
| 49 if (tool) |
| 50 WriteToolRule(tool_type, tool, rule_prefix); |
| 51 } |
| 52 out_ << std::endl; |
| 53 |
| 54 for (const auto& pair : rules) |
| 55 out_ << pair.second; |
| 47 } | 56 } |
| 48 | 57 |
| 49 // static | 58 // static |
| 50 bool NinjaToolchainWriter::RunAndWriteFile( | 59 bool NinjaToolchainWriter::RunAndWriteFile( |
| 51 const Settings* settings, | 60 const Settings* settings, |
| 52 const Toolchain* toolchain, | 61 const Toolchain* toolchain, |
| 53 const std::vector<const Target*>& targets) { | 62 const std::vector<NinjaWriter::TargetRulePair>& rules) { |
| 54 base::FilePath ninja_file(settings->build_settings()->GetFullPath( | 63 base::FilePath ninja_file(settings->build_settings()->GetFullPath( |
| 55 GetNinjaFileForToolchain(settings))); | 64 GetNinjaFileForToolchain(settings))); |
| 56 ScopedTrace trace(TraceItem::TRACE_FILE_WRITE, FilePathToUTF8(ninja_file)); | 65 ScopedTrace trace(TraceItem::TRACE_FILE_WRITE, FilePathToUTF8(ninja_file)); |
| 57 | 66 |
| 58 base::CreateDirectory(ninja_file.DirName()); | 67 base::CreateDirectory(ninja_file.DirName()); |
| 59 | 68 |
| 60 std::ofstream file; | 69 std::ofstream file; |
| 61 file.open(FilePathToUTF8(ninja_file).c_str(), | 70 file.open(FilePathToUTF8(ninja_file).c_str(), |
| 62 std::ios_base::out | std::ios_base::binary); | 71 std::ios_base::out | std::ios_base::binary); |
| 63 if (file.fail()) | 72 if (file.fail()) |
| 64 return false; | 73 return false; |
| 65 | 74 |
| 66 NinjaToolchainWriter gen(settings, toolchain, targets, file); | 75 NinjaToolchainWriter gen(settings, toolchain, file); |
| 67 gen.Run(); | 76 gen.Run(rules); |
| 68 return true; | 77 return true; |
| 69 } | 78 } |
| 70 | 79 |
| 71 void NinjaToolchainWriter::WriteRules() { | |
| 72 std::string rule_prefix = GetNinjaRulePrefixForToolchain(settings_); | |
| 73 | |
| 74 for (int i = Toolchain::TYPE_NONE + 1; i < Toolchain::TYPE_NUMTYPES; i++) { | |
| 75 Toolchain::ToolType tool_type = static_cast<Toolchain::ToolType>(i); | |
| 76 const Tool* tool = toolchain_->GetTool(tool_type); | |
| 77 if (tool) | |
| 78 WriteToolRule(tool_type, tool, rule_prefix); | |
| 79 } | |
| 80 out_ << std::endl; | |
| 81 } | |
| 82 | |
| 83 void NinjaToolchainWriter::WriteToolRule(const Toolchain::ToolType type, | 80 void NinjaToolchainWriter::WriteToolRule(const Toolchain::ToolType type, |
| 84 const Tool* tool, | 81 const Tool* tool, |
| 85 const std::string& rule_prefix) { | 82 const std::string& rule_prefix) { |
| 86 out_ << "rule " << rule_prefix << Toolchain::ToolTypeToName(type) | 83 out_ << "rule " << rule_prefix << Toolchain::ToolTypeToName(type) |
| 87 << std::endl; | 84 << std::endl; |
| 88 | 85 |
| 89 // Rules explicitly include shell commands, so don't try to escape. | 86 // Rules explicitly include shell commands, so don't try to escape. |
| 90 EscapeOptions options; | 87 EscapeOptions options; |
| 91 options.mode = ESCAPE_NINJA_PREFORMATTED_COMMAND; | 88 options.mode = ESCAPE_NINJA_PREFORMATTED_COMMAND; |
| 92 | 89 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 | 123 |
| 127 void NinjaToolchainWriter::WriteRulePattern(const char* name, | 124 void NinjaToolchainWriter::WriteRulePattern(const char* name, |
| 128 const SubstitutionPattern& pattern, | 125 const SubstitutionPattern& pattern, |
| 129 const EscapeOptions& options) { | 126 const EscapeOptions& options) { |
| 130 if (pattern.empty()) | 127 if (pattern.empty()) |
| 131 return; | 128 return; |
| 132 out_ << kIndent << name << " = "; | 129 out_ << kIndent << name << " = "; |
| 133 SubstitutionWriter::WriteWithNinjaVariables(pattern, options, out_); | 130 SubstitutionWriter::WriteWithNinjaVariables(pattern, options, out_); |
| 134 out_ << std::endl; | 131 out_ << std::endl; |
| 135 } | 132 } |
| 136 | |
| 137 void NinjaToolchainWriter::WriteSubninjas() { | |
| 138 // Write subninja commands for each generated target. | |
| 139 for (auto* target : targets_) { | |
| 140 OutputFile ninja_file(target->settings()->build_settings(), | |
| 141 GetNinjaFileForTarget(target)); | |
| 142 out_ << "subninja "; | |
| 143 path_output_.WriteFile(out_, ninja_file); | |
| 144 out_ << std::endl; | |
| 145 } | |
| 146 out_ << std::endl; | |
| 147 } | |
| OLD | NEW |