cmTarget.h 12 KB

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