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/file_util.h"
10 #include "base/strings/stringize_macros.h"
11 #include "tools/gn/build_settings.h"
12 #include "tools/gn/item_node.h"
13 #include "tools/gn/settings.h"
14 #include "tools/gn/target.h"
15 #include "tools/gn/toolchain.h"
16 #include "tools/gn/toolchain_manager.h"
17 #include "tools/gn/trace.h"
19 NinjaToolchainWriter::NinjaToolchainWriter(
20 const Settings
* settings
,
21 const std::vector
<const Target
*>& targets
,
22 const std::set
<std::string
>& skip_files
,
24 : settings_(settings
),
26 skip_files_(skip_files
),
28 path_output_(settings_
->build_settings()->build_dir(),
30 helper_(settings
->build_settings()) {
33 NinjaToolchainWriter::~NinjaToolchainWriter() {
36 void NinjaToolchainWriter::Run() {
42 bool NinjaToolchainWriter::RunAndWriteFile(
43 const Settings
* settings
,
44 const std::vector
<const Target
*>& targets
,
45 const std::set
<std::string
>& skip_files
) {
46 NinjaHelper
helper(settings
->build_settings());
47 base::FilePath
ninja_file(settings
->build_settings()->GetFullPath(
48 helper
.GetNinjaFileForToolchain(settings
).GetSourceFile(
49 settings
->build_settings())));
50 ScopedTrace
trace(TraceItem::TRACE_FILE_WRITE
, FilePathToUTF8(ninja_file
));
52 file_util::CreateDirectory(ninja_file
.DirName());
55 file
.open(FilePathToUTF8(ninja_file
).c_str(),
56 std::ios_base::out
| std::ios_base::binary
);
60 NinjaToolchainWriter
gen(settings
, targets
, skip_files
, file
);
65 void NinjaToolchainWriter::WriteRules() {
66 const Toolchain
* tc
= settings_
->build_settings()->toolchain_manager()
67 .GetToolchainDefinitionUnlocked(settings_
->toolchain_label());
70 std::string
indent(" ");
72 NinjaHelper
helper(settings_
->build_settings());
73 std::string rule_prefix
= helper
.GetRulePrefix(settings_
);
75 for (int i
= Toolchain::TYPE_NONE
+ 1; i
< Toolchain::TYPE_NUMTYPES
; i
++) {
76 Toolchain::ToolType tool_type
= static_cast<Toolchain::ToolType
>(i
);
77 const Toolchain::Tool
& tool
= tc
->GetTool(tool_type
);
78 if (tool
.command
.empty())
81 out_
<< "rule " << rule_prefix
<< Toolchain::ToolTypeToName(tool_type
)
84 #define WRITE_ARG(name) \
85 if (!tool.name.empty()) \
86 out_ << indent << " " STRINGIZE(name) " = " << tool.name << std::endl;
90 WRITE_ARG(description
);
94 WRITE_ARG(rspfile_content
);
100 void NinjaToolchainWriter::WriteSubninjas() {
101 // Write subninja commands for each generated target.
102 for (size_t i
= 0; i
< targets_
.size(); i
++) {
103 if (!targets_
[i
]->item_node()->should_generate() ||
104 (targets_
[i
]->settings()->build_settings()->using_external_generator()
105 && targets_
[i
]->external()))
108 OutputFile ninja_file
= helper_
.GetNinjaFileForTarget(targets_
[i
]);
109 if (skip_files_
.find(ninja_file
.value()) != skip_files_
.end())
113 path_output_
.WriteFile(out_
, ninja_file
);