Correct blacklist entry message
[chromium-blink-merge.git] / tools / gn / ninja_toolchain_writer.cc
blob5f33e69810c6b626a1fc317e46257a0b594a9473
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/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,
23 std::ostream& out)
24 : settings_(settings),
25 targets_(targets),
26 skip_files_(skip_files),
27 out_(out),
28 path_output_(settings_->build_settings()->build_dir(),
29 ESCAPE_NINJA, true),
30 helper_(settings->build_settings()) {
33 NinjaToolchainWriter::~NinjaToolchainWriter() {
36 void NinjaToolchainWriter::Run() {
37 WriteRules();
38 WriteSubninjas();
41 // static
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());
54 std::ofstream file;
55 file.open(FilePathToUTF8(ninja_file).c_str(),
56 std::ios_base::out | std::ios_base::binary);
57 if (file.fail())
58 return false;
60 NinjaToolchainWriter gen(settings, targets, skip_files, file);
61 gen.Run();
62 return true;
65 void NinjaToolchainWriter::WriteRules() {
66 const Toolchain* tc = settings_->build_settings()->toolchain_manager()
67 .GetToolchainDefinitionUnlocked(settings_->toolchain_label());
68 CHECK(tc);
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())
79 continue;
81 out_ << "rule " << rule_prefix << Toolchain::ToolTypeToName(tool_type)
82 << std::endl;
84 #define WRITE_ARG(name) \
85 if (!tool.name.empty()) \
86 out_ << indent << " " STRINGIZE(name) " = " << tool.name << std::endl;
87 WRITE_ARG(command);
88 WRITE_ARG(depfile);
89 WRITE_ARG(deps);
90 WRITE_ARG(description);
91 WRITE_ARG(pool);
92 WRITE_ARG(restat);
93 WRITE_ARG(rspfile);
94 WRITE_ARG(rspfile_content);
95 #undef WRITE_ARG
97 out_ << std::endl;
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()))
106 continue;
108 OutputFile ninja_file = helper_.GetNinjaFileForTarget(targets_[i]);
109 if (skip_files_.find(ninja_file.value()) != skip_files_.end())
110 continue;
112 out_ << "subninja ";
113 path_output_.WriteFile(out_, ninja_file);
114 out_ << std::endl;
116 out_ << std::endl;