cmTarget.h 12 KB

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