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_writer.h"
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 #include "tools/gn/settings.h"
14 NinjaWriter::NinjaWriter(const BuildSettings
* build_settings
,
16 : build_settings_(build_settings
),
20 NinjaWriter::~NinjaWriter() {
24 bool NinjaWriter::RunAndWriteFiles(const BuildSettings
* build_settings
,
27 NinjaWriter
writer(build_settings
, builder
);
29 std::vector
<const Settings
*> all_settings
;
30 std::vector
<const Target
*> default_targets
;
31 if (!writer
.WriteToolchains(&all_settings
, &default_targets
, err
))
33 return writer
.WriteRootBuildfiles(all_settings
, default_targets
, err
);
37 bool NinjaWriter::RunAndWriteToolchainFiles(
38 const BuildSettings
* build_settings
,
40 std::vector
<const Settings
*>* all_settings
,
42 NinjaWriter
writer(build_settings
, builder
);
43 std::vector
<const Target
*> default_targets
;
44 return writer
.WriteToolchains(all_settings
, &default_targets
, err
);
47 bool NinjaWriter::WriteToolchains(std::vector
<const Settings
*>* all_settings
,
48 std::vector
<const Target
*>* default_targets
,
50 // Categorize all targets by toolchain.
51 typedef std::map
<Label
, std::vector
<const Target
*> > CategorizedMap
;
52 CategorizedMap categorized
;
54 std::vector
<const BuilderRecord
*> all_records
= builder_
->GetAllRecords();
55 for (const auto& all_record
: all_records
) {
56 if (all_record
->type() == BuilderRecord::ITEM_TARGET
&&
57 all_record
->should_generate()) {
58 categorized
[all_record
->label().GetToolchainLabel()].push_back(
59 all_record
->item()->AsTarget());
62 if (categorized
.empty()) {
63 Err(Location(), "No targets.",
64 "I could not find any targets to write, so I'm doing nothing.")
69 Label default_label
= builder_
->loader()->GetDefaultToolchain();
71 // Write out the toolchain buildfiles, and also accumulate the set of
72 // all settings and find the list of targets in the default toolchain.
73 for (CategorizedMap::const_iterator i
= categorized
.begin();
74 i
!= categorized
.end(); ++i
) {
75 const Settings
* settings
=
76 builder_
->loader()->GetToolchainSettings(i
->first
);
77 const Toolchain
* toolchain
= builder_
->GetToolchain(i
->first
);
79 all_settings
->push_back(settings
);
80 if (!NinjaToolchainWriter::RunAndWriteFile(settings
, toolchain
,
83 "Couldn't open toolchain buildfile(s) for writing").PrintToStdout();
88 *default_targets
= categorized
[default_label
];
92 bool NinjaWriter::WriteRootBuildfiles(
93 const std::vector
<const Settings
*>& all_settings
,
94 const std::vector
<const Target
*>& default_targets
,
96 // All Settings objects should have the same default toolchain, and there
97 // should always be at least one settings object in the build.
98 CHECK(!all_settings
.empty());
99 const Toolchain
* default_toolchain
=
100 builder_
->GetToolchain(all_settings
[0]->default_toolchain_label());
102 // Write the root buildfile.
103 return NinjaBuildWriter::RunAndWriteFile(build_settings_
, all_settings
,
104 default_toolchain
, default_targets
,