cmTarget.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 "cmListFileCache.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 cmFileSet;
  22. class cmFindPackageStack;
  23. class cmGlobalGenerator;
  24. class cmInstallTargetGenerator;
  25. class cmMakefile;
  26. class cmPropertyMap;
  27. class cmSourceFile;
  28. class cmTargetExport;
  29. class cmTargetInternals;
  30. enum class cmFileSetVisibility;
  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 class Visibility
  40. {
  41. Normal,
  42. Generated,
  43. Imported,
  44. ImportedGlobally,
  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 const* mf = nullptr);
  141. void AddUtility(BT<std::pair<std::string, bool>> util);
  142. void AddCodegenDependency(std::string const& name);
  143. std::set<std::string> const& GetCodegenDeps() const;
  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, cmValue value);
  148. void SetProperty(const std::string& prop, std::nullptr_t)
  149. {
  150. this->SetProperty(prop, cmValue{ nullptr });
  151. }
  152. void SetProperty(const std::string& prop, const std::string& value)
  153. {
  154. this->SetProperty(prop, cmValue(value));
  155. }
  156. void AppendProperty(
  157. const std::string& prop, const std::string& value,
  158. cm::optional<cmListFileBacktrace> const& bt = cm::nullopt,
  159. bool asString = false);
  160. //! Might return a nullptr if the property is not set or invalid
  161. cmValue GetProperty(const std::string& prop) const;
  162. //! Always returns a valid pointer
  163. std::string const& GetSafeProperty(std::string const& prop) const;
  164. bool GetPropertyAsBool(const std::string& prop) const;
  165. void CheckProperty(const std::string& prop, cmMakefile* context) const;
  166. cmValue GetComputedProperty(const std::string& prop, cmMakefile& mf) const;
  167. //! Get all properties
  168. cmPropertyMap const& GetProperties() const;
  169. //! Return whether or not the target is for a DLL platform.
  170. bool IsDLLPlatform() const;
  171. //! Return whether or not we are targeting AIX.
  172. bool IsAIX() const;
  173. //! Return whether or not we are targeting Apple.
  174. bool IsApple() const;
  175. bool IsNormal() const;
  176. bool IsSynthetic() const;
  177. bool IsImported() const;
  178. bool IsImportedGloballyVisible() const;
  179. bool IsPerConfig() const;
  180. bool IsRuntimeBinary() const;
  181. bool CanCompileSources() const;
  182. bool GetMappedConfig(std::string const& desired_config, cmValue& loc,
  183. cmValue& imp, std::string& suffix) const;
  184. //! Return whether this target is an executable with symbol exports enabled.
  185. bool IsExecutableWithExports() const;
  186. //! Return whether this target is a shared library with symbol exports
  187. //! enabled.
  188. bool IsSharedLibraryWithExports() const;
  189. //! Return whether this target is a shared library Framework on Apple.
  190. bool IsFrameworkOnApple() const;
  191. //! Return whether this target is an executable Bundle on Apple.
  192. bool IsAppBundleOnApple() const;
  193. //! Return whether this target is a GUI executable on Android.
  194. bool IsAndroidGuiExecutable() const;
  195. bool HasKnownObjectFileLocation(std::string* reason = nullptr) const;
  196. //! Get a backtrace from the creation of the target.
  197. cmListFileBacktrace const& GetBacktrace() const;
  198. //! Get a find_package call stack from the creation of the target.
  199. cmFindPackageStack const& GetFindPackageStack() const;
  200. void InsertInclude(BT<std::string> const& entry, bool before = false);
  201. void InsertCompileOption(BT<std::string> const& entry, bool before = false);
  202. void InsertCompileDefinition(BT<std::string> const& entry);
  203. void InsertLinkOption(BT<std::string> const& entry, bool before = false);
  204. void InsertLinkDirectory(BT<std::string> const& entry, bool before = false);
  205. void InsertPrecompileHeader(BT<std::string> const& entry);
  206. void AppendBuildInterfaceIncludes();
  207. void FinalizeTargetConfiguration(
  208. const cmBTStringRange& noConfigCompileDefinitions,
  209. cm::optional<std::map<std::string, cmValue>>& perConfigCompileDefinitions);
  210. std::string GetDebugGeneratorExpressions(const std::string& value,
  211. cmTargetLinkLibraryType llt) const;
  212. void AddSystemIncludeDirectories(std::set<std::string> const& incs);
  213. std::set<std::string> const& GetSystemIncludeDirectories() const;
  214. void AddInstallIncludeDirectories(cmTargetExport const& te,
  215. cmStringRange const& incs);
  216. cmStringRange GetInstallIncludeDirectoriesEntries(
  217. cmTargetExport const& te) const;
  218. BTs<std::string> const* GetLanguageStandardProperty(
  219. const std::string& propertyName) const;
  220. void SetLanguageStandardProperty(std::string const& lang,
  221. std::string const& value,
  222. const std::string& feature);
  223. cmBTStringRange GetIncludeDirectoriesEntries() const;
  224. cmBTStringRange GetCompileOptionsEntries() const;
  225. cmBTStringRange GetCompileFeaturesEntries() const;
  226. cmBTStringRange GetCompileDefinitionsEntries() const;
  227. cmBTStringRange GetPrecompileHeadersEntries() const;
  228. cmBTStringRange GetSourceEntries() const;
  229. cmBTStringRange GetLinkOptionsEntries() const;
  230. cmBTStringRange GetLinkDirectoriesEntries() const;
  231. cmBTStringRange GetLinkImplementationEntries() const;
  232. cmBTStringRange GetLinkInterfaceEntries() const;
  233. cmBTStringRange GetLinkInterfaceDirectEntries() const;
  234. cmBTStringRange GetLinkInterfaceDirectExcludeEntries() const;
  235. void CopyPolicyStatuses(cmTarget const* tgt);
  236. void CopyImportedCxxModulesEntries(cmTarget const* tgt);
  237. void CopyImportedCxxModulesProperties(cmTarget const* tgt);
  238. cmBTStringRange GetHeaderSetsEntries() const;
  239. cmBTStringRange GetCxxModuleSetsEntries() const;
  240. cmBTStringRange GetInterfaceHeaderSetsEntries() const;
  241. cmBTStringRange GetInterfaceCxxModuleSetsEntries() const;
  242. std::string ImportedGetFullPath(const std::string& config,
  243. cmStateEnums::ArtifactType artifact) const;
  244. struct StrictTargetComparison
  245. {
  246. bool operator()(cmTarget const* t1, cmTarget const* t2) const;
  247. };
  248. const cmFileSet* GetFileSet(const std::string& name) const;
  249. cmFileSet* GetFileSet(const std::string& name);
  250. std::pair<cmFileSet*, bool> GetOrCreateFileSet(const std::string& name,
  251. const std::string& type,
  252. cmFileSetVisibility vis);
  253. std::vector<std::string> GetAllFileSetNames() const;
  254. std::vector<std::string> GetAllInterfaceFileSets() const;
  255. static std::string GetFileSetsPropertyName(const std::string& type);
  256. static std::string GetInterfaceFileSetsPropertyName(const std::string& type);
  257. bool HasFileSets() const;
  258. private:
  259. template <typename ValueType>
  260. void StoreProperty(const std::string& prop, ValueType value);
  261. // Internal representation details.
  262. friend class cmGeneratorTarget;
  263. const char* GetSuffixVariableInternal(
  264. cmStateEnums::ArtifactType artifact) const;
  265. const char* GetPrefixVariableInternal(
  266. cmStateEnums::ArtifactType artifact) const;
  267. std::unique_ptr<cmTargetInternals> impl;
  268. };