cmTarget.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 "cmFileSet.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 cmGlobalGenerator;
  22. class cmInstallTargetGenerator;
  23. class cmListFileBacktrace;
  24. class cmListFileContext;
  25. class cmMakefile;
  26. class cmPropertyMap;
  27. class cmSourceFile;
  28. class cmTargetExport;
  29. class cmTargetInternals;
  30. template <typename T>
  31. class BT;
  32. template <typename T>
  33. class BTs;
  34. /** \class cmTarget
  35. * \brief Represent a library or executable target loaded from a makefile.
  36. *
  37. * cmTarget represents a target loaded from a makefile.
  38. */
  39. class cmTarget
  40. {
  41. public:
  42. enum class Visibility
  43. {
  44. Normal,
  45. Generated,
  46. Imported,
  47. ImportedGlobally,
  48. };
  49. enum class PerConfig
  50. {
  51. Yes,
  52. No
  53. };
  54. cmTarget(std::string const& name, cmStateEnums::TargetType type,
  55. Visibility vis, cmMakefile* mf, PerConfig perConfig);
  56. cmTarget(cmTarget const&) = delete;
  57. cmTarget(cmTarget&&) noexcept;
  58. ~cmTarget();
  59. cmTarget& operator=(cmTarget const&) = delete;
  60. cmTarget& operator=(cmTarget&&) noexcept;
  61. //! Return the type of target.
  62. cmStateEnums::TargetType GetType() const;
  63. //! Get the cmMakefile that owns this target.
  64. cmMakefile* GetMakefile() const;
  65. //! Return the global generator.
  66. cmGlobalGenerator* GetGlobalGenerator() const;
  67. //! Set/Get the name of the target
  68. const std::string& GetName() const;
  69. //! Get the policy map
  70. cmPolicies::PolicyMap const& GetPolicyMap() const;
  71. //! Get policy status
  72. cmPolicies::PolicyStatus GetPolicyStatus(cmPolicies::PolicyID policy) const;
  73. #define DECLARE_TARGET_POLICY(POLICY) \
  74. cmPolicies::PolicyStatus GetPolicyStatus##POLICY() const \
  75. { \
  76. return this->GetPolicyStatus(cmPolicies::POLICY); \
  77. }
  78. CM_FOR_EACH_TARGET_POLICY(DECLARE_TARGET_POLICY)
  79. #undef DECLARE_TARGET_POLICY
  80. //! Get the list of the PRE_BUILD custom commands for this target
  81. std::vector<cmCustomCommand> const& GetPreBuildCommands() const;
  82. void AddPreBuildCommand(cmCustomCommand const& cmd);
  83. void AddPreBuildCommand(cmCustomCommand&& cmd);
  84. //! Get the list of the PRE_LINK custom commands for this target
  85. std::vector<cmCustomCommand> const& GetPreLinkCommands() const;
  86. void AddPreLinkCommand(cmCustomCommand const& cmd);
  87. void AddPreLinkCommand(cmCustomCommand&& cmd);
  88. //! Get the list of the POST_BUILD custom commands for this target
  89. std::vector<cmCustomCommand> const& GetPostBuildCommands() const;
  90. void AddPostBuildCommand(cmCustomCommand const& cmd);
  91. void AddPostBuildCommand(cmCustomCommand&& cmd);
  92. //! Add sources to the target.
  93. void AddSources(std::vector<std::string> const& srcs);
  94. void AddTracedSources(std::vector<std::string> const& srcs);
  95. std::string GetSourceCMP0049(const std::string& src);
  96. cmSourceFile* AddSource(const std::string& src, bool before = false);
  97. //! how we identify a library, by name and type
  98. using LibraryID = std::pair<std::string, cmTargetLinkLibraryType>;
  99. using LinkLibraryVectorType = std::vector<LibraryID>;
  100. LinkLibraryVectorType const& GetOriginalLinkLibraries() const;
  101. //! Clear the dependency information recorded for this target, if any.
  102. void ClearDependencyInformation(cmMakefile& mf) const;
  103. void AddLinkLibrary(cmMakefile& mf, std::string const& lib,
  104. cmTargetLinkLibraryType llt);
  105. enum TLLSignature
  106. {
  107. KeywordTLLSignature,
  108. PlainTLLSignature
  109. };
  110. bool PushTLLCommandTrace(TLLSignature signature,
  111. cmListFileContext const& lfc);
  112. void GetTllSignatureTraces(std::ostream& s, TLLSignature sig) const;
  113. /**
  114. * Set the path where this target should be installed. This is relative to
  115. * INSTALL_PREFIX
  116. */
  117. std::string const& GetInstallPath() const;
  118. void SetInstallPath(std::string const& name);
  119. /**
  120. * Set the path where this target (if it has a runtime part) should be
  121. * installed. This is relative to INSTALL_PREFIX
  122. */
  123. std::string const& GetRuntimeInstallPath() const;
  124. void SetRuntimeInstallPath(std::string const& name);
  125. /**
  126. * Get/Set whether there is an install rule for this target.
  127. */
  128. bool GetHaveInstallRule() const;
  129. void SetHaveInstallRule(bool hir);
  130. void AddInstallGenerator(cmInstallTargetGenerator* g);
  131. std::vector<cmInstallTargetGenerator*> const& GetInstallGenerators() const;
  132. /**
  133. * Get/Set whether this target was auto-created by a generator.
  134. */
  135. bool GetIsGeneratorProvided() const;
  136. void SetIsGeneratorProvided(bool igp);
  137. /**
  138. * Add a utility on which this project depends. A utility is an executable
  139. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  140. * commands. It is not a full path nor does it have an extension.
  141. */
  142. void AddUtility(std::string const& name, bool cross,
  143. cmMakefile* mf = nullptr);
  144. void AddUtility(BT<std::pair<std::string, bool>> util);
  145. //! Get the utilities used by this target
  146. std::set<BT<std::pair<std::string, bool>>> const& GetUtilities() const;
  147. //! Set/Get a property of this target file
  148. void SetProperty(const std::string& prop, cmValue value);
  149. void SetProperty(const std::string& prop, std::nullptr_t)
  150. {
  151. this->SetProperty(prop, cmValue{ nullptr });
  152. }
  153. void SetProperty(const std::string& prop, const std::string& value)
  154. {
  155. this->SetProperty(prop, cmValue(value));
  156. }
  157. void AppendProperty(
  158. const std::string& prop, const std::string& value,
  159. cm::optional<cmListFileBacktrace> const& bt = cm::nullopt,
  160. bool asString = false);
  161. //! Might return a nullptr if the property is not set or invalid
  162. cmValue GetProperty(const std::string& prop) const;
  163. //! Always returns a valid pointer
  164. std::string const& GetSafeProperty(std::string const& prop) const;
  165. bool GetPropertyAsBool(const std::string& prop) const;
  166. void CheckProperty(const std::string& prop, cmMakefile* context) const;
  167. cmValue GetComputedProperty(const std::string& prop, cmMakefile& mf) const;
  168. //! Get all properties
  169. cmPropertyMap const& GetProperties() const;
  170. //! Return whether or not the target is for a DLL platform.
  171. bool IsDLLPlatform() const;
  172. //! Return whether or not we are targeting AIX.
  173. bool IsAIX() const;
  174. //! Return whether or not we are targeting Apple.
  175. bool IsApple() const;
  176. bool IsNormal() const;
  177. bool IsSynthetic() const;
  178. bool IsImported() const;
  179. bool IsImportedGloballyVisible() const;
  180. bool IsPerConfig() 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. void InsertInclude(BT<std::string> const& entry, bool before = false);
  199. void InsertCompileOption(BT<std::string> const& entry, bool before = false);
  200. void InsertCompileDefinition(BT<std::string> const& entry);
  201. void InsertLinkOption(BT<std::string> const& entry, bool before = false);
  202. void InsertLinkDirectory(BT<std::string> const& entry, bool before = false);
  203. void InsertPrecompileHeader(BT<std::string> const& entry);
  204. void AppendBuildInterfaceIncludes();
  205. void FinalizeTargetConfiguration(
  206. const cmBTStringRange& noConfigCompileDefinitions,
  207. cm::optional<std::map<std::string, cmValue>>& perConfigCompileDefinitions);
  208. std::string GetDebugGeneratorExpressions(const std::string& value,
  209. cmTargetLinkLibraryType llt) const;
  210. void AddSystemIncludeDirectories(std::set<std::string> const& incs);
  211. std::set<std::string> const& GetSystemIncludeDirectories() const;
  212. void AddInstallIncludeDirectories(cmTargetExport const& te,
  213. cmStringRange const& incs);
  214. cmStringRange GetInstallIncludeDirectoriesEntries(
  215. cmTargetExport const& te) const;
  216. BTs<std::string> const* GetLanguageStandardProperty(
  217. const std::string& propertyName) const;
  218. void SetLanguageStandardProperty(std::string const& lang,
  219. std::string const& value,
  220. const std::string& feature);
  221. cmBTStringRange GetIncludeDirectoriesEntries() const;
  222. cmBTStringRange GetCompileOptionsEntries() const;
  223. cmBTStringRange GetCompileFeaturesEntries() const;
  224. cmBTStringRange GetCompileDefinitionsEntries() const;
  225. cmBTStringRange GetPrecompileHeadersEntries() const;
  226. cmBTStringRange GetSourceEntries() const;
  227. cmBTStringRange GetLinkOptionsEntries() const;
  228. cmBTStringRange GetLinkDirectoriesEntries() const;
  229. cmBTStringRange GetLinkImplementationEntries() const;
  230. cmBTStringRange GetLinkInterfaceEntries() const;
  231. cmBTStringRange GetLinkInterfaceDirectEntries() const;
  232. cmBTStringRange GetLinkInterfaceDirectExcludeEntries() const;
  233. cmBTStringRange GetHeaderSetsEntries() const;
  234. cmBTStringRange GetCxxModuleSetsEntries() const;
  235. cmBTStringRange GetInterfaceHeaderSetsEntries() const;
  236. cmBTStringRange GetInterfaceCxxModuleSetsEntries() const;
  237. std::string ImportedGetFullPath(const std::string& config,
  238. cmStateEnums::ArtifactType artifact) const;
  239. struct StrictTargetComparison
  240. {
  241. bool operator()(cmTarget const* t1, cmTarget const* t2) const;
  242. };
  243. const cmFileSet* GetFileSet(const std::string& name) const;
  244. cmFileSet* GetFileSet(const std::string& name);
  245. std::pair<cmFileSet*, bool> GetOrCreateFileSet(const std::string& name,
  246. const std::string& type,
  247. cmFileSetVisibility vis);
  248. std::vector<std::string> GetAllFileSetNames() const;
  249. std::vector<std::string> GetAllInterfaceFileSets() const;
  250. static std::string GetFileSetsPropertyName(const std::string& type);
  251. static std::string GetInterfaceFileSetsPropertyName(const std::string& type);
  252. private:
  253. template <typename ValueType>
  254. void StoreProperty(const std::string& prop, ValueType value);
  255. // Internal representation details.
  256. friend class cmGeneratorTarget;
  257. const char* GetSuffixVariableInternal(
  258. cmStateEnums::ArtifactType artifact) const;
  259. const char* GetPrefixVariableInternal(
  260. cmStateEnums::ArtifactType artifact) const;
  261. std::unique_ptr<cmTargetInternals> impl;
  262. };