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/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
;
32 class Target
: public Item
{
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
);
57 // Returns a string naming the output type.
58 static const char* GetStringForOutputType(OutputType type
);
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.
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
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 // Once this target has been resolved, all outputs from the target will be
225 // listed here. This will include things listed in the "outputs" for an
226 // action or a copy step, and the output library or executable file(s) from
229 // It will NOT include stamp files and object files.
230 const std::vector
<OutputFile
>& computed_outputs() const {
231 return computed_outputs_
;
234 // Returns outputs from this target. The link output file is the one that
235 // other targets link to when they depend on this target. This will only be
236 // valid for libraries and will be empty for all other target types.
238 // The dependency output file is the file that should be used to express
239 // a dependency on this one. It could be the same as the link output file
240 // (this will be the case for static libraries). For shared libraries it
241 // could be the same or different than the link output file, depending on the
242 // system. For actions this will be the stamp file.
244 // These are only known once the target is resolved and will be empty before
245 // that. This is a cache of the files to prevent every target that depends on
246 // a given library from recomputing the same pattern.
247 const OutputFile
& link_output_file() const {
248 return link_output_file_
;
250 const OutputFile
& dependency_output_file() const {
251 return dependency_output_file_
;
255 // Pulls necessary information from dependencies to this one when all
256 // dependencies have been resolved.
257 void PullDependentTarget(const Target
* dep
, bool is_public
);
258 void PullDependentTargets();
260 // These each pull specific things from dependencies to this one when all
261 // deps have been resolved.
262 void PullForwardedDependentConfigs();
263 void PullForwardedDependentConfigsFrom(const Target
* from
);
264 void PullRecursiveHardDeps();
266 // Fills the link and dependency output files when a target is resolved.
267 void FillOutputFiles();
269 // Validates the given thing when a target is resolved.
270 bool CheckVisibility(Err
* err
) const;
271 bool CheckTestonly(Err
* err
) const;
272 bool CheckNoNestedStaticLibs(Err
* err
) const;
273 void CheckSourcesGenerated() const;
274 void CheckSourceGenerated(const SourceFile
& source
) const;
276 OutputType output_type_
;
277 std::string output_name_
;
278 std::string output_extension_
;
281 bool all_headers_public_
;
282 FileList public_headers_
;
283 bool check_includes_
;
284 bool complete_static_lib_
;
287 std::vector
<std::string
> data_
;
289 LabelTargetVector private_deps_
;
290 LabelTargetVector public_deps_
;
291 LabelTargetVector data_deps_
;
293 UniqueVector
<LabelConfigPair
> configs_
;
294 UniqueVector
<LabelConfigPair
> all_dependent_configs_
;
295 UniqueVector
<LabelConfigPair
> public_configs_
;
296 UniqueVector
<LabelTargetPair
> forward_dependent_configs_
;
298 std::set
<Label
> allow_circular_includes_from_
;
300 // Static libraries, shared libraries, and source sets from transitive deps
301 // that need to be linked.
302 InheritedLibraries inherited_libraries_
;
304 // These libs and dirs are inherited from statically linked deps and all
305 // configs applying to this target.
306 OrderedSet
<SourceDir
> all_lib_dirs_
;
307 OrderedSet
<std::string
> all_libs_
;
309 // All hard deps from this target and all dependencies. Filled in when this
310 // target is marked resolved. This will not include the current target.
311 std::set
<const Target
*> recursive_hard_deps_
;
313 ConfigValues config_values_
; // Used for all binary targets.
314 ActionValues action_values_
; // Used for action[_foreach] targets.
316 // Toolchain used by this target. Null until target is resolved.
317 const Toolchain
* toolchain_
;
319 // Output files. Empty until the target is resolved.
320 std::vector
<OutputFile
> computed_outputs_
;
321 OutputFile link_output_file_
;
322 OutputFile dependency_output_file_
;
324 DISALLOW_COPY_AND_ASSIGN(Target
);
327 #endif // TOOLS_GN_TARGET_H_