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/gtest_prod_util.h"
13 #include "base/logging.h"
14 #include "base/macros.h"
15 #include "tools/gn/action_values.h"
16 #include "tools/gn/config_values.h"
17 #include "tools/gn/inherited_libraries.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. These are "file-like things" that can either be
123 // directories or files. They do not need to exist, these are just passed as
124 // runtime dependencies to external test systems as necessary.
125 const std::vector
<std::string
>& data() const { return data_
; }
126 std::vector
<std::string
>& data() { return data_
; }
128 // Returns true if targets depending on this one should have an order
130 bool hard_dep() const {
131 return output_type_
== ACTION
||
132 output_type_
== ACTION_FOREACH
||
133 output_type_
== COPY_FILES
;
136 // Returns the iterator range which can be used in range-based for loops
137 // to iterate over multiple types of deps in one loop:
138 // for (const auto& pair : target->GetDeps(Target::DEPS_ALL)) ...
139 DepsIteratorRange
GetDeps(DepsIterationType type
) const;
141 // Linked private dependencies.
142 const LabelTargetVector
& private_deps() const { return private_deps_
; }
143 LabelTargetVector
& private_deps() { return private_deps_
; }
145 // Linked public dependencies.
146 const LabelTargetVector
& public_deps() const { return public_deps_
; }
147 LabelTargetVector
& public_deps() { return public_deps_
; }
149 // Non-linked dependencies.
150 const LabelTargetVector
& data_deps() const { return data_deps_
; }
151 LabelTargetVector
& data_deps() { return data_deps_
; }
153 // List of configs that this class inherits settings from. Once a target is
154 // resolved, this will also list all-dependent and public configs.
155 const UniqueVector
<LabelConfigPair
>& configs() const { return configs_
; }
156 UniqueVector
<LabelConfigPair
>& configs() { return configs_
; }
158 // List of configs that all dependencies (direct and indirect) of this
159 // target get. These configs are not added to this target. Note that due
160 // to the way this is computed, there may be duplicates in this list.
161 const UniqueVector
<LabelConfigPair
>& all_dependent_configs() const {
162 return all_dependent_configs_
;
164 UniqueVector
<LabelConfigPair
>& all_dependent_configs() {
165 return all_dependent_configs_
;
168 // List of configs that targets depending directly on this one get. These
169 // configs are also added to this target.
170 const UniqueVector
<LabelConfigPair
>& public_configs() const {
171 return public_configs_
;
173 UniqueVector
<LabelConfigPair
>& public_configs() {
174 return public_configs_
;
177 // A list of a subset of deps where we'll re-export public_configs as
178 // public_configs of this target.
179 const UniqueVector
<LabelTargetPair
>& forward_dependent_configs() const {
180 return forward_dependent_configs_
;
182 UniqueVector
<LabelTargetPair
>& forward_dependent_configs() {
183 return forward_dependent_configs_
;
186 // Dependencies that can include files from this target.
187 const std::set
<Label
>& allow_circular_includes_from() const {
188 return allow_circular_includes_from_
;
190 std::set
<Label
>& allow_circular_includes_from() {
191 return allow_circular_includes_from_
;
194 const InheritedLibraries
& inherited_libraries() const {
195 return inherited_libraries_
;
198 // This config represents the configuration set directly on this target.
199 ConfigValues
& config_values() { return config_values_
; }
200 const ConfigValues
& config_values() const { return config_values_
; }
202 ActionValues
& action_values() { return action_values_
; }
203 const ActionValues
& action_values() const { return action_values_
; }
205 const OrderedSet
<SourceDir
>& all_lib_dirs() const { return all_lib_dirs_
; }
206 const OrderedSet
<std::string
>& all_libs() const { return all_libs_
; }
208 const std::set
<const Target
*>& recursive_hard_deps() const {
209 return recursive_hard_deps_
;
212 // The toolchain is only known once this target is resolved (all if its
213 // dependencies are known). They will be null until then. Generally, this can
214 // only be used during target writing.
215 const Toolchain
* toolchain() const { return toolchain_
; }
217 // Sets the toolchain. The toolchain must include a tool for this target
218 // or the error will be set and the function will return false. Unusually,
219 // this function's "err" output is optional since this is commonly used
220 // frequently by unit tests which become needlessly verbose.
221 bool SetToolchain(const Toolchain
* toolchain
, Err
* err
= nullptr);
223 // Once this target has been resolved, all outputs from the target will be
224 // listed here. This will include things listed in the "outputs" for an
225 // action or a copy step, and the output library or executable file(s) from
228 // It will NOT include stamp files and object files.
229 const std::vector
<OutputFile
>& computed_outputs() const {
230 return computed_outputs_
;
233 // Returns outputs from this target. The link output file is the one that
234 // other targets link to when they depend on this target. This will only be
235 // valid for libraries and will be empty for all other target types.
237 // The dependency output file is the file that should be used to express
238 // a dependency on this one. It could be the same as the link output file
239 // (this will be the case for static libraries). For shared libraries it
240 // could be the same or different than the link output file, depending on the
241 // system. For actions this will be the stamp file.
243 // These are only known once the target is resolved and will be empty before
244 // that. This is a cache of the files to prevent every target that depends on
245 // a given library from recomputing the same pattern.
246 const OutputFile
& link_output_file() const {
247 return link_output_file_
;
249 const OutputFile
& dependency_output_file() const {
250 return dependency_output_file_
;
254 FRIEND_TEST_ALL_PREFIXES(Target
, ResolvePrecompiledHeaders
);
256 // Pulls necessary information from dependencies to this one when all
257 // dependencies have been resolved.
258 void PullDependentTarget(const Target
* dep
, bool is_public
);
259 void PullDependentTargets();
261 // These each pull specific things from dependencies to this one when all
262 // deps have been resolved.
263 void PullForwardedDependentConfigs();
264 void PullForwardedDependentConfigsFrom(const Target
* from
);
265 void PullRecursiveHardDeps();
267 // Fills the link and dependency output files when a target is resolved.
268 void FillOutputFiles();
270 // Checks precompiled headers from configs and makes sure the resulting
271 // values are in config_values_.
272 bool ResolvePrecompiledHeaders(Err
* err
);
274 // Validates the given thing when a target is resolved.
275 bool CheckVisibility(Err
* err
) const;
276 bool CheckTestonly(Err
* err
) const;
277 bool CheckNoNestedStaticLibs(Err
* err
) const;
278 void CheckSourcesGenerated() const;
279 void CheckSourceGenerated(const SourceFile
& source
) const;
281 OutputType output_type_
;
282 std::string output_name_
;
283 std::string output_extension_
;
286 bool all_headers_public_
;
287 FileList public_headers_
;
288 bool check_includes_
;
289 bool complete_static_lib_
;
292 std::vector
<std::string
> data_
;
294 LabelTargetVector private_deps_
;
295 LabelTargetVector public_deps_
;
296 LabelTargetVector data_deps_
;
298 UniqueVector
<LabelConfigPair
> configs_
;
299 UniqueVector
<LabelConfigPair
> all_dependent_configs_
;
300 UniqueVector
<LabelConfigPair
> public_configs_
;
301 UniqueVector
<LabelTargetPair
> forward_dependent_configs_
;
303 std::set
<Label
> allow_circular_includes_from_
;
305 // Static libraries, shared libraries, and source sets from transitive deps
306 // that need to be linked.
307 InheritedLibraries inherited_libraries_
;
309 // These libs and dirs are inherited from statically linked deps and all
310 // configs applying to this target.
311 OrderedSet
<SourceDir
> all_lib_dirs_
;
312 OrderedSet
<std::string
> all_libs_
;
314 // All hard deps from this target and all dependencies. Filled in when this
315 // target is marked resolved. This will not include the current target.
316 std::set
<const Target
*> recursive_hard_deps_
;
318 // Used for all binary targets. The precompiled header values in this struct
319 // will be resolved to the ones to use for this target, if precompiled
321 ConfigValues config_values_
;
323 // Used for action[_foreach] targets.
324 ActionValues action_values_
;
326 // Toolchain used by this target. Null until target is resolved.
327 const Toolchain
* toolchain_
;
329 // Output files. Empty until the target is resolved.
330 std::vector
<OutputFile
> computed_outputs_
;
331 OutputFile link_output_file_
;
332 OutputFile dependency_output_file_
;
334 DISALLOW_COPY_AND_ASSIGN(Target
);
337 #endif // TOOLS_GN_TARGET_H_