cmTarget.h 9.5 KB

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