Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / tools / gn / target.h
blob081f81ee2c64ab7712ed3c71da1898edf8fc0a78
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_TARGET_H_
6 #define TOOLS_GN_TARGET_H_
8 #include <set>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/logging.h"
14 #include "base/strings/string_piece.h"
15 #include "base/synchronization/lock.h"
16 #include "tools/gn/action_values.h"
17 #include "tools/gn/config_values.h"
18 #include "tools/gn/inherited_libraries.h"
19 #include "tools/gn/item.h"
20 #include "tools/gn/label_ptr.h"
21 #include "tools/gn/ordered_set.h"
22 #include "tools/gn/output_file.h"
23 #include "tools/gn/source_file.h"
24 #include "tools/gn/unique_vector.h"
26 class DepsIteratorRange;
27 class InputFile;
28 class Settings;
29 class Token;
30 class Toolchain;
32 class Target : public Item {
33 public:
34 enum OutputType {
35 UNKNOWN,
36 GROUP,
37 EXECUTABLE,
38 SHARED_LIBRARY,
39 STATIC_LIBRARY,
40 SOURCE_SET,
41 COPY_FILES,
42 ACTION,
43 ACTION_FOREACH,
46 enum DepsIterationType {
47 DEPS_ALL, // Iterates through all public, private, and data deps.
48 DEPS_LINKED, // Iterates through all non-data dependencies.
51 typedef std::vector<SourceFile> FileList;
52 typedef std::vector<std::string> StringVector;
54 Target(const Settings* settings, const Label& label);
55 ~Target() override;
57 // Returns a string naming the output type.
58 static const char* GetStringForOutputType(OutputType type);
60 // Item overrides.
61 Target* AsTarget() override;
62 const Target* AsTarget() const override;
63 bool OnResolved(Err* err) override;
65 OutputType output_type() const { return output_type_; }
66 void set_output_type(OutputType t) { output_type_ = t; }
68 // Can be linked into other targets.
69 bool IsLinkable() const;
71 // Can have dependencies linked in.
72 bool IsFinal() const;
74 // Will be the empty string to use the target label as the output name.
75 // See GetComputedOutputName().
76 const std::string& output_name() const { return output_name_; }
77 void set_output_name(const std::string& name) { output_name_ = name; }
79 // Returns the output name for this target, which is the output_name if
80 // specified, or the target label if not. If the flag is set, it will also
81 // include any output prefix specified on the tool (often "lib" on Linux).
83 // Because this depends on the tool for this target, the toolchain must
84 // have been set before calling.
85 std::string GetComputedOutputName(bool include_prefix) const;
87 const std::string& output_extension() const { return output_extension_; }
88 void set_output_extension(const std::string& extension) {
89 output_extension_ = extension;
92 const FileList& sources() const { return sources_; }
93 FileList& sources() { return sources_; }
95 // Set to true when all sources are public. This is the default. In this case
96 // the public headers list should be empty.
97 bool all_headers_public() const { return all_headers_public_; }
98 void set_all_headers_public(bool p) { all_headers_public_ = p; }
100 // When all_headers_public is false, this is the list of public headers. It
101 // could be empty which would mean no headers are public.
102 const FileList& public_headers() const { return public_headers_; }
103 FileList& public_headers() { return public_headers_; }
105 // Whether this target's includes should be checked by "gn check".
106 bool check_includes() const { return check_includes_; }
107 void set_check_includes(bool ci) { check_includes_ = ci; }
109 // Whether this static_library target should have code linked in.
110 bool complete_static_lib() const { return complete_static_lib_; }
111 void set_complete_static_lib(bool complete) {
112 DCHECK_EQ(STATIC_LIBRARY, output_type_);
113 complete_static_lib_ = complete;
116 bool testonly() const { return testonly_; }
117 void set_testonly(bool value) { testonly_ = value; }
119 // Compile-time extra dependencies.
120 const FileList& inputs() const { return inputs_; }
121 FileList& inputs() { return inputs_; }
123 // Runtime dependencies.
124 const FileList& data() const { return data_; }
125 FileList& data() { return data_; }
127 // Returns true if targets depending on this one should have an order
128 // dependency.
129 bool hard_dep() const {
130 return output_type_ == ACTION ||
131 output_type_ == ACTION_FOREACH ||
132 output_type_ == COPY_FILES;
135 // Returns the iterator range which can be used in range-based for loops
136 // to iterate over multiple types of deps in one loop:
137 // for (const auto& pair : target->GetDeps(Target::DEPS_ALL)) ...
138 DepsIteratorRange GetDeps(DepsIterationType type) const;
140 // Linked private dependencies.
141 const LabelTargetVector& private_deps() const { return private_deps_; }
142 LabelTargetVector& private_deps() { return private_deps_; }
144 // Linked public dependencies.
145 const LabelTargetVector& public_deps() const { return public_deps_; }
146 LabelTargetVector& public_deps() { return public_deps_; }
148 // Non-linked dependencies.
149 const LabelTargetVector& data_deps() const { return data_deps_; }
150 LabelTargetVector& data_deps() { return data_deps_; }
152 // List of configs that this class inherits settings from. Once a target is
153 // resolved, this will also list all-dependent and public configs.
154 const UniqueVector<LabelConfigPair>& configs() const { return configs_; }
155 UniqueVector<LabelConfigPair>& configs() { return configs_; }
157 // List of configs that all dependencies (direct and indirect) of this
158 // target get. These configs are not added to this target. Note that due
159 // to the way this is computed, there may be duplicates in this list.
160 const UniqueVector<LabelConfigPair>& all_dependent_configs() const {
161 return all_dependent_configs_;
163 UniqueVector<LabelConfigPair>& all_dependent_configs() {
164 return all_dependent_configs_;
167 // List of configs that targets depending directly on this one get. These
168 // configs are also added to this target.
169 const UniqueVector<LabelConfigPair>& public_configs() const {
170 return public_configs_;
172 UniqueVector<LabelConfigPair>& public_configs() {
173 return public_configs_;
176 // A list of a subset of deps where we'll re-export public_configs as
177 // public_configs of this target.
178 const UniqueVector<LabelTargetPair>& forward_dependent_configs() const {
179 return forward_dependent_configs_;
181 UniqueVector<LabelTargetPair>& forward_dependent_configs() {
182 return forward_dependent_configs_;
185 // Dependencies that can include files from this target.
186 const std::set<Label>& allow_circular_includes_from() const {
187 return allow_circular_includes_from_;
189 std::set<Label>& allow_circular_includes_from() {
190 return allow_circular_includes_from_;
193 const InheritedLibraries& inherited_libraries() const {
194 return inherited_libraries_;
197 // This config represents the configuration set directly on this target.
198 ConfigValues& config_values() { return config_values_; }
199 const ConfigValues& config_values() const { return config_values_; }
201 ActionValues& action_values() { return action_values_; }
202 const ActionValues& action_values() const { return action_values_; }
204 const OrderedSet<SourceDir>& all_lib_dirs() const { return all_lib_dirs_; }
205 const OrderedSet<std::string>& all_libs() const { return all_libs_; }
207 const std::set<const Target*>& recursive_hard_deps() const {
208 return recursive_hard_deps_;
211 // The toolchain is only known once this target is resolved (all if its
212 // dependencies are known). They will be null until then. Generally, this can
213 // only be used during target writing.
214 const Toolchain* toolchain() const { return toolchain_; }
216 // Sets the toolchain. The toolchain must include a tool for this target
217 // or the error will be set and the function will return false. Unusually,
218 // this function's "err" output is optional since this is commonly used
219 // frequently by unit tests which become needlessly verbose.
220 bool SetToolchain(const Toolchain* toolchain, Err* err = nullptr);
222 // Returns outputs from this target. The link output file is the one that
223 // other targets link to when they depend on this target. This will only be
224 // valid for libraries and will be empty for all other target types.
226 // The dependency output file is the file that should be used to express
227 // a dependency on this one. It could be the same as the link output file
228 // (this will be the case for static libraries). For shared libraries it
229 // could be the same or different than the link output file, depending on the
230 // system. For actions this will be the stamp file.
232 // These are only known once the target is resolved and will be empty before
233 // that. This is a cache of the files to prevent every target that depends on
234 // a given library from recomputing the same pattern.
235 const OutputFile& link_output_file() const {
236 return link_output_file_;
238 const OutputFile& dependency_output_file() const {
239 return dependency_output_file_;
242 private:
243 // Pulls necessary information from dependencies to this one when all
244 // dependencies have been resolved.
245 void PullDependentTarget(const Target* dep, bool is_public);
246 void PullDependentTargets();
248 // These each pull specific things from dependencies to this one when all
249 // deps have been resolved.
250 void PullForwardedDependentConfigs();
251 void PullForwardedDependentConfigsFrom(const Target* from);
252 void PullRecursiveHardDeps();
254 // Fills the link and dependency output files when a target is resolved.
255 void FillOutputFiles();
257 // Validates the given thing when a target is resolved.
258 bool CheckVisibility(Err* err) const;
259 bool CheckTestonly(Err* err) const;
260 bool CheckNoNestedStaticLibs(Err* err) const;
262 OutputType output_type_;
263 std::string output_name_;
264 std::string output_extension_;
266 FileList sources_;
267 bool all_headers_public_;
268 FileList public_headers_;
269 bool check_includes_;
270 bool complete_static_lib_;
271 bool testonly_;
272 FileList inputs_;
273 FileList data_;
275 LabelTargetVector private_deps_;
276 LabelTargetVector public_deps_;
277 LabelTargetVector data_deps_;
279 UniqueVector<LabelConfigPair> configs_;
280 UniqueVector<LabelConfigPair> all_dependent_configs_;
281 UniqueVector<LabelConfigPair> public_configs_;
282 UniqueVector<LabelTargetPair> forward_dependent_configs_;
284 std::set<Label> allow_circular_includes_from_;
286 // Static libraries, shared libraries, and source sets from transitive deps
287 // that need to be linked.
288 InheritedLibraries inherited_libraries_;
290 // These libs and dirs are inherited from statically linked deps and all
291 // configs applying to this target.
292 OrderedSet<SourceDir> all_lib_dirs_;
293 OrderedSet<std::string> all_libs_;
295 // All hard deps from this target and all dependencies. Filled in when this
296 // target is marked resolved. This will not include the current target.
297 std::set<const Target*> recursive_hard_deps_;
299 ConfigValues config_values_; // Used for all binary targets.
300 ActionValues action_values_; // Used for action[_foreach] targets.
302 // Toolchain used by this target. Null until target is resolved.
303 const Toolchain* toolchain_;
305 // Output files. Null until the target is resolved.
306 OutputFile link_output_file_;
307 OutputFile dependency_output_file_;
309 DISALLOW_COPY_AND_ASSIGN(Target);
312 #endif // TOOLS_GN_TARGET_H_