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 "gn/action_values.h" 15 #include "gn/bundle_data.h" 16 #include "gn/config_values.h" 17 #include "gn/inherited_libraries.h" 18 #include "gn/item.h" 19 #include "gn/label_pattern.h" 20 #include "gn/label_ptr.h" 21 #include "gn/lib_file.h" 22 #include "gn/metadata.h" 23 #include "gn/output_file.h" 24 #include "gn/pointer_set.h" 25 #include "gn/rust_values.h" 26 #include "gn/source_file.h" 27 #include "gn/swift_values.h" 28 #include "gn/toolchain.h" 29 #include "gn/unique_vector.h" 30 31 class DepsIteratorRange; 32 class Settings; 33 class Target; 34 class Toolchain; 35 36 using TargetSet = PointerSet<const Target>; 37 38 class Target : public Item { 39 public: 40 enum OutputType { 41 UNKNOWN, 42 GROUP, 43 EXECUTABLE, 44 SHARED_LIBRARY, 45 LOADABLE_MODULE, 46 STATIC_LIBRARY, 47 SOURCE_SET, 48 COPY_FILES, 49 ACTION, 50 ACTION_FOREACH, 51 BUNDLE_DATA, 52 CREATE_BUNDLE, 53 GENERATED_FILE, 54 RUST_LIBRARY, 55 RUST_PROC_MACRO, 56 }; 57 58 enum DepsIterationType { 59 DEPS_ALL, // Iterates through all public, private, and data deps. 60 DEPS_LINKED, // Iterates through all non-data dependencies. 61 }; 62 63 using FileList = std::vector<SourceFile>; 64 using StringVector = std::vector<std::string>; 65 66 // We track the set of build files that may affect this target, please refer 67 // to Scope for how this is determined. 68 Target(const Settings* settings, 69 const Label& label, 70 const SourceFileSet& build_dependency_files = {}); 71 ~Target() override; 72 73 // Returns a string naming the output type. 74 static const char* GetStringForOutputType(OutputType type); 75 76 // Item overrides. 77 Target* AsTarget() override; 78 const Target* AsTarget() const override; 79 bool OnResolved(Err* err) override; 80 output_type()81 OutputType output_type() const { return output_type_; } set_output_type(OutputType t)82 void set_output_type(OutputType t) { output_type_ = t; } 83 84 // True for targets that compile source code (all types of libraries and 85 // executables). 86 bool IsBinary() const; 87 88 // Can be linked into other targets. 89 bool IsLinkable() const; 90 91 // True if the target links dependencies rather than propagated up the graph. 92 // This is also true of action and copy steps even though they don't link 93 // dependencies, because they also don't propagate libraries up. 94 bool IsFinal() const; 95 96 // Set when the target should normally be treated as a data dependency. These 97 // do not need to be treated as inputs or hard dependencies for normal build 98 // steps, but have to be kept in the dependency tree to be properly 99 // propagated. Treating these as data only decreases superfluous rebuilds and 100 // increases parallelism. 101 bool IsDataOnly() const; 102 103 // Return true if this target should be generated in the final build graph. 104 bool ShouldGenerate() const; 105 106 // Will be the empty string to use the target label as the output name. 107 // See GetComputedOutputName(). output_name()108 const std::string& output_name() const { return output_name_; } set_output_name(const std::string & name)109 void set_output_name(const std::string& name) { output_name_ = name; } 110 111 // Returns the output name for this target, which is the output_name if 112 // specified, or the target label if not. 113 // 114 // Because this depends on the tool for this target, the toolchain must 115 // have been set before calling. 116 std::string GetComputedOutputName() const; 117 output_prefix_override()118 bool output_prefix_override() const { return output_prefix_override_; } set_output_prefix_override(bool prefix_override)119 void set_output_prefix_override(bool prefix_override) { 120 output_prefix_override_ = prefix_override; 121 } 122 123 // Desired output directory for the final output. This will be used for 124 // the {{output_dir}} substitution in the tool if it is specified. If 125 // is_null, the tool default will be used. output_dir()126 const SourceDir& output_dir() const { return output_dir_; } set_output_dir(const SourceDir & dir)127 void set_output_dir(const SourceDir& dir) { output_dir_ = dir; } 128 129 // The output extension is really a tri-state: unset (output_extension_set 130 // is false and the string is empty, meaning the default extension should be 131 // used), the output extension is set but empty (output should have no 132 // extension) and the output extension is set but nonempty (use the given 133 // extension). output_extension()134 const std::string& output_extension() const { return output_extension_; } set_output_extension(const std::string & extension)135 void set_output_extension(const std::string& extension) { 136 output_extension_ = extension; 137 output_extension_set_ = true; 138 } output_extension_set()139 bool output_extension_set() const { return output_extension_set_; } 140 sources()141 const FileList& sources() const { return sources_; } sources()142 FileList& sources() { return sources_; } 143 source_types_used()144 const SourceFileTypeSet& source_types_used() const { 145 return source_types_used_; 146 } source_types_used()147 SourceFileTypeSet& source_types_used() { return source_types_used_; } 148 149 // Set to true when all sources are public. This is the default. In this case 150 // the public headers list should be empty. all_headers_public()151 bool all_headers_public() const { return all_headers_public_; } set_all_headers_public(bool p)152 void set_all_headers_public(bool p) { all_headers_public_ = p; } 153 154 // When all_headers_public is false, this is the list of public headers. It 155 // could be empty which would mean no headers are public. public_headers()156 const FileList& public_headers() const { return public_headers_; } public_headers()157 FileList& public_headers() { return public_headers_; } 158 159 // Whether this target's includes should be checked by "gn check". check_includes()160 bool check_includes() const { return check_includes_; } set_check_includes(bool ci)161 void set_check_includes(bool ci) { check_includes_ = ci; } 162 163 // Whether this static_library target should have code linked in. complete_static_lib()164 bool complete_static_lib() const { return complete_static_lib_; } set_complete_static_lib(bool complete)165 void set_complete_static_lib(bool complete) { 166 DCHECK_EQ(STATIC_LIBRARY, output_type_); 167 complete_static_lib_ = complete; 168 } 169 170 // Whether this copy target is linkable. copy_linkable_file()171 bool copy_linkable_file() const { return copy_linkable_file_; } set_copy_linkable_file(bool linkable)172 void set_copy_linkable_file(bool linkable) 173 { 174 DCHECK_EQ(COPY_FILES, output_type_); 175 copy_linkable_file_ = linkable; 176 } 177 178 // Metadata. Target takes ownership of the resulting scope. 179 const Metadata& metadata() const; 180 Metadata& metadata(); has_metadata()181 bool has_metadata() const { return metadata_.get(); } 182 183 // Get metadata from this target and its dependencies. This is intended to 184 // be called after the target is resolved. 185 bool GetMetadata(const std::vector<std::string>& keys_to_extract, 186 const std::vector<std::string>& keys_to_walk, 187 const SourceDir& rebase_dir, 188 bool deps_only, 189 std::vector<Value>* result, 190 TargetSet* targets_walked, 191 Err* err) const; 192 193 // GeneratedFile-related methods. 194 bool GenerateFile(Err* err) const; 195 196 // Metadata collection methods for GeneratedFile targets. 197 struct GeneratedFile { 198 Value output_conversion_; 199 Value contents_; // Value::NONE if metadata collection should occur. 200 SourceDir rebase_; 201 std::vector<std::string> data_keys_; 202 std::vector<std::string> walk_keys_; 203 }; 204 const GeneratedFile& generated_file() const; 205 GeneratedFile& generated_file(); has_generated_file()206 bool has_generated_file() const { return generated_file_.get(); } 207 contents()208 const Value& contents() const { return generated_file().contents_; } set_contents(const Value & value)209 void set_contents(const Value& value) { generated_file().contents_ = value; } output_conversion()210 const Value& output_conversion() const { 211 return generated_file().output_conversion_; 212 } set_output_conversion(const Value & value)213 void set_output_conversion(const Value& value) { 214 generated_file().output_conversion_ = value; 215 } 216 rebase()217 const SourceDir& rebase() const { return generated_file().rebase_; } set_rebase(const SourceDir & value)218 void set_rebase(const SourceDir& value) { generated_file().rebase_ = value; } data_keys()219 const std::vector<std::string>& data_keys() const { 220 return generated_file().data_keys_; 221 } data_keys()222 std::vector<std::string>& data_keys() { return generated_file().data_keys_; } walk_keys()223 const std::vector<std::string>& walk_keys() const { 224 return generated_file().walk_keys_; 225 } walk_keys()226 std::vector<std::string>& walk_keys() { return generated_file().walk_keys_; } 227 write_runtime_deps_output()228 OutputFile write_runtime_deps_output() const { 229 return write_runtime_deps_output_; 230 } set_write_runtime_deps_output(const OutputFile & value)231 void set_write_runtime_deps_output(const OutputFile& value) { 232 write_runtime_deps_output_ = value; 233 } 234 235 // Runtime dependencies. These are "file-like things" that can either be 236 // directories or files. They do not need to exist, these are just passed as 237 // runtime dependencies to external test systems as necessary. data()238 const std::vector<std::string>& data() const { return data_; } data()239 std::vector<std::string>& data() { return data_; } 240 241 // Information about the bundle. Only valid for CREATE_BUNDLE target after 242 // they have been resolved. 243 const BundleData& bundle_data() const; 244 BundleData& bundle_data(); has_bundle_data()245 bool has_bundle_data() const { return bundle_data_.get(); } 246 247 // Returns true if targets depending on this one should have an order 248 // dependency. hard_dep()249 bool hard_dep() const { 250 return output_type_ == ACTION || output_type_ == ACTION_FOREACH || 251 output_type_ == COPY_FILES || output_type_ == CREATE_BUNDLE || 252 output_type_ == BUNDLE_DATA || output_type_ == GENERATED_FILE || 253 builds_swift_module(); 254 } 255 256 // Returns the iterator range which can be used in range-based for loops 257 // to iterate over multiple types of deps in one loop: 258 // for (const auto& pair : target->GetDeps(Target::DEPS_ALL)) ... 259 DepsIteratorRange GetDeps(DepsIterationType type) const; 260 261 // Linked private dependencies. private_deps()262 const LabelTargetVector& private_deps() const { return private_deps_; } private_deps()263 LabelTargetVector& private_deps() { return private_deps_; } 264 265 // Linked whole archive dependencies. whole_archive_deps()266 const LabelTargetVector& whole_archive_deps() const { return whole_archive_deps_; } whole_archive_deps()267 LabelTargetVector& whole_archive_deps() { return whole_archive_deps_; } 268 269 // Linked no whole archive dependencies. no_whole_archive_deps()270 const LabelTargetVector& no_whole_archive_deps() const { return no_whole_archive_deps_; } no_whole_archive_deps()271 LabelTargetVector& no_whole_archive_deps() { return no_whole_archive_deps_; } 272 273 // Linked public dependencies. public_deps()274 const LabelTargetVector& public_deps() const { return public_deps_; } public_deps()275 LabelTargetVector& public_deps() { return public_deps_; } 276 277 // Non-linked dependencies. data_deps()278 const LabelTargetVector& data_deps() const { return data_deps_; } data_deps()279 LabelTargetVector& data_deps() { return data_deps_; } 280 281 // gen_deps only propagate the "should_generate" flag. These dependencies can 282 // have cycles so care should be taken if iterating over them recursively. gen_deps()283 const LabelTargetVector& gen_deps() const { return gen_deps_; } gen_deps()284 LabelTargetVector& gen_deps() { return gen_deps_; } 285 286 // List of configs that this class inherits settings from. Once a target is 287 // resolved, this will also list all-dependent and public configs. configs()288 const UniqueVector<LabelConfigPair>& configs() const { return configs_; } configs()289 UniqueVector<LabelConfigPair>& configs() { return configs_; } 290 291 // List of configs that all dependencies (direct and indirect) of this 292 // target get. These configs are not added to this target. Note that due 293 // to the way this is computed, there may be duplicates in this list. all_dependent_configs()294 const UniqueVector<LabelConfigPair>& all_dependent_configs() const { 295 return all_dependent_configs_; 296 } all_dependent_configs()297 UniqueVector<LabelConfigPair>& all_dependent_configs() { 298 return all_dependent_configs_; 299 } 300 301 // List of configs that targets depending directly on this one get. These 302 // configs are also added to this target. public_configs()303 const UniqueVector<LabelConfigPair>& public_configs() const { 304 return public_configs_; 305 } public_configs()306 UniqueVector<LabelConfigPair>& public_configs() { return public_configs_; } 307 308 // Dependencies that can include files from this target. allow_circular_includes_from()309 const std::set<Label>& allow_circular_includes_from() const { 310 return allow_circular_includes_from_; 311 } allow_circular_includes_from()312 std::set<Label>& allow_circular_includes_from() { 313 return allow_circular_includes_from_; 314 } 315 inherited_libraries()316 const InheritedLibraries& inherited_libraries() const { 317 return inherited_libraries_; 318 } 319 320 // Pool option pool()321 const LabelPtrPair<Pool>& pool() const { return pool_; } set_pool(LabelPtrPair<Pool> pool)322 void set_pool(LabelPtrPair<Pool> pool) { pool_ = std::move(pool); } 323 324 // This config represents the configuration set directly on this target. 325 ConfigValues& config_values(); 326 const ConfigValues& config_values() const; has_config_values()327 bool has_config_values() const { return config_values_.get(); } 328 329 ActionValues& action_values(); 330 const ActionValues& action_values() const; has_action_values()331 bool has_action_values() const { return action_values_.get(); } 332 333 SwiftValues& swift_values(); 334 const SwiftValues& swift_values() const; has_swift_values()335 bool has_swift_values() const { return swift_values_.get(); } 336 337 // Return true if this targets builds a SwiftModule builds_swift_module()338 bool builds_swift_module() const { 339 return IsBinary() && has_swift_values() && 340 source_types_used().SwiftSourceUsed(); 341 } 342 343 RustValues& rust_values(); 344 const RustValues& rust_values() const; has_rust_values()345 bool has_rust_values() const { return rust_values_.get(); } 346 347 // Transitive closure of libraries that are depended on by this target rust_transitive_inherited_libs()348 const InheritedLibraries& rust_transitive_inherited_libs() const { 349 return rust_transitive_inherited_libs_; 350 } rust_transitive_inheritable_libs()351 const InheritedLibraries& rust_transitive_inheritable_libs() const { 352 return rust_transitive_inheritable_libs_; 353 } 354 all_lib_dirs()355 const UniqueVector<SourceDir>& all_lib_dirs() const { return all_lib_dirs_; } all_libs()356 const UniqueVector<LibFile>& all_libs() const { return all_libs_; } 357 all_framework_dirs()358 const UniqueVector<SourceDir>& all_framework_dirs() const { 359 return all_framework_dirs_; 360 } all_frameworks()361 const UniqueVector<std::string>& all_frameworks() const { 362 return all_frameworks_; 363 } all_weak_frameworks()364 const UniqueVector<std::string>& all_weak_frameworks() const { 365 return all_weak_frameworks_; 366 } 367 recursive_hard_deps()368 const TargetSet& recursive_hard_deps() const { return recursive_hard_deps_; } 369 friends()370 std::vector<LabelPattern>& friends() { return friends_; } friends()371 const std::vector<LabelPattern>& friends() const { return friends_; } 372 assert_no_deps()373 std::vector<LabelPattern>& assert_no_deps() { return assert_no_deps_; } assert_no_deps()374 const std::vector<LabelPattern>& assert_no_deps() const { 375 return assert_no_deps_; 376 } 377 378 // The toolchain is only known once this target is resolved (all if its 379 // dependencies are known). They will be null until then. Generally, this can 380 // only be used during target writing. toolchain()381 const Toolchain* toolchain() const { return toolchain_; } 382 383 // Sets the toolchain. The toolchain must include a tool for this target 384 // or the error will be set and the function will return false. Unusually, 385 // this function's "err" output is optional since this is commonly used 386 // frequently by unit tests which become needlessly verbose. 387 bool SetToolchain(const Toolchain* toolchain, Err* err = nullptr); 388 389 // Once this target has been resolved, all outputs from the target will be 390 // listed here. This will include things listed in the "outputs" for an 391 // action or a copy step, and the output library or executable file(s) from 392 // binary targets. 393 // 394 // It will NOT include stamp files and object files. computed_outputs()395 const std::vector<OutputFile>& computed_outputs() const { 396 return computed_outputs_; 397 } 398 399 // Returns outputs from this target. The link output file is the one that 400 // other targets link to when they depend on this target. This will only be 401 // valid for libraries and will be empty for all other target types. 402 // 403 // The dependency output file is the file that should be used to express 404 // a dependency on this one. It could be the same as the link output file 405 // (this will be the case for static libraries). For shared libraries it 406 // could be the same or different than the link output file, depending on the 407 // system. For actions this will be the stamp file. 408 // 409 // These are only known once the target is resolved and will be empty before 410 // that. This is a cache of the files to prevent every target that depends on 411 // a given library from recomputing the same pattern. link_output_file()412 const OutputFile& link_output_file() const { return link_output_file_; } dependency_output_file()413 const OutputFile& dependency_output_file() const { 414 return dependency_output_file_; 415 } 416 417 // The subset of computed_outputs that are considered runtime outputs. runtime_outputs()418 const std::vector<OutputFile>& runtime_outputs() const { 419 return runtime_outputs_; 420 } 421 422 // Computes and returns the outputs of this target expressed as SourceFiles. 423 // 424 // For binary target this depends on the tool for this target so the toolchain 425 // must have been loaded beforehand. This will happen asynchronously so 426 // calling this on a binary target before the build is complete will produce a 427 // race condition. 428 // 429 // To resolve this, the caller passes in whether the entire build is complete 430 // (this is used for the introspection commands which run after everything 431 // else). 432 // 433 // If the build is complete, the toolchain will be used for binary targets to 434 // compute the outputs. If the build is not complete, calling this function 435 // for binary targets will produce an error. 436 // 437 // The |loc_for_error| is used to blame a location for any errors produced. It 438 // can be empty if there is no range (like this is being called based on the 439 // command-line. 440 bool GetOutputsAsSourceFiles(const LocationRange& loc_for_error, 441 bool build_complete, 442 std::vector<SourceFile>* outputs, 443 Err* err) const; 444 445 // Computes the set of output files resulting from compiling the given source 446 // file. 447 // 448 // For binary targets, if the file can be compiled and the tool exists, fills 449 // the outputs in and writes the tool type to computed_tool_type. If the file 450 // is not compilable, returns false. 451 // 452 // For action_foreach and copy targets, applies the output pattern to the 453 // given file name to compute the outputs. 454 // 455 // For all other target types, just returns the target outputs because such 456 // targets conceptually process all of their inputs as one step. 457 // 458 // The function can succeed with a "NONE" tool type for object files which 459 // are just passed to the output. The output will always be overwritten, not 460 // appended to. 461 bool GetOutputFilesForSource(const SourceFile& source, 462 const char** computed_tool_type, 463 std::vector<OutputFile>* outputs) const; 464 465 private: 466 FRIEND_TEST_ALL_PREFIXES(TargetTest, ResolvePrecompiledHeaders); 467 468 // Pulls necessary information from dependencies to this one when all 469 // dependencies have been resolved. 470 void PullDependentTargetConfigs(); 471 void PullDependentTargetLibsFrom(const Target* dep, bool is_public); 472 void PullDependentTargetLibs(); 473 void PullRecursiveHardDeps(); 474 void PullRecursiveBundleData(); 475 476 // Fills the link and dependency output files when a target is resolved. 477 bool FillOutputFiles(Err* err); 478 479 // Checks precompiled headers from configs and makes sure the resulting 480 // values are in config_values_. 481 bool ResolvePrecompiledHeaders(Err* err); 482 483 // Validates the given thing when a target is resolved. 484 bool CheckVisibility(Err* err) const; 485 bool CheckConfigVisibility(Err* err) const; 486 bool CheckTestonly(Err* err) const; 487 bool CheckAssertNoDeps(Err* err) const; 488 void CheckSourcesGenerated() const; 489 void CheckSourceGenerated(const SourceFile& source) const; 490 bool CheckSourceSetLanguages(Err* err) const; 491 492 OutputType output_type_ = UNKNOWN; 493 std::string output_name_; 494 bool output_prefix_override_ = false; 495 SourceDir output_dir_; 496 std::string output_extension_; 497 bool output_extension_set_ = false; 498 499 FileList sources_; 500 SourceFileTypeSet source_types_used_; 501 bool all_headers_public_ = true; 502 FileList public_headers_; 503 bool check_includes_ = true; 504 bool complete_static_lib_ = false; 505 bool copy_linkable_file_ = false; 506 std::vector<std::string> data_; 507 std::unique_ptr<BundleData> bundle_data_; 508 OutputFile write_runtime_deps_output_; 509 510 LabelTargetVector whole_archive_deps_; 511 LabelTargetVector no_whole_archive_deps_; 512 LabelTargetVector private_deps_; 513 LabelTargetVector public_deps_; 514 LabelTargetVector data_deps_; 515 LabelTargetVector gen_deps_; 516 517 // See getters for more info. 518 UniqueVector<LabelConfigPair> configs_; 519 UniqueVector<LabelConfigPair> all_dependent_configs_; 520 UniqueVector<LabelConfigPair> public_configs_; 521 522 std::set<Label> allow_circular_includes_from_; 523 524 // Static libraries, shared libraries, and source sets from transitive deps 525 // that need to be linked. 526 InheritedLibraries inherited_libraries_; 527 528 // These libs and dirs are inherited from statically linked deps and all 529 // configs applying to this target. 530 UniqueVector<SourceDir> all_lib_dirs_; 531 UniqueVector<LibFile> all_libs_; 532 533 // These frameworks and dirs are inherited from statically linked deps and 534 // all configs applying to this target. 535 UniqueVector<SourceDir> all_framework_dirs_; 536 UniqueVector<std::string> all_frameworks_; 537 UniqueVector<std::string> all_weak_frameworks_; 538 539 // All hard deps from this target and all dependencies. Filled in when this 540 // target is marked resolved. This will not include the current target. 541 TargetSet recursive_hard_deps_; 542 543 LabelPtrPair<Pool> pool_; 544 545 std::vector<LabelPattern> friends_; 546 std::vector<LabelPattern> assert_no_deps_; 547 548 // Used for all binary targets, and for inputs in regular targets. The 549 // precompiled header values in this struct will be resolved to the ones to 550 // use for this target, if precompiled headers are used. 551 std::unique_ptr<ConfigValues> config_values_; 552 553 // Used for action[_foreach] targets. 554 std::unique_ptr<ActionValues> action_values_; 555 556 // Used for Rust targets. 557 std::unique_ptr<RustValues> rust_values_; 558 559 // Used by all targets, only useful to generate Rust targets though. These 560 // present 2 different views of the public flags: 561 // 562 // Lists all transitive libraries, and for each one the public bit says if 563 // there is a public chain such that this target can make direct use of the 564 // lib. For each library marked public: "I have access to these targets." 565 InheritedLibraries rust_transitive_inherited_libs_; 566 // Lists all transitive libraries, and for each one the public bit says if a 567 // target depending on this target would inherit the libraries as public too. 568 // For each library marked public: "If you depend on me, you get access to 569 // these targets." 570 InheritedLibraries rust_transitive_inheritable_libs_; 571 572 // User for Swift targets. 573 std::unique_ptr<SwiftValues> swift_values_; 574 575 // Toolchain used by this target. Null until target is resolved. 576 const Toolchain* toolchain_ = nullptr; 577 578 // Output files. Empty until the target is resolved. 579 std::vector<OutputFile> computed_outputs_; 580 OutputFile link_output_file_; 581 OutputFile dependency_output_file_; 582 std::vector<OutputFile> runtime_outputs_; 583 584 std::unique_ptr<Metadata> metadata_; 585 586 // GeneratedFile as metadata collection values. 587 std::unique_ptr<GeneratedFile> generated_file_; 588 589 Target(const Target&) = delete; 590 Target& operator=(const Target&) = delete; 591 }; 592 593 extern const char kExecution_Help[]; 594 595 #endif // TOOLS_GN_TARGET_H_ 596