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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
|
// 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/builder.h"
#include "tools/gn/config.h"
#include "tools/gn/deps_iterator.h"
#include "tools/gn/err.h"
#include "tools/gn/loader.h"
#include "tools/gn/scheduler.h"
#include "tools/gn/settings.h"
#include "tools/gn/target.h"
#include "tools/gn/trace.h"
namespace {
typedef BuilderRecord::BuilderRecordSet BuilderRecordSet;
// Recursively looks in the tree for a given node, returning true if it
// was found in the dependecy graph. This is used to see if a given node
// participates in a cycle.
//
// If this returns true, the cycle will be in *path. This should point to an
// empty vector for the first call. During computation, the path will contain
// the full dependency path to the current node.
//
// Return false means no cycle was found.
bool RecursiveFindCycle(const BuilderRecord* search_in,
std::vector<const BuilderRecord*>* path) {
path->push_back(search_in);
for (const auto& cur : search_in->unresolved_deps()) {
std::vector<const BuilderRecord*>::iterator found =
std::find(path->begin(), path->end(), cur);
if (found != path->end()) {
// This item is already in the set, we found the cycle. Everything before
// the first definition of cur is irrelevant to the cycle.
path->erase(path->begin(), found);
path->push_back(cur);
return true;
}
if (RecursiveFindCycle(cur, path))
return true; // Found cycle.
}
path->pop_back();
return false;
}
} // namespace
Builder::Builder(Loader* loader) : loader_(loader) {
}
Builder::~Builder() {
}
void Builder::ItemDefined(scoped_ptr<Item> item) {
ScopedTrace trace(TraceItem::TRACE_DEFINE_TARGET, item->label());
trace.SetToolchain(item->settings()->toolchain_label());
BuilderRecord::ItemType type = BuilderRecord::TypeOfItem(item.get());
Err err;
BuilderRecord* record =
GetOrCreateRecordOfType(item->label(), item->defined_from(), type, &err);
if (!record) {
g_scheduler->FailWithError(err);
return;
}
// Check that it's not been already defined.
if (record->item()) {
err = Err(item->defined_from(), "Duplicate definition.",
"The item\n " + item->label().GetUserVisibleName(false) +
"\nwas already defined.");
err.AppendSubErr(Err(record->item()->defined_from(),
"Previous definition:"));
g_scheduler->FailWithError(err);
return;
}
record->set_item(item.Pass());
// Do target-specific dependency setup. This will also schedule dependency
// loads for targets that are required.
switch (type) {
case BuilderRecord::ITEM_TARGET:
TargetDefined(record, &err);
break;
case BuilderRecord::ITEM_TOOLCHAIN:
ToolchainDefined(record, &err);
break;
default:
break;
}
if (err.has_error()) {
g_scheduler->FailWithError(err);
return;
}
if (record->can_resolve()) {
if (!ResolveItem(record, &err)) {
g_scheduler->FailWithError(err);
return;
}
}
}
const Item* Builder::GetItem(const Label& label) const {
const BuilderRecord* record = GetRecord(label);
if (!record)
return NULL;
return record->item();
}
const Toolchain* Builder::GetToolchain(const Label& label) const {
const BuilderRecord* record = GetRecord(label);
if (!record)
return NULL;
if (!record->item())
return NULL;
return record->item()->AsToolchain();
}
std::vector<const BuilderRecord*> Builder::GetAllRecords() const {
std::vector<const BuilderRecord*> result;
result.reserve(records_.size());
for (const auto& record : records_)
result.push_back(record.second);
return result;
}
std::vector<const Target*> Builder::GetAllResolvedTargets() const {
std::vector<const Target*> result;
result.reserve(records_.size());
for (const auto& record : records_) {
if (record.second->type() == BuilderRecord::ITEM_TARGET &&
record.second->should_generate() && record.second->item())
result.push_back(record.second->item()->AsTarget());
}
return result;
}
const BuilderRecord* Builder::GetRecord(const Label& label) const {
// Forward to the non-const version.
return const_cast<Builder*>(this)->GetRecord(label);
}
BuilderRecord* Builder::GetRecord(const Label& label) {
RecordMap::iterator found = records_.find(label);
if (found == records_.end())
return NULL;
return found->second;
}
bool Builder::CheckForBadItems(Err* err) const {
// Look for errors where we find a defined node with an item that refers to
// an undefined one with no item. There may be other nodes in turn depending
// on our defined one, but listing those isn't helpful: we want to find the
// broken link.
//
// This finds normal "missing dependency" errors but does not find circular
// dependencies because in this case all items in the cycle will be GENERATED
// but none will be resolved. If this happens, we'll check explicitly for
// that below.
std::vector<const BuilderRecord*> bad_records;
std::string depstring;
for (const auto& record_pair : records_) {
const BuilderRecord* src = record_pair.second;
if (!src->should_generate())
continue; // Skip ungenerated nodes.
if (!src->resolved()) {
bad_records.push_back(src);
// Check dependencies.
for (const auto& dest : src->unresolved_deps()) {
if (!dest->item()) {
depstring += src->label().GetUserVisibleName(true) +
"\n needs " + dest->label().GetUserVisibleName(true) + "\n";
}
}
}
}
if (!depstring.empty()) {
*err = Err(Location(), "Unresolved dependencies.", depstring);
return false;
}
if (!bad_records.empty()) {
// Our logic above found a bad node but didn't identify the problem. This
// normally means a circular dependency.
depstring = CheckForCircularDependencies(bad_records);
if (depstring.empty()) {
// Something's very wrong, just dump out the bad nodes.
depstring = "I have no idea what went wrong, but these are unresolved, "
"possibly due to an\ninternal error:";
for (const auto& bad_record : bad_records) {
depstring += "\n\"" +
bad_record->label().GetUserVisibleName(false) + "\"";
}
*err = Err(Location(), "", depstring);
} else {
*err = Err(Location(), "Dependency cycle:", depstring);
}
return false;
}
return true;
}
bool Builder::TargetDefined(BuilderRecord* record, Err* err) {
Target* target = record->item()->AsTarget();
if (!AddDeps(record, target->public_deps(), err) ||
!AddDeps(record, target->private_deps(), err) ||
!AddDeps(record, target->data_deps(), err) ||
!AddDeps(record, target->configs().vector(), err) ||
!AddDeps(record, target->all_dependent_configs(), err) ||
!AddDeps(record, target->public_configs(), err) ||
!AddToolchainDep(record, target, err))
return false;
// All targets in the default toolchain get generated by default. We also
// check if this target was previously marked as "required" and force setting
// the bit again so the target's dependencies (which we now know) get the
// required bit pushed to them.
if (record->should_generate() || target->settings()->is_default())
RecursiveSetShouldGenerate(record, true);
return true;
}
bool Builder::ToolchainDefined(BuilderRecord* record, Err* err) {
Toolchain* toolchain = record->item()->AsToolchain();
if (!AddDeps(record, toolchain->deps(), err))
return false;
// The default toolchain gets generated by default. Also propogate the
// generate flag if it depends on items in a non-default toolchain.
if (record->should_generate() ||
toolchain->settings()->default_toolchain_label() == toolchain->label())
RecursiveSetShouldGenerate(record, true);
loader_->ToolchainLoaded(toolchain);
return true;
}
BuilderRecord* Builder::GetOrCreateRecordOfType(const Label& label,
const ParseNode* request_from,
BuilderRecord::ItemType type,
Err* err) {
BuilderRecord* record = GetRecord(label);
if (!record) {
// Not seen this record yet, create a new one.
record = new BuilderRecord(type, label);
record->set_originally_referenced_from(request_from);
records_[label] = record;
return record;
}
// Check types.
if (record->type() != type) {
std::string msg =
"The type of " + label.GetUserVisibleName(false) +
"\nhere is a " + BuilderRecord::GetNameForType(type) +
" but was previously seen as a " +
BuilderRecord::GetNameForType(record->type()) + ".\n\n"
"The most common cause is that the label of a config was put in the\n"
"in the deps section of a target (or vice-versa).";
*err = Err(request_from, "Item type does not match.", msg);
if (record->originally_referenced_from()) {
err->AppendSubErr(Err(record->originally_referenced_from(),
std::string()));
}
return NULL;
}
return record;
}
BuilderRecord* Builder::GetResolvedRecordOfType(const Label& label,
const ParseNode* origin,
BuilderRecord::ItemType type,
Err* err) {
BuilderRecord* record = GetRecord(label);
if (!record) {
*err = Err(origin, "Item not found",
"\"" + label.GetUserVisibleName(false) + "\" doesn't\n"
"refer to an existent thing.");
return NULL;
}
const Item* item = record->item();
if (!item) {
*err = Err(origin, "Item not resolved.",
"\"" + label.GetUserVisibleName(false) + "\" hasn't been resolved.\n");
return NULL;
}
if (!BuilderRecord::IsItemOfType(item, type)) {
*err = Err(origin,
std::string("This is not a ") + BuilderRecord::GetNameForType(type),
"\"" + label.GetUserVisibleName(false) + "\" refers to a " +
item->GetItemTypeName() + " instead of a " +
BuilderRecord::GetNameForType(type) + ".");
return NULL;
}
return record;
}
bool Builder::AddDeps(BuilderRecord* record,
const LabelConfigVector& configs,
Err* err) {
for (const auto& config : configs) {
BuilderRecord* dep_record = GetOrCreateRecordOfType(
config.label, config.origin, BuilderRecord::ITEM_CONFIG, err);
if (!dep_record)
return false;
record->AddDep(dep_record);
}
return true;
}
bool Builder::AddDeps(BuilderRecord* record,
const UniqueVector<LabelConfigPair>& configs,
Err* err) {
for (const auto& config : configs) {
BuilderRecord* dep_record = GetOrCreateRecordOfType(
config.label, config.origin, BuilderRecord::ITEM_CONFIG, err);
if (!dep_record)
return false;
record->AddDep(dep_record);
}
return true;
}
bool Builder::AddDeps(BuilderRecord* record,
const LabelTargetVector& targets,
Err* err) {
for (const auto& target : targets) {
BuilderRecord* dep_record = GetOrCreateRecordOfType(
target.label, target.origin, BuilderRecord::ITEM_TARGET, err);
if (!dep_record)
return false;
record->AddDep(dep_record);
}
return true;
}
bool Builder::AddToolchainDep(BuilderRecord* record,
const Target* target,
Err* err) {
BuilderRecord* toolchain_record = GetOrCreateRecordOfType(
target->settings()->toolchain_label(), target->defined_from(),
BuilderRecord::ITEM_TOOLCHAIN, err);
if (!toolchain_record)
return false;
record->AddDep(toolchain_record);
return true;
}
void Builder::RecursiveSetShouldGenerate(BuilderRecord* record,
bool force) {
if (!force && record->should_generate())
return; // Already set.
record->set_should_generate(true);
for (const auto& cur : record->all_deps()) {
if (!cur->should_generate()) {
ScheduleItemLoadIfNecessary(cur);
RecursiveSetShouldGenerate(cur, false);
}
}
}
void Builder::ScheduleItemLoadIfNecessary(BuilderRecord* record) {
const ParseNode* origin = record->originally_referenced_from();
loader_->Load(record->label(),
origin ? origin->GetRange() : LocationRange());
}
bool Builder::ResolveItem(BuilderRecord* record, Err* err) {
DCHECK(record->can_resolve() && !record->resolved());
if (record->type() == BuilderRecord::ITEM_TARGET) {
Target* target = record->item()->AsTarget();
if (!ResolveDeps(&target->public_deps(), err) ||
!ResolveDeps(&target->private_deps(), err) ||
!ResolveDeps(&target->data_deps(), err) ||
!ResolveConfigs(&target->configs(), err) ||
!ResolveConfigs(&target->all_dependent_configs(), err) ||
!ResolveConfigs(&target->public_configs(), err) ||
!ResolveForwardDependentConfigs(target, err) ||
!ResolveToolchain(target, err))
return false;
} else if (record->type() == BuilderRecord::ITEM_TOOLCHAIN) {
Toolchain* toolchain = record->item()->AsToolchain();
if (!ResolveDeps(&toolchain->deps(), err))
return false;
}
record->set_resolved(true);
if (!record->item()->OnResolved(err))
return false;
if (!resolved_callback_.is_null())
resolved_callback_.Run(record);
// Recursively update everybody waiting on this item to be resolved.
for (BuilderRecord* waiting : record->waiting_on_resolution()) {
DCHECK(waiting->unresolved_deps().find(record) !=
waiting->unresolved_deps().end());
waiting->unresolved_deps().erase(record);
if (waiting->can_resolve()) {
if (!ResolveItem(waiting, err))
return false;
}
}
record->waiting_on_resolution().clear();
return true;
}
bool Builder::ResolveDeps(LabelTargetVector* deps, Err* err) {
for (LabelTargetPair& cur : *deps) {
DCHECK(!cur.ptr);
BuilderRecord* record = GetResolvedRecordOfType(
cur.label, cur.origin, BuilderRecord::ITEM_TARGET, err);
if (!record)
return false;
cur.ptr = record->item()->AsTarget();
}
return true;
}
bool Builder::ResolveConfigs(UniqueVector<LabelConfigPair>* configs, Err* err) {
for (const auto& cur : *configs) {
DCHECK(!cur.ptr);
BuilderRecord* record = GetResolvedRecordOfType(
cur.label, cur.origin, BuilderRecord::ITEM_CONFIG, err);
if (!record)
return false;
const_cast<LabelConfigPair&>(cur).ptr = record->item()->AsConfig();
}
return true;
}
// "Forward dependent configs" should refer to targets in the deps that should
// have their configs forwarded.
bool Builder::ResolveForwardDependentConfigs(Target* target, Err* err) {
const UniqueVector<LabelTargetPair>& configs =
target->forward_dependent_configs();
// Assume that the lists are small so that brute-force n^2 is appropriate.
for (const auto& config : configs) {
for (const auto& dep_pair : target->GetDeps(Target::DEPS_LINKED)) {
if (config.label == dep_pair.label) {
DCHECK(dep_pair.ptr); // Should already be resolved.
// UniqueVector's contents are constant so uniqueness is preserved, but
// we want to update this pointer which doesn't change uniqueness
// (uniqueness in this vector is determined by the label only).
const_cast<LabelTargetPair&>(config).ptr = dep_pair.ptr;
break;
}
}
if (!config.ptr) {
*err = Err(target->defined_from(),
"Target in forward_dependent_configs_from was not listed in the deps",
"This target has a forward_dependent_configs_from entry that was "
"not present in\nthe deps. A target can only forward things it "
"depends on. It was forwarding:\n " +
config.label.GetUserVisibleName(false));
return false;
}
}
return true;
}
bool Builder::ResolveToolchain(Target* target, Err* err) {
BuilderRecord* record = GetResolvedRecordOfType(
target->settings()->toolchain_label(), target->defined_from(),
BuilderRecord::ITEM_TOOLCHAIN, err);
if (!record) {
*err = Err(target->defined_from(),
"Toolchain for target not defined.",
"I was hoping to find a toolchain " +
target->settings()->toolchain_label().GetUserVisibleName(false));
return false;
}
if (!target->SetToolchain(record->item()->AsToolchain(), err))
return false;
return true;
}
std::string Builder::CheckForCircularDependencies(
const std::vector<const BuilderRecord*>& bad_records) const {
std::vector<const BuilderRecord*> cycle;
if (!RecursiveFindCycle(bad_records[0], &cycle))
return std::string(); // Didn't find a cycle, something else is wrong.
std::string ret;
for (size_t i = 0; i < cycle.size(); i++) {
ret += " " + cycle[i]->label().GetUserVisibleName(false);
if (i != cycle.size() - 1)
ret += " ->";
ret += "\n";
}
return ret;
}
|