cmTarget.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 <cm/optional>
  12. #include "cmAlgorithms.h"
  13. #include "cmListFileCache.h"
  14. #include "cmPolicies.h"
  15. #include "cmStateTypes.h"
  16. #include "cmStringAlgorithms.h"
  17. #include "cmTargetLinkLibraryType.h"
  18. #include "cmValue.h"
  19. class cmCustomCommand;
  20. class cmFileSet;
  21. class cmFindPackageStack;
  22. class cmGlobalGenerator;
  23. class cmInstallTargetGenerator;
  24. class cmMakefile;
  25. class cmPropertyMap;
  26. class cmSourceFile;
  27. class cmTargetExport;
  28. class cmTargetInternals;
  29. enum class cmFileSetVisibility;
  30. /** \class cmTarget
  31. * \brief Represent a library or executable target loaded from a makefile.
  32. *
  33. * cmTarget represents a target loaded from a makefile.
  34. */
  35. class cmTarget
  36. {
  37. public:
  38. enum class Visibility
  39. {
  40. Normal,
  41. Generated,
  42. Imported,
  43. ImportedGlobally,
  44. Foreign,
  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. const std::string& GetTemplateName() const;
  67. //! Get the policy map
  68. cmPolicies::PolicyMap const& GetPolicyMap() const;
  69. //! Get policy status
  70. cmPolicies::PolicyStatus GetPolicyStatus(cmPolicies::PolicyID policy) const;
  71. #define DECLARE_TARGET_POLICY(POLICY) \
  72. cmPolicies::PolicyStatus GetPolicyStatus##POLICY() const \
  73. { \
  74. return this->GetPolicyStatus(cmPolicies::POLICY); \
  75. }
  76. CM_FOR_EACH_TARGET_POLICY(DECLARE_TARGET_POLICY)
  77. #undef DECLARE_TARGET_POLICY
  78. //! Get the list of the PRE_BUILD custom commands for this target
  79. std::vector<cmCustomCommand> const& GetPreBuildCommands() const;
  80. void AddPreBuildCommand(cmCustomCommand const& cmd);
  81. void AddPreBuildCommand(cmCustomCommand&& cmd);
  82. //! Get the list of the PRE_LINK custom commands for this target
  83. std::vector<cmCustomCommand> const& GetPreLinkCommands() const;
  84. void AddPreLinkCommand(cmCustomCommand const& cmd);
  85. void AddPreLinkCommand(cmCustomCommand&& cmd);
  86. //! Get the list of the POST_BUILD custom commands for this target
  87. std::vector<cmCustomCommand> const& GetPostBuildCommands() const;
  88. void AddPostBuildCommand(cmCustomCommand const& cmd);
  89. void AddPostBuildCommand(cmCustomCommand&& cmd);
  90. //! Add sources to the target.
  91. void AddSources(std::vector<std::string> const& srcs);
  92. void AddTracedSources(std::vector<std::string> const& srcs);
  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 IsForeign() const;
  180. bool IsPerConfig() const;
  181. bool IsRuntimeBinary() const;
  182. bool CanCompileSources() const;
  183. bool GetMappedConfig(std::string const& desired_config, cmValue& loc,
  184. cmValue& imp, std::string& suffix) const;
  185. //! Return whether this target is an executable with symbol exports enabled.
  186. bool IsExecutableWithExports() const;
  187. //! Return whether this target is a shared library with symbol exports
  188. //! enabled.
  189. bool IsSharedLibraryWithExports() const;
  190. //! Return whether this target is a shared library Framework on Apple.
  191. bool IsFrameworkOnApple() const;
  192. //! Return whether to archive shared library or not on AIX.
  193. bool IsArchivedAIXSharedLibrary() const;
  194. //! Return whether this target is an executable Bundle on Apple.
  195. bool IsAppBundleOnApple() const;
  196. //! Return whether this target is a GUI executable on Android.
  197. bool IsAndroidGuiExecutable() const;
  198. bool HasKnownObjectFileLocation(std::string* reason = nullptr) const;
  199. //! Get a backtrace from the creation of the target.
  200. cmListFileBacktrace const& GetBacktrace() const;
  201. //! Get a find_package call stack from the creation of the target.
  202. cmFindPackageStack const& GetFindPackageStack() const;
  203. void InsertInclude(BT<std::string> const& entry, bool before = false);
  204. void InsertCompileOption(BT<std::string> const& entry, bool before = false);
  205. void InsertCompileDefinition(BT<std::string> const& entry);
  206. void InsertLinkOption(BT<std::string> const& entry, bool before = false);
  207. void InsertLinkDirectory(BT<std::string> const& entry, bool before = false);
  208. void InsertPrecompileHeader(BT<std::string> const& entry);
  209. void AppendBuildInterfaceIncludes();
  210. void FinalizeTargetConfiguration(const cmBTStringRange& compileDefinitions);
  211. std::string GetDebugGeneratorExpressions(const std::string& value,
  212. cmTargetLinkLibraryType llt) const;
  213. void AddSystemIncludeDirectories(std::set<std::string> const& incs);
  214. std::set<std::string> const& GetSystemIncludeDirectories() const;
  215. void AddInstallIncludeDirectories(cmTargetExport const& te,
  216. cmStringRange const& incs);
  217. cmStringRange GetInstallIncludeDirectoriesEntries(
  218. cmTargetExport const& te) const;
  219. BTs<std::string> const* GetLanguageStandardProperty(
  220. const std::string& propertyName) const;
  221. void SetLanguageStandardProperty(std::string const& lang,
  222. std::string const& value,
  223. const std::string& feature);
  224. cmBTStringRange GetIncludeDirectoriesEntries() const;
  225. cmBTStringRange GetCompileOptionsEntries() const;
  226. cmBTStringRange GetCompileFeaturesEntries() const;
  227. cmBTStringRange GetCompileDefinitionsEntries() const;
  228. cmBTStringRange GetPrecompileHeadersEntries() const;
  229. cmBTStringRange GetSourceEntries() const;
  230. cmBTStringRange GetLinkOptionsEntries() const;
  231. cmBTStringRange GetLinkDirectoriesEntries() const;
  232. cmBTStringRange GetLinkImplementationEntries() const;
  233. cmBTStringRange GetLinkInterfaceEntries() const;
  234. cmBTStringRange GetLinkInterfaceDirectEntries() const;
  235. cmBTStringRange GetLinkInterfaceDirectExcludeEntries() const;
  236. void CopyPolicyStatuses(cmTarget const* tgt);
  237. void CopyImportedCxxModulesEntries(cmTarget const* tgt);
  238. void CopyImportedCxxModulesProperties(cmTarget const* tgt);
  239. cmBTStringRange GetHeaderSetsEntries() const;
  240. cmBTStringRange GetCxxModuleSetsEntries() const;
  241. cmBTStringRange GetInterfaceHeaderSetsEntries() const;
  242. cmBTStringRange GetInterfaceCxxModuleSetsEntries() const;
  243. std::string ImportedGetFullPath(const std::string& config,
  244. cmStateEnums::ArtifactType artifact) const;
  245. struct StrictTargetComparison
  246. {
  247. bool operator()(cmTarget const* t1, cmTarget const* t2) const;
  248. };
  249. const cmFileSet* GetFileSet(const std::string& name) const;
  250. cmFileSet* GetFileSet(const std::string& name);
  251. std::pair<cmFileSet*, bool> GetOrCreateFileSet(const std::string& name,
  252. const std::string& type,
  253. cmFileSetVisibility vis);
  254. std::vector<std::string> GetAllFileSetNames() const;
  255. std::vector<std::string> GetAllInterfaceFileSets() const;
  256. static std::string GetFileSetsPropertyName(const std::string& type);
  257. static std::string GetInterfaceFileSetsPropertyName(const std::string& type);
  258. bool HasFileSets() const;
  259. private:
  260. // Internal representation details.
  261. friend class cmGeneratorTarget;
  262. const char* GetSuffixVariableInternal(
  263. cmStateEnums::ArtifactType artifact) const;
  264. const char* GetPrefixVariableInternal(
  265. cmStateEnums::ArtifactType artifact) const;
  266. std::unique_ptr<cmTargetInternals> impl;
  267. };