blob: 9dd306420a4f1c9942d6a7e436fa9093458d6dc2 [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
12#include "base/basictypes.h"
13#include "base/compiler_specific.h"
14#include "base/logging.h"
15#include "base/strings/string_piece.h"
16#include "base/synchronization/lock.h"
[email protected]2fbe1012014-03-20 17:59:1517#include "tools/gn/action_values.h"
[email protected]96ea63d2013-07-30 10:17:0718#include "tools/gn/config_values.h"
19#include "tools/gn/item.h"
[email protected]68d1dd32013-11-01 21:59:5120#include "tools/gn/label_ptr.h"
[email protected]b1e468f2013-09-10 22:58:0221#include "tools/gn/ordered_set.h"
[email protected]0dfcae72014-08-19 22:52:1622#include "tools/gn/output_file.h"
[email protected]96ea63d2013-07-30 10:17:0723#include "tools/gn/source_file.h"
[email protected]8fc56182014-08-06 21:44:3324#include "tools/gn/unique_vector.h"
[email protected]96ea63d2013-07-30 10:17:0725
26class 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,
38 STATIC_LIBRARY,
[email protected]7ff2f5d2013-10-08 21:30:3439 SOURCE_SET,
[email protected]96ea63d2013-07-30 10:17:0740 COPY_FILES,
[email protected]2fbe1012014-03-20 17:59:1541 ACTION,
42 ACTION_FOREACH,
[email protected]96ea63d2013-07-30 10:17:0743 };
44 typedef std::vector<SourceFile> FileList;
45 typedef std::vector<std::string> StringVector;
46
47 Target(const Settings* settings, const Label& label);
48 virtual ~Target();
49
[email protected]ac1128e2013-08-23 00:26:5650 // Returns a string naming the output type.
51 static const char* GetStringForOutputType(OutputType type);
52
[email protected]96ea63d2013-07-30 10:17:0753 // Item overrides.
54 virtual Target* AsTarget() OVERRIDE;
55 virtual const Target* AsTarget() const OVERRIDE;
Brett Wilson85423a02014-09-02 19:29:4256 virtual bool OnResolved(Err* err) OVERRIDE;
[email protected]96ea63d2013-07-30 10:17:0757
[email protected]96ea63d2013-07-30 10:17:0758 OutputType output_type() const { return output_type_; }
59 void set_output_type(OutputType t) { output_type_ = t; }
60
cmasonea426cf932014-09-13 00:51:4761 // Can be linked into other targets.
[email protected]96ea63d2013-07-30 10:17:0762 bool IsLinkable() const;
63
cmasonea426cf932014-09-13 00:51:4764 // Can have dependencies linked in.
65 bool IsFinal() const;
66
[email protected]b5c19e32013-09-10 23:01:2567 // Will be the empty string to use the target label as the output name.
[email protected]0dfcae72014-08-19 22:52:1668 // See GetComputedOutputName().
[email protected]b5c19e32013-09-10 23:01:2569 const std::string& output_name() const { return output_name_; }
70 void set_output_name(const std::string& name) { output_name_ = name; }
71
[email protected]0dfcae72014-08-19 22:52:1672 // Returns the output name for this target, which is the output_name if
73 // specified, or the target label if not. If the flag is set, it will also
74 // include any output prefix specified on the tool (often "lib" on Linux).
75 //
76 // Because this depends on the tool for this target, the toolchain must
77 // have been set before calling.
78 std::string GetComputedOutputName(bool include_prefix) const;
79
[email protected]b9473ee2014-02-28 21:51:1080 const std::string& output_extension() const { return output_extension_; }
81 void set_output_extension(const std::string& extension) {
82 output_extension_ = extension;
83 }
84
[email protected]96ea63d2013-07-30 10:17:0785 const FileList& sources() const { return sources_; }
[email protected]3c1274d2013-09-10 22:21:2186 FileList& sources() { return sources_; }
[email protected]96ea63d2013-07-30 10:17:0787
[email protected]126d8b52014-04-07 22:17:3588 // Set to true when all sources are public. This is the default. In this case
89 // the public headers list should be empty.
90 bool all_headers_public() const { return all_headers_public_; }
91 void set_all_headers_public(bool p) { all_headers_public_ = p; }
92
93 // When all_headers_public is false, this is the list of public headers. It
94 // could be empty which would mean no headers are public.
95 const FileList& public_headers() const { return public_headers_; }
96 FileList& public_headers() { return public_headers_; }
97
brettw81db36a2014-08-29 22:52:3698 // Whether this target's includes should be checked by "gn check".
99 bool check_includes() const { return check_includes_; }
100 void set_check_includes(bool ci) { check_includes_ = ci; }
101
cmasonea426cf932014-09-13 00:51:47102 // Whether this static_library target should have code linked in.
103 bool complete_static_lib() const { return complete_static_lib_; }
104 void set_complete_static_lib(bool complete) {
105 DCHECK_EQ(STATIC_LIBRARY, output_type_);
106 complete_static_lib_ = complete;
107 }
108
Brett Wilson85423a02014-09-02 19:29:42109 bool testonly() const { return testonly_; }
110 void set_testonly(bool value) { testonly_ = value; }
111
[email protected]234be5202013-09-11 20:44:02112 // Compile-time extra dependencies.
[email protected]61a6fca2014-06-17 20:26:53113 const FileList& inputs() const { return inputs_; }
114 FileList& inputs() { return inputs_; }
[email protected]234be5202013-09-11 20:44:02115
116 // Runtime dependencies.
[email protected]96ea63d2013-07-30 10:17:07117 const FileList& data() const { return data_; }
[email protected]3c1274d2013-09-10 22:21:21118 FileList& data() { return data_; }
[email protected]96ea63d2013-07-30 10:17:07119
[email protected]ef348fe2014-05-01 18:31:31120 // Returns true if targets depending on this one should have an order
121 // dependency.
122 bool hard_dep() const {
123 return output_type_ == ACTION ||
124 output_type_ == ACTION_FOREACH ||
125 output_type_ == COPY_FILES;
126 }
[email protected]234be5202013-09-11 20:44:02127
brettwc2e821a32014-09-17 01:07:14128 // Linked private dependencies.
129 const LabelTargetVector& private_deps() const { return private_deps_; }
130 LabelTargetVector& private_deps() { return private_deps_; }
131
132 // Linked public dependencies.
133 const LabelTargetVector& public_deps() const { return public_deps_; }
134 LabelTargetVector& public_deps() { return public_deps_; }
[email protected]96ea63d2013-07-30 10:17:07135
[email protected]4441041b2013-08-06 21:11:06136 // Non-linked dependencies.
brettwc2e821a32014-09-17 01:07:14137 const LabelTargetVector& data_deps() const { return data_deps_; }
138 LabelTargetVector& data_deps() { return data_deps_; }
[email protected]4441041b2013-08-06 21:11:06139
[email protected]08035b92014-05-13 19:40:56140 // List of configs that this class inherits settings from. Once a target is
brettwc2e821a32014-09-17 01:07:14141 // resolved, this will also list all-dependent and public configs.
[email protected]8fc56182014-08-06 21:44:33142 const UniqueVector<LabelConfigPair>& configs() const { return configs_; }
143 UniqueVector<LabelConfigPair>& configs() { return configs_; }
[email protected]96ea63d2013-07-30 10:17:07144
145 // List of configs that all dependencies (direct and indirect) of this
[email protected]62a610632013-08-21 23:45:23146 // target get. These configs are not added to this target. Note that due
147 // to the way this is computed, there may be duplicates in this list.
[email protected]8fc56182014-08-06 21:44:33148 const UniqueVector<LabelConfigPair>& all_dependent_configs() const {
[email protected]96ea63d2013-07-30 10:17:07149 return all_dependent_configs_;
150 }
[email protected]8fc56182014-08-06 21:44:33151 UniqueVector<LabelConfigPair>& all_dependent_configs() {
[email protected]3c1274d2013-09-10 22:21:21152 return all_dependent_configs_;
153 }
[email protected]96ea63d2013-07-30 10:17:07154
155 // List of configs that targets depending directly on this one get. These
brettwc2e821a32014-09-17 01:07:14156 // configs are also added to this target.
157 const UniqueVector<LabelConfigPair>& public_configs() const {
158 return public_configs_;
[email protected]96ea63d2013-07-30 10:17:07159 }
brettwc2e821a32014-09-17 01:07:14160 UniqueVector<LabelConfigPair>& public_configs() {
161 return public_configs_;
[email protected]3c1274d2013-09-10 22:21:21162 }
[email protected]96ea63d2013-07-30 10:17:07163
brettwc2e821a32014-09-17 01:07:14164 // A list of a subset of deps where we'll re-export public_configs as
165 // public_configs of this target.
[email protected]8fc56182014-08-06 21:44:33166 const UniqueVector<LabelTargetPair>& forward_dependent_configs() const {
[email protected]62a610632013-08-21 23:45:23167 return forward_dependent_configs_;
168 }
[email protected]8fc56182014-08-06 21:44:33169 UniqueVector<LabelTargetPair>& forward_dependent_configs() {
[email protected]3c1274d2013-09-10 22:21:21170 return forward_dependent_configs_;
171 }
[email protected]62a610632013-08-21 23:45:23172
brettw81db36a2014-08-29 22:52:36173 // Dependencies that can include files from this target.
174 const std::set<Label>& allow_circular_includes_from() const {
175 return allow_circular_includes_from_;
176 }
177 std::set<Label>& allow_circular_includes_from() {
178 return allow_circular_includes_from_;
179 }
180
[email protected]8fc56182014-08-06 21:44:33181 const UniqueVector<const Target*>& inherited_libraries() const {
[email protected]96ea63d2013-07-30 10:17:07182 return inherited_libraries_;
183 }
184
185 // This config represents the configuration set directly on this target.
186 ConfigValues& config_values() { return config_values_; }
187 const ConfigValues& config_values() const { return config_values_; }
188
[email protected]2fbe1012014-03-20 17:59:15189 ActionValues& action_values() { return action_values_; }
190 const ActionValues& action_values() const { return action_values_; }
[email protected]c0822d7f2013-08-13 17:10:56191
[email protected]2ed04ae2013-10-07 20:17:16192 const OrderedSet<SourceDir>& all_lib_dirs() const { return all_lib_dirs_; }
193 const OrderedSet<std::string>& all_libs() const { return all_libs_; }
[email protected]3c1274d2013-09-10 22:21:21194
[email protected]ef348fe2014-05-01 18:31:31195 const std::set<const Target*>& recursive_hard_deps() const {
196 return recursive_hard_deps_;
197 }
198
[email protected]0dfcae72014-08-19 22:52:16199 // The toolchain is only known once this target is resolved (all if its
200 // dependencies are known). They will be null until then. Generally, this can
201 // only be used during target writing.
202 const Toolchain* toolchain() const { return toolchain_; }
203
204 // Sets the toolchain. The toolchain must include a tool for this target
205 // or the error will be set and the function will return false. Unusually,
206 // this function's "err" output is optional since this is commonly used
207 // frequently by unit tests which become needlessly verbose.
208 bool SetToolchain(const Toolchain* toolchain, Err* err = NULL);
209
210 // Returns outputs from this target. The link output file is the one that
211 // other targets link to when they depend on this target. This will only be
212 // valid for libraries and will be empty for all other target types.
213 //
214 // The dependency output file is the file that should be used to express
215 // a dependency on this one. It could be the same as the link output file
216 // (this will be the case for static libraries). For shared libraries it
217 // could be the same or different than the link output file, depending on the
218 // system. For actions this will be the stamp file.
219 //
220 // These are only known once the target is resolved and will be empty before
221 // that. This is a cache of the files to prevent every target that depends on
222 // a given library from recomputing the same pattern.
223 const OutputFile& link_output_file() const {
224 return link_output_file_;
225 }
226 const OutputFile& dependency_output_file() const {
227 return dependency_output_file_;
228 }
229
[email protected]96ea63d2013-07-30 10:17:07230 private:
[email protected]ef348fe2014-05-01 18:31:31231 // Pulls necessary information from dependencies to this one when all
[email protected]3c1274d2013-09-10 22:21:21232 // dependencies have been resolved.
[email protected]8fc56182014-08-06 21:44:33233 void PullDependentTargetInfo();
[email protected]3c1274d2013-09-10 22:21:21234
[email protected]ef348fe2014-05-01 18:31:31235 // These each pull specific things from dependencies to this one when all
236 // deps have been resolved.
[email protected]8499f51f2014-04-24 23:43:02237 void PullForwardedDependentConfigs();
brettwc2e821a32014-09-17 01:07:14238 void PullForwardedDependentConfigsFrom(const Target* from);
[email protected]ef348fe2014-05-01 18:31:31239 void PullRecursiveHardDeps();
[email protected]8499f51f2014-04-24 23:43:02240
[email protected]0dfcae72014-08-19 22:52:16241 // Fills the link and dependency output files when a target is resolved.
242 void FillOutputFiles();
243
Brett Wilson85423a02014-09-02 19:29:42244 // Validates the given thing when a target is resolved.
245 bool CheckVisibility(Err* err) const;
246 bool CheckTestonly(Err* err) const;
247
[email protected]96ea63d2013-07-30 10:17:07248 OutputType output_type_;
[email protected]b5c19e32013-09-10 23:01:25249 std::string output_name_;
[email protected]b9473ee2014-02-28 21:51:10250 std::string output_extension_;
[email protected]96ea63d2013-07-30 10:17:07251
252 FileList sources_;
[email protected]126d8b52014-04-07 22:17:35253 bool all_headers_public_;
254 FileList public_headers_;
brettw81db36a2014-08-29 22:52:36255 bool check_includes_;
cmasonea426cf932014-09-13 00:51:47256 bool complete_static_lib_;
Brett Wilson85423a02014-09-02 19:29:42257 bool testonly_;
[email protected]61a6fca2014-06-17 20:26:53258 FileList inputs_;
[email protected]96ea63d2013-07-30 10:17:07259 FileList data_;
[email protected]3c1274d2013-09-10 22:21:21260
[email protected]234be5202013-09-11 20:44:02261 bool hard_dep_;
262
brettwc2e821a32014-09-17 01:07:14263 LabelTargetVector private_deps_;
264 LabelTargetVector public_deps_;
265 LabelTargetVector data_deps_;
[email protected]3c1274d2013-09-10 22:21:21266
[email protected]8fc56182014-08-06 21:44:33267 UniqueVector<LabelConfigPair> configs_;
268 UniqueVector<LabelConfigPair> all_dependent_configs_;
brettwc2e821a32014-09-17 01:07:14269 UniqueVector<LabelConfigPair> public_configs_;
[email protected]8fc56182014-08-06 21:44:33270 UniqueVector<LabelTargetPair> forward_dependent_configs_;
[email protected]96ea63d2013-07-30 10:17:07271
brettw81db36a2014-08-29 22:52:36272 std::set<Label> allow_circular_includes_from_;
273
[email protected]0a79fe42013-08-29 21:06:26274 bool external_;
275
[email protected]7ff2f5d2013-10-08 21:30:34276 // Static libraries and source sets from transitive deps. These things need
[email protected]678b5b942014-05-30 16:30:21277 // to be linked only with the end target (executable, shared library). Source
278 // sets do not get pushed beyond static library boundaries, and neither
279 // source sets nor static libraries get pushed beyond sahred library
280 // boundaries.
[email protected]8fc56182014-08-06 21:44:33281 UniqueVector<const Target*> inherited_libraries_;
[email protected]96ea63d2013-07-30 10:17:07282
[email protected]2ed04ae2013-10-07 20:17:16283 // These libs and dirs are inherited from statically linked deps and all
284 // configs applying to this target.
285 OrderedSet<SourceDir> all_lib_dirs_;
286 OrderedSet<std::string> all_libs_;
[email protected]3c1274d2013-09-10 22:21:21287
[email protected]ef348fe2014-05-01 18:31:31288 // All hard deps from this target and all dependencies. Filled in when this
289 // target is marked resolved. This will not include the current target.
290 std::set<const Target*> recursive_hard_deps_;
291
[email protected]c0822d7f2013-08-13 17:10:56292 ConfigValues config_values_; // Used for all binary targets.
[email protected]2fbe1012014-03-20 17:59:15293 ActionValues action_values_; // Used for action[_foreach] targets.
[email protected]96ea63d2013-07-30 10:17:07294
[email protected]0dfcae72014-08-19 22:52:16295 // Toolchain used by this target. Null until target is resolved.
296 const Toolchain* toolchain_;
297
298 // Output files. Null until the target is resolved.
299 OutputFile link_output_file_;
300 OutputFile dependency_output_file_;
301
[email protected]96ea63d2013-07-30 10:17:07302 DISALLOW_COPY_AND_ASSIGN(Target);
303};
304
[email protected]8fc56182014-08-06 21:44:33305namespace BASE_HASH_NAMESPACE {
306
307#if defined(COMPILER_GCC)
308template<> struct hash<const Target*> {
309 std::size_t operator()(const Target* t) const {
310 return reinterpret_cast<std::size_t>(t);
311 }
312};
313#elif defined(COMPILER_MSVC)
314inline size_t hash_value(const Target* t) {
315 return reinterpret_cast<size_t>(t);
316}
317#endif // COMPILER...
318
319} // namespace BASE_HASH_NAMESPACE
320
[email protected]96ea63d2013-07-30 10:17:07321#endif // TOOLS_GN_TARGET_H_