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/gtest_prod_util.h" 13 #include "base/logging.h" 14 #include "base/macros.h" 15 #include "gn/action_values.h" 16 #include "gn/bundle_data.h" 17 #include "gn/config_values.h" 18 #include "gn/inherited_libraries.h" 19 #include "gn/item.h" 20 #include "gn/label_pattern.h" 21 #include "gn/label_ptr.h" 22 #include "gn/lib_file.h" 23 #include "gn/metadata.h" 24 #include "gn/ordered_set.h" 25 #include "gn/output_file.h" 26 #include "gn/rust_values.h" 27 #include "gn/source_file.h" 28 #include "gn/toolchain.h" 29 #include "gn/unique_vector.h" 30 31 class DepsIteratorRange; 32 class Settings; 33 class Toolchain; 34 35 class Target : public Item { 36 public: 37 enum OutputType { 38 UNKNOWN, 39 GROUP, 40 EXECUTABLE, 41 SHARED_LIBRARY, 42 LOADABLE_MODULE, 43 STATIC_LIBRARY, 44 SOURCE_SET, 45 COPY_FILES, 46 ACTION, 47 ACTION_FOREACH, 48 BUNDLE_DATA, 49 CREATE_BUNDLE, 50 GENERATED_FILE, 51 RUST_LIBRARY, 52 RUST_PROC_MACRO, 53 }; 54 55 enum DepsIterationType { 56 DEPS_ALL, // Iterates through all public, private, and data deps. 57 DEPS_LINKED, // Iterates through all non-data dependencies. 58 }; 59 60 using FileList = std::vector<SourceFile>; 61 using StringVector = std::vector<std::string>; 62 63 // We track the set of build files that may affect this target, please refer 64 // to Scope for how this is determined. 65 Target(const Settings* settings, 66 const Label& label, 67 const SourceFileSet& build_dependency_files = {}); 68 ~Target() override; 69 70 // Returns a string naming the output type. 71 static const char* GetStringForOutputType(OutputType type); 72 73 // Item overrides. 74 Target* AsTarget() override; 75 const Target* AsTarget() const override; 76 bool OnResolved(Err* err) override; 77 output_type()78 OutputType output_type() const { return output_type_; } set_output_type(OutputType t)79 void set_output_type(OutputType t) { output_type_ = t; } 80 81 // True for targets that compile source code (all types of libaries and 82 // executables). 83 bool IsBinary() const; 84 85 // Can be linked into other targets. 86 bool IsLinkable() const; 87 88 // True if the target links dependencies rather than propogated up the graph. 89 // This is also true of action and copy steps even though they don't link 90 // dependencies, because they also don't propogate libraries up. 91 bool IsFinal() const; 92 93 // Will be the empty string to use the target label as the output name. 94 // See GetComputedOutputName(). output_name()95 const std::string& output_name() const { return output_name_; } set_output_name(const std::string & name)96 void set_output_name(const std::string& name) { output_name_ = name; } 97 98 // Returns the output name for this target, which is the output_name if 99 // specified, or the target label if not. 100 // 101 // Because this depends on the tool for this target, the toolchain must 102 // have been set before calling. 103 std::string GetComputedOutputName() const; 104 output_prefix_override()105 bool output_prefix_override() const { return output_prefix_override_; } set_output_prefix_override(bool prefix_override)106 void set_output_prefix_override(bool prefix_override) { 107 output_prefix_override_ = prefix_override; 108 } 109 110 // Desired output directory for the final output. This will be used for 111 // the {{output_dir}} substitution in the tool if it is specified. If 112 // is_null, the tool default will be used. output_dir()113 const SourceDir& output_dir() const { return output_dir_; } set_output_dir(const SourceDir & dir)114 void set_output_dir(const SourceDir& dir) { output_dir_ = dir; } 115 116 // The output extension is really a tri-state: unset (output_extension_set 117 // is false and the string is empty, meaning the default extension should be 118 // used), the output extension is set but empty (output should have no 119 // extension) and the output extension is set but nonempty (use the given 120 // extension). output_extension()121 const std::string& output_extension() const { return output_extension_; } set_output_extension(const std::string & extension)122 void set_output_extension(const std::string& extension) { 123 output_extension_ = extension; 124 output_extension_set_ = true; 125 } output_extension_set()126 bool output_extension_set() const { return output_extension_set_; } 127 sources()128 const FileList& sources() const { return sources_; } sources()129 FileList& sources() { return sources_; } 130 source_types_used()131 const SourceFileTypeSet& source_types_used() const { 132 return source_types_used_; 133 } source_types_used()134 SourceFileTypeSet& source_types_used() { return source_types_used_; } 135 136 // Set to true when all sources are public. This is the default. In this case 137 // the public headers list should be empty. all_headers_public()138 bool all_headers_public() const { return all_headers_public_; } set_all_headers_public(bool p)139 void set_all_headers_public(bool p) { all_headers_public_ = p; } 140 141 // When all_headers_public is false, this is the list of public headers. It 142 // could be empty which would mean no headers are public. public_headers()143 const FileList& public_headers() const { return public_headers_; } public_headers()144 FileList& public_headers() { return public_headers_; } 145 146 // Whether this target's includes should be checked by "gn check". check_includes()147 bool check_includes() const { return check_includes_; } set_check_includes(bool ci)148 void set_check_includes(bool ci) { check_includes_ = ci; } 149 150 // Whether this static_library target should have code linked in. complete_static_lib()151 bool complete_static_lib() const { return complete_static_lib_; } set_complete_static_lib(bool complete)152 void set_complete_static_lib(bool complete) { 153 DCHECK_EQ(STATIC_LIBRARY, output_type_); 154 complete_static_lib_ = complete; 155 } 156 157 // Metadata. Target takes ownership of the resulting scope. metadata()158 const Metadata& metadata() const { return metadata_; } metadata()159 Metadata& metadata() { return metadata_; } 160 161 // Get metadata from this target and its dependencies. This is intended to 162 // be called after the target is resolved. 163 bool GetMetadata(const std::vector<std::string>& keys_to_extract, 164 const std::vector<std::string>& keys_to_walk, 165 const SourceDir& rebase_dir, 166 bool deps_only, 167 std::vector<Value>* result, 168 std::set<const Target*>* targets_walked, 169 Err* err) const; 170 171 // GeneratedFile-related methods. 172 bool GenerateFile(Err* err) const; 173 contents()174 const Value& contents() const { return contents_; } set_contents(const Value & value)175 void set_contents(const Value& value) { contents_ = value; } output_conversion()176 const Value& output_conversion() const { return output_conversion_; } set_output_conversion(const Value & value)177 void set_output_conversion(const Value& value) { output_conversion_ = value; } 178 179 // Metadata collection methods for GeneratedFile targets. rebase()180 const SourceDir& rebase() const { return rebase_; } set_rebase(const SourceDir & value)181 void set_rebase(const SourceDir& value) { rebase_ = value; } data_keys()182 const std::vector<std::string>& data_keys() const { return data_keys_; } data_keys()183 std::vector<std::string>& data_keys() { return data_keys_; } walk_keys()184 const std::vector<std::string>& walk_keys() const { return walk_keys_; } walk_keys()185 std::vector<std::string>& walk_keys() { return walk_keys_; } 186 testonly()187 bool testonly() const { return testonly_; } set_testonly(bool value)188 void set_testonly(bool value) { testonly_ = value; } 189 write_runtime_deps_output()190 OutputFile write_runtime_deps_output() const { 191 return write_runtime_deps_output_; 192 } set_write_runtime_deps_output(const OutputFile & value)193 void set_write_runtime_deps_output(const OutputFile& value) { 194 write_runtime_deps_output_ = value; 195 } 196 197 // Runtime dependencies. These are "file-like things" that can either be 198 // directories or files. They do not need to exist, these are just passed as 199 // runtime dependencies to external test systems as necessary. data()200 const std::vector<std::string>& data() const { return data_; } data()201 std::vector<std::string>& data() { return data_; } 202 203 // Information about the bundle. Only valid for CREATE_BUNDLE target after 204 // they have been resolved. bundle_data()205 const BundleData& bundle_data() const { return bundle_data_; } bundle_data()206 BundleData& bundle_data() { return bundle_data_; } 207 208 // Returns true if targets depending on this one should have an order 209 // dependency. hard_dep()210 bool hard_dep() const { 211 return output_type_ == ACTION || output_type_ == ACTION_FOREACH || 212 output_type_ == COPY_FILES || output_type_ == CREATE_BUNDLE || 213 output_type_ == BUNDLE_DATA || output_type_ == GENERATED_FILE; 214 } 215 216 // Returns the iterator range which can be used in range-based for loops 217 // to iterate over multiple types of deps in one loop: 218 // for (const auto& pair : target->GetDeps(Target::DEPS_ALL)) ... 219 DepsIteratorRange GetDeps(DepsIterationType type) const; 220 221 // Linked private dependencies. private_deps()222 const LabelTargetVector& private_deps() const { return private_deps_; } private_deps()223 LabelTargetVector& private_deps() { return private_deps_; } 224 225 // Linked public dependencies. public_deps()226 const LabelTargetVector& public_deps() const { return public_deps_; } public_deps()227 LabelTargetVector& public_deps() { return public_deps_; } 228 229 // Non-linked dependencies. data_deps()230 const LabelTargetVector& data_deps() const { return data_deps_; } data_deps()231 LabelTargetVector& data_deps() { return data_deps_; } 232 233 // List of configs that this class inherits settings from. Once a target is 234 // resolved, this will also list all-dependent and public configs. configs()235 const UniqueVector<LabelConfigPair>& configs() const { return configs_; } configs()236 UniqueVector<LabelConfigPair>& configs() { return configs_; } 237 238 // List of configs that all dependencies (direct and indirect) of this 239 // target get. These configs are not added to this target. Note that due 240 // to the way this is computed, there may be duplicates in this list. all_dependent_configs()241 const UniqueVector<LabelConfigPair>& all_dependent_configs() const { 242 return all_dependent_configs_; 243 } all_dependent_configs()244 UniqueVector<LabelConfigPair>& all_dependent_configs() { 245 return all_dependent_configs_; 246 } 247 248 // List of configs that targets depending directly on this one get. These 249 // configs are also added to this target. public_configs()250 const UniqueVector<LabelConfigPair>& public_configs() const { 251 return public_configs_; 252 } public_configs()253 UniqueVector<LabelConfigPair>& public_configs() { return public_configs_; } 254 255 // Dependencies that can include files from this target. allow_circular_includes_from()256 const std::set<Label>& allow_circular_includes_from() const { 257 return allow_circular_includes_from_; 258 } allow_circular_includes_from()259 std::set<Label>& allow_circular_includes_from() { 260 return allow_circular_includes_from_; 261 } 262 inherited_libraries()263 const InheritedLibraries& inherited_libraries() const { 264 return inherited_libraries_; 265 } 266 267 // This config represents the configuration set directly on this target. config_values()268 ConfigValues& config_values() { return config_values_; } config_values()269 const ConfigValues& config_values() const { return config_values_; } 270 action_values()271 ActionValues& action_values() { return action_values_; } action_values()272 const ActionValues& action_values() const { return action_values_; } 273 rust_values()274 RustValues& rust_values() { return rust_values_; } rust_values()275 const RustValues& rust_values() const { return rust_values_; } 276 all_lib_dirs()277 const OrderedSet<SourceDir>& all_lib_dirs() const { return all_lib_dirs_; } all_libs()278 const OrderedSet<LibFile>& all_libs() const { return all_libs_; } 279 all_framework_dirs()280 const OrderedSet<SourceDir>& all_framework_dirs() const { 281 return all_framework_dirs_; 282 } all_frameworks()283 const OrderedSet<std::string>& all_frameworks() const { 284 return all_frameworks_; 285 } 286 recursive_hard_deps()287 const std::set<const Target*>& recursive_hard_deps() const { 288 return recursive_hard_deps_; 289 } 290 friends()291 std::vector<LabelPattern>& friends() { return friends_; } friends()292 const std::vector<LabelPattern>& friends() const { return friends_; } 293 assert_no_deps()294 std::vector<LabelPattern>& assert_no_deps() { return assert_no_deps_; } assert_no_deps()295 const std::vector<LabelPattern>& assert_no_deps() const { 296 return assert_no_deps_; 297 } 298 299 // The toolchain is only known once this target is resolved (all if its 300 // dependencies are known). They will be null until then. Generally, this can 301 // only be used during target writing. toolchain()302 const Toolchain* toolchain() const { return toolchain_; } 303 304 // Sets the toolchain. The toolchain must include a tool for this target 305 // or the error will be set and the function will return false. Unusually, 306 // this function's "err" output is optional since this is commonly used 307 // frequently by unit tests which become needlessly verbose. 308 bool SetToolchain(const Toolchain* toolchain, Err* err = nullptr); 309 310 // Once this target has been resolved, all outputs from the target will be 311 // listed here. This will include things listed in the "outputs" for an 312 // action or a copy step, and the output library or executable file(s) from 313 // binary targets. 314 // 315 // It will NOT include stamp files and object files. computed_outputs()316 const std::vector<OutputFile>& computed_outputs() const { 317 return computed_outputs_; 318 } 319 320 // Returns outputs from this target. The link output file is the one that 321 // other targets link to when they depend on this target. This will only be 322 // valid for libraries and will be empty for all other target types. 323 // 324 // The dependency output file is the file that should be used to express 325 // a dependency on this one. It could be the same as the link output file 326 // (this will be the case for static libraries). For shared libraries it 327 // could be the same or different than the link output file, depending on the 328 // system. For actions this will be the stamp file. 329 // 330 // These are only known once the target is resolved and will be empty before 331 // that. This is a cache of the files to prevent every target that depends on 332 // a given library from recomputing the same pattern. link_output_file()333 const OutputFile& link_output_file() const { return link_output_file_; } dependency_output_file()334 const OutputFile& dependency_output_file() const { 335 return dependency_output_file_; 336 } 337 338 // The subset of computed_outputs that are considered runtime outputs. runtime_outputs()339 const std::vector<OutputFile>& runtime_outputs() const { 340 return runtime_outputs_; 341 } 342 343 // Computes the set of output files resulting from compiling the given source 344 // file. If the file can be compiled and the tool exists, fills the outputs 345 // in and writes the tool type to computed_tool_type. If the file is not 346 // compilable, returns false. 347 // 348 // The function can succeed with a "NONE" tool type for object files which 349 // are just passed to the output. The output will always be overwritten, not 350 // appended to. 351 bool GetOutputFilesForSource(const SourceFile& source, 352 const char** computed_tool_type, 353 std::vector<OutputFile>* outputs) const; 354 355 private: 356 FRIEND_TEST_ALL_PREFIXES(TargetTest, ResolvePrecompiledHeaders); 357 358 // Pulls necessary information from dependencies to this one when all 359 // dependencies have been resolved. 360 void PullDependentTargetConfigs(); 361 void PullDependentTargetLibsFrom(const Target* dep, bool is_public); 362 void PullDependentTargetLibs(); 363 void PullRecursiveHardDeps(); 364 void PullRecursiveBundleData(); 365 366 // Fills the link and dependency output files when a target is resolved. 367 bool FillOutputFiles(Err* err); 368 369 // Checks precompiled headers from configs and makes sure the resulting 370 // values are in config_values_. 371 bool ResolvePrecompiledHeaders(Err* err); 372 373 // Validates the given thing when a target is resolved. 374 bool CheckVisibility(Err* err) const; 375 bool CheckTestonly(Err* err) const; 376 bool CheckAssertNoDeps(Err* err) const; 377 void CheckSourcesGenerated() const; 378 void CheckSourceGenerated(const SourceFile& source) const; 379 380 OutputType output_type_ = UNKNOWN; 381 std::string output_name_; 382 bool output_prefix_override_ = false; 383 SourceDir output_dir_; 384 std::string output_extension_; 385 bool output_extension_set_ = false; 386 387 FileList sources_; 388 SourceFileTypeSet source_types_used_; 389 bool all_headers_public_ = true; 390 FileList public_headers_; 391 bool check_includes_ = true; 392 bool complete_static_lib_ = false; 393 bool testonly_ = false; 394 std::vector<std::string> data_; 395 BundleData bundle_data_; 396 OutputFile write_runtime_deps_output_; 397 398 LabelTargetVector private_deps_; 399 LabelTargetVector public_deps_; 400 LabelTargetVector data_deps_; 401 402 // See getters for more info. 403 UniqueVector<LabelConfigPair> configs_; 404 UniqueVector<LabelConfigPair> all_dependent_configs_; 405 UniqueVector<LabelConfigPair> public_configs_; 406 407 std::set<Label> allow_circular_includes_from_; 408 409 // Static libraries, shared libraries, and source sets from transitive deps 410 // that need to be linked. 411 InheritedLibraries inherited_libraries_; 412 413 // These libs and dirs are inherited from statically linked deps and all 414 // configs applying to this target. 415 OrderedSet<SourceDir> all_lib_dirs_; 416 OrderedSet<LibFile> all_libs_; 417 418 // These frameworks and dirs are inherited from statically linked deps and 419 // all configs applying to this target. 420 OrderedSet<SourceDir> all_framework_dirs_; 421 OrderedSet<std::string> all_frameworks_; 422 423 // All hard deps from this target and all dependencies. Filled in when this 424 // target is marked resolved. This will not include the current target. 425 std::set<const Target*> recursive_hard_deps_; 426 427 std::vector<LabelPattern> friends_; 428 std::vector<LabelPattern> assert_no_deps_; 429 430 // Used for all binary targets, and for inputs in regular targets. The 431 // precompiled header values in this struct will be resolved to the ones to 432 // use for this target, if precompiled headers are used. 433 ConfigValues config_values_; 434 435 // Used for action[_foreach] targets. 436 ActionValues action_values_; 437 438 // Used for Rust targets. 439 RustValues rust_values_; 440 441 // Toolchain used by this target. Null until target is resolved. 442 const Toolchain* toolchain_ = nullptr; 443 444 // Output files. Empty until the target is resolved. 445 std::vector<OutputFile> computed_outputs_; 446 OutputFile link_output_file_; 447 OutputFile dependency_output_file_; 448 std::vector<OutputFile> runtime_outputs_; 449 450 Metadata metadata_; 451 452 // GeneratedFile values. 453 Value output_conversion_; 454 Value contents_; // Value::NONE if metadata collection should occur. 455 456 // GeneratedFile as metadata collection values. 457 SourceDir rebase_; 458 std::vector<std::string> data_keys_; 459 std::vector<std::string> walk_keys_; 460 461 DISALLOW_COPY_AND_ASSIGN(Target); 462 }; 463 464 extern const char kExecution_Help[]; 465 466 #endif // TOOLS_GN_TARGET_H_ 467