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
|
// 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/gyp_helper.h"
#include "tools/gn/gyp_target_writer.h"
#include "tools/gn/item_node.h"
#include "tools/gn/location.h"
#include "tools/gn/setup.h"
#include "tools/gn/source_file.h"
#include "tools/gn/standard_out.h"
#include "tools/gn/target.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;
// Groups targets sharing the same label between debug and release.
void CorrelateTargets(const std::vector<const Target*>& debug_targets,
const std::vector<const Target*>& release_targets,
CorrelatedTargetsMap* correlated) {
for (size_t i = 0; i < debug_targets.size(); i++) {
const Target* target = debug_targets[i];
(*correlated)[target->label()].debug = target;
}
for (size_t i = 0; i < release_targets.size(); i++) {
const Target* target = release_targets[i];
(*correlated)[target->label()].release = target;
}
}
// 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 Target* 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;
}
// Check the flags that determine if and where we write the GYP file.
if (group.debug->item_node()->should_generate() !=
group.release->item_node()->should_generate() ||
group.debug->external() != group.release->external() ||
group.debug->gyp_file() != group.release->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 (group.debug->sources().size() != group.release->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 < group.debug->sources().size(); i++) {
if (group.debug->sources()[i] != group.release->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" +
group.debug->sources()[i].value());
return false;
}
}
// Check that the deps match.
if (group.debug->deps().size() != group.release->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 < group.debug->deps().size(); i++) {
if (group.debug->deps()[i]->label() != group.release->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" +
group.debug->deps()[i]->label().GetUserVisibleName(true));
return false;
}
}
return true;
}
// Python uses shlex.split, which we partially emulate here.
//
// Advances to the next "word" in a GYP_DEFINES entry. This is something
// separated by whitespace or '='. We allow backslash escaping and quoting.
// The return value will be the index into the array immediately following the
// word, and the contents of the word will be placed into |*word|.
size_t GetNextGypDefinesWord(const std::string& defines,
size_t cur,
std::string* word) {
size_t i = cur;
bool is_quoted = false;
if (cur < defines.size() && defines[cur] == '"') {
i++;
is_quoted = true;
}
for (; i < defines.size(); i++) {
// Handle certain escape sequences: \\, \", \<space>.
if (defines[i] == '\\' && i < defines.size() - 1 &&
(defines[i + 1] == '\\' ||
defines[i + 1] == ' ' ||
defines[i + 1] == '=' ||
defines[i + 1] == '"')) {
i++;
word->push_back(defines[i]);
continue;
}
if (is_quoted && defines[i] == '"') {
// Got to the end of the quoted sequence.
return i + 1;
}
if (!is_quoted && (defines[i] == ' ' || defines[i] == '=')) {
return i;
}
word->push_back(defines[i]);
}
return i;
}
// Advances to the beginning of the next word, or the size of the string if
// the end was encountered.
size_t AdvanceToNextGypDefinesWord(const std::string& defines, size_t cur) {
while (cur < defines.size() && defines[cur] == ' ')
cur++;
return cur;
}
// The GYP defines looks like:
// component=shared_library
// component=shared_library foo=1
// component=shared_library foo=1 windows_sdk_dir="C:\Program Files\..."
StringStringMap GetGypDefines() {
StringStringMap result;
scoped_ptr<base::Environment> env(base::Environment::Create());
std::string defines;
if (!env->GetVar("GYP_DEFINES", &defines) || defines.empty())
return result;
size_t cur = 0;
while (cur < defines.size()) {
std::string key;
cur = AdvanceToNextGypDefinesWord(defines, cur);
cur = GetNextGypDefinesWord(defines, cur, &key);
// The words should be separated by an equals.
cur = AdvanceToNextGypDefinesWord(defines, cur);
if (cur == defines.size())
break;
if (defines[cur] != '=')
continue;
cur++; // Skip over '='.
std::string value;
cur = AdvanceToNextGypDefinesWord(defines, cur);
cur = GetNextGypDefinesWord(defines, cur, &value);
result[key] = value;
}
return result;
}
// Returns a set of args from known GYP define values.
Scope::KeyValueMap GetArgsFromGypDefines() {
StringStringMap gyp_defines = GetGypDefines();
Scope::KeyValueMap result;
static const char kIsComponentBuild[] = "is_component_build";
if (gyp_defines["component"] == "shared_library") {
result[kIsComponentBuild] = Value(NULL, true);
} else {
result[kIsComponentBuild] = Value(NULL, false);
}
// Windows SDK path. GYP and the GN build use the same name.
static const char kWinSdkPath[] = "windows_sdk_path";
if (!gyp_defines[kWinSdkPath].empty())
result[kWinSdkPath] = Value(NULL, gyp_defines[kWinSdkPath]);
return result;
}
// Returns the number of targets, number of GYP files.
std::pair<int, int> WriteGypFiles(
const BuildSettings& debug_settings,
const BuildSettings& release_settings,
Err* err) {
// Group all targets by output GYP file name.
std::vector<const Target*> debug_targets;
std::vector<const Target*> release_targets;
debug_settings.target_manager().GetAllTargets(&debug_targets);
release_settings.target_manager().GetAllTargets(&release_targets);
// Match up the debug and release version of each target by label.
CorrelatedTargetsMap correlated;
CorrelateTargets(debug_targets, release_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->item_node()->should_generate())
continue; // Skip non-generated ones.
if (group.debug->external())
continue; // Skip external ones.
if (group.debug->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, err)].push_back(
group);
if (err->has_error())
return std::make_pair(0, 0);
}
// Write each GYP file.
for (GroupedTargetsMap::iterator i = grouped_targets.begin();
i != grouped_targets.end(); ++i) {
GypTargetWriter::WriteFile(i->first, i->second, 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"
"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";
setup_debug->build_settings().build_args().AddArgOverrides(
GetArgsFromGypDefines());
setup_debug->build_settings().build_args().AddArgOverride(
kIsDebug, Value(NULL, true));
// 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(
SourceDir(setup_release->build_settings().build_dir().value() +
"gn_release.tmp/"));
// Run both debug and release builds in parallel.
setup_release->RunPreMessageLoop();
if (!setup_debug->Run())
return 1;
if (!setup_release->RunPostMessageLoop())
return 1;
Err err;
std::pair<int, int> counts = WriteGypFiles(setup_debug->build_settings(),
setup_release->build_settings(),
&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
|