cmTarget.h 9.4 KB

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