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/gtest_prod_util.h"
14 #include "base/logging.h"
15 #include "base/strings/string_piece.h"
16 #include "base/synchronization/lock.h"
17 #include "tools/gn/action_values.h"
18 #include "tools/gn/config_values.h"
19 #include "tools/gn/inherited_libraries.h"
20 #include "tools/gn/item.h"
21 #include "tools/gn/label_ptr.h"
22 #include "tools/gn/ordered_set.h"
23 #include "tools/gn/output_file.h"
24 #include "tools/gn/source_file.h"
25 #include "tools/gn/unique_vector.h"
27 class DepsIteratorRange
;
33 class Target
: public Item
{
47 enum DepsIterationType
{
48 DEPS_ALL
, // Iterates through all public, private, and data deps.
49 DEPS_LINKED
, // Iterates through all non-data dependencies.
52 typedef std::vector
<SourceFile
> FileList
;
53 typedef std::vector
<std::string
> StringVector
;
55 Target(const Settings
* settings
, const Label
& label
);
58 // Returns a string naming the output type.
59 static const char* GetStringForOutputType(OutputType type
);
62 Target
* AsTarget() override
;
63 const Target
* AsTarget() const override
;
64 bool OnResolved(Err
* err
) override
;
66 OutputType
output_type() const { return output_type_
; }
67 void set_output_type(OutputType t
) { output_type_
= t
; }
69 // Can be linked into other targets.
70 bool IsLinkable() const;
72 // Can have dependencies linked in.
75 // Will be the empty string to use the target label as the output name.
76 // See GetComputedOutputName().
77 const std::string
& output_name() const { return output_name_
; }
78 void set_output_name(const std::string
& name
) { output_name_
= name
; }
80 // Returns the output name for this target, which is the output_name if
81 // specified, or the target label if not. If the flag is set, it will also
82 // include any output prefix specified on the tool (often "lib" on Linux).
84 // Because this depends on the tool for this target, the toolchain must
85 // have been set before calling.
86 std::string
GetComputedOutputName(bool include_prefix
) const;
88 const std::string
& output_extension() const { return output_extension_
; }
89 void set_output_extension(const std::string
& extension
) {
90 output_extension_
= extension
;
93 const FileList
& sources() const { return sources_
; }
94 FileList
& sources() { return sources_
; }
96 // Set to true when all sources are public. This is the default. In this case
97 // the public headers list should be empty.
98 bool all_headers_public() const { return all_headers_public_
; }
99 void set_all_headers_public(bool p
) { all_headers_public_
= p
; }
101 // When all_headers_public is false, this is the list of public headers. It
102 // could be empty which would mean no headers are public.
103 const FileList
& public_headers() const { return public_headers_
; }
104 FileList
& public_headers() { return public_headers_
; }
106 // Whether this target's includes should be checked by "gn check".
107 bool check_includes() const { return check_includes_
; }
108 void set_check_includes(bool ci
) { check_includes_
= ci
; }
110 // Whether this static_library target should have code linked in.
111 bool complete_static_lib() const { return complete_static_lib_
; }
112 void set_complete_static_lib(bool complete
) {
113 DCHECK_EQ(STATIC_LIBRARY
, output_type_
);
114 complete_static_lib_
= complete
;
117 bool testonly() const { return testonly_
; }
118 void set_testonly(bool value
) { testonly_
= value
; }
120 // Compile-time extra dependencies.
121 const FileList
& inputs() const { return inputs_
; }
122 FileList
& inputs() { return inputs_
; }
124 // Runtime dependencies. These are "file-like things" that can either be
125 // directories or files. They do not need to exist, these are just passed as
126 // runtime dependencies to external test systems as necessary.
127 const std::vector
<std::string
>& data() const { return data_
; }
128 std::vector
<std::string
>& data() { return data_
; }
130 // Returns true if targets depending on this one should have an order
132 bool hard_dep() const {
133 return output_type_
== ACTION
||
134 output_type_
== ACTION_FOREACH
||
135 output_type_
== COPY_FILES
;
138 // Returns the iterator range which can be used in range-based for loops
139 // to iterate over multiple types of deps in one loop:
140 // for (const auto& pair : target->GetDeps(Target::DEPS_ALL)) ...
141 DepsIteratorRange
GetDeps(DepsIterationType type
) const;
143 // Linked private dependencies.
144 const LabelTargetVector
& private_deps() const { return private_deps_
; }
145 LabelTargetVector
& private_deps() { return private_deps_
; }
147 // Linked public dependencies.
148 const LabelTargetVector
& public_deps() const { return public_deps_
; }
149 LabelTargetVector
& public_deps() { return public_deps_
; }
151 // Non-linked dependencies.
152 const LabelTargetVector
& data_deps() const { return data_deps_
; }
153 LabelTargetVector
& data_deps() { return data_deps_
; }
155 // List of configs that this class inherits settings from. Once a target is
156 // resolved, this will also list all-dependent and public configs.
157 const UniqueVector
<LabelConfigPair
>& configs() const { return configs_
; }
158 UniqueVector
<LabelConfigPair
>& configs() { return configs_
; }
160 // List of configs that all dependencies (direct and indirect) of this
161 // target get. These configs are not added to this target. Note that due
162 // to the way this is computed, there may be duplicates in this list.
163 const UniqueVector
<LabelConfigPair
>& all_dependent_configs() const {
164 return all_dependent_configs_
;
166 UniqueVector
<LabelConfigPair
>& all_dependent_configs() {
167 return all_dependent_configs_
;
170 // List of configs that targets depending directly on this one get. These
171 // configs are also added to this target.
172 const UniqueVector
<LabelConfigPair
>& public_configs() const {
173 return public_configs_
;
175 UniqueVector
<LabelConfigPair
>& public_configs() {
176 return public_configs_
;
179 // A list of a subset of deps where we'll re-export public_configs as
180 // public_configs of this target.
181 const UniqueVector
<LabelTargetPair
>& forward_dependent_configs() const {
182 return forward_dependent_configs_
;
184 UniqueVector
<LabelTargetPair
>& forward_dependent_configs() {
185 return forward_dependent_configs_
;
188 // Dependencies that can include files from this target.
189 const std::set
<Label
>& allow_circular_includes_from() const {
190 return allow_circular_includes_from_
;
192 std::set
<Label
>& allow_circular_includes_from() {
193 return allow_circular_includes_from_
;
196 const InheritedLibraries
& inherited_libraries() const {
197 return inherited_libraries_
;
200 // This config represents the configuration set directly on this target.
201 ConfigValues
& config_values() { return config_values_
; }
202 const ConfigValues
& config_values() const { return config_values_
; }
204 ActionValues
& action_values() { return action_values_
; }
205 const ActionValues
& action_values() const { return action_values_
; }
207 const OrderedSet
<SourceDir
>& all_lib_dirs() const { return all_lib_dirs_
; }
208 const OrderedSet
<std::string
>& all_libs() const { return all_libs_
; }
210 const std::set
<const Target
*>& recursive_hard_deps() const {
211 return recursive_hard_deps_
;
214 // The toolchain is only known once this target is resolved (all if its
215 // dependencies are known). They will be null until then. Generally, this can
216 // only be used during target writing.
217 const Toolchain
* toolchain() const { return toolchain_
; }
219 // Sets the toolchain. The toolchain must include a tool for this target
220 // or the error will be set and the function will return false. Unusually,
221 // this function's "err" output is optional since this is commonly used
222 // frequently by unit tests which become needlessly verbose.
223 bool SetToolchain(const Toolchain
* toolchain
, Err
* err
= nullptr);
225 // Once this target has been resolved, all outputs from the target will be
226 // listed here. This will include things listed in the "outputs" for an
227 // action or a copy step, and the output library or executable file(s) from
230 // It will NOT include stamp files and object files.
231 const std::vector
<OutputFile
>& computed_outputs() const {
232 return computed_outputs_
;
235 // Returns outputs from this target. The link output file is the one that
236 // other targets link to when they depend on this target. This will only be
237 // valid for libraries and will be empty for all other target types.
239 // The dependency output file is the file that should be used to express
240 // a dependency on this one. It could be the same as the link output file
241 // (this will be the case for static libraries). For shared libraries it
242 // could be the same or different than the link output file, depending on the
243 // system. For actions this will be the stamp file.
245 // These are only known once the target is resolved and will be empty before
246 // that. This is a cache of the files to prevent every target that depends on
247 // a given library from recomputing the same pattern.
248 const OutputFile
& link_output_file() const {
249 return link_output_file_
;
251 const OutputFile
& dependency_output_file() const {
252 return dependency_output_file_
;
256 FRIEND_TEST_ALL_PREFIXES(Target
, ResolvePrecompiledHeaders
);
258 // Pulls necessary information from dependencies to this one when all
259 // dependencies have been resolved.
260 void PullDependentTarget(const Target
* dep
, bool is_public
);
261 void PullDependentTargets();
263 // These each pull specific things from dependencies to this one when all
264 // deps have been resolved.
265 void PullForwardedDependentConfigs();
266 void PullForwardedDependentConfigsFrom(const Target
* from
);
267 void PullRecursiveHardDeps();
269 // Fills the link and dependency output files when a target is resolved.
270 void FillOutputFiles();
272 // Checks precompiled headers from configs and makes sure the resulting
273 // values are in config_values_.
274 bool ResolvePrecompiledHeaders(Err
* err
);
276 // Validates the given thing when a target is resolved.
277 bool CheckVisibility(Err
* err
) const;
278 bool CheckTestonly(Err
* err
) const;
279 bool CheckNoNestedStaticLibs(Err
* err
) const;
280 void CheckSourcesGenerated() const;
281 void CheckSourceGenerated(const SourceFile
& source
) const;
283 OutputType output_type_
;
284 std::string output_name_
;
285 std::string output_extension_
;
288 bool all_headers_public_
;
289 FileList public_headers_
;
290 bool check_includes_
;
291 bool complete_static_lib_
;
294 std::vector
<std::string
> data_
;
296 LabelTargetVector private_deps_
;
297 LabelTargetVector public_deps_
;
298 LabelTargetVector data_deps_
;
300 UniqueVector
<LabelConfigPair
> configs_
;
301 UniqueVector
<LabelConfigPair
> all_dependent_configs_
;
302 UniqueVector
<LabelConfigPair
> public_configs_
;
303 UniqueVector
<LabelTargetPair
> forward_dependent_configs_
;
305 std::set
<Label
> allow_circular_includes_from_
;
307 // Static libraries, shared libraries, and source sets from transitive deps
308 // that need to be linked.
309 InheritedLibraries inherited_libraries_
;
311 // These libs and dirs are inherited from statically linked deps and all
312 // configs applying to this target.
313 OrderedSet
<SourceDir
> all_lib_dirs_
;
314 OrderedSet
<std::string
> all_libs_
;
316 // All hard deps from this target and all dependencies. Filled in when this
317 // target is marked resolved. This will not include the current target.
318 std::set
<const Target
*> recursive_hard_deps_
;
320 // Used for all binary targets. The precompiled header values in this struct
321 // will be resolved to the ones to use for this target, if precompiled
323 ConfigValues config_values_
;
325 // Used for action[_foreach] targets.
326 ActionValues action_values_
;
328 // Toolchain used by this target. Null until target is resolved.
329 const Toolchain
* toolchain_
;
331 // Output files. Empty until the target is resolved.
332 std::vector
<OutputFile
> computed_outputs_
;
333 OutputFile link_output_file_
;
334 OutputFile dependency_output_file_
;
336 DISALLOW_COPY_AND_ASSIGN(Target
);
339 #endif // TOOLS_GN_TARGET_H_