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