cmTarget.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. Foreign,
  46. };
  47. enum class PerConfig
  48. {
  49. Yes,
  50. No
  51. };
  52. cmTarget(std::string const& name, cmStateEnums::TargetType type,
  53. Visibility vis, cmMakefile* mf, PerConfig perConfig);
  54. cmTarget(cmTarget const&) = delete;
  55. cmTarget(cmTarget&&) noexcept;
  56. ~cmTarget();
  57. cmTarget& operator=(cmTarget const&) = delete;
  58. cmTarget& operator=(cmTarget&&) noexcept;
  59. //! Return the type of target.
  60. cmStateEnums::TargetType GetType() const;
  61. //! Get the cmMakefile that owns this target.
  62. cmMakefile* GetMakefile() const;
  63. //! Return the global generator.
  64. cmGlobalGenerator* GetGlobalGenerator() const;
  65. //! Set/Get the name of the target
  66. const std::string& GetName() const;
  67. const std::string& GetTemplateName() 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 const* mf = nullptr);
  143. void AddUtility(BT<std::pair<std::string, bool>> util);
  144. void AddCodegenDependency(std::string const& name);
  145. std::set<std::string> const& GetCodegenDeps() const;
  146. //! Get the utilities used by this target
  147. std::set<BT<std::pair<std::string, bool>>> const& GetUtilities() const;
  148. //! Set/Get a property of this target file
  149. void SetProperty(const std::string& prop, cmValue value);
  150. void SetProperty(const std::string& prop, std::nullptr_t)
  151. {
  152. this->SetProperty(prop, cmValue{ nullptr });
  153. }
  154. void SetProperty(const std::string& prop, const std::string& value)
  155. {
  156. this->SetProperty(prop, cmValue(value));
  157. }
  158. void AppendProperty(
  159. const std::string& prop, const std::string& value,
  160. cm::optional<cmListFileBacktrace> const& bt = cm::nullopt,
  161. bool asString = false);
  162. //! Might return a nullptr if the property is not set or invalid
  163. cmValue GetProperty(const std::string& prop) const;
  164. //! Always returns a valid pointer
  165. std::string const& GetSafeProperty(std::string const& prop) const;
  166. bool GetPropertyAsBool(const std::string& prop) const;
  167. void CheckProperty(const std::string& prop, cmMakefile* context) const;
  168. cmValue GetComputedProperty(const std::string& prop, cmMakefile& mf) const;
  169. //! Get all properties
  170. cmPropertyMap const& GetProperties() const;
  171. //! Return whether or not the target is for a DLL platform.
  172. bool IsDLLPlatform() const;
  173. //! Return whether or not we are targeting AIX.
  174. bool IsAIX() const;
  175. //! Return whether or not we are targeting Apple.
  176. bool IsApple() const;
  177. bool IsNormal() const;
  178. bool IsSynthetic() const;
  179. bool IsImported() const;
  180. bool IsImportedGloballyVisible() const;
  181. bool IsForeign() const;
  182. bool IsPerConfig() const;
  183. bool IsRuntimeBinary() const;
  184. bool CanCompileSources() const;
  185. bool GetMappedConfig(std::string const& desired_config, cmValue& loc,
  186. cmValue& imp, std::string& suffix) const;
  187. //! Return whether this target is an executable with symbol exports enabled.
  188. bool IsExecutableWithExports() const;
  189. //! Return whether this target is a shared library with symbol exports
  190. //! enabled.
  191. bool IsSharedLibraryWithExports() const;
  192. //! Return whether this target is a shared library Framework on Apple.
  193. bool IsFrameworkOnApple() const;
  194. //! Return whether to archive shared library or not on AIX.
  195. bool IsArchivedAIXSharedLibrary() const;
  196. //! Return whether this target is an executable Bundle on Apple.
  197. bool IsAppBundleOnApple() const;
  198. //! Return whether this target is a GUI executable on Android.
  199. bool IsAndroidGuiExecutable() const;
  200. bool HasKnownObjectFileLocation(std::string* reason = nullptr) const;
  201. //! Get a backtrace from the creation of the target.
  202. cmListFileBacktrace const& GetBacktrace() const;
  203. //! Get a find_package call stack from the creation of the target.
  204. cmFindPackageStack const& GetFindPackageStack() const;
  205. void InsertInclude(BT<std::string> const& entry, bool before = false);
  206. void InsertCompileOption(BT<std::string> const& entry, bool before = false);
  207. void InsertCompileDefinition(BT<std::string> const& entry);
  208. void InsertLinkOption(BT<std::string> const& entry, bool before = false);
  209. void InsertLinkDirectory(BT<std::string> const& entry, bool before = false);
  210. void InsertPrecompileHeader(BT<std::string> const& entry);
  211. void AppendBuildInterfaceIncludes();
  212. void FinalizeTargetConfiguration(
  213. const cmBTStringRange& noConfigCompileDefinitions,
  214. cm::optional<std::map<std::string, cmValue>>& perConfigCompileDefinitions);
  215. std::string GetDebugGeneratorExpressions(const std::string& value,
  216. cmTargetLinkLibraryType llt) const;
  217. void AddSystemIncludeDirectories(std::set<std::string> const& incs);
  218. std::set<std::string> const& GetSystemIncludeDirectories() const;
  219. void AddInstallIncludeDirectories(cmTargetExport const& te,
  220. cmStringRange const& incs);
  221. cmStringRange GetInstallIncludeDirectoriesEntries(
  222. cmTargetExport const& te) const;
  223. BTs<std::string> const* GetLanguageStandardProperty(
  224. const std::string& propertyName) const;
  225. void SetLanguageStandardProperty(std::string const& lang,
  226. std::string const& value,
  227. const std::string& feature);
  228. cmBTStringRange GetIncludeDirectoriesEntries() const;
  229. cmBTStringRange GetCompileOptionsEntries() const;
  230. cmBTStringRange GetCompileFeaturesEntries() const;
  231. cmBTStringRange GetCompileDefinitionsEntries() const;
  232. cmBTStringRange GetPrecompileHeadersEntries() const;
  233. cmBTStringRange GetSourceEntries() const;
  234. cmBTStringRange GetLinkOptionsEntries() const;
  235. cmBTStringRange GetLinkDirectoriesEntries() const;
  236. cmBTStringRange GetLinkImplementationEntries() const;
  237. cmBTStringRange GetLinkInterfaceEntries() const;
  238. cmBTStringRange GetLinkInterfaceDirectEntries() const;
  239. cmBTStringRange GetLinkInterfaceDirectExcludeEntries() const;
  240. void CopyPolicyStatuses(cmTarget const* tgt);
  241. void CopyImportedCxxModulesEntries(cmTarget const* tgt);
  242. void CopyImportedCxxModulesProperties(cmTarget const* tgt);
  243. cmBTStringRange GetHeaderSetsEntries() const;
  244. cmBTStringRange GetCxxModuleSetsEntries() const;
  245. cmBTStringRange GetInterfaceHeaderSetsEntries() const;
  246. cmBTStringRange GetInterfaceCxxModuleSetsEntries() const;
  247. std::string ImportedGetFullPath(const std::string& config,
  248. cmStateEnums::ArtifactType artifact) const;
  249. struct StrictTargetComparison
  250. {
  251. bool operator()(cmTarget const* t1, cmTarget const* t2) const;
  252. };
  253. const cmFileSet* GetFileSet(const std::string& name) const;
  254. cmFileSet* GetFileSet(const std::string& name);
  255. std::pair<cmFileSet*, bool> GetOrCreateFileSet(const std::string& name,
  256. const std::string& type,
  257. cmFileSetVisibility vis);
  258. std::vector<std::string> GetAllFileSetNames() const;
  259. std::vector<std::string> GetAllInterfaceFileSets() const;
  260. static std::string GetFileSetsPropertyName(const std::string& type);
  261. static std::string GetInterfaceFileSetsPropertyName(const std::string& type);
  262. bool HasFileSets() const;
  263. private:
  264. // Internal representation details.
  265. friend class cmGeneratorTarget;
  266. const char* GetSuffixVariableInternal(
  267. cmStateEnums::ArtifactType artifact) const;
  268. const char* GetPrefixVariableInternal(
  269. cmStateEnums::ArtifactType artifact) const;
  270. std::unique_ptr<cmTargetInternals> impl;
  271. };