cmTarget.h 9.5 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. 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, std::string const& lib,
  90. cmTargetLinkLibraryType llt);
  91. enum TLLSignature
  92. {
  93. KeywordTLLSignature,
  94. PlainTLLSignature
  95. };
  96. bool PushTLLCommandTrace(TLLSignature signature,
  97. cmListFileContext const& lfc);
  98. void GetTllSignatureTraces(std::ostream& s, TLLSignature sig) const;
  99. /**
  100. * Set the path where this target should be installed. This is relative to
  101. * INSTALL_PREFIX
  102. */
  103. std::string const& GetInstallPath() const;
  104. void SetInstallPath(std::string const& name);
  105. /**
  106. * Set the path where this target (if it has a runtime part) should be
  107. * installed. This is relative to INSTALL_PREFIX
  108. */
  109. std::string const& GetRuntimeInstallPath() const;
  110. void SetRuntimeInstallPath(std::string const& name);
  111. /**
  112. * Get/Set whether there is an install rule for this target.
  113. */
  114. bool GetHaveInstallRule() const;
  115. void SetHaveInstallRule(bool hir);
  116. void AddInstallGenerator(cmInstallTargetGenerator* g);
  117. std::vector<cmInstallTargetGenerator*> const& GetInstallGenerators() const;
  118. /**
  119. * Get/Set whether this target was auto-created by a generator.
  120. */
  121. bool GetIsGeneratorProvided() const;
  122. void SetIsGeneratorProvided(bool igp);
  123. /**
  124. * Add a utility on which this project depends. A utility is an executable
  125. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  126. * commands. It is not a full path nor does it have an extension.
  127. */
  128. void AddUtility(std::string const& name, bool cross,
  129. cmMakefile* mf = nullptr);
  130. //! Get the utilities used by this target
  131. std::set<BT<std::pair<std::string, bool>>> const& GetUtilities() const;
  132. //! Set/Get a property of this target file
  133. void SetProperty(const std::string& prop, const char* value);
  134. void SetProperty(const std::string& prop, const std::string& value)
  135. {
  136. SetProperty(prop, value.c_str());
  137. }
  138. void AppendProperty(const std::string& prop, const std::string& 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 IsPerConfig() const;
  158. bool GetMappedConfig(std::string const& desired_config, const char*& loc,
  159. const char*& imp, std::string& suffix) const;
  160. //! Return whether this target is an executable with symbol exports enabled.
  161. bool IsExecutableWithExports() const;
  162. //! Return whether this target is a shared library Framework on Apple.
  163. bool IsFrameworkOnApple() const;
  164. //! Return whether this target is an executable Bundle on Apple.
  165. bool IsAppBundleOnApple() const;
  166. //! Get a backtrace from the creation of the target.
  167. cmListFileBacktrace const& GetBacktrace() const;
  168. void InsertInclude(std::string const& entry, cmListFileBacktrace const& bt,
  169. bool before = false);
  170. void InsertCompileOption(std::string const& entry,
  171. cmListFileBacktrace const& bt, bool before = false);
  172. void InsertCompileDefinition(std::string const& entry,
  173. cmListFileBacktrace const& bt);
  174. void InsertLinkOption(std::string const& entry,
  175. cmListFileBacktrace const& bt, bool before = false);
  176. void InsertLinkDirectory(std::string const& entry,
  177. cmListFileBacktrace const& bt, bool before = false);
  178. void InsertPrecompileHeader(std::string const& entry,
  179. cmListFileBacktrace const& bt);
  180. void AppendBuildInterfaceIncludes();
  181. std::string GetDebugGeneratorExpressions(const std::string& value,
  182. cmTargetLinkLibraryType llt) const;
  183. void AddSystemIncludeDirectories(std::set<std::string> const& incs);
  184. std::set<std::string> const& GetSystemIncludeDirectories() const;
  185. cmStringRange GetIncludeDirectoriesEntries() const;
  186. cmBacktraceRange GetIncludeDirectoriesBacktraces() const;
  187. cmStringRange GetCompileOptionsEntries() const;
  188. cmBacktraceRange GetCompileOptionsBacktraces() const;
  189. cmStringRange GetCompileFeaturesEntries() const;
  190. cmBacktraceRange GetCompileFeaturesBacktraces() const;
  191. cmStringRange GetCompileDefinitionsEntries() const;
  192. cmBacktraceRange GetCompileDefinitionsBacktraces() const;
  193. cmStringRange GetPrecompileHeadersEntries() const;
  194. cmBacktraceRange GetPrecompileHeadersBacktraces() const;
  195. cmStringRange GetSourceEntries() const;
  196. cmBacktraceRange GetSourceBacktraces() const;
  197. cmStringRange GetLinkOptionsEntries() const;
  198. cmBacktraceRange GetLinkOptionsBacktraces() const;
  199. cmStringRange GetLinkDirectoriesEntries() const;
  200. cmBacktraceRange GetLinkDirectoriesBacktraces() const;
  201. cmStringRange GetLinkImplementationEntries() const;
  202. cmBacktraceRange GetLinkImplementationBacktraces() const;
  203. std::string ImportedGetFullPath(const std::string& config,
  204. cmStateEnums::ArtifactType artifact) const;
  205. struct StrictTargetComparison
  206. {
  207. bool operator()(cmTarget const* t1, cmTarget const* t2) const;
  208. };
  209. private:
  210. // Internal representation details.
  211. friend class cmGeneratorTarget;
  212. const char* GetSuffixVariableInternal(
  213. cmStateEnums::ArtifactType artifact) const;
  214. const char* GetPrefixVariableInternal(
  215. cmStateEnums::ArtifactType artifact) const;
  216. private:
  217. std::unique_ptr<cmTargetInternals> impl;
  218. };
  219. #endif