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
,
26 NinjaWriter
writer(build_settings
, builder
);
28 std::vector
<const Settings
*> all_settings
;
29 std::vector
<const Target
*> default_targets
;
30 if (!writer
.WriteToolchains(&all_settings
, &default_targets
))
32 return writer
.WriteRootBuildfiles(all_settings
, default_targets
);
36 bool NinjaWriter::RunAndWriteToolchainFiles(
37 const BuildSettings
* build_settings
,
39 std::vector
<const Settings
*>* all_settings
) {
40 NinjaWriter
writer(build_settings
, builder
);
41 std::vector
<const Target
*> default_targets
;
42 return writer
.WriteToolchains(all_settings
, &default_targets
);
45 bool NinjaWriter::WriteToolchains(std::vector
<const Settings
*>* all_settings
,
46 std::vector
<const Target
*>* default_targets
) {
47 // Categorize all targets by toolchain.
48 typedef std::map
<Label
, std::vector
<const Target
*> > CategorizedMap
;
49 CategorizedMap categorized
;
51 std::vector
<const BuilderRecord
*> all_records
= builder_
->GetAllRecords();
52 for (size_t i
= 0; i
< all_records
.size(); i
++) {
53 if (all_records
[i
]->type() == BuilderRecord::ITEM_TARGET
&&
54 all_records
[i
]->should_generate()) {
55 categorized
[all_records
[i
]->label().GetToolchainLabel()].push_back(
56 all_records
[i
]->item()->AsTarget());
59 if (categorized
.empty()) {
60 Err(Location(), "No targets.",
61 "I could not find any targets to write, so I'm doing nothing.")
66 Label default_label
= builder_
->loader()->GetDefaultToolchain();
68 // Write out the toolchain buildfiles, and also accumulate the set of
69 // all settings and find the list of targets in the default toolchain.
70 for (CategorizedMap::const_iterator i
= categorized
.begin();
71 i
!= categorized
.end(); ++i
) {
72 const Settings
* settings
=
73 builder_
->loader()->GetToolchainSettings(i
->first
);
74 const Toolchain
* toolchain
= builder_
->GetToolchain(i
->first
);
76 all_settings
->push_back(settings
);
77 if (!NinjaToolchainWriter::RunAndWriteFile(settings
, toolchain
,
80 "Couldn't open toolchain buildfile(s) for writing").PrintToStdout();
85 *default_targets
= categorized
[default_label
];
89 bool NinjaWriter::WriteRootBuildfiles(
90 const std::vector
<const Settings
*>& all_settings
,
91 const std::vector
<const Target
*>& default_targets
) {
92 // All Settings objects should have the same default toolchain, and there
93 // should always be at least one settings object in the build.
94 CHECK(!all_settings
.empty());
95 const Toolchain
* default_toolchain
=
96 builder_
->GetToolchain(all_settings
[0]->default_toolchain_label());
98 // Write the root buildfile.
99 if (!NinjaBuildWriter::RunAndWriteFile(build_settings_
, all_settings
,
100 default_toolchain
, default_targets
)) {
102 "Couldn't open toolchain buildfile(s) for writing").PrintToStdout();