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_
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/item.h"
19 #include "tools/gn/label_ptr.h"
20 #include "tools/gn/ordered_set.h"
21 #include "tools/gn/output_file.h"
22 #include "tools/gn/source_file.h"
23 #include "tools/gn/unique_vector.h"
25 class DepsIteratorRange
;
31 class Target
: public Item
{
45 enum DepsIterationType
{
46 DEPS_ALL
, // Iterates through all public, private, and data deps.
47 DEPS_LINKED
, // Iterates through all non-data dependencies.
50 typedef std::vector
<SourceFile
> FileList
;
51 typedef std::vector
<std::string
> StringVector
;
53 Target(const Settings
* settings
, const Label
& label
);
56 // Returns a string naming the output type.
57 static const char* GetStringForOutputType(OutputType type
);
60 Target
* AsTarget() override
;
61 const Target
* AsTarget() const override
;
62 bool OnResolved(Err
* err
) override
;
64 OutputType
output_type() const { return output_type_
; }
65 void set_output_type(OutputType t
) { output_type_
= t
; }
67 // Can be linked into other targets.
68 bool IsLinkable() const;
70 // Can have dependencies linked in.
73 // Will be the empty string to use the target label as the output name.
74 // See GetComputedOutputName().
75 const std::string
& output_name() const { return output_name_
; }
76 void set_output_name(const std::string
& name
) { output_name_
= name
; }
78 // Returns the output name for this target, which is the output_name if
79 // specified, or the target label if not. If the flag is set, it will also
80 // include any output prefix specified on the tool (often "lib" on Linux).
82 // Because this depends on the tool for this target, the toolchain must
83 // have been set before calling.
84 std::string
GetComputedOutputName(bool include_prefix
) const;
86 const std::string
& output_extension() const { return output_extension_
; }
87 void set_output_extension(const std::string
& extension
) {
88 output_extension_
= extension
;
91 const FileList
& sources() const { return sources_
; }
92 FileList
& sources() { return sources_
; }
94 // Set to true when all sources are public. This is the default. In this case
95 // the public headers list should be empty.
96 bool all_headers_public() const { return all_headers_public_
; }
97 void set_all_headers_public(bool p
) { all_headers_public_
= p
; }
99 // When all_headers_public is false, this is the list of public headers. It
100 // could be empty which would mean no headers are public.
101 const FileList
& public_headers() const { return public_headers_
; }
102 FileList
& public_headers() { return public_headers_
; }
104 // Whether this target's includes should be checked by "gn check".
105 bool check_includes() const { return check_includes_
; }
106 void set_check_includes(bool ci
) { check_includes_
= ci
; }
108 // Whether this static_library target should have code linked in.
109 bool complete_static_lib() const { return complete_static_lib_
; }
110 void set_complete_static_lib(bool complete
) {
111 DCHECK_EQ(STATIC_LIBRARY
, output_type_
);
112 complete_static_lib_
= complete
;
115 bool testonly() const { return testonly_
; }
116 void set_testonly(bool value
) { testonly_
= value
; }
118 // Compile-time extra dependencies.
119 const FileList
& inputs() const { return inputs_
; }
120 FileList
& inputs() { return inputs_
; }
122 // Runtime dependencies.
123 const FileList
& data() const { return data_
; }
124 FileList
& data() { return data_
; }
126 // Returns true if targets depending on this one should have an order
128 bool hard_dep() const {
129 return output_type_
== ACTION
||
130 output_type_
== ACTION_FOREACH
||
131 output_type_
== COPY_FILES
;
134 // Returns the iterator range which can be used in range-based for loops
135 // to iterate over multiple types of deps in one loop:
136 // for (const auto& pair : target->GetDeps(Target::DEPS_ALL)) ...
137 DepsIteratorRange
GetDeps(DepsIterationType type
) const;
139 // Linked private dependencies.
140 const LabelTargetVector
& private_deps() const { return private_deps_
; }
141 LabelTargetVector
& private_deps() { return private_deps_
; }
143 // Linked public dependencies.
144 const LabelTargetVector
& public_deps() const { return public_deps_
; }
145 LabelTargetVector
& public_deps() { return public_deps_
; }
147 // Non-linked dependencies.
148 const LabelTargetVector
& data_deps() const { return data_deps_
; }
149 LabelTargetVector
& data_deps() { return data_deps_
; }
151 // List of configs that this class inherits settings from. Once a target is
152 // resolved, this will also list all-dependent and public configs.
153 const UniqueVector
<LabelConfigPair
>& configs() const { return configs_
; }
154 UniqueVector
<LabelConfigPair
>& configs() { return configs_
; }
156 // List of configs that all dependencies (direct and indirect) of this
157 // target get. These configs are not added to this target. Note that due
158 // to the way this is computed, there may be duplicates in this list.
159 const UniqueVector
<LabelConfigPair
>& all_dependent_configs() const {
160 return all_dependent_configs_
;
162 UniqueVector
<LabelConfigPair
>& all_dependent_configs() {
163 return all_dependent_configs_
;
166 // List of configs that targets depending directly on this one get. These
167 // configs are also added to this target.
168 const UniqueVector
<LabelConfigPair
>& public_configs() const {
169 return public_configs_
;
171 UniqueVector
<LabelConfigPair
>& public_configs() {
172 return public_configs_
;
175 // A list of a subset of deps where we'll re-export public_configs as
176 // public_configs of this target.
177 const UniqueVector
<LabelTargetPair
>& forward_dependent_configs() const {
178 return forward_dependent_configs_
;
180 UniqueVector
<LabelTargetPair
>& forward_dependent_configs() {
181 return forward_dependent_configs_
;
184 // Dependencies that can include files from this target.
185 const std::set
<Label
>& allow_circular_includes_from() const {
186 return allow_circular_includes_from_
;
188 std::set
<Label
>& allow_circular_includes_from() {
189 return allow_circular_includes_from_
;
192 const UniqueVector
<const Target
*>& inherited_libraries() const {
193 return inherited_libraries_
;
196 // This config represents the configuration set directly on this target.
197 ConfigValues
& config_values() { return config_values_
; }
198 const ConfigValues
& config_values() const { return config_values_
; }
200 ActionValues
& action_values() { return action_values_
; }
201 const ActionValues
& action_values() const { return action_values_
; }
203 const OrderedSet
<SourceDir
>& all_lib_dirs() const { return all_lib_dirs_
; }
204 const OrderedSet
<std::string
>& all_libs() const { return all_libs_
; }
206 const std::set
<const Target
*>& recursive_hard_deps() const {
207 return recursive_hard_deps_
;
210 // The toolchain is only known once this target is resolved (all if its
211 // dependencies are known). They will be null until then. Generally, this can
212 // only be used during target writing.
213 const Toolchain
* toolchain() const { return toolchain_
; }
215 // Sets the toolchain. The toolchain must include a tool for this target
216 // or the error will be set and the function will return false. Unusually,
217 // this function's "err" output is optional since this is commonly used
218 // frequently by unit tests which become needlessly verbose.
219 bool SetToolchain(const Toolchain
* toolchain
, Err
* err
= nullptr);
221 // Returns outputs from this target. The link output file is the one that
222 // other targets link to when they depend on this target. This will only be
223 // valid for libraries and will be empty for all other target types.
225 // The dependency output file is the file that should be used to express
226 // a dependency on this one. It could be the same as the link output file
227 // (this will be the case for static libraries). For shared libraries it
228 // could be the same or different than the link output file, depending on the
229 // system. For actions this will be the stamp file.
231 // These are only known once the target is resolved and will be empty before
232 // that. This is a cache of the files to prevent every target that depends on
233 // a given library from recomputing the same pattern.
234 const OutputFile
& link_output_file() const {
235 return link_output_file_
;
237 const OutputFile
& dependency_output_file() const {
238 return dependency_output_file_
;
242 // Pulls necessary information from dependencies to this one when all
243 // dependencies have been resolved.
244 void PullDependentTargetInfo();
246 // These each pull specific things from dependencies to this one when all
247 // deps have been resolved.
248 void PullForwardedDependentConfigs();
249 void PullForwardedDependentConfigsFrom(const Target
* from
);
250 void PullRecursiveHardDeps();
252 // Fills the link and dependency output files when a target is resolved.
253 void FillOutputFiles();
255 // Validates the given thing when a target is resolved.
256 bool CheckVisibility(Err
* err
) const;
257 bool CheckTestonly(Err
* err
) const;
258 bool CheckNoNestedStaticLibs(Err
* err
) const;
260 OutputType output_type_
;
261 std::string output_name_
;
262 std::string output_extension_
;
265 bool all_headers_public_
;
266 FileList public_headers_
;
267 bool check_includes_
;
268 bool complete_static_lib_
;
273 LabelTargetVector private_deps_
;
274 LabelTargetVector public_deps_
;
275 LabelTargetVector data_deps_
;
277 UniqueVector
<LabelConfigPair
> configs_
;
278 UniqueVector
<LabelConfigPair
> all_dependent_configs_
;
279 UniqueVector
<LabelConfigPair
> public_configs_
;
280 UniqueVector
<LabelTargetPair
> forward_dependent_configs_
;
282 std::set
<Label
> allow_circular_includes_from_
;
284 // Static libraries and source sets from transitive deps. These things need
285 // to be linked only with the end target (executable, shared library). Source
286 // sets do not get pushed beyond static library boundaries, and neither
287 // source sets nor static libraries get pushed beyond sahred library
289 UniqueVector
<const Target
*> inherited_libraries_
;
291 // These libs and dirs are inherited from statically linked deps and all
292 // configs applying to this target.
293 OrderedSet
<SourceDir
> all_lib_dirs_
;
294 OrderedSet
<std::string
> all_libs_
;
296 // All hard deps from this target and all dependencies. Filled in when this
297 // target is marked resolved. This will not include the current target.
298 std::set
<const Target
*> recursive_hard_deps_
;
300 ConfigValues config_values_
; // Used for all binary targets.
301 ActionValues action_values_
; // Used for action[_foreach] targets.
303 // Toolchain used by this target. Null until target is resolved.
304 const Toolchain
* toolchain_
;
306 // Output files. Null until the target is resolved.
307 OutputFile link_output_file_
;
308 OutputFile dependency_output_file_
;
310 DISALLOW_COPY_AND_ASSIGN(Target
);
313 #endif // TOOLS_GN_TARGET_H_