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/action_values.h" 18 #include "tools/gn/config_values.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/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 ACTION, 39 ACTION_FOREACH, 40 }; 41 typedef std::vector<SourceFile> FileList; 42 typedef std::vector<std::string> StringVector; 43 44 Target(const Settings* settings, const Label& label); 45 virtual ~Target(); 46 47 // Returns a string naming the output type. 48 static const char* GetStringForOutputType(OutputType type); 49 50 // Item overrides. 51 virtual Target* AsTarget() OVERRIDE; 52 virtual const Target* AsTarget() const OVERRIDE; 53 virtual void OnResolved() OVERRIDE; 54 55 OutputType output_type() const { return output_type_; } 56 void set_output_type(OutputType t) { output_type_ = t; } 57 58 bool IsLinkable() const; 59 60 // Will be the empty string to use the target label as the output name. 61 const std::string& output_name() const { return output_name_; } 62 void set_output_name(const std::string& name) { output_name_ = name; } 63 64 const std::string& output_extension() const { return output_extension_; } 65 void set_output_extension(const std::string& extension) { 66 output_extension_ = extension; 67 } 68 69 const FileList& sources() const { return sources_; } 70 FileList& sources() { return sources_; } 71 72 // Set to true when all sources are public. This is the default. In this case 73 // the public headers list should be empty. 74 bool all_headers_public() const { return all_headers_public_; } 75 void set_all_headers_public(bool p) { all_headers_public_ = p; } 76 77 // When all_headers_public is false, this is the list of public headers. It 78 // could be empty which would mean no headers are public. 79 const FileList& public_headers() const { return public_headers_; } 80 FileList& public_headers() { return public_headers_; } 81 82 // Compile-time extra dependencies. 83 const FileList& inputs() const { return inputs_; } 84 FileList& inputs() { return inputs_; } 85 86 // Runtime dependencies. 87 const FileList& data() const { return data_; } 88 FileList& data() { return data_; } 89 90 // Returns true if targets depending on this one should have an order 91 // dependency. 92 bool hard_dep() const { 93 return output_type_ == ACTION || 94 output_type_ == ACTION_FOREACH || 95 output_type_ == COPY_FILES; 96 } 97 98 // Linked dependencies. 99 const LabelTargetVector& deps() const { return deps_; } 100 LabelTargetVector& deps() { return deps_; } 101 102 // Non-linked dependencies. 103 const LabelTargetVector& datadeps() const { return datadeps_; } 104 LabelTargetVector& datadeps() { return datadeps_; } 105 106 // List of configs that this class inherits settings from. Once a target is 107 // resolved, this will also list all- and direct-dependent configs. 108 const LabelConfigVector& configs() const { return configs_; } 109 LabelConfigVector& configs() { return configs_; } 110 111 // List of configs that all dependencies (direct and indirect) of this 112 // target get. These configs are not added to this target. Note that due 113 // to the way this is computed, there may be duplicates in this list. 114 const LabelConfigVector& all_dependent_configs() const { 115 return all_dependent_configs_; 116 } 117 LabelConfigVector& all_dependent_configs() { 118 return all_dependent_configs_; 119 } 120 121 // List of configs that targets depending directly on this one get. These 122 // configs are not added to this target. 123 const LabelConfigVector& direct_dependent_configs() const { 124 return direct_dependent_configs_; 125 } 126 LabelConfigVector& direct_dependent_configs() { 127 return direct_dependent_configs_; 128 } 129 130 // A list of a subset of deps where we'll re-export direct_dependent_configs 131 // as direct_dependent_configs of this target. 132 const LabelTargetVector& forward_dependent_configs() const { 133 return forward_dependent_configs_; 134 } 135 LabelTargetVector& forward_dependent_configs() { 136 return forward_dependent_configs_; 137 } 138 139 const std::set<const Target*>& inherited_libraries() const { 140 return inherited_libraries_; 141 } 142 143 // This config represents the configuration set directly on this target. 144 ConfigValues& config_values() { return config_values_; } 145 const ConfigValues& config_values() const { return config_values_; } 146 147 ActionValues& action_values() { return action_values_; } 148 const ActionValues& action_values() const { return action_values_; } 149 150 const OrderedSet<SourceDir>& all_lib_dirs() const { return all_lib_dirs_; } 151 const OrderedSet<std::string>& all_libs() const { return all_libs_; } 152 153 const std::set<const Target*>& recursive_hard_deps() const { 154 return recursive_hard_deps_; 155 } 156 157 private: 158 // Pulls necessary information from dependencies to this one when all 159 // dependencies have been resolved. 160 void PullDependentTargetInfo(std::set<const Config*>* unique_configs); 161 162 // These each pull specific things from dependencies to this one when all 163 // deps have been resolved. 164 void PullForwardedDependentConfigs(); 165 void PullRecursiveHardDeps(); 166 167 OutputType output_type_; 168 std::string output_name_; 169 std::string output_extension_; 170 171 FileList sources_; 172 bool all_headers_public_; 173 FileList public_headers_; 174 FileList inputs_; 175 FileList data_; 176 177 bool hard_dep_; 178 179 // Note that if there are any groups in the deps, once the target is resolved 180 // these vectors will list *both* the groups as well as the groups' deps. 181 // 182 // This is because, in general, groups should be "transparent" ways to add 183 // groups of dependencies, so adding the groups deps make this happen with 184 // no additional complexity when iterating over a target's deps. 185 // 186 // However, a group may also have specific settings and configs added to it, 187 // so we also need the group in the list so we find these things. But you 188 // shouldn't need to look inside the deps of the group since those will 189 // already be added. 190 LabelTargetVector deps_; 191 LabelTargetVector datadeps_; 192 193 LabelConfigVector configs_; 194 LabelConfigVector all_dependent_configs_; 195 LabelConfigVector direct_dependent_configs_; 196 LabelTargetVector forward_dependent_configs_; 197 198 bool external_; 199 200 // Static libraries and source sets from transitive deps. These things need 201 // to be linked only with the end target (executable, shared library). Source 202 // sets do not get pushed beyond static library boundaries, and neither 203 // source sets nor static libraries get pushed beyond sahred library 204 // boundaries. 205 std::set<const Target*> inherited_libraries_; 206 207 // These libs and dirs are inherited from statically linked deps and all 208 // configs applying to this target. 209 OrderedSet<SourceDir> all_lib_dirs_; 210 OrderedSet<std::string> all_libs_; 211 212 // All hard deps from this target and all dependencies. Filled in when this 213 // target is marked resolved. This will not include the current target. 214 std::set<const Target*> recursive_hard_deps_; 215 216 ConfigValues config_values_; // Used for all binary targets. 217 ActionValues action_values_; // Used for action[_foreach] targets. 218 219 DISALLOW_COPY_AND_ASSIGN(Target); 220 }; 221 222 #endif // TOOLS_GN_TARGET_H_ 223