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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
// 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 <iostream>
#include <map>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/environment.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "tools/gn/build_settings.h"
#include "tools/gn/commands.h"
#include "tools/gn/err.h"
#include "tools/gn/filesystem_utils.h"
#include "tools/gn/gyp_helper.h"
#include "tools/gn/gyp_target_writer.h"
#include "tools/gn/location.h"
#include "tools/gn/parser.h"
#include "tools/gn/setup.h"
#include "tools/gn/source_file.h"
#include "tools/gn/standard_out.h"
#include "tools/gn/target.h"
#include "tools/gn/tokenizer.h"
namespace commands {
namespace {
typedef GypTargetWriter::TargetGroup TargetGroup;
typedef std::map<Label, TargetGroup> CorrelatedTargetsMap;
typedef std::map<SourceFile, std::vector<TargetGroup> > GroupedTargetsMap;
typedef std::map<std::string, std::string> StringStringMap;
typedef std::vector<const BuilderRecord*> RecordVector;
// This function appends a suffix to the given source directory name. We append
// a suffix to the last directory component rather than adding a new level so
// that the relative location of the files don't change (i.e. a file
// relative to the build dir might be "../../foo/bar.cc") and we want these to
// be the same in all builds, and in particular the GYP build directories.
SourceDir AppendDirSuffix(const SourceDir& base, const std::string& suffix) {
return SourceDir(DirectoryWithNoLastSlash(base) + suffix + "/");
}
std::vector<const BuilderRecord*> GetAllResolvedTargetRecords(
const Builder* builder) {
std::vector<const BuilderRecord*> all = builder->GetAllRecords();
std::vector<const BuilderRecord*> result;
result.reserve(all.size());
for (size_t i = 0; i < all.size(); i++) {
if (all[i]->type() == BuilderRecord::ITEM_TARGET &&
all[i]->should_generate() &&
all[i]->item())
result.push_back(all[i]);
}
return result;
}
// Groups targets sharing the same label between debug and release.
//
// We strip the toolchain label because the 64-bit and 32-bit builds, for
// example, will have different toolchains but we want to correlate them.
//
// TODO(brettw) this assumes that everything in the build has the same
// toolchain. To support cross-compiling and nacl, we'll need to differentiate
// the 32-vs-64-bit case and the default-toolchain-vs-not case. When we find
// a target not using hte default toolchain, we should probably just shell
// out to ninja.
void CorrelateTargets(const RecordVector& debug_targets,
const RecordVector& release_targets,
const RecordVector& host_debug_targets,
const RecordVector& host_release_targets,
const RecordVector& debug64_targets,
const RecordVector& release64_targets,
CorrelatedTargetsMap* correlated) {
// Normal.
for (size_t i = 0; i < debug_targets.size(); i++) {
const BuilderRecord* record = debug_targets[i];
(*correlated)[record->label().GetWithNoToolchain()].debug = record;
}
for (size_t i = 0; i < release_targets.size(); i++) {
const BuilderRecord* record = release_targets[i];
(*correlated)[record->label().GetWithNoToolchain()].release = record;
}
// Host build.
for (size_t i = 0; i < host_debug_targets.size(); i++) {
const BuilderRecord* record = host_debug_targets[i];
(*correlated)[record->label().GetWithNoToolchain()].host_debug = record;
}
for (size_t i = 0; i < host_release_targets.size(); i++) {
const BuilderRecord* record = host_release_targets[i];
(*correlated)[record->label().GetWithNoToolchain()].host_release = record;
}
// Host build.
for (size_t i = 0; i < debug64_targets.size(); i++) {
const BuilderRecord* record = debug64_targets[i];
(*correlated)[record->label().GetWithNoToolchain()].debug64 = record;
}
for (size_t i = 0; i < release64_targets.size(); i++) {
const BuilderRecord* record = release64_targets[i];
(*correlated)[record->label().GetWithNoToolchain()].release64 = record;
}
}
// Verifies that both debug and release variants match. They can differ only
// by flags.
bool EnsureTargetsMatch(const TargetGroup& group, Err* err) {
// Check that both debug and release made this target.
if (!group.debug || !group.release) {
const BuilderRecord* non_null_one =
group.debug ? group.debug : group.release;
*err = Err(Location(), "The debug and release builds did not both generate "
"a target with the name\n" +
non_null_one->label().GetUserVisibleName(true));
return false;
}
const Target* debug_target = group.debug->item()->AsTarget();
const Target* release_target = group.release->item()->AsTarget();
// Check the flags that determine if and where we write the GYP file.
if (group.debug->should_generate() != group.release->should_generate() ||
debug_target->external() != release_target->external() ||
debug_target->gyp_file() != release_target->gyp_file()) {
*err = Err(Location(), "The metadata for the target\n" +
group.debug->label().GetUserVisibleName(true) +
"\ndoesn't match between the debug and release builds.");
return false;
}
// Check that the sources match.
if (debug_target->sources().size() != release_target->sources().size()) {
*err = Err(Location(), "The source file count for the target\n" +
group.debug->label().GetUserVisibleName(true) +
"\ndoesn't have the same number of files between the debug and "
"release builds.");
return false;
}
for (size_t i = 0; i < debug_target->sources().size(); i++) {
if (debug_target->sources()[i] != release_target->sources()[i]) {
*err = Err(Location(), "The debug and release version of the target \n" +
group.debug->label().GetUserVisibleName(true) +
"\ndon't agree on the file\n" +
debug_target->sources()[i].value());
return false;
}
}
// Check that the deps match.
if (debug_target->deps().size() != release_target->deps().size()) {
*err = Err(Location(), "The source file count for the target\n" +
group.debug->label().GetUserVisibleName(true) +
"\ndoesn't have the same number of deps between the debug and "
"release builds.");
return false;
}
for (size_t i = 0; i < debug_target->deps().size(); i++) {
if (debug_target->deps()[i].label != release_target->deps()[i].label) {
*err = Err(Location(), "The debug and release version of the target \n" +
group.debug->label().GetUserVisibleName(true) +
"\ndon't agree on the dep\n" +
debug_target->deps()[i].label.GetUserVisibleName(true));
return false;
}
}
return true;
}
// Returns the (number of targets, number of GYP files).
std::pair<int, int> WriteGypFiles(CommonSetup* debug_setup,
CommonSetup* release_setup,
CommonSetup* host_debug_setup,
CommonSetup* host_release_setup,
CommonSetup* debug64_setup,
CommonSetup* release64_setup,
Err* err) {
// Group all targets by output GYP file name.
std::vector<const BuilderRecord*> debug_targets =
GetAllResolvedTargetRecords(debug_setup->builder());
std::vector<const BuilderRecord*> release_targets =
GetAllResolvedTargetRecords(release_setup->builder());
// Host build is optional.
std::vector<const BuilderRecord*> host_debug_targets;
std::vector<const BuilderRecord*> host_release_targets;
if (host_debug_setup && host_release_setup) {
host_debug_targets = GetAllResolvedTargetRecords(
host_debug_setup->builder());
host_release_targets = GetAllResolvedTargetRecords(
host_release_setup->builder());
}
// 64-bit build is optional.
std::vector<const BuilderRecord*> debug64_targets;
std::vector<const BuilderRecord*> release64_targets;
if (debug64_setup && release64_setup) {
debug64_targets = GetAllResolvedTargetRecords(
debug64_setup->builder());
release64_targets = GetAllResolvedTargetRecords(
release64_setup->builder());
}
// Match up the debug and release version of each target by label.
CorrelatedTargetsMap correlated;
CorrelateTargets(debug_targets, release_targets,
host_debug_targets, host_release_targets,
debug64_targets, release64_targets,
&correlated);
GypHelper helper;
GroupedTargetsMap grouped_targets;
int target_count = 0;
for (CorrelatedTargetsMap::iterator i = correlated.begin();
i != correlated.end(); ++i) {
const TargetGroup& group = i->second;
if (!group.debug->should_generate())
continue; // Skip non-generated ones.
if (group.debug->item()->AsTarget()->external())
continue; // Skip external ones.
if (group.debug->item()->AsTarget()->gyp_file().is_null())
continue; // Skip ones without GYP files.
if (!EnsureTargetsMatch(group, err))
return std::make_pair(0, 0);
target_count++;
grouped_targets[
helper.GetGypFileForTarget(group.debug->item()->AsTarget(), err)]
.push_back(group);
if (err->has_error())
return std::make_pair(0, 0);
}
// Extract the toolchain for the debug targets.
const Toolchain* debug_toolchain = NULL;
if (!grouped_targets.empty()) {
debug_toolchain = debug_setup->builder()->GetToolchain(
grouped_targets.begin()->second[0].debug->item()->settings()->
default_toolchain_label());
}
// Write each GYP file.
for (GroupedTargetsMap::iterator i = grouped_targets.begin();
i != grouped_targets.end(); ++i) {
GypTargetWriter::WriteFile(i->first, i->second, debug_toolchain, err);
if (err->has_error())
return std::make_pair(0, 0);
}
return std::make_pair(target_count,
static_cast<int>(grouped_targets.size()));
}
} // namespace
// Suppress output on success.
const char kSwitchQuiet[] = "q";
const char kGyp[] = "gyp";
const char kGyp_HelpShort[] =
"gyp: Make GYP files from GN.";
const char kGyp_Help[] =
"gyp: Make GYP files from GN.\n"
"\n"
" This command will generate GYP files from GN sources. You can then run\n"
" GYP over the result to produce a build. Native GYP targets can depend\n"
" on any GN target except source sets. GN targets can depend on native\n"
" GYP targets, but all/direct dependent settings will NOT be pushed\n"
" across the boundary.\n"
"\n"
" To make this work you first need to manually run GN, then GYP, then\n"
" do the build. Because GN doesn't generate the final .ninja files,\n"
" there will be no rules to regenerate the .ninja files if the inputs\n"
" change, so you will have to manually repeat these steps each time\n"
" something changes:\n"
"\n"
" out/Debug/gn gyp\n"
" python build/gyp_chromiunm\n"
" ninja -C out/Debug foo_target\n"
"\n"
" Two variables are used to control how a target relates to GYP:\n"
"\n"
" - \"external != true\" and \"gyp_file\" is set: This target will be\n"
" written to the named GYP file in the source tree (not restricted to\n"
" an output or generated files directory).\n"
"\n"
" - \"external == true\" and \"gyp_file\" is set: The target will not\n"
" be written to a GYP file. But other targets being written to GYP\n"
" files can depend on it, and they will reference the given GYP file\n"
" name for GYP to use. This allows you to specify how GN->GYP\n"
" dependencies and named, and provides a place to manually set the\n"
" dependent configs from GYP to GN.\n"
"\n"
" - \"gyp_file\" is unset: Like the previous case, but if a GN target is\n"
" being written to a GYP file that depends on this one, the default\n"
" GYP file name will be assumed. The default name will match the name\n"
" of the current directory, so \"//foo/bar:baz\" would be\n"
" \"<(DEPTH)/foo/bar/bar.gyp:baz\".\n"
"\n"
"Switches\n"
" --gyp_vars\n"
" The GYP variables converted to a GN-style string lookup.\n"
" For example:\n"
" --gyp_vars=\"component=\\\"shared_library\\\" use_aura=\\\"1\\\"\"\n"
"\n"
"Example:\n"
" # This target is assumed to be in the GYP build in the file\n"
" # \"foo/foo.gyp\". This declaration tells GN where to find the GYP\n"
" # equivalent, and gives it some direct dependent settings that targets\n"
" # depending on it should receive (since these don't flow from GYP to\n"
" # GN-generated targets).\n"
" shared_library(\"gyp_target\") {\n"
" gyp_file = \"//foo/foo.gyp\"\n"
" external = true\n"
" direct_dependen_configs = [ \":gyp_target_config\" ]\n"
" }\n"
"\n"
" executable(\"my_app\") {\n"
" deps = [ \":gyp_target\" ]\n"
" gyp_file = \"//foo/myapp.gyp\"\n"
" sources = ...\n"
" }\n";
int RunGyp(const std::vector<std::string>& args) {
const CommandLine* cmdline = CommandLine::ForCurrentProcess();
base::TimeTicks begin_time = base::TimeTicks::Now();
// Deliberately leaked to avoid expensive process teardown.
Setup* setup_debug = new Setup;
if (!setup_debug->DoSetup())
return 1;
const char kIsDebug[] = "is_debug";
SourceDir base_build_dir = setup_debug->build_settings().build_dir();
setup_debug->build_settings().SetBuildDir(
AppendDirSuffix(base_build_dir, ".Debug"));
// Make a release build based on the debug one. We use a new directory for
// the build output so that they don't stomp on each other.
DependentSetup* setup_release = new DependentSetup(setup_debug);
setup_release->build_settings().build_args().AddArgOverride(
kIsDebug, Value(NULL, false));
setup_release->build_settings().SetBuildDir(
AppendDirSuffix(base_build_dir, ".Release"));
// Host build.
DependentSetup* setup_host_debug = NULL;
DependentSetup* setup_host_release = NULL;
// TODO(brettw) hook up host build.
// 64-bit build (Windows only).
DependentSetup* setup_debug64 = NULL;
DependentSetup* setup_release64 = NULL;
#if defined(OS_WIN)
static const char kForceWin64[] = "force_win64";
setup_debug64 = new DependentSetup(setup_debug);
setup_debug64->build_settings().build_args().AddArgOverride(
kForceWin64, Value(NULL, true));
setup_debug64->build_settings().SetBuildDir(
AppendDirSuffix(base_build_dir, ".Debug64"));
setup_release64 = new DependentSetup(setup_release);
setup_release64->build_settings().build_args().AddArgOverride(
kForceWin64, Value(NULL, true));
setup_release64->build_settings().SetBuildDir(
AppendDirSuffix(base_build_dir, ".Release64"));
#endif
// Run all the builds in parellel.
setup_release->RunPreMessageLoop();
if (setup_host_debug && setup_host_release) {
setup_host_debug->RunPreMessageLoop();
setup_host_release->RunPreMessageLoop();
}
if (setup_debug64 && setup_release64) {
setup_debug64->RunPreMessageLoop();
setup_release64->RunPreMessageLoop();
}
if (!setup_debug->Run())
return 1;
if (!setup_release->RunPostMessageLoop())
return 1;
if (setup_host_debug && !setup_host_debug->RunPostMessageLoop())
return 1;
if (setup_host_release && !setup_host_release->RunPostMessageLoop())
return 1;
if (setup_debug64 && !setup_debug64->RunPostMessageLoop())
return 1;
if (setup_release64 && !setup_release64->RunPostMessageLoop())
return 1;
Err err;
std::pair<int, int> counts =
WriteGypFiles(setup_debug, setup_release,
setup_host_debug, setup_host_release,
setup_debug64, setup_release64,
&err);
if (err.has_error()) {
err.PrintToStdout();
return 1;
}
// Timing info.
base::TimeTicks end_time = base::TimeTicks::Now();
if (!cmdline->HasSwitch(kSwitchQuiet)) {
OutputString("Done. ", DECORATION_GREEN);
std::string stats = "Wrote " +
base::IntToString(counts.first) + " targets to " +
base::IntToString(counts.second) + " GYP files read from " +
base::IntToString(
setup_debug->scheduler().input_file_manager()->GetInputFileCount())
+ " GN files in " +
base::IntToString((end_time - begin_time).InMilliseconds()) + "ms\n";
OutputString(stats);
}
return 0;
}
} // namespace commands
|