cmTarget.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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);
  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 SetProperty(const std::string& prop, const std::string& value)
  136. {
  137. SetProperty(prop, value.c_str());
  138. }
  139. void AppendProperty(const std::string& prop, const char* value,
  140. bool asString = false);
  141. void AppendProperty(const std::string& prop, const std::string& value,
  142. bool asString = false)
  143. {
  144. AppendProperty(prop, value.c_str(), asString);
  145. }
  146. //! Might return a nullptr if the property is not set or invalid
  147. const char* GetProperty(const std::string& prop) const;
  148. //! Always returns a valid pointer
  149. const char* GetSafeProperty(const std::string& prop) const;
  150. bool GetPropertyAsBool(const std::string& prop) const;
  151. void CheckProperty(const std::string& prop, cmMakefile* context) const;
  152. const char* GetComputedProperty(const std::string& prop,
  153. cmMessenger* messenger,
  154. cmListFileBacktrace const& context) const;
  155. //! Get all properties
  156. cmPropertyMap const& GetProperties() const;
  157. //! Return whether or not the target is for a DLL platform.
  158. bool IsDLLPlatform() const;
  159. //! Return whether or not we are targeting AIX.
  160. bool IsAIX() const;
  161. bool IsImported() const;
  162. bool IsImportedGloballyVisible() const;
  163. bool GetMappedConfig(std::string const& desired_config, const char** loc,
  164. const char** imp, std::string& suffix) const;
  165. //! Return whether this target is an executable with symbol exports enabled.
  166. bool IsExecutableWithExports() const;
  167. //! Return whether this target is a shared library Framework on Apple.
  168. bool IsFrameworkOnApple() const;
  169. //! Return whether this target is an executable Bundle on Apple.
  170. bool IsAppBundleOnApple() const;
  171. //! Get a backtrace from the creation of the target.
  172. cmListFileBacktrace const& GetBacktrace() const;
  173. void InsertInclude(std::string const& entry, cmListFileBacktrace const& bt,
  174. bool before = false);
  175. void InsertCompileOption(std::string const& entry,
  176. cmListFileBacktrace const& bt, bool before = false);
  177. void InsertCompileDefinition(std::string const& entry,
  178. cmListFileBacktrace const& bt);
  179. void InsertLinkOption(std::string const& entry,
  180. cmListFileBacktrace const& bt, bool before = false);
  181. void InsertLinkDirectory(std::string const& entry,
  182. cmListFileBacktrace const& bt, bool before = false);
  183. void InsertPrecompileHeader(std::string const& entry,
  184. cmListFileBacktrace const& bt);
  185. void AppendBuildInterfaceIncludes();
  186. std::string GetDebugGeneratorExpressions(const std::string& value,
  187. cmTargetLinkLibraryType llt) const;
  188. void AddSystemIncludeDirectories(std::set<std::string> const& incs);
  189. std::set<std::string> const& GetSystemIncludeDirectories() const;
  190. cmStringRange GetIncludeDirectoriesEntries() const;
  191. cmBacktraceRange GetIncludeDirectoriesBacktraces() const;
  192. cmStringRange GetCompileOptionsEntries() const;
  193. cmBacktraceRange GetCompileOptionsBacktraces() const;
  194. cmStringRange GetCompileFeaturesEntries() const;
  195. cmBacktraceRange GetCompileFeaturesBacktraces() const;
  196. cmStringRange GetCompileDefinitionsEntries() const;
  197. cmBacktraceRange GetCompileDefinitionsBacktraces() const;
  198. cmStringRange GetPrecompileHeadersEntries() const;
  199. cmBacktraceRange GetPrecompileHeadersBacktraces() const;
  200. cmStringRange GetSourceEntries() const;
  201. cmBacktraceRange GetSourceBacktraces() const;
  202. cmStringRange GetLinkOptionsEntries() const;
  203. cmBacktraceRange GetLinkOptionsBacktraces() const;
  204. cmStringRange GetLinkDirectoriesEntries() const;
  205. cmBacktraceRange GetLinkDirectoriesBacktraces() const;
  206. cmStringRange GetLinkImplementationEntries() const;
  207. cmBacktraceRange GetLinkImplementationBacktraces() const;
  208. std::string ImportedGetFullPath(const std::string& config,
  209. cmStateEnums::ArtifactType artifact) const;
  210. struct StrictTargetComparison
  211. {
  212. bool operator()(cmTarget const* t1, cmTarget const* t2) const;
  213. };
  214. private:
  215. // Internal representation details.
  216. friend class cmGeneratorTarget;
  217. const char* GetSuffixVariableInternal(
  218. cmStateEnums::ArtifactType artifact) const;
  219. const char* GetPrefixVariableInternal(
  220. cmStateEnums::ArtifactType artifact) const;
  221. private:
  222. std::unique_ptr<cmTargetInternals> impl;
  223. };
  224. #endif