Home | History | Annotate | Download | only in gn
      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/location.h"
      8 #include "tools/gn/ninja_build_writer.h"
      9 #include "tools/gn/ninja_toolchain_writer.h"
     10 
     11 
     12 NinjaWriter::NinjaWriter(const BuildSettings* build_settings)
     13     : build_settings_(build_settings) {
     14 }
     15 
     16 NinjaWriter::~NinjaWriter() {
     17 }
     18 
     19 // static
     20 bool NinjaWriter::RunAndWriteFiles(const BuildSettings* build_settings) {
     21   NinjaWriter writer(build_settings);
     22   return writer.WriteRootBuildfiles();
     23 }
     24 
     25 bool NinjaWriter::WriteRootBuildfiles() {
     26   // Categorize all targets by toolchain.
     27   typedef std::map<Label, std::vector<const Target*> > CategorizedMap;
     28   CategorizedMap categorized;
     29 
     30   std::vector<const Target*> all_targets;
     31   build_settings_->target_manager().GetAllTargets(&all_targets);
     32   if (all_targets.empty()) {
     33     Err(Location(), "No targets.",
     34         "I could not find any targets to write, so I'm doing nothing.")
     35         .PrintToStdout();
     36     return false;
     37   }
     38   for (size_t i = 0; i < all_targets.size(); i++) {
     39     categorized[all_targets[i]->label().GetToolchainLabel()].push_back(
     40         all_targets[i]);
     41   }
     42 
     43   Label default_label =
     44       build_settings_->toolchain_manager().GetDefaultToolchainUnlocked();
     45 
     46   // Write out the toolchain buildfiles, and also accumulate the set of
     47   // all settings and find the list of targets in the default toolchain.
     48   std::vector<const Settings*> all_settings;
     49   const std::vector<const Target*>* default_targets = NULL;
     50   for (CategorizedMap::const_iterator i = categorized.begin();
     51        i != categorized.end(); ++i) {
     52     const Settings* settings;
     53     {
     54       base::AutoLock lock(build_settings_->item_tree().lock());
     55       Err ignored;
     56       settings =
     57           build_settings_->toolchain_manager().GetSettingsForToolchainLocked(
     58               LocationRange(), i->first, &ignored);
     59     }
     60     if (i->first == default_label)
     61       default_targets = &i->second;
     62     all_settings.push_back(settings);
     63     if (!NinjaToolchainWriter::RunAndWriteFile(settings, i->second)) {
     64       Err(Location(),
     65           "Couldn't open toolchain buildfile(s) for writing").PrintToStdout();
     66       return false;
     67     }
     68   }
     69 
     70   // Write the root buildfile.
     71   if (!NinjaBuildWriter::RunAndWriteFile(build_settings_, all_settings,
     72                                          *default_targets)) {
     73     Err(Location(),
     74         "Couldn't open toolchain buildfile(s) for writing").PrintToStdout();
     75     return false;
     76   }
     77   return true;
     78 }
     79