blob: 6b28f1f745d313928a6419468b38fc9014efe101 [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"
sdefresne1232aae2016-03-10 20:16:3616#include "tools/gn/bundle_data.h"
[email protected]96ea63d2013-07-30 10:17:0717#include "tools/gn/config_values.h"
brettw54d767532015-04-16 17:40:4918#include "tools/gn/inherited_libraries.h"
[email protected]96ea63d2013-07-30 10:17:0719#include "tools/gn/item.h"
brettw81aa4e8f2016-01-26 18:11:2820#include "tools/gn/label_pattern.h"
[email protected]68d1dd32013-11-01 21:59:5121#include "tools/gn/label_ptr.h"
agrievef865ab8f2015-12-22 03:04:0222#include "tools/gn/lib_file.h"
[email protected]b1e468f2013-09-10 22:58:0223#include "tools/gn/ordered_set.h"
[email protected]0dfcae72014-08-19 22:52:1624#include "tools/gn/output_file.h"
[email protected]96ea63d2013-07-30 10:17:0725#include "tools/gn/source_file.h"
brettw65e89852016-01-20 23:58:1226#include "tools/gn/toolchain.h"
[email protected]8fc56182014-08-06 21:44:3327#include "tools/gn/unique_vector.h"
[email protected]96ea63d2013-07-30 10:17:0728
brettwa09df112014-09-27 22:27:1029class DepsIteratorRange;
[email protected]96ea63d2013-07-30 10:17:0730class InputFile;
31class Settings;
32class Token;
[email protected]0dfcae72014-08-19 22:52:1633class Toolchain;
[email protected]96ea63d2013-07-30 10:17:0734
35class Target : public Item {
36 public:
37 enum OutputType {
[email protected]c0822d7f2013-08-13 17:10:5638 UNKNOWN,
39 GROUP,
[email protected]96ea63d2013-07-30 10:17:0740 EXECUTABLE,
41 SHARED_LIBRARY,
andybons027840d2015-10-14 18:49:3042 LOADABLE_MODULE,
[email protected]96ea63d2013-07-30 10:17:0743 STATIC_LIBRARY,
[email protected]7ff2f5d2013-10-08 21:30:3444 SOURCE_SET,
[email protected]96ea63d2013-07-30 10:17:0745 COPY_FILES,
[email protected]2fbe1012014-03-20 17:59:1546 ACTION,
47 ACTION_FOREACH,
sdefresnedb6b992c2016-03-04 16:32:1948 BUNDLE_DATA,
sdefresne1232aae2016-03-10 20:16:3649 CREATE_BUNDLE,
[email protected]96ea63d2013-07-30 10:17:0750 };
brettwa09df112014-09-27 22:27:1051
52 enum DepsIterationType {
53 DEPS_ALL, // Iterates through all public, private, and data deps.
54 DEPS_LINKED, // Iterates through all non-data dependencies.
55 };
56
[email protected]96ea63d2013-07-30 10:17:0757 typedef std::vector<SourceFile> FileList;
58 typedef std::vector<std::string> StringVector;
59
60 Target(const Settings* settings, const Label& label);
Viet-Trung Luu50df3ed2014-10-22 05:00:1261 ~Target() override;
[email protected]96ea63d2013-07-30 10:17:0762
[email protected]ac1128e2013-08-23 00:26:5663 // Returns a string naming the output type.
64 static const char* GetStringForOutputType(OutputType type);
65
[email protected]96ea63d2013-07-30 10:17:0766 // Item overrides.
Viet-Trung Luu50df3ed2014-10-22 05:00:1267 Target* AsTarget() override;
68 const Target* AsTarget() const override;
69 bool OnResolved(Err* err) override;
[email protected]96ea63d2013-07-30 10:17:0770
[email protected]96ea63d2013-07-30 10:17:0771 OutputType output_type() const { return output_type_; }
72 void set_output_type(OutputType t) { output_type_ = t; }
73
brettw65e89852016-01-20 23:58:1274 // True for targets that compile source code (all types of libaries and
75 // executables).
76 bool IsBinary() const;
77
cmasonea426cf932014-09-13 00:51:4778 // Can be linked into other targets.
[email protected]96ea63d2013-07-30 10:17:0779 bool IsLinkable() const;
80
brettw374d4fd2015-11-14 00:57:0381 // True if the target links dependencies rather than propogated up the graph.
82 // This is also true of action and copy steps even though they don't link
83 // dependencies, because they also don't propogate libraries up.
cmasonea426cf932014-09-13 00:51:4784 bool IsFinal() const;
85
[email protected]b5c19e32013-09-10 23:01:2586 // Will be the empty string to use the target label as the output name.
[email protected]0dfcae72014-08-19 22:52:1687 // See GetComputedOutputName().
[email protected]b5c19e32013-09-10 23:01:2588 const std::string& output_name() const { return output_name_; }
89 void set_output_name(const std::string& name) { output_name_ = name; }
90
[email protected]0dfcae72014-08-19 22:52:1691 // Returns the output name for this target, which is the output_name if
brettw2a642a12016-04-07 23:51:1292 // specified, or the target label if not.
[email protected]0dfcae72014-08-19 22:52:1693 //
94 // Because this depends on the tool for this target, the toolchain must
95 // have been set before calling.
brettw2a642a12016-04-07 23:51:1296 std::string GetComputedOutputName() const;
[email protected]0dfcae72014-08-19 22:52:1697
brettw2a642a12016-04-07 23:51:1298 bool output_prefix_override() const { return output_prefix_override_; }
99 void set_output_prefix_override(bool prefix_override) {
100 output_prefix_override_ = prefix_override;
101 }
102
brettwf2eba7f2016-04-14 18:14:45103 // Desired output directory for the final output. This will be used for
104 // the {{output_dir}} substitution in the tool if it is specified. If
105 // is_null, the tool default will be used.
106 const SourceDir& output_dir() const { return output_dir_; }
107 void set_output_dir(const SourceDir& dir) { output_dir_ = dir; }
108
brettw2a642a12016-04-07 23:51:12109 // The output extension is really a tri-state: unset (output_extension_set
110 // is false and the string is empty, meaning the default extension should be
111 // used), the output extension is set but empty (output should have no
112 // extension) and the output extension is set but nonempty (use the given
113 // extension).
[email protected]b9473ee2014-02-28 21:51:10114 const std::string& output_extension() const { return output_extension_; }
115 void set_output_extension(const std::string& extension) {
116 output_extension_ = extension;
brettw2a642a12016-04-07 23:51:12117 output_extension_set_ = true;
118 }
119 bool output_extension_set() const {
120 return output_extension_set_;
[email protected]b9473ee2014-02-28 21:51:10121 }
122
[email protected]96ea63d2013-07-30 10:17:07123 const FileList& sources() const { return sources_; }
[email protected]3c1274d2013-09-10 22:21:21124 FileList& sources() { return sources_; }
[email protected]96ea63d2013-07-30 10:17:07125
[email protected]126d8b52014-04-07 22:17:35126 // Set to true when all sources are public. This is the default. In this case
127 // the public headers list should be empty.
128 bool all_headers_public() const { return all_headers_public_; }
129 void set_all_headers_public(bool p) { all_headers_public_ = p; }
130
131 // When all_headers_public is false, this is the list of public headers. It
132 // could be empty which would mean no headers are public.
133 const FileList& public_headers() const { return public_headers_; }
134 FileList& public_headers() { return public_headers_; }
135
brettw81db36a2014-08-29 22:52:36136 // Whether this target's includes should be checked by "gn check".
137 bool check_includes() const { return check_includes_; }
138 void set_check_includes(bool ci) { check_includes_ = ci; }
139
cmasonea426cf932014-09-13 00:51:47140 // Whether this static_library target should have code linked in.
141 bool complete_static_lib() const { return complete_static_lib_; }
142 void set_complete_static_lib(bool complete) {
143 DCHECK_EQ(STATIC_LIBRARY, output_type_);
144 complete_static_lib_ = complete;
145 }
146
Brett Wilson85423a02014-09-02 19:29:42147 bool testonly() const { return testonly_; }
148 void set_testonly(bool value) { testonly_ = value; }
149
agrieve0f8ad422016-03-31 22:00:07150 OutputFile write_runtime_deps_output() const {
151 return write_runtime_deps_output_;
152 }
153 void set_write_runtime_deps_output(const OutputFile& value) {
154 write_runtime_deps_output_ = value;
155 }
156
[email protected]234be5202013-09-11 20:44:02157 // Compile-time extra dependencies.
[email protected]61a6fca2014-06-17 20:26:53158 const FileList& inputs() const { return inputs_; }
159 FileList& inputs() { return inputs_; }
[email protected]234be5202013-09-11 20:44:02160
brettwe903c0f2015-06-03 22:40:17161 // Runtime dependencies. These are "file-like things" that can either be
162 // directories or files. They do not need to exist, these are just passed as
163 // runtime dependencies to external test systems as necessary.
164 const std::vector<std::string>& data() const { return data_; }
165 std::vector<std::string>& data() { return data_; }
[email protected]96ea63d2013-07-30 10:17:07166
sdefresne1232aae2016-03-10 20:16:36167 // Information about the bundle. Only valid for CREATE_BUNDLE target after
168 // they have been resolved.
169 const BundleData& bundle_data() const { return bundle_data_; }
170 BundleData& bundle_data() { return bundle_data_; }
171
[email protected]ef348fe2014-05-01 18:31:31172 // Returns true if targets depending on this one should have an order
173 // dependency.
174 bool hard_dep() const {
175 return output_type_ == ACTION ||
176 output_type_ == ACTION_FOREACH ||
sdefresne1232aae2016-03-10 20:16:36177 output_type_ == COPY_FILES ||
178 output_type_ == CREATE_BUNDLE;
[email protected]ef348fe2014-05-01 18:31:31179 }
[email protected]234be5202013-09-11 20:44:02180
brettwa09df112014-09-27 22:27:10181 // Returns the iterator range which can be used in range-based for loops
182 // to iterate over multiple types of deps in one loop:
183 // for (const auto& pair : target->GetDeps(Target::DEPS_ALL)) ...
184 DepsIteratorRange GetDeps(DepsIterationType type) const;
185
brettwc2e821a32014-09-17 01:07:14186 // Linked private dependencies.
187 const LabelTargetVector& private_deps() const { return private_deps_; }
188 LabelTargetVector& private_deps() { return private_deps_; }
189
190 // Linked public dependencies.
191 const LabelTargetVector& public_deps() const { return public_deps_; }
192 LabelTargetVector& public_deps() { return public_deps_; }
[email protected]96ea63d2013-07-30 10:17:07193
[email protected]4441041b2013-08-06 21:11:06194 // Non-linked dependencies.
brettwc2e821a32014-09-17 01:07:14195 const LabelTargetVector& data_deps() const { return data_deps_; }
196 LabelTargetVector& data_deps() { return data_deps_; }
[email protected]4441041b2013-08-06 21:11:06197
[email protected]08035b92014-05-13 19:40:56198 // List of configs that this class inherits settings from. Once a target is
brettwc2e821a32014-09-17 01:07:14199 // resolved, this will also list all-dependent and public configs.
[email protected]8fc56182014-08-06 21:44:33200 const UniqueVector<LabelConfigPair>& configs() const { return configs_; }
201 UniqueVector<LabelConfigPair>& configs() { return configs_; }
[email protected]96ea63d2013-07-30 10:17:07202
203 // List of configs that all dependencies (direct and indirect) of this
[email protected]62a610632013-08-21 23:45:23204 // target get. These configs are not added to this target. Note that due
205 // to the way this is computed, there may be duplicates in this list.
[email protected]8fc56182014-08-06 21:44:33206 const UniqueVector<LabelConfigPair>& all_dependent_configs() const {
[email protected]96ea63d2013-07-30 10:17:07207 return all_dependent_configs_;
208 }
[email protected]8fc56182014-08-06 21:44:33209 UniqueVector<LabelConfigPair>& all_dependent_configs() {
[email protected]3c1274d2013-09-10 22:21:21210 return all_dependent_configs_;
211 }
[email protected]96ea63d2013-07-30 10:17:07212
213 // List of configs that targets depending directly on this one get. These
brettwc2e821a32014-09-17 01:07:14214 // configs are also added to this target.
215 const UniqueVector<LabelConfigPair>& public_configs() const {
216 return public_configs_;
[email protected]96ea63d2013-07-30 10:17:07217 }
brettwc2e821a32014-09-17 01:07:14218 UniqueVector<LabelConfigPair>& public_configs() {
219 return public_configs_;
[email protected]3c1274d2013-09-10 22:21:21220 }
[email protected]96ea63d2013-07-30 10:17:07221
brettw81db36a2014-08-29 22:52:36222 // Dependencies that can include files from this target.
223 const std::set<Label>& allow_circular_includes_from() const {
224 return allow_circular_includes_from_;
225 }
226 std::set<Label>& allow_circular_includes_from() {
227 return allow_circular_includes_from_;
228 }
229
brettw54d767532015-04-16 17:40:49230 const InheritedLibraries& inherited_libraries() const {
[email protected]96ea63d2013-07-30 10:17:07231 return inherited_libraries_;
232 }
233
234 // This config represents the configuration set directly on this target.
235 ConfigValues& config_values() { return config_values_; }
236 const ConfigValues& config_values() const { return config_values_; }
237
[email protected]2fbe1012014-03-20 17:59:15238 ActionValues& action_values() { return action_values_; }
239 const ActionValues& action_values() const { return action_values_; }
[email protected]c0822d7f2013-08-13 17:10:56240
[email protected]2ed04ae2013-10-07 20:17:16241 const OrderedSet<SourceDir>& all_lib_dirs() const { return all_lib_dirs_; }
agrievef865ab8f2015-12-22 03:04:02242 const OrderedSet<LibFile>& all_libs() const { return all_libs_; }
[email protected]3c1274d2013-09-10 22:21:21243
[email protected]ef348fe2014-05-01 18:31:31244 const std::set<const Target*>& recursive_hard_deps() const {
245 return recursive_hard_deps_;
246 }
247
brettw81aa4e8f2016-01-26 18:11:28248 std::vector<LabelPattern>& assert_no_deps() {
249 return assert_no_deps_;
250 }
251 const std::vector<LabelPattern>& assert_no_deps() const {
252 return assert_no_deps_;
253 }
254
[email protected]0dfcae72014-08-19 22:52:16255 // The toolchain is only known once this target is resolved (all if its
256 // dependencies are known). They will be null until then. Generally, this can
257 // only be used during target writing.
258 const Toolchain* toolchain() const { return toolchain_; }
259
260 // Sets the toolchain. The toolchain must include a tool for this target
261 // or the error will be set and the function will return false. Unusually,
262 // this function's "err" output is optional since this is commonly used
263 // frequently by unit tests which become needlessly verbose.
tfarina026335872015-01-13 03:35:39264 bool SetToolchain(const Toolchain* toolchain, Err* err = nullptr);
[email protected]0dfcae72014-08-19 22:52:16265
brettw56affab2015-06-04 22:01:03266 // Once this target has been resolved, all outputs from the target will be
267 // listed here. This will include things listed in the "outputs" for an
268 // action or a copy step, and the output library or executable file(s) from
269 // binary targets.
270 //
271 // It will NOT include stamp files and object files.
272 const std::vector<OutputFile>& computed_outputs() const {
273 return computed_outputs_;
274 }
275
[email protected]0dfcae72014-08-19 22:52:16276 // Returns outputs from this target. The link output file is the one that
277 // other targets link to when they depend on this target. This will only be
278 // valid for libraries and will be empty for all other target types.
279 //
280 // The dependency output file is the file that should be used to express
281 // a dependency on this one. It could be the same as the link output file
282 // (this will be the case for static libraries). For shared libraries it
283 // could be the same or different than the link output file, depending on the
284 // system. For actions this will be the stamp file.
285 //
286 // These are only known once the target is resolved and will be empty before
287 // that. This is a cache of the files to prevent every target that depends on
288 // a given library from recomputing the same pattern.
289 const OutputFile& link_output_file() const {
290 return link_output_file_;
291 }
292 const OutputFile& dependency_output_file() const {
293 return dependency_output_file_;
294 }
brettw15e0e74f2016-07-26 18:00:24295
296 // The subset of computed_outputs that are considered runtime outputs.
297 const std::vector<OutputFile>& runtime_outputs() const {
298 return runtime_outputs_;
Nico Webere3398c12016-02-11 02:25:20299 }
[email protected]0dfcae72014-08-19 22:52:16300
brettw65e89852016-01-20 23:58:12301 // Computes the set of output files resulting from compiling the given source
302 // file. If the file can be compiled and the tool exists, fills the outputs
303 // in and writes the tool type to computed_tool_type. If the file is not
304 // compilable, returns false.
305 //
306 // The function can succeed with a "NONE" tool type for object files which
307 // are just passed to the output. The output will always be overwritten, not
308 // appended to.
309 bool GetOutputFilesForSource(const SourceFile& source,
310 Toolchain::ToolType* computed_tool_type,
311 std::vector<OutputFile>* outputs) const;
312
[email protected]96ea63d2013-07-30 10:17:07313 private:
brettw3dab5fe2015-06-29 23:00:15314 FRIEND_TEST_ALL_PREFIXES(Target, ResolvePrecompiledHeaders);
315
[email protected]ef348fe2014-05-01 18:31:31316 // Pulls necessary information from dependencies to this one when all
[email protected]3c1274d2013-09-10 22:21:21317 // dependencies have been resolved.
brettw567cb6882015-12-11 18:38:26318 void PullDependentTargetConfigs();
319 void PullDependentTargetLibsFrom(const Target* dep, bool is_public);
320 void PullDependentTargetLibs();
[email protected]ef348fe2014-05-01 18:31:31321 void PullRecursiveHardDeps();
sdefresne1232aae2016-03-10 20:16:36322 void PullRecursiveBundleData();
[email protected]8499f51f2014-04-24 23:43:02323
[email protected]0dfcae72014-08-19 22:52:16324 // Fills the link and dependency output files when a target is resolved.
325 void FillOutputFiles();
326
brettw3dab5fe2015-06-29 23:00:15327 // Checks precompiled headers from configs and makes sure the resulting
328 // values are in config_values_.
329 bool ResolvePrecompiledHeaders(Err* err);
330
Brett Wilson85423a02014-09-02 19:29:42331 // Validates the given thing when a target is resolved.
332 bool CheckVisibility(Err* err) const;
333 bool CheckTestonly(Err* err) const;
brettw81aa4e8f2016-01-26 18:11:28334 bool CheckAssertNoDeps(Err* err) const;
brettw56affab2015-06-04 22:01:03335 void CheckSourcesGenerated() const;
336 void CheckSourceGenerated(const SourceFile& source) const;
Brett Wilson85423a02014-09-02 19:29:42337
[email protected]96ea63d2013-07-30 10:17:07338 OutputType output_type_;
[email protected]b5c19e32013-09-10 23:01:25339 std::string output_name_;
brettw2a642a12016-04-07 23:51:12340 bool output_prefix_override_;
brettwf2eba7f2016-04-14 18:14:45341 SourceDir output_dir_;
[email protected]b9473ee2014-02-28 21:51:10342 std::string output_extension_;
brettw2a642a12016-04-07 23:51:12343 bool output_extension_set_;
[email protected]96ea63d2013-07-30 10:17:07344
345 FileList sources_;
[email protected]126d8b52014-04-07 22:17:35346 bool all_headers_public_;
347 FileList public_headers_;
brettw81db36a2014-08-29 22:52:36348 bool check_includes_;
cmasonea426cf932014-09-13 00:51:47349 bool complete_static_lib_;
Brett Wilson85423a02014-09-02 19:29:42350 bool testonly_;
[email protected]61a6fca2014-06-17 20:26:53351 FileList inputs_;
brettwe903c0f2015-06-03 22:40:17352 std::vector<std::string> data_;
sdefresne1232aae2016-03-10 20:16:36353 BundleData bundle_data_;
agrieve0f8ad422016-03-31 22:00:07354 OutputFile write_runtime_deps_output_;
[email protected]3c1274d2013-09-10 22:21:21355
brettwc2e821a32014-09-17 01:07:14356 LabelTargetVector private_deps_;
357 LabelTargetVector public_deps_;
358 LabelTargetVector data_deps_;
[email protected]3c1274d2013-09-10 22:21:21359
brettw567cb6882015-12-11 18:38:26360 // See getters for more info.
[email protected]8fc56182014-08-06 21:44:33361 UniqueVector<LabelConfigPair> configs_;
362 UniqueVector<LabelConfigPair> all_dependent_configs_;
brettwc2e821a32014-09-17 01:07:14363 UniqueVector<LabelConfigPair> public_configs_;
[email protected]96ea63d2013-07-30 10:17:07364
brettw81db36a2014-08-29 22:52:36365 std::set<Label> allow_circular_includes_from_;
366
brettw54d767532015-04-16 17:40:49367 // Static libraries, shared libraries, and source sets from transitive deps
368 // that need to be linked.
369 InheritedLibraries inherited_libraries_;
[email protected]96ea63d2013-07-30 10:17:07370
[email protected]2ed04ae2013-10-07 20:17:16371 // These libs and dirs are inherited from statically linked deps and all
372 // configs applying to this target.
373 OrderedSet<SourceDir> all_lib_dirs_;
agrievef865ab8f2015-12-22 03:04:02374 OrderedSet<LibFile> all_libs_;
[email protected]3c1274d2013-09-10 22:21:21375
[email protected]ef348fe2014-05-01 18:31:31376 // All hard deps from this target and all dependencies. Filled in when this
377 // target is marked resolved. This will not include the current target.
378 std::set<const Target*> recursive_hard_deps_;
379
brettw81aa4e8f2016-01-26 18:11:28380 std::vector<LabelPattern> assert_no_deps_;
381
brettw3dab5fe2015-06-29 23:00:15382 // Used for all binary targets. The precompiled header values in this struct
383 // will be resolved to the ones to use for this target, if precompiled
384 // headers are used.
385 ConfigValues config_values_;
386
387 // Used for action[_foreach] targets.
388 ActionValues action_values_;
[email protected]96ea63d2013-07-30 10:17:07389
[email protected]0dfcae72014-08-19 22:52:16390 // Toolchain used by this target. Null until target is resolved.
391 const Toolchain* toolchain_;
392
brettw56affab2015-06-04 22:01:03393 // Output files. Empty until the target is resolved.
394 std::vector<OutputFile> computed_outputs_;
[email protected]0dfcae72014-08-19 22:52:16395 OutputFile link_output_file_;
396 OutputFile dependency_output_file_;
brettw15e0e74f2016-07-26 18:00:24397 std::vector<OutputFile> runtime_outputs_;
[email protected]0dfcae72014-08-19 22:52:16398
[email protected]96ea63d2013-07-30 10:17:07399 DISALLOW_COPY_AND_ASSIGN(Target);
400};
401
[email protected]96ea63d2013-07-30 10:17:07402#endif // TOOLS_GN_TARGET_H_