1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "tools/gn/ninja_build_writer.h"
#include <fstream>
#include <map>
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/process/process_handle.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "tools/gn/build_settings.h"
#include "tools/gn/err.h"
#include "tools/gn/escape.h"
#include "tools/gn/filesystem_utils.h"
#include "tools/gn/input_file_manager.h"
#include "tools/gn/ninja_utils.h"
#include "tools/gn/scheduler.h"
#include "tools/gn/target.h"
#include "tools/gn/trace.h"
#if defined(OS_WIN)
#include <windows.h>
#endif
namespace {
std::string GetSelfInvocationCommand(const BuildSettings* build_settings) {
base::FilePath executable;
PathService::Get(base::FILE_EXE, &executable);
CommandLine cmdline(executable.NormalizePathSeparatorsTo('/'));
cmdline.AppendArg("gen");
cmdline.AppendArg(build_settings->build_dir().value());
cmdline.AppendSwitchPath("--root", build_settings->root_path());
cmdline.AppendSwitch("-q"); // Don't write output.
EscapeOptions escape_shell;
escape_shell.mode = ESCAPE_NINJA_COMMAND;
#if defined(OS_WIN)
// The command line code quoting varies by platform. We have one string,
// possibly with spaces, that we want to quote. The Windows command line
// quotes again, so we don't want quoting. The Posix one doesn't.
escape_shell.inhibit_quoting = true;
#endif
const CommandLine& our_cmdline = *CommandLine::ForCurrentProcess();
const CommandLine::SwitchMap& switches = our_cmdline.GetSwitches();
for (CommandLine::SwitchMap::const_iterator i = switches.begin();
i != switches.end(); ++i) {
// Only write arguments we haven't already written. Always skip "args"
// since those will have been written to the file and will be used
// implicitly in the future. Keeping --args would mean changes to the file
// would be ignored.
if (i->first != "q" && i->first != "root" && i->first != "args") {
std::string escaped_value =
EscapeString(FilePathToUTF8(i->second), escape_shell, NULL);
cmdline.AppendSwitchASCII(i->first, escaped_value);
}
}
#if defined(OS_WIN)
return base::WideToUTF8(cmdline.GetCommandLineString());
#else
return cmdline.GetCommandLineString();
#endif
}
} // namespace
NinjaBuildWriter::NinjaBuildWriter(
const BuildSettings* build_settings,
const std::vector<const Settings*>& all_settings,
const Toolchain* default_toolchain,
const std::vector<const Target*>& default_toolchain_targets,
std::ostream& out,
std::ostream& dep_out)
: build_settings_(build_settings),
all_settings_(all_settings),
default_toolchain_(default_toolchain),
default_toolchain_targets_(default_toolchain_targets),
out_(out),
dep_out_(dep_out),
path_output_(build_settings->build_dir(), ESCAPE_NINJA) {
}
NinjaBuildWriter::~NinjaBuildWriter() {
}
bool NinjaBuildWriter::Run(Err* err) {
WriteNinjaRules();
WriteLinkPool();
WriteSubninjas();
return WritePhonyAndAllRules(err);
}
// static
bool NinjaBuildWriter::RunAndWriteFile(
const BuildSettings* build_settings,
const std::vector<const Settings*>& all_settings,
const Toolchain* default_toolchain,
const std::vector<const Target*>& default_toolchain_targets,
Err* err) {
ScopedTrace trace(TraceItem::TRACE_FILE_WRITE, "build.ninja");
base::FilePath ninja_file(build_settings->GetFullPath(
SourceFile(build_settings->build_dir().value() + "build.ninja")));
base::CreateDirectory(ninja_file.DirName());
std::ofstream file;
file.open(FilePathToUTF8(ninja_file).c_str(),
std::ios_base::out | std::ios_base::binary);
if (file.fail()) {
*err = Err(Location(), "Couldn't open build.ninja for writing");
return false;
}
std::ofstream depfile;
depfile.open((FilePathToUTF8(ninja_file) + ".d").c_str(),
std::ios_base::out | std::ios_base::binary);
if (depfile.fail()) {
*err = Err(Location(), "Couldn't open depfile for writing");
return false;
}
NinjaBuildWriter gen(build_settings, all_settings, default_toolchain,
default_toolchain_targets, file, depfile);
return gen.Run(err);
}
void NinjaBuildWriter::WriteNinjaRules() {
out_ << "rule gn\n";
out_ << " command = " << GetSelfInvocationCommand(build_settings_) << "\n";
out_ << " description = Regenerating ninja files\n\n";
// This rule will regenerate the ninja files when any input file has changed.
out_ << "build build.ninja: gn\n"
<< " generator = 1\n"
<< " depfile = build.ninja.d\n";
// Input build files. These go in the ".d" file. If we write them as
// dependencies in the .ninja file itself, ninja will expect the files to
// exist and will error if they don't. When files are listed in a depfile,
// missing files are ignored.
dep_out_ << "build.ninja:";
std::vector<base::FilePath> input_files;
g_scheduler->input_file_manager()->GetAllPhysicalInputFileNames(&input_files);
for (size_t i = 0; i < input_files.size(); i++)
dep_out_ << " " << FilePathToUTF8(input_files[i]);
// Other files read by the build.
std::vector<base::FilePath> other_files = g_scheduler->GetGenDependencies();
for (size_t i = 0; i < other_files.size(); i++)
dep_out_ << " " << FilePathToUTF8(other_files[i]);
out_ << std::endl;
}
void NinjaBuildWriter::WriteLinkPool() {
out_ << "pool link_pool\n"
<< " depth = " << default_toolchain_->concurrent_links() << std::endl
<< std::endl;
}
void NinjaBuildWriter::WriteSubninjas() {
for (size_t i = 0; i < all_settings_.size(); i++) {
out_ << "subninja ";
path_output_.WriteFile(out_, GetNinjaFileForToolchain(all_settings_[i]));
out_ << std::endl;
}
out_ << std::endl;
}
bool NinjaBuildWriter::WritePhonyAndAllRules(Err* err) {
std::string all_rules;
// Write phony rules for all uniquely-named targets in the default toolchain.
// Don't do other toolchains or we'll get naming conflicts, and if the name
// isn't unique, also skip it. The exception is for the toplevel targets
// which we also find.
std::map<std::string, int> small_name_count;
std::vector<const Target*> toplevel_targets;
base::hash_set<std::string> target_files;
for (size_t i = 0; i < default_toolchain_targets_.size(); i++) {
const Target* target = default_toolchain_targets_[i];
const Label& label = target->label();
small_name_count[label.name()]++;
// Look for targets with a name of the form
// dir = "//foo/", name = "foo"
// i.e. where the target name matches the top level directory. We will
// always write phony rules for these even if there is another target with
// the same short name.
const std::string& dir_string = label.dir().value();
if (dir_string.size() == label.name().size() + 3 && // Size matches.
dir_string[0] == '/' && dir_string[1] == '/' && // "//" at beginning.
dir_string[dir_string.size() - 1] == '/' && // "/" at end.
dir_string.compare(2, label.name().size(), label.name()) == 0)
toplevel_targets.push_back(target);
}
for (size_t i = 0; i < default_toolchain_targets_.size(); i++) {
const Target* target = default_toolchain_targets_[i];
const Label& label = target->label();
OutputFile target_file(target->dependency_output_file());
// The output files may have leading "./" so normalize those away.
NormalizePath(&target_file.value());
if (!target_files.insert(target_file.value()).second) {
*err = Err(Location(), "Duplicate rules for " + target_file.value());
return false;
}
// Write the long name "foo/bar:baz" for the target "//foo/bar:baz".
std::string long_name = label.GetUserVisibleName(false);
base::TrimString(long_name, "/", &long_name);
WritePhonyRule(target, target_file, long_name);
// Write the directory name with no target name if they match
// (e.g. "//foo/bar:bar" -> "foo/bar").
if (FindLastDirComponent(label.dir()) == label.name()) {
std::string medium_name = DirectoryWithNoLastSlash(label.dir());
base::TrimString(medium_name, "/", &medium_name);
// That may have generated a name the same as the short name of the
// target which we already wrote.
if (medium_name != label.name())
WritePhonyRule(target, target_file, medium_name);
}
// Write short names for ones which are unique.
if (small_name_count[label.name()] == 1)
WritePhonyRule(target, target_file, label.name());
if (!all_rules.empty())
all_rules.append(" $\n ");
all_rules.append(target_file.value());
}
// Pick up phony rules for the toplevel targets with non-unique names (which
// would have been skipped in the above loop).
for (size_t i = 0; i < toplevel_targets.size(); i++) {
if (small_name_count[toplevel_targets[i]->label().name()] > 1) {
const Target* target = toplevel_targets[i];
WritePhonyRule(target, target->dependency_output_file(),
target->label().name());
}
}
if (!all_rules.empty()) {
out_ << "\nbuild all: phony " << all_rules << std::endl;
out_ << "default all" << std::endl;
}
return true;
}
void NinjaBuildWriter::WritePhonyRule(const Target* target,
const OutputFile& target_file,
const std::string& phony_name) {
if (target_file.value() == phony_name)
return; // No need for a phony rule.
EscapeOptions ninja_escape;
ninja_escape.mode = ESCAPE_NINJA;
// Escape for special chars Ninja will handle.
std::string escaped = EscapeString(phony_name, ninja_escape, NULL);
out_ << "build " << escaped << ": phony ";
path_output_.WriteFile(out_, target_file);
out_ << std::endl;
}
|