cmTarget.h 11 KB

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