Chromium Code Reviews| 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 | 14 |
| 14 NinjaWriter::NinjaWriter(const BuildSettings* build_settings, | 15 NinjaWriter::NinjaWriter(const BuildSettings* build_settings, |
| 15 Builder* builder) | 16 Builder* builder) |
| 16 : build_settings_(build_settings), | 17 : build_settings_(build_settings), |
| 17 builder_(builder) { | 18 builder_(builder) { |
| 18 } | 19 } |
| 19 | 20 |
| 20 NinjaWriter::~NinjaWriter() { | 21 NinjaWriter::~NinjaWriter() { |
| 21 } | 22 } |
| 22 | 23 |
| 23 // static | 24 // static |
| 24 bool NinjaWriter::RunAndWriteFiles(const BuildSettings* build_settings, | 25 bool NinjaWriter::RunAndWriteFiles(const BuildSettings* build_settings, |
| 25 Builder* builder, | 26 Builder* builder, |
| 26 Err* err) { | 27 Err* err) { |
| 27 NinjaWriter writer(build_settings, builder); | 28 NinjaWriter writer(build_settings, builder); |
| 28 | 29 |
| 29 std::vector<const Settings*> all_settings; | 30 std::vector<const Settings*> all_settings; |
| 30 std::vector<const Target*> default_targets; | 31 std::vector<const Target*> default_targets; |
| 31 if (!writer.WriteToolchains(&all_settings, &default_targets, err)) | 32 if (!writer.WriteToolchains(&all_settings, &default_targets, err)) |
| 32 return false; | 33 return false; |
| 34 | |
| 35 // Sort targets so that they are in a deterministic order. | |
|
M-A Ruel
2015/12/07 15:09:50
I'd prefer WriteToolchains() to return a sorted li
Zachary Forman
2015/12/08 09:40:43
This is a good idea. It'll improve speed (as the w
| |
| 36 std::sort( | |
| 37 default_targets.begin(), default_targets.end(), | |
| 38 [](const Target* a, const Target* b) { return a->label() < b->label(); }); | |
| 39 | |
| 33 return writer.WriteRootBuildfiles(all_settings, default_targets, err); | 40 return writer.WriteRootBuildfiles(all_settings, default_targets, err); |
| 34 } | 41 } |
| 35 | 42 |
| 36 // static | 43 // static |
| 37 bool NinjaWriter::RunAndWriteToolchainFiles( | 44 bool NinjaWriter::RunAndWriteToolchainFiles( |
| 38 const BuildSettings* build_settings, | 45 const BuildSettings* build_settings, |
| 39 Builder* builder, | 46 Builder* builder, |
| 40 std::vector<const Settings*>* all_settings, | 47 std::vector<const Settings*>* all_settings, |
| 41 Err* err) { | 48 Err* err) { |
| 42 NinjaWriter writer(build_settings, builder); | 49 NinjaWriter writer(build_settings, builder); |
| 43 std::vector<const Target*> default_targets; | 50 std::vector<const Target*> default_targets; |
| 44 return writer.WriteToolchains(all_settings, &default_targets, err); | 51 return writer.WriteToolchains(all_settings, &default_targets, err); |
| 45 } | 52 } |
| 46 | 53 |
| 47 bool NinjaWriter::WriteToolchains(std::vector<const Settings*>* all_settings, | 54 bool NinjaWriter::WriteToolchains(std::vector<const Settings*>* all_settings, |
| 48 std::vector<const Target*>* default_targets, | 55 std::vector<const Target*>* default_targets, |
| 49 Err* err) { | 56 Err* err) { |
| 50 // Categorize all targets by toolchain. | 57 // Categorize all targets by toolchain. |
| 51 typedef std::map<Label, std::vector<const Target*> > CategorizedMap; | 58 typedef std::map<Label, std::vector<const Target*> > CategorizedMap; |
|
M-A Ruel
2015/12/07 15:09:50
You could use a set<> for Target* here, so they wo
Zachary Forman
2015/12/08 09:40:42
A std::set<Target*> would be sorted by pointer ord
M-A Ruel
2015/12/08 13:49:29
I had meant with a std:less<> that short based on
| |
| 52 CategorizedMap categorized; | 59 CategorizedMap categorized; |
| 53 | 60 |
| 54 std::vector<const BuilderRecord*> all_records = builder_->GetAllRecords(); | 61 std::vector<const BuilderRecord*> all_records = builder_->GetAllRecords(); |
| 55 for (const auto& all_record : all_records) { | 62 for (const auto& all_record : all_records) { |
| 56 if (all_record->type() == BuilderRecord::ITEM_TARGET && | 63 if (all_record->type() == BuilderRecord::ITEM_TARGET && |
| 57 all_record->should_generate()) { | 64 all_record->should_generate()) { |
| 58 categorized[all_record->label().GetToolchainLabel()].push_back( | 65 categorized[all_record->label().GetToolchainLabel()].push_back( |
| 59 all_record->item()->AsTarget()); | 66 all_record->item()->AsTarget()); |
| 60 } | 67 } |
| 61 } | 68 } |
| 62 if (categorized.empty()) { | 69 if (categorized.empty()) { |
| 63 Err(Location(), "No targets.", | 70 Err(Location(), "No targets.", |
| 64 "I could not find any targets to write, so I'm doing nothing.") | 71 "I could not find any targets to write, so I'm doing nothing.") |
| 65 .PrintToStdout(); | 72 .PrintToStdout(); |
| 66 return false; | 73 return false; |
| 67 } | 74 } |
| 68 | 75 |
| 69 Label default_label = builder_->loader()->GetDefaultToolchain(); | 76 Label default_label = builder_->loader()->GetDefaultToolchain(); |
| 70 | 77 |
| 71 // Write out the toolchain buildfiles, and also accumulate the set of | 78 // Write out the toolchain buildfiles, and also accumulate the set of |
| 72 // all settings and find the list of targets in the default toolchain. | 79 // all settings and find the list of targets in the default toolchain. |
| 73 for (CategorizedMap::const_iterator i = categorized.begin(); | 80 for (CategorizedMap::iterator i = categorized.begin(); i != categorized.end(); |
|
M-A Ruel
2015/12/07 15:09:50
If you use a set above, you can switch to an enume
Zachary Forman
2015/12/08 09:40:42
Have done.
| |
| 74 i != categorized.end(); ++i) { | 81 ++i) { |
| 75 const Settings* settings = | 82 const Settings* settings = |
| 76 builder_->loader()->GetToolchainSettings(i->first); | 83 builder_->loader()->GetToolchainSettings(i->first); |
| 77 const Toolchain* toolchain = builder_->GetToolchain(i->first); | 84 const Toolchain* toolchain = builder_->GetToolchain(i->first); |
| 78 | 85 |
| 79 all_settings->push_back(settings); | 86 all_settings->push_back(settings); |
| 87 | |
| 80 if (!NinjaToolchainWriter::RunAndWriteFile(settings, toolchain, | 88 if (!NinjaToolchainWriter::RunAndWriteFile(settings, toolchain, |
| 81 i->second)) { | 89 i->second)) { |
| 82 Err(Location(), | 90 Err(Location(), |
| 83 "Couldn't open toolchain buildfile(s) for writing").PrintToStdout(); | 91 "Couldn't open toolchain buildfile(s) for writing").PrintToStdout(); |
| 84 return false; | 92 return false; |
| 85 } | 93 } |
| 86 } | 94 } |
| 87 | 95 |
| 88 *default_targets = categorized[default_label]; | 96 *default_targets = categorized[default_label]; |
| 89 return true; | 97 return true; |
| 90 } | 98 } |
| 91 | 99 |
| 92 bool NinjaWriter::WriteRootBuildfiles( | 100 bool NinjaWriter::WriteRootBuildfiles( |
| 93 const std::vector<const Settings*>& all_settings, | 101 const std::vector<const Settings*>& all_settings, |
| 94 const std::vector<const Target*>& default_targets, | 102 const std::vector<const Target*>& default_targets, |
| 95 Err* err) { | 103 Err* err) { |
| 96 // All Settings objects should have the same default toolchain, and there | 104 // All Settings objects should have the same default toolchain, and there |
| 97 // should always be at least one settings object in the build. | 105 // should always be at least one settings object in the build. |
| 98 CHECK(!all_settings.empty()); | 106 CHECK(!all_settings.empty()); |
| 99 const Toolchain* default_toolchain = | 107 const Toolchain* default_toolchain = |
| 100 builder_->GetToolchain(all_settings[0]->default_toolchain_label()); | 108 builder_->GetToolchain(all_settings[0]->default_toolchain_label()); |
| 101 | 109 |
| 102 // Write the root buildfile. | 110 // Write the root buildfile. |
| 103 return NinjaBuildWriter::RunAndWriteFile(build_settings_, all_settings, | 111 return NinjaBuildWriter::RunAndWriteFile(build_settings_, all_settings, |
| 104 default_toolchain, default_targets, | 112 default_toolchain, default_targets, |
| 105 err); | 113 err); |
| 106 } | 114 } |
| OLD | NEW |