• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
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"
17 #include "tools/gn/config_values.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/script_values.h"
22 #include "tools/gn/source_file.h"
23 
24 class InputFile;
25 class Settings;
26 class Token;
27 
28 class Target : public Item {
29  public:
30   enum OutputType {
31     UNKNOWN,
32     GROUP,
33     EXECUTABLE,
34     SHARED_LIBRARY,
35     STATIC_LIBRARY,
36     SOURCE_SET,
37     COPY_FILES,
38     CUSTOM,
39   };
40   typedef std::vector<SourceFile> FileList;
41   typedef std::vector<std::string> StringVector;
42 
43   Target(const Settings* settings, const Label& label);
44   virtual ~Target();
45 
46   // Returns a string naming the output type.
47   static const char* GetStringForOutputType(OutputType type);
48 
49   // Item overrides.
50   virtual Target* AsTarget() OVERRIDE;
51   virtual const Target* AsTarget() const OVERRIDE;
52   virtual void OnResolved() OVERRIDE;
53 
output_type()54   OutputType output_type() const { return output_type_; }
set_output_type(OutputType t)55   void set_output_type(OutputType t) { output_type_ = t; }
56 
57   bool IsLinkable() const;
58 
59   // Will be the empty string to use the target label as the output name.
output_name()60   const std::string& output_name() const { return output_name_; }
set_output_name(const std::string & name)61   void set_output_name(const std::string& name) { output_name_ = name; }
62 
sources()63   const FileList& sources() const { return sources_; }
sources()64   FileList& sources() { return sources_; }
65 
66   // Compile-time extra dependencies.
source_prereqs()67   const FileList& source_prereqs() const { return source_prereqs_; }
source_prereqs()68   FileList& source_prereqs() { return source_prereqs_; }
69 
70   // Runtime dependencies.
data()71   const FileList& data() const { return data_; }
data()72   FileList& data() { return data_; }
73 
74   // Targets depending on this one should have an order dependency.
hard_dep()75   bool hard_dep() const { return hard_dep_; }
set_hard_dep(bool hd)76   void set_hard_dep(bool hd) { hard_dep_ = hd; }
77 
78   // Linked dependencies.
deps()79   const LabelTargetVector& deps() const { return deps_; }
deps()80   LabelTargetVector& deps() { return deps_; }
81 
82   // Non-linked dependencies.
datadeps()83   const LabelTargetVector& datadeps() const { return datadeps_; }
datadeps()84   LabelTargetVector& datadeps() { return datadeps_; }
85 
86   // List of configs that this class inherits settings from.
configs()87   const LabelConfigVector& configs() const { return configs_; }
configs()88   LabelConfigVector& configs() { return configs_; }
89 
90   // List of configs that all dependencies (direct and indirect) of this
91   // target get. These configs are not added to this target. Note that due
92   // to the way this is computed, there may be duplicates in this list.
all_dependent_configs()93   const LabelConfigVector& all_dependent_configs() const {
94     return all_dependent_configs_;
95   }
all_dependent_configs()96   LabelConfigVector& all_dependent_configs() {
97     return all_dependent_configs_;
98   }
99 
100   // List of configs that targets depending directly on this one get. These
101   // configs are not added to this target.
direct_dependent_configs()102   const LabelConfigVector& direct_dependent_configs() const {
103     return direct_dependent_configs_;
104   }
direct_dependent_configs()105   LabelConfigVector& direct_dependent_configs() {
106     return direct_dependent_configs_;
107   }
108 
109   // A list of a subset of deps where we'll re-export direct_dependent_configs
110   // as direct_dependent_configs of this target.
forward_dependent_configs()111   const LabelTargetVector& forward_dependent_configs() const {
112     return forward_dependent_configs_;
113   }
forward_dependent_configs()114   LabelTargetVector& forward_dependent_configs() {
115     return forward_dependent_configs_;
116   }
117 
external()118   bool external() const { return external_; }
set_external(bool e)119   void set_external(bool e) { external_ = e; }
120 
inherited_libraries()121   const std::set<const Target*>& inherited_libraries() const {
122     return inherited_libraries_;
123   }
124 
125   // This config represents the configuration set directly on this target.
config_values()126   ConfigValues& config_values() { return config_values_; }
config_values()127   const ConfigValues& config_values() const { return config_values_; }
128 
script_values()129   ScriptValues& script_values() { return script_values_; }
script_values()130   const ScriptValues& script_values() const { return script_values_; }
131 
all_lib_dirs()132   const OrderedSet<SourceDir>& all_lib_dirs() const { return all_lib_dirs_; }
all_libs()133   const OrderedSet<std::string>& all_libs() const { return all_libs_; }
134 
gyp_file()135   const SourceFile& gyp_file() const { return gyp_file_; }
set_gyp_file(const SourceFile & gf)136   void set_gyp_file(const SourceFile& gf) { gyp_file_ = gf; }
137 
138  private:
139   // Pulls necessary information from dependents to this one when all
140   // dependencies have been resolved.
141   void PullDependentTargetInfo(std::set<const Config*>* unique_configs);
142 
143   OutputType output_type_;
144   std::string output_name_;
145 
146   FileList sources_;
147   FileList source_prereqs_;
148   FileList data_;
149 
150   bool hard_dep_;
151 
152   // Note that if there are any groups in the deps, once the target is resolved
153   // these vectors will list *both* the groups as well as the groups' deps.
154   //
155   // This is because, in general, groups should be "transparent" ways to add
156   // groups of dependencies, so adding the groups deps make this happen with
157   // no additional complexity when iterating over a target's deps.
158   //
159   // However, a group may also have specific settings and configs added to it,
160   // so we also need the group in the list so we find these things. But you
161   // shouldn't need to look inside the deps of the group since those will
162   // already be added.
163   LabelTargetVector deps_;
164   LabelTargetVector datadeps_;
165 
166   LabelConfigVector configs_;
167   LabelConfigVector all_dependent_configs_;
168   LabelConfigVector direct_dependent_configs_;
169   LabelTargetVector forward_dependent_configs_;
170 
171   bool external_;
172 
173   // Static libraries and source sets from transitive deps. These things need
174   // to be linked only with the end target (executable, shared library). These
175   // do not get pushed beyond shared library boundaries.
176   std::set<const Target*> inherited_libraries_;
177 
178   // These libs and dirs are inherited from statically linked deps and all
179   // configs applying to this target.
180   OrderedSet<SourceDir> all_lib_dirs_;
181   OrderedSet<std::string> all_libs_;
182 
183   ConfigValues config_values_;  // Used for all binary targets.
184   ScriptValues script_values_;  // Used for script (CUSTOM) targets.
185 
186   SourceFile gyp_file_;
187 
188   DISALLOW_COPY_AND_ASSIGN(Target);
189 };
190 
191 #endif  // TOOLS_GN_TARGET_H_
192