Include all dupe types (event when value is zero) in scan stats.
[chromium-blink-merge.git] / tools / gn / target.h
blob00500489923ddec832ede0ea067eb9f0183b5b83
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. These are "file-like things" that can either be
124 // directories or files. They do not need to exist, these are just passed as
125 // runtime dependencies to external test systems as necessary.
126 const std::vector<std::string>& data() const { return data_; }
127 std::vector<std::string>& data() { return data_; }
129 // Returns true if targets depending on this one should have an order
130 // dependency.
131 bool hard_dep() const {
132 return output_type_ == ACTION ||
133 output_type_ == ACTION_FOREACH ||
134 output_type_ == COPY_FILES;
137 // Returns the iterator range which can be used in range-based for loops
138 // to iterate over multiple types of deps in one loop:
139 // for (const auto& pair : target->GetDeps(Target::DEPS_ALL)) ...
140 DepsIteratorRange GetDeps(DepsIterationType type) const;
142 // Linked private dependencies.
143 const LabelTargetVector& private_deps() const { return private_deps_; }
144 LabelTargetVector& private_deps() { return private_deps_; }
146 // Linked public dependencies.
147 const LabelTargetVector& public_deps() const { return public_deps_; }
148 LabelTargetVector& public_deps() { return public_deps_; }
150 // Non-linked dependencies.
151 const LabelTargetVector& data_deps() const { return data_deps_; }
152 LabelTargetVector& data_deps() { return data_deps_; }
154 // List of configs that this class inherits settings from. Once a target is
155 // resolved, this will also list all-dependent and public configs.
156 const UniqueVector<LabelConfigPair>& configs() const { return configs_; }
157 UniqueVector<LabelConfigPair>& configs() { return configs_; }
159 // List of configs that all dependencies (direct and indirect) of this
160 // target get. These configs are not added to this target. Note that due
161 // to the way this is computed, there may be duplicates in this list.
162 const UniqueVector<LabelConfigPair>& all_dependent_configs() const {
163 return all_dependent_configs_;
165 UniqueVector<LabelConfigPair>& all_dependent_configs() {
166 return all_dependent_configs_;
169 // List of configs that targets depending directly on this one get. These
170 // configs are also added to this target.
171 const UniqueVector<LabelConfigPair>& public_configs() const {
172 return public_configs_;
174 UniqueVector<LabelConfigPair>& public_configs() {
175 return public_configs_;
178 // A list of a subset of deps where we'll re-export public_configs as
179 // public_configs of this target.
180 const UniqueVector<LabelTargetPair>& forward_dependent_configs() const {
181 return forward_dependent_configs_;
183 UniqueVector<LabelTargetPair>& forward_dependent_configs() {
184 return forward_dependent_configs_;
187 // Dependencies that can include files from this target.
188 const std::set<Label>& allow_circular_includes_from() const {
189 return allow_circular_includes_from_;
191 std::set<Label>& allow_circular_includes_from() {
192 return allow_circular_includes_from_;
195 const InheritedLibraries& inherited_libraries() const {
196 return inherited_libraries_;
199 // This config represents the configuration set directly on this target.
200 ConfigValues& config_values() { return config_values_; }
201 const ConfigValues& config_values() const { return config_values_; }
203 ActionValues& action_values() { return action_values_; }
204 const ActionValues& action_values() const { return action_values_; }
206 const OrderedSet<SourceDir>& all_lib_dirs() const { return all_lib_dirs_; }
207 const OrderedSet<std::string>& all_libs() const { return all_libs_; }
209 const std::set<const Target*>& recursive_hard_deps() const {
210 return recursive_hard_deps_;
213 // The toolchain is only known once this target is resolved (all if its
214 // dependencies are known). They will be null until then. Generally, this can
215 // only be used during target writing.
216 const Toolchain* toolchain() const { return toolchain_; }
218 // Sets the toolchain. The toolchain must include a tool for this target
219 // or the error will be set and the function will return false. Unusually,
220 // this function's "err" output is optional since this is commonly used
221 // frequently by unit tests which become needlessly verbose.
222 bool SetToolchain(const Toolchain* toolchain, Err* err = nullptr);
224 // Returns outputs from this target. The link output file is the one that
225 // other targets link to when they depend on this target. This will only be
226 // valid for libraries and will be empty for all other target types.
228 // The dependency output file is the file that should be used to express
229 // a dependency on this one. It could be the same as the link output file
230 // (this will be the case for static libraries). For shared libraries it
231 // could be the same or different than the link output file, depending on the
232 // system. For actions this will be the stamp file.
234 // These are only known once the target is resolved and will be empty before
235 // that. This is a cache of the files to prevent every target that depends on
236 // a given library from recomputing the same pattern.
237 const OutputFile& link_output_file() const {
238 return link_output_file_;
240 const OutputFile& dependency_output_file() const {
241 return dependency_output_file_;
244 private:
245 // Pulls necessary information from dependencies to this one when all
246 // dependencies have been resolved.
247 void PullDependentTarget(const Target* dep, bool is_public);
248 void PullDependentTargets();
250 // These each pull specific things from dependencies to this one when all
251 // deps have been resolved.
252 void PullForwardedDependentConfigs();
253 void PullForwardedDependentConfigsFrom(const Target* from);
254 void PullRecursiveHardDeps();
256 // Fills the link and dependency output files when a target is resolved.
257 void FillOutputFiles();
259 // Validates the given thing when a target is resolved.
260 bool CheckVisibility(Err* err) const;
261 bool CheckTestonly(Err* err) const;
262 bool CheckNoNestedStaticLibs(Err* err) const;
264 OutputType output_type_;
265 std::string output_name_;
266 std::string output_extension_;
268 FileList sources_;
269 bool all_headers_public_;
270 FileList public_headers_;
271 bool check_includes_;
272 bool complete_static_lib_;
273 bool testonly_;
274 FileList inputs_;
275 std::vector<std::string> data_;
277 LabelTargetVector private_deps_;
278 LabelTargetVector public_deps_;
279 LabelTargetVector data_deps_;
281 UniqueVector<LabelConfigPair> configs_;
282 UniqueVector<LabelConfigPair> all_dependent_configs_;
283 UniqueVector<LabelConfigPair> public_configs_;
284 UniqueVector<LabelTargetPair> forward_dependent_configs_;
286 std::set<Label> allow_circular_includes_from_;
288 // Static libraries, shared libraries, and source sets from transitive deps
289 // that need to be linked.
290 InheritedLibraries inherited_libraries_;
292 // These libs and dirs are inherited from statically linked deps and all
293 // configs applying to this target.
294 OrderedSet<SourceDir> all_lib_dirs_;
295 OrderedSet<std::string> all_libs_;
297 // All hard deps from this target and all dependencies. Filled in when this
298 // target is marked resolved. This will not include the current target.
299 std::set<const Target*> recursive_hard_deps_;
301 ConfigValues config_values_; // Used for all binary targets.
302 ActionValues action_values_; // Used for action[_foreach] targets.
304 // Toolchain used by this target. Null until target is resolved.
305 const Toolchain* toolchain_;
307 // Output files. Null until the target is resolved.
308 OutputFile link_output_file_;
309 OutputFile dependency_output_file_;
311 DISALLOW_COPY_AND_ASSIGN(Target);
314 #endif // TOOLS_GN_TARGET_H_