cmTarget.h 10 KB

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