cmTarget.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 "cmAlgorithms.h"
  12. #include "cmListFileCache.h"
  13. #include "cmPolicies.h"
  14. #include "cmProperty.h"
  15. #include "cmStateTypes.h"
  16. #include "cmStringAlgorithms.h"
  17. #include "cmTargetLinkLibraryType.h"
  18. class cmCustomCommand;
  19. class cmGlobalGenerator;
  20. class cmInstallTargetGenerator;
  21. class cmMakefile;
  22. class cmMessenger;
  23. class cmPropertyMap;
  24. class cmSourceFile;
  25. class cmTargetInternals;
  26. /** \class cmTarget
  27. * \brief Represent a library or executable target loaded from a makefile.
  28. *
  29. * cmTarget represents a target loaded from a makefile.
  30. */
  31. class cmTarget
  32. {
  33. public:
  34. enum Visibility
  35. {
  36. VisibilityNormal,
  37. VisibilityImported,
  38. VisibilityImportedGlobally
  39. };
  40. enum class PerConfig
  41. {
  42. Yes,
  43. No
  44. };
  45. cmTarget(std::string const& name, cmStateEnums::TargetType type,
  46. Visibility vis, cmMakefile* mf, PerConfig perConfig);
  47. cmTarget(cmTarget const&) = delete;
  48. cmTarget(cmTarget&&) noexcept;
  49. ~cmTarget();
  50. cmTarget& operator=(cmTarget const&) = delete;
  51. cmTarget& operator=(cmTarget&&) noexcept;
  52. //! Return the type of target.
  53. cmStateEnums::TargetType GetType() const;
  54. //! Get the cmMakefile that owns this target.
  55. cmMakefile* GetMakefile() const;
  56. //! Return the global generator.
  57. cmGlobalGenerator* GetGlobalGenerator() const;
  58. //! Set/Get the name of the target
  59. const std::string& GetName() const;
  60. //! Get the policy map
  61. cmPolicies::PolicyMap const& GetPolicyMap() const;
  62. //! Get policy status
  63. cmPolicies::PolicyStatus GetPolicyStatus(cmPolicies::PolicyID policy) const;
  64. #define DECLARE_TARGET_POLICY(POLICY) \
  65. cmPolicies::PolicyStatus GetPolicyStatus##POLICY() const \
  66. { \
  67. return this->GetPolicyStatus(cmPolicies::POLICY); \
  68. }
  69. CM_FOR_EACH_TARGET_POLICY(DECLARE_TARGET_POLICY)
  70. #undef DECLARE_TARGET_POLICY
  71. //! Get the list of the PRE_BUILD custom commands for this target
  72. std::vector<cmCustomCommand> const& GetPreBuildCommands() const;
  73. void AddPreBuildCommand(cmCustomCommand const& cmd);
  74. void AddPreBuildCommand(cmCustomCommand&& cmd);
  75. //! Get the list of the PRE_LINK custom commands for this target
  76. std::vector<cmCustomCommand> const& GetPreLinkCommands() const;
  77. void AddPreLinkCommand(cmCustomCommand const& cmd);
  78. void AddPreLinkCommand(cmCustomCommand&& cmd);
  79. //! Get the list of the POST_BUILD custom commands for this target
  80. std::vector<cmCustomCommand> const& GetPostBuildCommands() const;
  81. void AddPostBuildCommand(cmCustomCommand const& cmd);
  82. void AddPostBuildCommand(cmCustomCommand&& cmd);
  83. //! Add sources to the target.
  84. void AddSources(std::vector<std::string> const& srcs);
  85. void AddTracedSources(std::vector<std::string> const& srcs);
  86. std::string GetSourceCMP0049(const std::string& src);
  87. cmSourceFile* AddSource(const std::string& src, bool before = false);
  88. //! how we identify a library, by name and type
  89. using LibraryID = std::pair<std::string, cmTargetLinkLibraryType>;
  90. using LinkLibraryVectorType = std::vector<LibraryID>;
  91. LinkLibraryVectorType const& GetOriginalLinkLibraries() const;
  92. //! Clear the dependency information recorded for this target, if any.
  93. void ClearDependencyInformation(cmMakefile& mf) const;
  94. void AddLinkLibrary(cmMakefile& mf, std::string const& lib,
  95. cmTargetLinkLibraryType llt);
  96. enum TLLSignature
  97. {
  98. KeywordTLLSignature,
  99. PlainTLLSignature
  100. };
  101. bool PushTLLCommandTrace(TLLSignature signature,
  102. cmListFileContext const& lfc);
  103. void GetTllSignatureTraces(std::ostream& s, TLLSignature sig) const;
  104. /**
  105. * Set the path where this target should be installed. This is relative to
  106. * INSTALL_PREFIX
  107. */
  108. std::string const& GetInstallPath() const;
  109. void SetInstallPath(std::string const& name);
  110. /**
  111. * Set the path where this target (if it has a runtime part) should be
  112. * installed. This is relative to INSTALL_PREFIX
  113. */
  114. std::string const& GetRuntimeInstallPath() const;
  115. void SetRuntimeInstallPath(std::string const& name);
  116. /**
  117. * Get/Set whether there is an install rule for this target.
  118. */
  119. bool GetHaveInstallRule() const;
  120. void SetHaveInstallRule(bool hir);
  121. void AddInstallGenerator(cmInstallTargetGenerator* g);
  122. std::vector<cmInstallTargetGenerator*> const& GetInstallGenerators() const;
  123. /**
  124. * Get/Set whether this target was auto-created by a generator.
  125. */
  126. bool GetIsGeneratorProvided() const;
  127. void SetIsGeneratorProvided(bool igp);
  128. /**
  129. * Add a utility on which this project depends. A utility is an executable
  130. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  131. * commands. It is not a full path nor does it have an extension.
  132. */
  133. void AddUtility(std::string const& name, bool cross,
  134. cmMakefile* mf = nullptr);
  135. void AddUtility(BT<std::pair<std::string, bool>> util);
  136. //! Get the utilities used by this target
  137. std::set<BT<std::pair<std::string, bool>>> const& GetUtilities() const;
  138. //! Set/Get a property of this target file
  139. void SetProperty(const std::string& prop, const char* value);
  140. void SetProperty(const std::string& prop, const std::string& value)
  141. {
  142. this->SetProperty(prop, value.c_str());
  143. }
  144. void AppendProperty(const std::string& prop, const std::string& value,
  145. bool asString = false);
  146. //! Might return a nullptr if the property is not set or invalid
  147. cmProp GetProperty(const std::string& prop) const;
  148. //! Always returns a valid pointer
  149. std::string const& GetSafeProperty(std::string const& prop) const;
  150. bool GetPropertyAsBool(const std::string& prop) const;
  151. void CheckProperty(const std::string& prop, cmMakefile* context) const;
  152. cmProp GetComputedProperty(const std::string& prop, cmMessenger* messenger,
  153. cmListFileBacktrace const& context) const;
  154. //! Get all properties
  155. cmPropertyMap const& GetProperties() const;
  156. //! Return whether or not the target is for a DLL platform.
  157. bool IsDLLPlatform() const;
  158. //! Return whether or not we are targeting AIX.
  159. bool IsAIX() const;
  160. bool IsImported() const;
  161. bool IsImportedGloballyVisible() const;
  162. bool IsPerConfig() const;
  163. bool CanCompileSources() const;
  164. bool GetMappedConfig(std::string const& desired_config, cmProp& loc,
  165. cmProp& imp, std::string& suffix) const;
  166. //! Return whether this target is an executable with symbol exports enabled.
  167. bool IsExecutableWithExports() const;
  168. //! Return whether this target is a shared library Framework on Apple.
  169. bool IsFrameworkOnApple() const;
  170. //! Return whether this target is an executable Bundle on Apple.
  171. bool IsAppBundleOnApple() const;
  172. //! Return whether this target is a GUI executable on Android.
  173. bool IsAndroidGuiExecutable() const;
  174. //! Get a backtrace from the creation of the target.
  175. cmListFileBacktrace const& GetBacktrace() const;
  176. void InsertInclude(std::string const& entry, cmListFileBacktrace const& bt,
  177. bool before = false);
  178. void InsertCompileOption(std::string const& entry,
  179. cmListFileBacktrace const& bt, bool before = false);
  180. void InsertCompileDefinition(std::string const& entry,
  181. cmListFileBacktrace const& bt);
  182. void InsertLinkOption(std::string const& entry,
  183. cmListFileBacktrace const& bt, bool before = false);
  184. void InsertLinkDirectory(std::string const& entry,
  185. cmListFileBacktrace const& bt, bool before = false);
  186. void InsertPrecompileHeader(std::string const& entry,
  187. cmListFileBacktrace const& bt);
  188. void AppendBuildInterfaceIncludes();
  189. std::string GetDebugGeneratorExpressions(const std::string& value,
  190. cmTargetLinkLibraryType llt) const;
  191. void AddSystemIncludeDirectories(std::set<std::string> const& incs);
  192. std::set<std::string> const& GetSystemIncludeDirectories() const;
  193. BTs<std::string> const* GetLanguageStandardProperty(
  194. const std::string& propertyName) const;
  195. void SetLanguageStandardProperty(std::string const& lang,
  196. std::string const& value,
  197. const std::string& feature);
  198. cmStringRange GetIncludeDirectoriesEntries() const;
  199. cmBacktraceRange GetIncludeDirectoriesBacktraces() const;
  200. cmStringRange GetCompileOptionsEntries() const;
  201. cmBacktraceRange GetCompileOptionsBacktraces() const;
  202. cmStringRange GetCompileFeaturesEntries() const;
  203. cmBacktraceRange GetCompileFeaturesBacktraces() const;
  204. cmStringRange GetCompileDefinitionsEntries() const;
  205. cmBacktraceRange GetCompileDefinitionsBacktraces() const;
  206. cmStringRange GetPrecompileHeadersEntries() const;
  207. cmBacktraceRange GetPrecompileHeadersBacktraces() const;
  208. cmStringRange GetSourceEntries() const;
  209. cmBacktraceRange GetSourceBacktraces() const;
  210. cmStringRange GetLinkOptionsEntries() const;
  211. cmBacktraceRange GetLinkOptionsBacktraces() const;
  212. cmStringRange GetLinkDirectoriesEntries() const;
  213. cmBacktraceRange GetLinkDirectoriesBacktraces() const;
  214. cmStringRange GetLinkImplementationEntries() const;
  215. cmBacktraceRange GetLinkImplementationBacktraces() const;
  216. std::string ImportedGetFullPath(const std::string& config,
  217. cmStateEnums::ArtifactType artifact) const;
  218. struct StrictTargetComparison
  219. {
  220. bool operator()(cmTarget const* t1, cmTarget const* t2) const;
  221. };
  222. private:
  223. // Internal representation details.
  224. friend class cmGeneratorTarget;
  225. const char* GetSuffixVariableInternal(
  226. cmStateEnums::ArtifactType artifact) const;
  227. const char* GetPrefixVariableInternal(
  228. cmStateEnums::ArtifactType artifact) const;
  229. std::unique_ptr<cmTargetInternals> impl;
  230. };