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.
5 #ifndef TOOLS_GN_BUILDER_H_
6 #define TOOLS_GN_BUILDER_H_
8 #include "base/callback.h"
9 #include "base/containers/hash_tables.h"
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "tools/gn/builder_record.h"
13 #include "tools/gn/label.h"
14 #include "tools/gn/label_ptr.h"
15 #include "tools/gn/unique_vector.h"
22 class Builder
: public base::RefCountedThreadSafe
<Builder
> {
24 typedef base::Callback
<void(const BuilderRecord
*)> ResolvedCallback
;
26 explicit Builder(Loader
* loader
);
28 // The resolved callback is called whenever a target has been resolved. This
29 // will be executed only on the main thread.
30 void set_resolved_callback(const ResolvedCallback
& cb
) {
31 resolved_callback_
= cb
;
34 Loader
* loader() const { return loader_
; }
36 void ItemDefined(scoped_ptr
<Item
> item
);
38 // Returns NULL if there is not a thing with the corresponding label.
39 const Item
* GetItem(const Label
& label
) const;
40 const Toolchain
* GetToolchain(const Label
& label
) const;
42 std::vector
<const BuilderRecord
*> GetAllRecords() const;
44 // Returns targets which should be generated and which are defined.
45 std::vector
<const Target
*> GetAllResolvedTargets() const;
47 // Returns the record for the given label, or NULL if it doesn't exist.
48 // Mostly used for unit tests.
49 const BuilderRecord
* GetRecord(const Label
& label
) const;
50 BuilderRecord
* GetRecord(const Label
& label
);
52 // If there are any undefined references, returns false and sets the error.
53 bool CheckForBadItems(Err
* err
) const;
56 friend class base::RefCountedThreadSafe
<Builder
>;
60 bool TargetDefined(BuilderRecord
* record
, Err
* err
);
61 bool ConfigDefined(BuilderRecord
* record
, Err
* err
);
62 bool ToolchainDefined(BuilderRecord
* record
, Err
* err
);
64 // Returns the record associated with the given label. This function checks
65 // that if we already have references for it, the type matches. If no record
66 // exists yet, a new one will be created.
68 // If any of the conditions fail, the return value will be null and the error
69 // will be set. request_from is used as the source of the error.
70 BuilderRecord
* GetOrCreateRecordOfType(const Label
& label
,
71 const ParseNode
* request_from
,
72 BuilderRecord::ItemType type
,
75 // Returns the record associated with the given label. This function checks
76 // that it's already been resolved to the correct type.
78 // If any of the conditions fail, the return value will be null and the error
79 // will be set. request_from is used as the source of the error.
80 BuilderRecord
* GetResolvedRecordOfType(const Label
& label
,
81 const ParseNode
* request_from
,
82 BuilderRecord::ItemType type
,
85 bool AddDeps(BuilderRecord
* record
,
86 const LabelConfigVector
& configs
,
88 bool AddDeps(BuilderRecord
* record
,
89 const UniqueVector
<LabelConfigPair
>& configs
,
91 bool AddDeps(BuilderRecord
* record
,
92 const LabelTargetVector
& targets
,
94 bool AddToolchainDep(BuilderRecord
* record
,
98 // Given a target, sets the "should generate" bit and pushes it through the
99 // dependency tree. Any time the bit it set, we ensure that the given item is
100 // scheduled to be loaded.
102 // If the force flag is set, we'll ignore the current state of the record's
103 // should_generate flag, and set it on the dependents every time. This is
104 // used when defining a target: the "should generate" may have been set
105 // before the item was defined (if it is required by something that is
106 // required). In this case, we need to re-push the "should generate" flag
107 // to the item's dependencies.
108 void RecursiveSetShouldGenerate(BuilderRecord
* record
, bool force
);
110 void ScheduleItemLoadIfNecessary(BuilderRecord
* record
);
112 // This takes a BuilderRecord with resolved depdencies, and fills in the
113 // target's Label*Vectors with the resolved pointers.
114 bool ResolveItem(BuilderRecord
* record
, Err
* err
);
116 // Fills in the pointers in the given vector based on the labels. We assume
117 // that everything should be resolved by this point, so will return an error
118 // if anything isn't found or if the type doesn't match.
119 bool ResolveDeps(LabelTargetVector
* deps
, Err
* err
);
120 bool ResolveConfigs(UniqueVector
<LabelConfigPair
>* configs
, Err
* err
);
121 bool ResolveForwardDependentConfigs(Target
* target
, Err
* err
);
122 bool ResolveToolchain(Target
* target
, Err
* err
);
124 // Given a list of unresolved records, tries to find any circular
125 // dependencies and returns the string describing the problem. If no circular
126 // deps were found, returns the empty string.
127 std::string
CheckForCircularDependencies(
128 const std::vector
<const BuilderRecord
*>& bad_records
) const;
130 // Non owning pointer.
134 typedef base::hash_map
<Label
, BuilderRecord
*> RecordMap
;
137 ResolvedCallback resolved_callback_
;
139 DISALLOW_COPY_AND_ASSIGN(Builder
);
142 #endif // TOOLS_GN_BUILDER_H_