| 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_writer.h" | 5 #include "tools/gn/ninja_writer.h" |
| 6 | 6 |
| 7 #include "tools/gn/builder.h" | 7 #include "tools/gn/builder.h" |
| 8 #include "tools/gn/loader.h" | 8 #include "tools/gn/loader.h" |
| 9 #include "tools/gn/location.h" | 9 #include "tools/gn/location.h" |
| 10 #include "tools/gn/ninja_build_writer.h" | 10 #include "tools/gn/ninja_build_writer.h" |
| 11 #include "tools/gn/ninja_toolchain_writer.h" | 11 #include "tools/gn/ninja_toolchain_writer.h" |
| 12 #include "tools/gn/settings.h" | 12 #include "tools/gn/settings.h" |
| 13 #include "tools/gn/target.h" | 13 #include "tools/gn/target.h" |
| 14 | 14 |
| 15 NinjaWriter::NinjaWriter(const BuildSettings* build_settings, | 15 NinjaWriter::NinjaWriter(const Builder& builder) |
| 16 Builder* builder) | 16 : builder_(builder) { |
| 17 : build_settings_(build_settings), | |
| 18 builder_(builder) { | |
| 19 } | 17 } |
| 20 | 18 |
| 21 NinjaWriter::~NinjaWriter() { | 19 NinjaWriter::~NinjaWriter() { |
| 22 } | 20 } |
| 23 | 21 |
| 24 // static | 22 // static |
| 25 bool NinjaWriter::RunAndWriteFiles(const BuildSettings* build_settings, | 23 bool NinjaWriter::RunAndWriteFiles( |
| 26 Builder* builder, | 24 const BuildSettings* build_settings, |
| 27 Err* err) { | 25 const Builder& builder, |
| 28 NinjaWriter writer(build_settings, builder); | 26 const PerToolchainRules& per_toolchain_rules, |
| 27 Err* err) { |
| 28 NinjaWriter writer(builder); |
| 29 | 29 |
| 30 std::vector<const Settings*> all_settings; | 30 if (!writer.WriteToolchains(per_toolchain_rules, err)) |
| 31 std::vector<const Target*> default_targets; | |
| 32 std::vector<const Pool*> all_pools; | |
| 33 if (!writer.WriteToolchains(&all_settings, &default_targets, &all_pools, err)) | |
| 34 return false; | 31 return false; |
| 35 return writer.WriteRootBuildfiles(all_settings, default_targets, all_pools, | 32 return NinjaBuildWriter::RunAndWriteFile(build_settings, builder, err); |
| 36 err); | |
| 37 } | 33 } |
| 38 | 34 |
| 39 // static | 35 bool NinjaWriter::WriteToolchains(const PerToolchainRules& per_toolchain_rules, |
| 40 bool NinjaWriter::RunAndWriteToolchainFiles( | |
| 41 const BuildSettings* build_settings, | |
| 42 Builder* builder, | |
| 43 std::vector<const Settings*>* all_settings, | |
| 44 Err* err) { | |
| 45 NinjaWriter writer(build_settings, builder); | |
| 46 std::vector<const Target*> default_targets; | |
| 47 std::vector<const Pool*> all_pools; | |
| 48 return writer.WriteToolchains(all_settings, &default_targets, &all_pools, | |
| 49 err); | |
| 50 } | |
| 51 | |
| 52 bool NinjaWriter::WriteToolchains(std::vector<const Settings*>* all_settings, | |
| 53 std::vector<const Target*>* default_targets, | |
| 54 std::vector<const Pool*>* all_pools, | |
| 55 Err* err) { | 36 Err* err) { |
| 56 // Categorize all targets by toolchain. | 37 if (per_toolchain_rules.empty()) { |
| 57 typedef std::map<Label, std::vector<const Target*> > CategorizedMap; | |
| 58 CategorizedMap categorized; | |
| 59 | |
| 60 std::vector<const BuilderRecord*> all_records = builder_->GetAllRecords(); | |
| 61 for (auto* all_record : all_records) { | |
| 62 if (all_record->type() == BuilderRecord::ITEM_TARGET && | |
| 63 all_record->should_generate()) { | |
| 64 categorized[all_record->label().GetToolchainLabel()].push_back( | |
| 65 all_record->item()->AsTarget()); | |
| 66 } | |
| 67 } | |
| 68 if (categorized.empty()) { | |
| 69 Err(Location(), "No targets.", | 38 Err(Location(), "No targets.", |
| 70 "I could not find any targets to write, so I'm doing nothing.") | 39 "I could not find any targets to write, so I'm doing nothing.") |
| 71 .PrintToStdout(); | 40 .PrintToStdout(); |
| 72 return false; | 41 return false; |
| 73 } | 42 } |
| 74 | 43 |
| 75 for (auto& i : categorized) { | 44 for (const auto& i : per_toolchain_rules) { |
| 76 // Sort targets so that they are in a deterministic order. | 45 const Toolchain* toolchain = i.first; |
| 77 std::sort(i.second.begin(), i.second.end(), | |
| 78 [](const Target* a, const Target* b) { | |
| 79 return a->label() < b->label(); | |
| 80 }); | |
| 81 } | |
| 82 | |
| 83 Label default_label = builder_->loader()->GetDefaultToolchain(); | |
| 84 | |
| 85 // Write out the toolchain buildfiles, and also accumulate the set of | |
| 86 // all settings and find the list of targets in the default toolchain. | |
| 87 UniqueVector<const Pool*> pools; | |
| 88 for (const auto& i : categorized) { | |
| 89 const Settings* settings = | 46 const Settings* settings = |
| 90 builder_->loader()->GetToolchainSettings(i.first); | 47 builder_.loader()->GetToolchainSettings(toolchain->label()); |
| 91 const Toolchain* toolchain = builder_->GetToolchain(i.first); | |
| 92 | |
| 93 all_settings->push_back(settings); | |
| 94 | |
| 95 if (!NinjaToolchainWriter::RunAndWriteFile(settings, toolchain, i.second)) { | 48 if (!NinjaToolchainWriter::RunAndWriteFile(settings, toolchain, i.second)) { |
| 96 Err(Location(), | 49 Err(Location(), |
| 97 "Couldn't open toolchain buildfile(s) for writing").PrintToStdout(); | 50 "Couldn't open toolchain buildfile(s) for writing").PrintToStdout(); |
| 98 return false; | 51 return false; |
| 99 } | 52 } |
| 100 | |
| 101 for (int j = Toolchain::TYPE_NONE + 1; j < Toolchain::TYPE_NUMTYPES; j++) { | |
| 102 Toolchain::ToolType tool_type = static_cast<Toolchain::ToolType>(j); | |
| 103 const Tool* tool = toolchain->GetTool(tool_type); | |
| 104 if (tool && tool->pool().ptr) | |
| 105 pools.push_back(tool->pool().ptr); | |
| 106 } | |
| 107 } | 53 } |
| 108 | 54 |
| 109 *all_pools = pools.vector(); | |
| 110 *default_targets = categorized[default_label]; | |
| 111 return true; | 55 return true; |
| 112 } | 56 } |
| 113 | |
| 114 bool NinjaWriter::WriteRootBuildfiles( | |
| 115 const std::vector<const Settings*>& all_settings, | |
| 116 const std::vector<const Target*>& default_targets, | |
| 117 const std::vector<const Pool*>& all_pools, | |
| 118 Err* err) { | |
| 119 // All Settings objects should have the same default toolchain, and there | |
| 120 // should always be at least one settings object in the build. | |
| 121 CHECK(!all_settings.empty()); | |
| 122 const Toolchain* default_toolchain = | |
| 123 builder_->GetToolchain(all_settings[0]->default_toolchain_label()); | |
| 124 | |
| 125 // Write the root buildfile. | |
| 126 return NinjaBuildWriter::RunAndWriteFile(build_settings_, all_settings, | |
| 127 default_toolchain, default_targets, | |
| 128 all_pools, err); | |
| 129 } | |
| OLD | NEW |