cmTarget.h 11 KB

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