Add ICU message format support
[chromium-blink-merge.git] / tools / gn / ninja_toolchain_writer.cc
blobc22fb2c43527d04cd92d2d71138df06441941be3
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_toolchain_writer.h"
7 #include <fstream>
9 #include "base/files/file_util.h"
10 #include "base/strings/stringize_macros.h"
11 #include "tools/gn/build_settings.h"
12 #include "tools/gn/filesystem_utils.h"
13 #include "tools/gn/ninja_utils.h"
14 #include "tools/gn/settings.h"
15 #include "tools/gn/substitution_writer.h"
16 #include "tools/gn/target.h"
17 #include "tools/gn/toolchain.h"
18 #include "tools/gn/trace.h"
20 namespace {
22 const char kIndent[] = " ";
24 } // namespace
26 NinjaToolchainWriter::NinjaToolchainWriter(
27 const Settings* settings,
28 const Toolchain* toolchain,
29 const std::vector<const Target*>& targets,
30 std::ostream& out)
31 : settings_(settings),
32 toolchain_(toolchain),
33 targets_(targets),
34 out_(out),
35 path_output_(settings_->build_settings()->build_dir(),
36 settings_->build_settings()->root_path_utf8(),
37 ESCAPE_NINJA) {
40 NinjaToolchainWriter::~NinjaToolchainWriter() {
43 void NinjaToolchainWriter::Run() {
44 WriteRules();
45 WriteSubninjas();
48 // static
49 bool NinjaToolchainWriter::RunAndWriteFile(
50 const Settings* settings,
51 const Toolchain* toolchain,
52 const std::vector<const Target*>& targets) {
53 base::FilePath ninja_file(settings->build_settings()->GetFullPath(
54 GetNinjaFileForToolchain(settings)));
55 ScopedTrace trace(TraceItem::TRACE_FILE_WRITE, FilePathToUTF8(ninja_file));
57 base::CreateDirectory(ninja_file.DirName());
59 std::ofstream file;
60 file.open(FilePathToUTF8(ninja_file).c_str(),
61 std::ios_base::out | std::ios_base::binary);
62 if (file.fail())
63 return false;
65 NinjaToolchainWriter gen(settings, toolchain, targets, file);
66 gen.Run();
67 return true;
70 void NinjaToolchainWriter::WriteRules() {
71 std::string rule_prefix = GetNinjaRulePrefixForToolchain(settings_);
73 for (int i = Toolchain::TYPE_NONE + 1; i < Toolchain::TYPE_NUMTYPES; i++) {
74 Toolchain::ToolType tool_type = static_cast<Toolchain::ToolType>(i);
75 const Tool* tool = toolchain_->GetTool(tool_type);
76 if (tool)
77 WriteToolRule(tool_type, tool, rule_prefix);
79 out_ << std::endl;
82 void NinjaToolchainWriter::WriteToolRule(const Toolchain::ToolType type,
83 const Tool* tool,
84 const std::string& rule_prefix) {
85 out_ << "rule " << rule_prefix << Toolchain::ToolTypeToName(type)
86 << std::endl;
88 // Rules explicitly include shell commands, so don't try to escape.
89 EscapeOptions options;
90 options.mode = ESCAPE_NINJA_PREFORMATTED_COMMAND;
92 CHECK(!tool->command().empty()) << "Command should not be empty";
93 WriteRulePattern("command", tool->command(), options);
95 WriteRulePattern("description", tool->description(), options);
96 WriteRulePattern("rspfile", tool->rspfile(), options);
97 WriteRulePattern("rspfile_content", tool->rspfile_content(), options);
99 if (tool->depsformat() == Tool::DEPS_GCC) {
100 // GCC-style deps require a depfile.
101 if (!tool->depfile().empty()) {
102 WriteRulePattern("depfile", tool->depfile(), options);
103 out_ << kIndent << "deps = gcc" << std::endl;
105 } else if (tool->depsformat() == Tool::DEPS_MSVC) {
106 // MSVC deps don't have a depfile.
107 out_ << kIndent << "deps = msvc" << std::endl;
110 // The link pool applies to linker tools. Don't count TYPE_ALINK since
111 // static libraries are not generally intensive to write.
112 if (type == Toolchain::TYPE_SOLINK || type == Toolchain::TYPE_LINK)
113 out_ << kIndent << "pool = link_pool\n";
115 if (tool->restat())
116 out_ << kIndent << "restat = 1" << std::endl;
119 void NinjaToolchainWriter::WriteRulePattern(const char* name,
120 const SubstitutionPattern& pattern,
121 const EscapeOptions& options) {
122 if (pattern.empty())
123 return;
124 out_ << kIndent << name << " = ";
125 SubstitutionWriter::WriteWithNinjaVariables(pattern, options, out_);
126 out_ << std::endl;
129 void NinjaToolchainWriter::WriteSubninjas() {
130 // Write subninja commands for each generated target.
131 for (const auto& target : targets_) {
132 OutputFile ninja_file(target->settings()->build_settings(),
133 GetNinjaFileForTarget(target));
134 out_ << "subninja ";
135 path_output_.WriteFile(out_, ninja_file);
136 out_ << std::endl;
138 out_ << std::endl;