blob: 383a2af08368747ded8c3e6822ff2de4e384de76 [file] [log] [blame]
[email protected]96ea63d2013-07-30 10:17:071// 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.
4
5#ifndef TOOLS_GN_TARGET_H_
6#define TOOLS_GN_TARGET_H_
7
8#include <set>
9#include <string>
10#include <vector>
11
brettw3dab5fe2015-06-29 23:00:1512#include "base/gtest_prod_util.h"
[email protected]96ea63d2013-07-30 10:17:0713#include "base/logging.h"
tfarinaf51a763e2015-08-10 00:55:3814#include "base/macros.h"
[email protected]2fbe1012014-03-20 17:59:1515#include "tools/gn/action_values.h"
[email protected]96ea63d2013-07-30 10:17:0716#include "tools/gn/config_values.h"
brettw54d767532015-04-16 17:40:4917#include "tools/gn/inherited_libraries.h"
[email protected]96ea63d2013-07-30 10:17:0718#include "tools/gn/item.h"
[email protected]68d1dd32013-11-01 21:59:5119#include "tools/gn/label_ptr.h"
[email protected]b1e468f2013-09-10 22:58:0220#include "tools/gn/ordered_set.h"
[email protected]0dfcae72014-08-19 22:52:1621#include "tools/gn/output_file.h"
[email protected]96ea63d2013-07-30 10:17:0722#include "tools/gn/source_file.h"
[email protected]8fc56182014-08-06 21:44:3323#include "tools/gn/unique_vector.h"
[email protected]96ea63d2013-07-30 10:17:0724
brettwa09df112014-09-27 22:27:1025class DepsIteratorRange;
[email protected]96ea63d2013-07-30 10:17:0726class InputFile;
27class Settings;
28class Token;
[email protected]0dfcae72014-08-19 22:52:1629class Toolchain;
[email protected]96ea63d2013-07-30 10:17:0730
31class Target : public Item {
32 public:
33 enum OutputType {
[email protected]c0822d7f2013-08-13 17:10:5634 UNKNOWN,
35 GROUP,
[email protected]96ea63d2013-07-30 10:17:0736 EXECUTABLE,
37 SHARED_LIBRARY,
andybons027840d2015-10-14 18:49:3038 LOADABLE_MODULE,
[email protected]96ea63d2013-07-30 10:17:0739 STATIC_LIBRARY,
[email protected]7ff2f5d2013-10-08 21:30:3440 SOURCE_SET,
[email protected]96ea63d2013-07-30 10:17:0741 COPY_FILES,
[email protected]2fbe1012014-03-20 17:59:1542 ACTION,
43 ACTION_FOREACH,
[email protected]96ea63d2013-07-30 10:17:0744 };
brettwa09df112014-09-27 22:27:1045
46 enum DepsIterationType {
47 DEPS_ALL, // Iterates through all public, private, and data deps.
48 DEPS_LINKED, // Iterates through all non-data dependencies.
49 };
50
[email protected]96ea63d2013-07-30 10:17:0751 typedef std::vector<SourceFile> FileList;
52 typedef std::vector<std::string> StringVector;
53
54 Target(const Settings* settings, const Label& label);
Viet-Trung Luu50df3ed2014-10-22 05:00:1255 ~Target() override;
[email protected]96ea63d2013-07-30 10:17:0756
[email protected]ac1128e2013-08-23 00:26:5657 // Returns a string naming the output type.
58 static const char* GetStringForOutputType(OutputType type);
59
[email protected]96ea63d2013-07-30 10:17:0760 // Item overrides.
Viet-Trung Luu50df3ed2014-10-22 05:00:1261 Target* AsTarget() override;
62 const Target* AsTarget() const override;
63 bool OnResolved(Err* err) override;
[email protected]96ea63d2013-07-30 10:17:0764
[email protected]96ea63d2013-07-30 10:17:0765 OutputType output_type() const { return output_type_; }
66 void set_output_type(OutputType t) { output_type_ = t; }
67
cmasonea426cf932014-09-13 00:51:4768 // Can be linked into other targets.
[email protected]96ea63d2013-07-30 10:17:0769 bool IsLinkable() const;
70
cmasonea426cf932014-09-13 00:51:4771 // Can have dependencies linked in.
72 bool IsFinal() const;
73
[email protected]b5c19e32013-09-10 23:01:2574 // Will be the empty string to use the target label as the output name.
[email protected]0dfcae72014-08-19 22:52:1675 // See GetComputedOutputName().
[email protected]b5c19e32013-09-10 23:01:2576 const std::string& output_name() const { return output_name_; }
77 void set_output_name(const std::string& name) { output_name_ = name; }
78
[email protected]0dfcae72014-08-19 22:52:1679 // 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).
82 //
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;
86
[email protected]b9473ee2014-02-28 21:51:1087 const std::string& output_extension() const { return output_extension_; }
88 void set_output_extension(const std::string& extension) {
89 output_extension_ = extension;
90 }
91
[email protected]96ea63d2013-07-30 10:17:0792 const FileList& sources() const { return sources_; }
[email protected]3c1274d2013-09-10 22:21:2193 FileList& sources() { return sources_; }
[email protected]96ea63d2013-07-30 10:17:0794
[email protected]126d8b52014-04-07 22:17:3595 // 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; }
99
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_; }
104
brettw81db36a2014-08-29 22:52:36105 // 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; }
108
cmasonea426cf932014-09-13 00:51:47109 // 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;
114 }
115
Brett Wilson85423a02014-09-02 19:29:42116 bool testonly() const { return testonly_; }
117 void set_testonly(bool value) { testonly_ = value; }
118
[email protected]234be5202013-09-11 20:44:02119 // Compile-time extra dependencies.
[email protected]61a6fca2014-06-17 20:26:53120 const FileList& inputs() const { return inputs_; }
121 FileList& inputs() { return inputs_; }
[email protected]234be5202013-09-11 20:44:02122
brettwe903c0f2015-06-03 22:40:17123 // 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_; }
[email protected]96ea63d2013-07-30 10:17:07128
[email protected]ef348fe2014-05-01 18:31:31129 // 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;
135 }
[email protected]234be5202013-09-11 20:44:02136
brettwa09df112014-09-27 22:27:10137 // 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;
141
brettwc2e821a32014-09-17 01:07:14142 // Linked private dependencies.
143 const LabelTargetVector& private_deps() const { return private_deps_; }
144 LabelTargetVector& private_deps() { return private_deps_; }
145
146 // Linked public dependencies.
147 const LabelTargetVector& public_deps() const { return public_deps_; }
148 LabelTargetVector& public_deps() { return public_deps_; }
[email protected]96ea63d2013-07-30 10:17:07149
[email protected]4441041b2013-08-06 21:11:06150 // Non-linked dependencies.
brettwc2e821a32014-09-17 01:07:14151 const LabelTargetVector& data_deps() const { return data_deps_; }
152 LabelTargetVector& data_deps() { return data_deps_; }
[email protected]4441041b2013-08-06 21:11:06153
[email protected]08035b92014-05-13 19:40:56154 // List of configs that this class inherits settings from. Once a target is
brettwc2e821a32014-09-17 01:07:14155 // resolved, this will also list all-dependent and public configs.
[email protected]8fc56182014-08-06 21:44:33156 const UniqueVector<LabelConfigPair>& configs() const { return configs_; }
157 UniqueVector<LabelConfigPair>& configs() { return configs_; }
[email protected]96ea63d2013-07-30 10:17:07158
159 // List of configs that all dependencies (direct and indirect) of this
[email protected]62a610632013-08-21 23:45:23160 // 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.
[email protected]8fc56182014-08-06 21:44:33162 const UniqueVector<LabelConfigPair>& all_dependent_configs() const {
[email protected]96ea63d2013-07-30 10:17:07163 return all_dependent_configs_;
164 }
[email protected]8fc56182014-08-06 21:44:33165 UniqueVector<LabelConfigPair>& all_dependent_configs() {
[email protected]3c1274d2013-09-10 22:21:21166 return all_dependent_configs_;
167 }
[email protected]96ea63d2013-07-30 10:17:07168
169 // List of configs that targets depending directly on this one get. These
brettwc2e821a32014-09-17 01:07:14170 // configs are also added to this target.
171 const UniqueVector<LabelConfigPair>& public_configs() const {
172 return public_configs_;
[email protected]96ea63d2013-07-30 10:17:07173 }
brettwc2e821a32014-09-17 01:07:14174 UniqueVector<LabelConfigPair>& public_configs() {
175 return public_configs_;
[email protected]3c1274d2013-09-10 22:21:21176 }
[email protected]96ea63d2013-07-30 10:17:07177
brettw81db36a2014-08-29 22:52:36178 // Dependencies that can include files from this target.
179 const std::set<Label>& allow_circular_includes_from() const {
180 return allow_circular_includes_from_;
181 }
182 std::set<Label>& allow_circular_includes_from() {
183 return allow_circular_includes_from_;
184 }
185
brettw54d767532015-04-16 17:40:49186 const InheritedLibraries& inherited_libraries() const {
[email protected]96ea63d2013-07-30 10:17:07187 return inherited_libraries_;
188 }
189
190 // This config represents the configuration set directly on this target.
191 ConfigValues& config_values() { return config_values_; }
192 const ConfigValues& config_values() const { return config_values_; }
193
[email protected]2fbe1012014-03-20 17:59:15194 ActionValues& action_values() { return action_values_; }
195 const ActionValues& action_values() const { return action_values_; }
[email protected]c0822d7f2013-08-13 17:10:56196
[email protected]2ed04ae2013-10-07 20:17:16197 const OrderedSet<SourceDir>& all_lib_dirs() const { return all_lib_dirs_; }
198 const OrderedSet<std::string>& all_libs() const { return all_libs_; }
[email protected]3c1274d2013-09-10 22:21:21199
[email protected]ef348fe2014-05-01 18:31:31200 const std::set<const Target*>& recursive_hard_deps() const {
201 return recursive_hard_deps_;
202 }
203
[email protected]0dfcae72014-08-19 22:52:16204 // The toolchain is only known once this target is resolved (all if its
205 // dependencies are known). They will be null until then. Generally, this can
206 // only be used during target writing.
207 const Toolchain* toolchain() const { return toolchain_; }
208
209 // Sets the toolchain. The toolchain must include a tool for this target
210 // or the error will be set and the function will return false. Unusually,
211 // this function's "err" output is optional since this is commonly used
212 // frequently by unit tests which become needlessly verbose.
tfarina026335872015-01-13 03:35:39213 bool SetToolchain(const Toolchain* toolchain, Err* err = nullptr);
[email protected]0dfcae72014-08-19 22:52:16214
brettw56affab2015-06-04 22:01:03215 // Once this target has been resolved, all outputs from the target will be
216 // listed here. This will include things listed in the "outputs" for an
217 // action or a copy step, and the output library or executable file(s) from
218 // binary targets.
219 //
220 // It will NOT include stamp files and object files.
221 const std::vector<OutputFile>& computed_outputs() const {
222 return computed_outputs_;
223 }
224
[email protected]0dfcae72014-08-19 22:52:16225 // Returns outputs from this target. The link output file is the one that
226 // other targets link to when they depend on this target. This will only be
227 // valid for libraries and will be empty for all other target types.
228 //
229 // The dependency output file is the file that should be used to express
230 // a dependency on this one. It could be the same as the link output file
231 // (this will be the case for static libraries). For shared libraries it
232 // could be the same or different than the link output file, depending on the
233 // system. For actions this will be the stamp file.
234 //
235 // These are only known once the target is resolved and will be empty before
236 // that. This is a cache of the files to prevent every target that depends on
237 // a given library from recomputing the same pattern.
238 const OutputFile& link_output_file() const {
239 return link_output_file_;
240 }
241 const OutputFile& dependency_output_file() const {
242 return dependency_output_file_;
243 }
244
[email protected]96ea63d2013-07-30 10:17:07245 private:
brettw3dab5fe2015-06-29 23:00:15246 FRIEND_TEST_ALL_PREFIXES(Target, ResolvePrecompiledHeaders);
247
[email protected]ef348fe2014-05-01 18:31:31248 // Pulls necessary information from dependencies to this one when all
[email protected]3c1274d2013-09-10 22:21:21249 // dependencies have been resolved.
brettw54d767532015-04-16 17:40:49250 void PullDependentTarget(const Target* dep, bool is_public);
251 void PullDependentTargets();
[email protected]3c1274d2013-09-10 22:21:21252
[email protected]ef348fe2014-05-01 18:31:31253 // These each pull specific things from dependencies to this one when all
254 // deps have been resolved.
tfarinab445e7c2015-10-08 21:38:56255 void PullPublicConfigs();
256 void PullPublicConfigsFrom(const Target* from);
[email protected]ef348fe2014-05-01 18:31:31257 void PullRecursiveHardDeps();
[email protected]8499f51f2014-04-24 23:43:02258
[email protected]0dfcae72014-08-19 22:52:16259 // Fills the link and dependency output files when a target is resolved.
260 void FillOutputFiles();
261
brettw3dab5fe2015-06-29 23:00:15262 // Checks precompiled headers from configs and makes sure the resulting
263 // values are in config_values_.
264 bool ResolvePrecompiledHeaders(Err* err);
265
Brett Wilson85423a02014-09-02 19:29:42266 // Validates the given thing when a target is resolved.
267 bool CheckVisibility(Err* err) const;
268 bool CheckTestonly(Err* err) const;
cmasonea6fa7142014-09-18 23:47:38269 bool CheckNoNestedStaticLibs(Err* err) const;
brettw56affab2015-06-04 22:01:03270 void CheckSourcesGenerated() const;
271 void CheckSourceGenerated(const SourceFile& source) const;
Brett Wilson85423a02014-09-02 19:29:42272
[email protected]96ea63d2013-07-30 10:17:07273 OutputType output_type_;
[email protected]b5c19e32013-09-10 23:01:25274 std::string output_name_;
[email protected]b9473ee2014-02-28 21:51:10275 std::string output_extension_;
[email protected]96ea63d2013-07-30 10:17:07276
277 FileList sources_;
[email protected]126d8b52014-04-07 22:17:35278 bool all_headers_public_;
279 FileList public_headers_;
brettw81db36a2014-08-29 22:52:36280 bool check_includes_;
cmasonea426cf932014-09-13 00:51:47281 bool complete_static_lib_;
Brett Wilson85423a02014-09-02 19:29:42282 bool testonly_;
[email protected]61a6fca2014-06-17 20:26:53283 FileList inputs_;
brettwe903c0f2015-06-03 22:40:17284 std::vector<std::string> data_;
[email protected]3c1274d2013-09-10 22:21:21285
brettwc2e821a32014-09-17 01:07:14286 LabelTargetVector private_deps_;
287 LabelTargetVector public_deps_;
288 LabelTargetVector data_deps_;
[email protected]3c1274d2013-09-10 22:21:21289
[email protected]8fc56182014-08-06 21:44:33290 UniqueVector<LabelConfigPair> configs_;
291 UniqueVector<LabelConfigPair> all_dependent_configs_;
brettwc2e821a32014-09-17 01:07:14292 UniqueVector<LabelConfigPair> public_configs_;
[email protected]96ea63d2013-07-30 10:17:07293
brettw81db36a2014-08-29 22:52:36294 std::set<Label> allow_circular_includes_from_;
295
brettw54d767532015-04-16 17:40:49296 // Static libraries, shared libraries, and source sets from transitive deps
297 // that need to be linked.
298 InheritedLibraries inherited_libraries_;
[email protected]96ea63d2013-07-30 10:17:07299
[email protected]2ed04ae2013-10-07 20:17:16300 // These libs and dirs are inherited from statically linked deps and all
301 // configs applying to this target.
302 OrderedSet<SourceDir> all_lib_dirs_;
303 OrderedSet<std::string> all_libs_;
[email protected]3c1274d2013-09-10 22:21:21304
[email protected]ef348fe2014-05-01 18:31:31305 // All hard deps from this target and all dependencies. Filled in when this
306 // target is marked resolved. This will not include the current target.
307 std::set<const Target*> recursive_hard_deps_;
308
brettw3dab5fe2015-06-29 23:00:15309 // Used for all binary targets. The precompiled header values in this struct
310 // will be resolved to the ones to use for this target, if precompiled
311 // headers are used.
312 ConfigValues config_values_;
313
314 // Used for action[_foreach] targets.
315 ActionValues action_values_;
[email protected]96ea63d2013-07-30 10:17:07316
[email protected]0dfcae72014-08-19 22:52:16317 // Toolchain used by this target. Null until target is resolved.
318 const Toolchain* toolchain_;
319
brettw56affab2015-06-04 22:01:03320 // Output files. Empty until the target is resolved.
321 std::vector<OutputFile> computed_outputs_;
[email protected]0dfcae72014-08-19 22:52:16322 OutputFile link_output_file_;
323 OutputFile dependency_output_file_;
324
[email protected]96ea63d2013-07-30 10:17:07325 DISALLOW_COPY_AND_ASSIGN(Target);
326};
327
[email protected]96ea63d2013-07-30 10:17:07328#endif // TOOLS_GN_TARGET_H_