1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "gn/builder.h"
6
7 #include <stddef.h>
8 #include <algorithm>
9 #include <utility>
10
11 #include "gn/action_values.h"
12 #include "gn/config.h"
13 #include "gn/deps_iterator.h"
14 #include "gn/err.h"
15 #include "gn/loader.h"
16 #include "gn/pool.h"
17 #include "gn/scheduler.h"
18 #include "gn/settings.h"
19 #include "gn/target.h"
20 #include "gn/trace.h"
21
22 namespace {
23
24 using BuilderRecordSet = BuilderRecord::BuilderRecordSet;
25
26 // Recursively looks in the tree for a given node, returning true if it
27 // was found in the dependency graph. This is used to see if a given node
28 // participates in a cycle.
29 //
30 // If this returns true, the cycle will be in *path. This should point to an
31 // empty vector for the first call. During computation, the path will contain
32 // the full dependency path to the current node.
33 //
34 // Return false means no cycle was found.
RecursiveFindCycle(const BuilderRecord * search_in,std::vector<const BuilderRecord * > * path)35 bool RecursiveFindCycle(const BuilderRecord* search_in,
36 std::vector<const BuilderRecord*>* path) {
37 path->push_back(search_in);
38 for (const auto& cur : search_in->GetSortedUnresolvedDeps()) {
39 std::vector<const BuilderRecord*>::iterator found =
40 std::find(path->begin(), path->end(), cur);
41 if (found != path->end()) {
42 // This item is already in the set, we found the cycle. Everything before
43 // the first definition of cur is irrelevant to the cycle.
44 path->erase(path->begin(), found);
45 path->push_back(cur);
46 return true;
47 }
48
49 if (RecursiveFindCycle(cur, path))
50 return true; // Found cycle.
51 }
52 path->pop_back();
53 return false;
54 }
55
56 } // namespace
57
Builder(Loader * loader)58 Builder::Builder(Loader* loader) : loader_(loader) {}
59
60 Builder::~Builder() = default;
61
ItemDefined(std::unique_ptr<Item> item)62 void Builder::ItemDefined(std::unique_ptr<Item> item) {
63 ScopedTrace trace(TraceItem::TRACE_DEFINE_TARGET, item->label());
64 trace.SetToolchain(item->settings()->toolchain_label());
65
66 BuilderRecord::ItemType type = BuilderRecord::TypeOfItem(item.get());
67
68 Err err;
69 BuilderRecord* record =
70 GetOrCreateRecordOfType(item->label(), item->defined_from(), type, &err);
71 if (!record) {
72 g_scheduler->FailWithError(err);
73 return;
74 }
75
76 // Check that it's not been already defined.
77 if (record->item()) {
78 err = Err(item->defined_from(), "Duplicate definition.",
79 "The item\n " + item->label().GetUserVisibleName(false) +
80 "\nwas already defined.");
81 err.AppendSubErr(
82 Err(record->item()->defined_from(), "Previous definition:"));
83 g_scheduler->FailWithError(err);
84 return;
85 }
86
87 record->set_item(std::move(item));
88
89 // Do target-specific dependency setup. This will also schedule dependency
90 // loads for targets that are required.
91 switch (type) {
92 case BuilderRecord::ITEM_TARGET:
93 TargetDefined(record, &err);
94 break;
95 case BuilderRecord::ITEM_CONFIG:
96 ConfigDefined(record, &err);
97 break;
98 case BuilderRecord::ITEM_TOOLCHAIN:
99 ToolchainDefined(record, &err);
100 break;
101 default:
102 break;
103 }
104 if (err.has_error()) {
105 g_scheduler->FailWithError(err);
106 return;
107 }
108
109 if (record->can_resolve()) {
110 if (!ResolveItem(record, &err)) {
111 g_scheduler->FailWithError(err);
112 return;
113 }
114 }
115 }
116
GetItem(const Label & label) const117 const Item* Builder::GetItem(const Label& label) const {
118 const BuilderRecord* record = GetRecord(label);
119 if (!record)
120 return nullptr;
121 return record->item();
122 }
123
GetToolchain(const Label & label) const124 const Toolchain* Builder::GetToolchain(const Label& label) const {
125 const BuilderRecord* record = GetRecord(label);
126 if (!record)
127 return nullptr;
128 if (!record->item())
129 return nullptr;
130 return record->item()->AsToolchain();
131 }
132
GetAllRecords() const133 std::vector<const BuilderRecord*> Builder::GetAllRecords() const {
134 std::vector<const BuilderRecord*> result;
135 result.reserve(records_.size());
136 for (const auto& record : records_)
137 result.push_back(&record);
138 // Ensure deterministic outputs.
139 std::sort(result.begin(), result.end(), BuilderRecord::LabelCompare);
140 return result;
141 }
142
GetAllResolvedItems() const143 std::vector<const Item*> Builder::GetAllResolvedItems() const {
144 std::vector<const Item*> result;
145 result.reserve(records_.size());
146 for (const auto& record : records_) {
147 if (record.type() != BuilderRecord::ITEM_UNKNOWN &&
148 record.should_generate() && record.item()) {
149 result.push_back(record.item());
150 }
151 }
152 // Ensure deterministic outputs.
153 std::sort(result.begin(), result.end(), [](const Item* a, const Item* b) {
154 return a->label() < b->label();
155 });
156 return result;
157 }
158
GetAllResolvedTargets() const159 std::vector<const Target*> Builder::GetAllResolvedTargets() const {
160 std::vector<const Target*> result;
161 result.reserve(records_.size());
162 for (const auto& record : records_) {
163 if (record.type() == BuilderRecord::ITEM_TARGET &&
164 record.should_generate() && record.item())
165 result.push_back(record.item()->AsTarget());
166 }
167 // Ensure deterministic outputs.
168 std::sort(result.begin(), result.end(), [](const Target* a, const Target* b) {
169 return a->label() < b->label();
170 });
171 return result;
172 }
173
GetRecord(const Label & label) const174 const BuilderRecord* Builder::GetRecord(const Label& label) const {
175 // Forward to the non-const version.
176 return const_cast<Builder*>(this)->GetRecord(label);
177 }
178
GetRecord(const Label & label)179 BuilderRecord* Builder::GetRecord(const Label& label) {
180 return records_.find(label);
181 }
182
CheckForBadItems(Err * err) const183 bool Builder::CheckForBadItems(Err* err) const {
184 // Look for errors where we find a defined node with an item that refers to
185 // an undefined one with no item. There may be other nodes in turn depending
186 // on our defined one, but listing those isn't helpful: we want to find the
187 // broken link.
188 //
189 // This finds normal "missing dependency" errors but does not find circular
190 // dependencies because in this case all items in the cycle will be GENERATED
191 // but none will be resolved. If this happens, we'll check explicitly for
192 // that below.
193 std::vector<const BuilderRecord*> bad_records;
194 for (const auto& src : records_) {
195 if (!src.should_generate())
196 continue; // Skip ungenerated nodes.
197
198 if (!src.resolved())
199 bad_records.push_back(&src);
200 }
201 if (bad_records.empty())
202 return true;
203
204 // Sort by label to ensure deterministic outputs.
205 std::sort(bad_records.begin(), bad_records.end(),
206 BuilderRecord::LabelCompare);
207
208 std::string depstring;
209 for (const auto& src : bad_records) {
210 // Check dependencies.
211 for (const auto* dest : src->GetSortedUnresolvedDeps()) {
212 if (!dest->item()) {
213 depstring += src->label().GetUserVisibleName(true) + "\n needs " +
214 dest->label().GetUserVisibleName(true) + "\n";
215 }
216 }
217 }
218
219 if (!depstring.empty()) {
220 *err = Err(Location(), "Unresolved dependencies.", depstring);
221 return false;
222 }
223
224 if (!bad_records.empty()) {
225 // Our logic above found a bad node but didn't identify the problem. This
226 // normally means a circular dependency.
227 depstring = CheckForCircularDependencies(bad_records);
228 if (depstring.empty()) {
229 // Something's very wrong, just dump out the bad nodes.
230 depstring =
231 "I have no idea what went wrong, but these are unresolved, "
232 "possibly due to an\ninternal error:";
233 for (auto* bad_record : bad_records) {
234 depstring +=
235 "\n\"" + bad_record->label().GetUserVisibleName(false) + "\"";
236 }
237 *err = Err(Location(), "", depstring);
238 } else {
239 *err = Err(Location(), "Dependency cycle:", depstring);
240 }
241 return false;
242 }
243
244 return true;
245 }
246
TargetDefined(BuilderRecord * record,Err * err)247 bool Builder::TargetDefined(BuilderRecord* record, Err* err) {
248 Target* target = record->item()->AsTarget();
249
250 if (!AddDeps(record, target->public_deps(), err) ||
251 !AddDeps(record, target->private_deps(), err) ||
252 !AddDeps(record, target->data_deps(), err) ||
253 !AddDeps(record, target->configs().vector(), err) ||
254 !AddDeps(record, target->all_dependent_configs(), err) ||
255 !AddDeps(record, target->public_configs(), err) ||
256 !AddGenDeps(record, target->gen_deps(), err) ||
257 !AddActionValuesDep(record, target->action_values(), err) ||
258 !AddToolchainDep(record, target, err))
259 return false;
260
261 // All targets in the default toolchain get generated by default. We also
262 // check if this target was previously marked as "required" and force setting
263 // the bit again so the target's dependencies (which we now know) get the
264 // required bit pushed to them.
265 if (record->should_generate() || target->settings()->is_default())
266 RecursiveSetShouldGenerate(record, true);
267
268 return true;
269 }
270
ConfigDefined(BuilderRecord * record,Err * err)271 bool Builder::ConfigDefined(BuilderRecord* record, Err* err) {
272 Config* config = record->item()->AsConfig();
273 if (!AddDeps(record, config->configs(), err))
274 return false;
275
276 // Make sure all deps of this config are scheduled to be loaded. For other
277 // item types like targets, the "should generate" flag is propagated around
278 // to mark whether this should happen. We could call
279 // RecursiveSetShouldGenerate to do this step here, but since configs nor
280 // anything they depend on is actually written, the "generate" flag isn't
281 // relevant and means extra book keeping. Just force load any deps of this
282 // config.
283 for (auto it = record->all_deps().begin(); it.valid(); ++it) {
284 ScheduleItemLoadIfNecessary(*it);
285 }
286
287 return true;
288 }
289
ToolchainDefined(BuilderRecord * record,Err * err)290 bool Builder::ToolchainDefined(BuilderRecord* record, Err* err) {
291 Toolchain* toolchain = record->item()->AsToolchain();
292
293 if (!AddDeps(record, toolchain->deps(), err))
294 return false;
295
296 for (const auto& tool : toolchain->tools()) {
297 if (tool.second->pool().label.is_null())
298 continue;
299
300 BuilderRecord* dep_record = GetOrCreateRecordOfType(
301 tool.second->pool().label, tool.second->pool().origin,
302 BuilderRecord::ITEM_POOL, err);
303 if (!dep_record)
304 return false;
305 record->AddDep(dep_record);
306 }
307
308 // The default toolchain gets generated by default. Also propagate the
309 // generate flag if it depends on items in a non-default toolchain.
310 if (record->should_generate() ||
311 toolchain->settings()->default_toolchain_label() == toolchain->label())
312 RecursiveSetShouldGenerate(record, true);
313
314 loader_->ToolchainLoaded(toolchain);
315 return true;
316 }
317
GetOrCreateRecordForTesting(const Label & label)318 BuilderRecord* Builder::GetOrCreateRecordForTesting(const Label& label) {
319 Err err;
320 return GetOrCreateRecordOfType(label, nullptr, BuilderRecord::ITEM_UNKNOWN, &err);
321 }
322
GetOrCreateRecordOfType(const Label & label,const ParseNode * request_from,BuilderRecord::ItemType type,Err * err)323 BuilderRecord* Builder::GetOrCreateRecordOfType(const Label& label,
324 const ParseNode* request_from,
325 BuilderRecord::ItemType type,
326 Err* err) {
327 auto pair = records_.try_emplace(label, request_from, type);
328 BuilderRecord* record = pair.second;
329
330 // Check types, if the record was not just created.
331 if (!pair.first && record->type() != type) {
332 std::string msg =
333 "The type of " + label.GetUserVisibleName(false) + "\nhere is a " +
334 BuilderRecord::GetNameForType(type) + " but was previously seen as a " +
335 BuilderRecord::GetNameForType(record->type()) +
336 ".\n\n"
337 "The most common cause is that the label of a config was put in the\n"
338 "in the deps section of a target (or vice-versa).";
339 *err = Err(request_from, "Item type does not match.", msg);
340 if (record->originally_referenced_from()) {
341 err->AppendSubErr(
342 Err(record->originally_referenced_from(), std::string()));
343 }
344 return nullptr;
345 }
346
347 return record;
348 }
349
GetResolvedRecordOfType(const Label & label,const ParseNode * origin,BuilderRecord::ItemType type,Err * err)350 BuilderRecord* Builder::GetResolvedRecordOfType(const Label& label,
351 const ParseNode* origin,
352 BuilderRecord::ItemType type,
353 Err* err) {
354 BuilderRecord* record = GetRecord(label);
355 if (!record) {
356 *err = Err(origin, "Item not found",
357 "\"" + label.GetUserVisibleName(false) +
358 "\" doesn't\n"
359 "refer to an existent thing.");
360 return nullptr;
361 }
362
363 const Item* item = record->item();
364 if (!item) {
365 *err = Err(
366 origin, "Item not resolved.",
367 "\"" + label.GetUserVisibleName(false) + "\" hasn't been resolved.\n");
368 return nullptr;
369 }
370
371 if (!BuilderRecord::IsItemOfType(item, type)) {
372 *err =
373 Err(origin,
374 std::string("This is not a ") + BuilderRecord::GetNameForType(type),
375 "\"" + label.GetUserVisibleName(false) + "\" refers to a " +
376 item->GetItemTypeName() + " instead of a " +
377 BuilderRecord::GetNameForType(type) + ".");
378 return nullptr;
379 }
380 return record;
381 }
382
AddDeps(BuilderRecord * record,const LabelConfigVector & configs,Err * err)383 bool Builder::AddDeps(BuilderRecord* record,
384 const LabelConfigVector& configs,
385 Err* err) {
386 for (const auto& config : configs) {
387 BuilderRecord* dep_record = GetOrCreateRecordOfType(
388 config.label, config.origin, BuilderRecord::ITEM_CONFIG, err);
389 if (!dep_record)
390 return false;
391 record->AddDep(dep_record);
392 }
393 return true;
394 }
395
AddDeps(BuilderRecord * record,const UniqueVector<LabelConfigPair> & configs,Err * err)396 bool Builder::AddDeps(BuilderRecord* record,
397 const UniqueVector<LabelConfigPair>& configs,
398 Err* err) {
399 for (const auto& config : configs) {
400 BuilderRecord* dep_record = GetOrCreateRecordOfType(
401 config.label, config.origin, BuilderRecord::ITEM_CONFIG, err);
402 if (!dep_record)
403 return false;
404 record->AddDep(dep_record);
405 }
406 return true;
407 }
408
AddDeps(BuilderRecord * record,const LabelTargetVector & targets,Err * err)409 bool Builder::AddDeps(BuilderRecord* record,
410 const LabelTargetVector& targets,
411 Err* err) {
412 for (const auto& target : targets) {
413 BuilderRecord* dep_record = GetOrCreateRecordOfType(
414 target.label, target.origin, BuilderRecord::ITEM_TARGET, err);
415 if (!dep_record)
416 return false;
417 record->AddDep(dep_record);
418 }
419 return true;
420 }
421
AddGenDeps(BuilderRecord * record,const LabelTargetVector & targets,Err * err)422 bool Builder::AddGenDeps(BuilderRecord* record,
423 const LabelTargetVector& targets,
424 Err* err) {
425 for (const auto& target : targets) {
426 BuilderRecord* dep_record = GetOrCreateRecordOfType(
427 target.label, target.origin, BuilderRecord::ITEM_TARGET, err);
428 if (!dep_record)
429 return false;
430 record->AddGenDep(dep_record);
431 }
432 return true;
433 }
434
AddActionValuesDep(BuilderRecord * record,const ActionValues & action_values,Err * err)435 bool Builder::AddActionValuesDep(BuilderRecord* record,
436 const ActionValues& action_values,
437 Err* err) {
438 if (action_values.pool().label.is_null())
439 return true;
440
441 BuilderRecord* pool_record = GetOrCreateRecordOfType(
442 action_values.pool().label, action_values.pool().origin,
443 BuilderRecord::ITEM_POOL, err);
444 if (!pool_record)
445 return false;
446 record->AddDep(pool_record);
447
448 return true;
449 }
450
AddToolchainDep(BuilderRecord * record,const Target * target,Err * err)451 bool Builder::AddToolchainDep(BuilderRecord* record,
452 const Target* target,
453 Err* err) {
454 BuilderRecord* toolchain_record = GetOrCreateRecordOfType(
455 target->settings()->toolchain_label(), target->defined_from(),
456 BuilderRecord::ITEM_TOOLCHAIN, err);
457 if (!toolchain_record)
458 return false;
459 record->AddDep(toolchain_record);
460
461 return true;
462 }
463
RecursiveSetShouldGenerate(BuilderRecord * record,bool force)464 void Builder::RecursiveSetShouldGenerate(BuilderRecord* record, bool force) {
465 if (!record->should_generate()) {
466 // This function can encounter cycles because gen_deps aren't a DAG. Setting
467 // the should_generate flag before iterating avoids infinite recursion in
468 // that case.
469 record->set_should_generate(true);
470
471 // This may have caused the item to go into "resolved and generated" state.
472 if (record->resolved() && resolved_and_generated_callback_)
473 resolved_and_generated_callback_(record);
474 } else if (!force) {
475 return; // Already set and we're not required to iterate dependencies.
476 }
477
478 for (auto it = record->all_deps().begin(); it.valid(); ++it) {
479 BuilderRecord* cur = *it;
480 if (!cur->should_generate()) {
481 ScheduleItemLoadIfNecessary(cur);
482 RecursiveSetShouldGenerate(cur, false);
483 }
484 }
485 }
486
ScheduleItemLoadIfNecessary(BuilderRecord * record)487 void Builder::ScheduleItemLoadIfNecessary(BuilderRecord* record) {
488 const ParseNode* origin = record->originally_referenced_from();
489 loader_->Load(record->label(), origin ? origin->GetRange() : LocationRange());
490 }
491
ResolveItem(BuilderRecord * record,Err * err)492 bool Builder::ResolveItem(BuilderRecord* record, Err* err) {
493 DCHECK(record->can_resolve() && !record->resolved());
494
495 if (record->type() == BuilderRecord::ITEM_TARGET) {
496 Target* target = record->item()->AsTarget();
497 if (!ResolveDeps(&target->public_deps(), err) ||
498 !ResolveDeps(&target->private_deps(), err) ||
499 !ResolveDeps(&target->data_deps(), err) ||
500 !ResolveConfigs(&target->configs(), err) ||
501 !ResolveConfigs(&target->all_dependent_configs(), err) ||
502 !ResolveConfigs(&target->public_configs(), err) ||
503 !ResolveActionValues(&target->action_values(), err) ||
504 !ResolveToolchain(target, err))
505 return false;
506 } else if (record->type() == BuilderRecord::ITEM_CONFIG) {
507 Config* config = record->item()->AsConfig();
508 if (!ResolveConfigs(&config->configs(), err))
509 return false;
510 } else if (record->type() == BuilderRecord::ITEM_TOOLCHAIN) {
511 Toolchain* toolchain = record->item()->AsToolchain();
512 if (!ResolveDeps(&toolchain->deps(), err))
513 return false;
514 if (!ResolvePools(toolchain, err))
515 return false;
516 }
517
518 record->set_resolved(true);
519
520 if (!record->item()->OnResolved(err))
521 return false;
522 if (record->should_generate() && resolved_and_generated_callback_)
523 resolved_and_generated_callback_(record);
524
525 // Recursively update everybody waiting on this item to be resolved.
526 const BuilderRecordSet waiting_deps = record->waiting_on_resolution();
527 for (auto it = waiting_deps.begin(); it.valid(); ++it) {
528 BuilderRecord* waiting = *it;
529 if (waiting->OnResolvedDep(record)) {
530 if (!ResolveItem(waiting, err))
531 return false;
532 }
533 }
534 record->waiting_on_resolution().clear();
535 return true;
536 }
537
ResolveDeps(LabelTargetVector * deps,Err * err)538 bool Builder::ResolveDeps(LabelTargetVector* deps, Err* err) {
539 for (LabelTargetPair& cur : *deps) {
540 DCHECK(!cur.ptr);
541
542 BuilderRecord* record = GetResolvedRecordOfType(
543 cur.label, cur.origin, BuilderRecord::ITEM_TARGET, err);
544 if (!record)
545 return false;
546 cur.ptr = record->item()->AsTarget();
547 }
548 return true;
549 }
550
ResolveConfigs(UniqueVector<LabelConfigPair> * configs,Err * err)551 bool Builder::ResolveConfigs(UniqueVector<LabelConfigPair>* configs, Err* err) {
552 for (const auto& cur : *configs) {
553 DCHECK(!cur.ptr);
554
555 BuilderRecord* record = GetResolvedRecordOfType(
556 cur.label, cur.origin, BuilderRecord::ITEM_CONFIG, err);
557 if (!record)
558 return false;
559 const_cast<LabelConfigPair&>(cur).ptr = record->item()->AsConfig();
560 }
561 return true;
562 }
563
ResolveToolchain(Target * target,Err * err)564 bool Builder::ResolveToolchain(Target* target, Err* err) {
565 BuilderRecord* record = GetResolvedRecordOfType(
566 target->settings()->toolchain_label(), target->defined_from(),
567 BuilderRecord::ITEM_TOOLCHAIN, err);
568 if (!record) {
569 *err = Err(
570 target->defined_from(), "Toolchain for target not defined.",
571 "I was hoping to find a toolchain " +
572 target->settings()->toolchain_label().GetUserVisibleName(false));
573 return false;
574 }
575
576 if (!target->SetToolchain(record->item()->AsToolchain(), err))
577 return false;
578
579 return true;
580 }
581
ResolveActionValues(ActionValues * action_values,Err * err)582 bool Builder::ResolveActionValues(ActionValues* action_values, Err* err) {
583 if (action_values->pool().label.is_null())
584 return true;
585
586 BuilderRecord* record = GetResolvedRecordOfType(
587 action_values->pool().label, action_values->pool().origin,
588 BuilderRecord::ITEM_POOL, err);
589 if (!record)
590 return false;
591 action_values->set_pool(LabelPtrPair<Pool>(record->item()->AsPool()));
592
593 return true;
594 }
595
ResolvePools(Toolchain * toolchain,Err * err)596 bool Builder::ResolvePools(Toolchain* toolchain, Err* err) {
597 for (const auto& tool : toolchain->tools()) {
598 if (tool.second->pool().label.is_null())
599 continue;
600
601 BuilderRecord* record = GetResolvedRecordOfType(
602 tool.second->pool().label, toolchain->defined_from(),
603 BuilderRecord::ITEM_POOL, err);
604 if (!record) {
605 *err = Err(tool.second->pool().origin, "Pool for tool not defined.",
606 "I was hoping to find a pool " +
607 tool.second->pool().label.GetUserVisibleName(false));
608 return false;
609 }
610
611 tool.second->set_pool(LabelPtrPair<Pool>(record->item()->AsPool()));
612 }
613
614 return true;
615 }
616
CheckForCircularDependencies(const std::vector<const BuilderRecord * > & bad_records) const617 std::string Builder::CheckForCircularDependencies(
618 const std::vector<const BuilderRecord*>& bad_records) const {
619 std::vector<const BuilderRecord*> cycle;
620 if (!RecursiveFindCycle(bad_records[0], &cycle))
621 return std::string(); // Didn't find a cycle, something else is wrong.
622
623 std::string ret;
624 for (size_t i = 0; i < cycle.size(); i++) {
625 ret += " " + cycle[i]->label().GetUserVisibleName(loader_->GetDefaultToolchain());
626 if (i != cycle.size() - 1)
627 ret += " ->";
628 ret += "\n";
629 }
630
631 return ret;
632 }
633