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"
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"
22 const char kIndent
[] = " ";
26 NinjaToolchainWriter::NinjaToolchainWriter(
27 const Settings
* settings
,
28 const Toolchain
* toolchain
,
29 const std::vector
<const Target
*>& targets
,
31 : settings_(settings
),
32 toolchain_(toolchain
),
35 path_output_(settings_
->build_settings()->build_dir(),
36 settings_
->build_settings()->root_path_utf8(),
40 NinjaToolchainWriter::~NinjaToolchainWriter() {
43 void NinjaToolchainWriter::Run() {
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());
60 file
.open(FilePathToUTF8(ninja_file
).c_str(),
61 std::ios_base::out
| std::ios_base::binary
);
65 NinjaToolchainWriter
gen(settings
, toolchain
, targets
, file
);
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
);
77 WriteToolRule(tool_type
, tool
, rule_prefix
);
82 void NinjaToolchainWriter::WriteToolRule(const Toolchain::ToolType type
,
84 const std::string
& rule_prefix
) {
85 out_
<< "rule " << rule_prefix
<< Toolchain::ToolTypeToName(type
)
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";
116 out_
<< kIndent
<< "restat = 1" << std::endl
;
119 void NinjaToolchainWriter::WriteRulePattern(const char* name
,
120 const SubstitutionPattern
& pattern
,
121 const EscapeOptions
& options
) {
124 out_
<< kIndent
<< name
<< " = ";
125 SubstitutionWriter::WriteWithNinjaVariables(pattern
, options
, out_
);
129 void NinjaToolchainWriter::WriteSubninjas() {
130 // Write subninja commands for each generated target.
131 for (size_t i
= 0; i
< targets_
.size(); i
++) {
132 OutputFile
ninja_file(targets_
[i
]->settings()->build_settings(),
133 GetNinjaFileForTarget(targets_
[i
]));
135 path_output_
.WriteFile(out_
, ninja_file
);