-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.cc
604 lines (519 loc) · 19.7 KB
/
builder.cc
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
// 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 <stddef.h>
#include <utility>
#include "tools/gn/action_values.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/pool.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 (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() = default;
void Builder::ItemDefined(std::unique_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(std::move(item));
// 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_CONFIG:
ConfigDefined(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 nullptr;
return record->item();
}
const Toolchain* Builder::GetToolchain(const Label& label) const {
const BuilderRecord* record = GetRecord(label);
if (!record)
return nullptr;
if (!record->item())
return nullptr;
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.get());
return result;
}
std::vector<const Item*> Builder::GetAllResolvedItems() const {
std::vector<const Item*> result;
result.reserve(records_.size());
for (const auto& record : records_) {
if (record.second->type() != BuilderRecord::ITEM_UNKNOWN &&
record.second->should_generate() && record.second->item()) {
result.push_back(record.second->item());
}
}
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) {
auto found = records_.find(label);
return (found != records_.end()) ? found->second.get() : nullptr;
}
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.get();
if (!src->should_generate())
continue; // Skip ungenerated nodes.
if (!src->resolved()) {
bad_records.push_back(src);
// Check dependencies.
for (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 (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) ||
!AddActionValuesDep(record, target->action_values(), 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::ConfigDefined(BuilderRecord* record, Err* err) {
Config* config = record->item()->AsConfig();
if (!AddDeps(record, config->configs(), err))
return false;
// Make sure all deps of this config are scheduled to be loaded. For other
// item types like targets, the "should generate" flag is propagated around
// to mark whether this should happen. We could call
// RecursiveSetShouldGenerate to do this step here, but since configs nor
// anything they depend on is actually written, the "generate" flag isn't
// relevant and means extra book keeping. Just force load any deps of this
// config.
for (auto* cur : record->all_deps())
ScheduleItemLoadIfNecessary(cur);
return true;
}
bool Builder::ToolchainDefined(BuilderRecord* record, Err* err) {
Toolchain* toolchain = record->item()->AsToolchain();
if (!AddDeps(record, toolchain->deps(), err))
return false;
for (int i = Toolchain::TYPE_NONE + 1; i < Toolchain::TYPE_NUMTYPES; i++) {
Toolchain::ToolType tool_type = static_cast<Toolchain::ToolType>(i);
Tool* tool = toolchain->GetTool(tool_type);
if (!tool || tool->pool().label.is_null())
continue;
BuilderRecord* dep_record = GetOrCreateRecordOfType(
tool->pool().label, tool->pool().origin, BuilderRecord::ITEM_POOL, err);
if (!dep_record)
return false;
record->AddDep(dep_record);
}
// 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.
auto new_record = std::make_unique<BuilderRecord>(type, label);
new_record->set_originally_referenced_from(request_from);
record = new_record.get();
records_[label] = std::move(new_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 nullptr;
}
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 nullptr;
}
const Item* item = record->item();
if (!item) {
*err = Err(origin, "Item not resolved.",
"\"" + label.GetUserVisibleName(false) + "\" hasn't been resolved.\n");
return nullptr;
}
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 nullptr;
}
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::AddActionValuesDep(BuilderRecord* record,
const ActionValues& action_values,
Err* err) {
if (action_values.pool().label.is_null())
return true;
BuilderRecord* pool_record = GetOrCreateRecordOfType(
action_values.pool().label, action_values.pool().origin,
BuilderRecord::ITEM_POOL, err);
if (!pool_record)
return false;
record->AddDep(pool_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 (!record->should_generate()) {
record->set_should_generate(true);
// This may have caused the item to go into "resolved and generated" state.
if (record->resolved() && !resolved_and_generated_callback_.is_null())
resolved_and_generated_callback_.Run(record);
} else if (!force) {
return; // Already set and we're not required to iterate dependencies.
}
for (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) ||
!ResolveActionValues(&target->action_values(), err) ||
!ResolveToolchain(target, err))
return false;
} else if (record->type() == BuilderRecord::ITEM_CONFIG) {
Config* config = record->item()->AsConfig();
if (!ResolveConfigs(&config->configs(), err))
return false;
} else if (record->type() == BuilderRecord::ITEM_TOOLCHAIN) {
Toolchain* toolchain = record->item()->AsToolchain();
if (!ResolveDeps(&toolchain->deps(), err))
return false;
if (!ResolvePools(toolchain, err))
return false;
}
record->set_resolved(true);
if (!record->item()->OnResolved(err))
return false;
if (record->should_generate() && !resolved_and_generated_callback_.is_null())
resolved_and_generated_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;
}
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;
}
bool Builder::ResolveActionValues(ActionValues* action_values, Err* err) {
if (action_values->pool().label.is_null())
return true;
BuilderRecord* record = GetResolvedRecordOfType(
action_values->pool().label, action_values->pool().origin,
BuilderRecord::ITEM_POOL, err);
if (!record)
return false;
action_values->set_pool(LabelPtrPair<Pool>(record->item()->AsPool()));
return true;
}
bool Builder::ResolvePools(Toolchain* toolchain, Err* err) {
for (int i = Toolchain::TYPE_NONE + 1; i < Toolchain::TYPE_NUMTYPES; i++) {
Toolchain::ToolType tool_type = static_cast<Toolchain::ToolType>(i);
Tool* tool = toolchain->GetTool(tool_type);
if (!tool || tool->pool().label.is_null())
continue;
BuilderRecord* record =
GetResolvedRecordOfType(tool->pool().label, toolchain->defined_from(),
BuilderRecord::ITEM_POOL, err);
if (!record) {
*err = Err(tool->pool().origin, "Pool for tool not defined.",
"I was hoping to find a pool " +
tool->pool().label.GetUserVisibleName(false));
return false;
}
tool->set_pool(LabelPtrPair<Pool>(record->item()->AsPool()));
}
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;
}