cmTarget.h 10 KB

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