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. 4 5 #include "tools/gn/ninja_writer.h" 6 7 #include "tools/gn/builder.h" 8 #include "tools/gn/loader.h" 9 #include "tools/gn/location.h" 10 #include "tools/gn/ninja_build_writer.h" 11 #include "tools/gn/ninja_toolchain_writer.h" 12 13 NinjaWriter::NinjaWriter(const BuildSettings* build_settings, 14 Builder* builder) 15 : build_settings_(build_settings), 16 builder_(builder) { 17 } 18 19 NinjaWriter::~NinjaWriter() { 20 } 21 22 // static 23 bool NinjaWriter::RunAndWriteFiles(const BuildSettings* build_settings, 24 Builder* builder) { 25 NinjaWriter writer(build_settings, builder); 26 27 std::vector<const Settings*> all_settings; 28 std::vector<const Target*> default_targets; 29 if (!writer.WriteToolchains(&all_settings, &default_targets)) 30 return false; 31 return writer.WriteRootBuildfiles(all_settings, default_targets); 32 } 33 34 // static 35 bool NinjaWriter::RunAndWriteToolchainFiles( 36 const BuildSettings* build_settings, 37 Builder* builder, 38 std::vector<const Settings*>* all_settings) { 39 NinjaWriter writer(build_settings, builder); 40 std::vector<const Target*> default_targets; 41 return writer.WriteToolchains(all_settings, &default_targets); 42 } 43 44 bool NinjaWriter::WriteToolchains(std::vector<const Settings*>* all_settings, 45 std::vector<const Target*>* default_targets) { 46 // Categorize all targets by toolchain. 47 typedef std::map<Label, std::vector<const Target*> > CategorizedMap; 48 CategorizedMap categorized; 49 50 std::vector<const BuilderRecord*> all_records = builder_->GetAllRecords(); 51 for (size_t i = 0; i < all_records.size(); i++) { 52 if (all_records[i]->type() == BuilderRecord::ITEM_TARGET && 53 all_records[i]->should_generate()) { 54 categorized[all_records[i]->label().GetToolchainLabel()].push_back( 55 all_records[i]->item()->AsTarget()); 56 } 57 } 58 if (categorized.empty()) { 59 Err(Location(), "No targets.", 60 "I could not find any targets to write, so I'm doing nothing.") 61 .PrintToStdout(); 62 return false; 63 } 64 65 Label default_label = builder_->loader()->GetDefaultToolchain(); 66 67 // Write out the toolchain buildfiles, and also accumulate the set of 68 // all settings and find the list of targets in the default toolchain. 69 for (CategorizedMap::const_iterator i = categorized.begin(); 70 i != categorized.end(); ++i) { 71 const Settings* settings = 72 builder_->loader()->GetToolchainSettings(i->first); 73 const Toolchain* toolchain = builder_->GetToolchain(i->first); 74 75 all_settings->push_back(settings); 76 if (!NinjaToolchainWriter::RunAndWriteFile(settings, toolchain, 77 i->second)) { 78 Err(Location(), 79 "Couldn't open toolchain buildfile(s) for writing").PrintToStdout(); 80 return false; 81 } 82 } 83 84 *default_targets = categorized[default_label]; 85 return true; 86 } 87 88 bool NinjaWriter::WriteRootBuildfiles( 89 const std::vector<const Settings*>& all_settings, 90 const std::vector<const Target*>& default_targets) { 91 // Write the root buildfile. 92 if (!NinjaBuildWriter::RunAndWriteFile(build_settings_, all_settings, 93 default_targets)) { 94 Err(Location(), 95 "Couldn't open toolchain buildfile(s) for writing").PrintToStdout(); 96 return false; 97 } 98 return true; 99 } 100