cmTarget.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmTarget_h
  4. #define cmTarget_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <iosfwd>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include "cmAlgorithms.h"
  13. #include "cmListFileCache.h"
  14. #include "cmPolicies.h"
  15. #include "cmStateTypes.h"
  16. #include "cmTargetLinkLibraryType.h"
  17. class cmCustomCommand;
  18. class cmGlobalGenerator;
  19. class cmMakefile;
  20. class cmMessenger;
  21. class cmPropertyMap;
  22. class cmSourceFile;
  23. class cmTargetInternals;
  24. /** \class cmTarget
  25. * \brief Represent a library or executable target loaded from a makefile.
  26. *
  27. * cmTarget represents a target loaded from a makefile.
  28. */
  29. class cmTarget
  30. {
  31. public:
  32. enum Visibility
  33. {
  34. VisibilityNormal,
  35. VisibilityImported,
  36. VisibilityImportedGlobally
  37. };
  38. enum CustomCommandType
  39. {
  40. PRE_BUILD,
  41. PRE_LINK,
  42. POST_BUILD
  43. };
  44. cmTarget(std::string const& name, cmStateEnums::TargetType type,
  45. Visibility vis, cmMakefile* mf);
  46. cmTarget(cmTarget const&) = delete;
  47. cmTarget(cmTarget&&) noexcept;
  48. ~cmTarget();
  49. cmTarget& operator=(cmTarget const&) = delete;
  50. cmTarget& operator=(cmTarget&&) noexcept;
  51. //! Return the type of target.
  52. cmStateEnums::TargetType GetType() const;
  53. //! Get the cmMakefile that owns this target.
  54. cmMakefile* GetMakefile() const;
  55. //! Return the global generator.
  56. cmGlobalGenerator* GetGlobalGenerator() const;
  57. //! Set/Get the name of the target
  58. const std::string& GetName() const;
  59. //! Get the policy map
  60. cmPolicies::PolicyMap const& GetPolicyMap() const;
  61. //! Get policy status
  62. cmPolicies::PolicyStatus GetPolicyStatus(cmPolicies::PolicyID policy) const;
  63. #define DECLARE_TARGET_POLICY(POLICY) \
  64. cmPolicies::PolicyStatus GetPolicyStatus##POLICY() const \
  65. { \
  66. return this->GetPolicyStatus(cmPolicies::POLICY); \
  67. }
  68. CM_FOR_EACH_TARGET_POLICY(DECLARE_TARGET_POLICY)
  69. #undef DECLARE_TARGET_POLICY
  70. //! Get the list of the PRE_BUILD custom commands for this target
  71. std::vector<cmCustomCommand> const& GetPreBuildCommands() const;
  72. void AddPreBuildCommand(cmCustomCommand const& cmd);
  73. //! Get the list of the PRE_LINK custom commands for this target
  74. std::vector<cmCustomCommand> const& GetPreLinkCommands() const;
  75. void AddPreLinkCommand(cmCustomCommand const& cmd);
  76. //! Get the list of the POST_BUILD custom commands for this target
  77. std::vector<cmCustomCommand> const& GetPostBuildCommands() const;
  78. void AddPostBuildCommand(cmCustomCommand const& cmd);
  79. //! Add sources to the target.
  80. void AddSources(std::vector<std::string> const& srcs);
  81. void AddTracedSources(std::vector<std::string> const& srcs);
  82. cmSourceFile* AddSourceCMP0049(const std::string& src);
  83. cmSourceFile* AddSource(const std::string& src, bool before = false);
  84. //! how we identify a library, by name and type
  85. typedef std::pair<std::string, cmTargetLinkLibraryType> LibraryID;
  86. typedef std::vector<LibraryID> LinkLibraryVectorType;
  87. LinkLibraryVectorType const& GetOriginalLinkLibraries() const;
  88. //! Clear the dependency information recorded for this target, if any.
  89. void ClearDependencyInformation(cmMakefile& mf);
  90. void AddLinkLibrary(cmMakefile& mf, const std::string& lib,
  91. cmTargetLinkLibraryType llt);
  92. void AddLinkLibrary(cmMakefile& mf, std::string const& lib,
  93. std::string const& libRef, cmTargetLinkLibraryType llt);
  94. enum TLLSignature
  95. {
  96. KeywordTLLSignature,
  97. PlainTLLSignature
  98. };
  99. bool PushTLLCommandTrace(TLLSignature signature,
  100. cmListFileContext const& lfc);
  101. void GetTllSignatureTraces(std::ostream& s, TLLSignature sig) const;
  102. /**
  103. * Set the path where this target should be installed. This is relative to
  104. * INSTALL_PREFIX
  105. */
  106. std::string const& GetInstallPath() const;
  107. void SetInstallPath(std::string const& name);
  108. /**
  109. * Set the path where this target (if it has a runtime part) should be
  110. * installed. This is relative to INSTALL_PREFIX
  111. */
  112. std::string const& GetRuntimeInstallPath() const;
  113. void SetRuntimeInstallPath(std::string const& name);
  114. /**
  115. * Get/Set whether there is an install rule for this target.
  116. */
  117. bool GetHaveInstallRule() const;
  118. void SetHaveInstallRule(bool hir);
  119. /**
  120. * Get/Set whether this target was auto-created by a generator.
  121. */
  122. bool GetIsGeneratorProvided() const;
  123. void SetIsGeneratorProvided(bool igp);
  124. /**
  125. * Add a utility on which this project depends. A utility is an executable
  126. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  127. * commands. It is not a full path nor does it have an extension.
  128. */
  129. void AddUtility(std::string const& name, cmMakefile* mf = nullptr);
  130. //! Get the utilities used by this target
  131. std::set<BT<std::string>> const& GetUtilities() const;
  132. //! Set/Get a property of this target file
  133. void SetProperty(const std::string& prop, const char* value);
  134. void AppendProperty(const std::string& prop, const char* value,
  135. bool asString = false);
  136. //! Might return a nullptr if the property is not set or invalid
  137. const char* GetProperty(const std::string& prop) const;
  138. //! Always returns a valid pointer
  139. const char* GetSafeProperty(const std::string& prop) const;
  140. bool GetPropertyAsBool(const std::string& prop) const;
  141. void CheckProperty(const std::string& prop, cmMakefile* context) const;
  142. const char* GetComputedProperty(const std::string& prop,
  143. cmMessenger* messenger,
  144. cmListFileBacktrace const& context) const;
  145. //! Get all properties
  146. cmPropertyMap const& GetProperties() const;
  147. //! Return whether or not the target is for a DLL platform.
  148. bool IsDLLPlatform() const;
  149. bool IsImported() const;
  150. bool IsImportedGloballyVisible() const;
  151. bool GetMappedConfig(std::string const& desired_config, const char** loc,
  152. const char** imp, std::string& suffix) const;
  153. //! Return whether this target is an executable with symbol exports enabled.
  154. bool IsExecutableWithExports() const;
  155. //! Return whether this target is a shared library Framework on Apple.
  156. bool IsFrameworkOnApple() const;
  157. //! Return whether this target is an executable Bundle on Apple.
  158. bool IsAppBundleOnApple() const;
  159. //! Get a backtrace from the creation of the target.
  160. cmListFileBacktrace const& GetBacktrace() const;
  161. void InsertInclude(std::string const& entry, cmListFileBacktrace const& bt,
  162. bool before = false);
  163. void InsertCompileOption(std::string const& entry,
  164. cmListFileBacktrace const& bt, bool before = false);
  165. void InsertCompileDefinition(std::string const& entry,
  166. cmListFileBacktrace const& bt);
  167. void InsertLinkOption(std::string const& entry,
  168. cmListFileBacktrace const& bt, bool before = false);
  169. void InsertLinkDirectory(std::string const& entry,
  170. cmListFileBacktrace const& bt, bool before = false);
  171. void AppendBuildInterfaceIncludes();
  172. std::string GetDebugGeneratorExpressions(const std::string& value,
  173. cmTargetLinkLibraryType llt) const;
  174. void AddSystemIncludeDirectories(std::set<std::string> const& incs);
  175. std::set<std::string> const& GetSystemIncludeDirectories() const;
  176. cmStringRange GetIncludeDirectoriesEntries() const;
  177. cmBacktraceRange GetIncludeDirectoriesBacktraces() const;
  178. cmStringRange GetCompileOptionsEntries() const;
  179. cmBacktraceRange GetCompileOptionsBacktraces() const;
  180. cmStringRange GetCompileFeaturesEntries() const;
  181. cmBacktraceRange GetCompileFeaturesBacktraces() const;
  182. cmStringRange GetCompileDefinitionsEntries() const;
  183. cmBacktraceRange GetCompileDefinitionsBacktraces() const;
  184. cmStringRange GetSourceEntries() const;
  185. cmBacktraceRange GetSourceBacktraces() const;
  186. cmStringRange GetLinkOptionsEntries() const;
  187. cmBacktraceRange GetLinkOptionsBacktraces() const;
  188. cmStringRange GetLinkDirectoriesEntries() const;
  189. cmBacktraceRange GetLinkDirectoriesBacktraces() const;
  190. cmStringRange GetLinkImplementationEntries() const;
  191. cmBacktraceRange GetLinkImplementationBacktraces() const;
  192. std::string ImportedGetFullPath(const std::string& config,
  193. cmStateEnums::ArtifactType artifact) const;
  194. struct StrictTargetComparison
  195. {
  196. bool operator()(cmTarget const* t1, cmTarget const* t2) const;
  197. };
  198. private:
  199. // Internal representation details.
  200. friend class cmGeneratorTarget;
  201. const char* GetSuffixVariableInternal(
  202. cmStateEnums::ArtifactType artifact) const;
  203. const char* GetPrefixVariableInternal(
  204. cmStateEnums::ArtifactType artifact) const;
  205. private:
  206. std::unique_ptr<cmTargetInternals> impl;
  207. };
  208. #endif