cmTarget.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmTarget_h
  4. #define cmTarget_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <iosfwd>
  7. #include <set>
  8. #include <string>
  9. #include <unordered_map>
  10. #include <utility>
  11. #include <vector>
  12. #include "cmAlgorithms.h"
  13. #include "cmCustomCommand.h"
  14. #include "cmListFileCache.h"
  15. #include "cmPolicies.h"
  16. #include "cmStateTypes.h"
  17. #include "cmTargetLinkLibraryType.h"
  18. class cmGlobalGenerator;
  19. class cmMakefile;
  20. class cmMessenger;
  21. class cmPropertyMap;
  22. class cmSourceFile;
  23. class cmTargetInternals;
  24. class cmTargetInternalPointer
  25. {
  26. public:
  27. cmTargetInternalPointer();
  28. cmTargetInternalPointer(cmTargetInternalPointer const& r);
  29. ~cmTargetInternalPointer();
  30. cmTargetInternalPointer& operator=(cmTargetInternalPointer const& r);
  31. cmTargetInternals* operator->() const { return this->Pointer; }
  32. cmTargetInternals* Get() const { return this->Pointer; }
  33. private:
  34. cmTargetInternals* Pointer;
  35. };
  36. /** \class cmTarget
  37. * \brief Represent a library or executable target loaded from a makefile.
  38. *
  39. * cmTarget represents a target loaded from
  40. * a makefile.
  41. */
  42. class cmTarget
  43. {
  44. public:
  45. enum Visibility
  46. {
  47. VisibilityNormal,
  48. VisibilityImported,
  49. VisibilityImportedGlobally
  50. };
  51. cmTarget(std::string const& name, cmStateEnums::TargetType type,
  52. Visibility vis, cmMakefile* mf);
  53. enum CustomCommandType
  54. {
  55. PRE_BUILD,
  56. PRE_LINK,
  57. POST_BUILD
  58. };
  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 { return this->Name; }
  67. #define DECLARE_TARGET_POLICY(POLICY) \
  68. cmPolicies::PolicyStatus GetPolicyStatus##POLICY() const \
  69. { \
  70. return this->PolicyMap.Get(cmPolicies::POLICY); \
  71. }
  72. CM_FOR_EACH_TARGET_POLICY(DECLARE_TARGET_POLICY)
  73. #undef DECLARE_TARGET_POLICY
  74. /**
  75. * Get the list of the custom commands for this target
  76. */
  77. std::vector<cmCustomCommand> const& GetPreBuildCommands() const
  78. {
  79. return this->PreBuildCommands;
  80. }
  81. std::vector<cmCustomCommand> const& GetPreLinkCommands() const
  82. {
  83. return this->PreLinkCommands;
  84. }
  85. std::vector<cmCustomCommand> const& GetPostBuildCommands() const
  86. {
  87. return this->PostBuildCommands;
  88. }
  89. void AddPreBuildCommand(cmCustomCommand const& cmd)
  90. {
  91. this->PreBuildCommands.push_back(cmd);
  92. }
  93. void AddPreLinkCommand(cmCustomCommand const& cmd)
  94. {
  95. this->PreLinkCommands.push_back(cmd);
  96. }
  97. void AddPostBuildCommand(cmCustomCommand const& cmd)
  98. {
  99. this->PostBuildCommands.push_back(cmd);
  100. }
  101. /**
  102. * Add sources to the target.
  103. */
  104. void AddSources(std::vector<std::string> const& srcs);
  105. void AddTracedSources(std::vector<std::string> const& srcs);
  106. cmSourceFile* AddSourceCMP0049(const std::string& src);
  107. cmSourceFile* AddSource(const std::string& src, bool before = false);
  108. //* how we identify a library, by name and type
  109. typedef std::pair<std::string, cmTargetLinkLibraryType> LibraryID;
  110. typedef std::vector<LibraryID> LinkLibraryVectorType;
  111. const LinkLibraryVectorType& GetOriginalLinkLibraries() const
  112. {
  113. return this->OriginalLinkLibraries;
  114. }
  115. /**
  116. * Clear the dependency information recorded for this target, if any.
  117. */
  118. void ClearDependencyInformation(cmMakefile& mf);
  119. void AddLinkLibrary(cmMakefile& mf, const std::string& lib,
  120. cmTargetLinkLibraryType llt);
  121. void AddLinkLibrary(cmMakefile& mf, std::string const& lib,
  122. std::string const& libRef, cmTargetLinkLibraryType llt);
  123. enum TLLSignature
  124. {
  125. KeywordTLLSignature,
  126. PlainTLLSignature
  127. };
  128. bool PushTLLCommandTrace(TLLSignature signature,
  129. cmListFileContext const& lfc);
  130. void GetTllSignatureTraces(std::ostream& s, TLLSignature sig) const;
  131. /**
  132. * Set the path where this target should be installed. This is relative to
  133. * INSTALL_PREFIX
  134. */
  135. std::string GetInstallPath() const { return this->InstallPath; }
  136. void SetInstallPath(const char* name) { this->InstallPath = name; }
  137. /**
  138. * Set the path where this target (if it has a runtime part) should be
  139. * installed. This is relative to INSTALL_PREFIX
  140. */
  141. std::string GetRuntimeInstallPath() const
  142. {
  143. return this->RuntimeInstallPath;
  144. }
  145. void SetRuntimeInstallPath(const char* name)
  146. {
  147. this->RuntimeInstallPath = name;
  148. }
  149. /**
  150. * Get/Set whether there is an install rule for this target.
  151. */
  152. bool GetHaveInstallRule() const { return this->HaveInstallRule; }
  153. void SetHaveInstallRule(bool h) { this->HaveInstallRule = h; }
  154. /**
  155. * Get/Set whether this target was auto-created by a generator.
  156. */
  157. bool GetIsGeneratorProvided() const { return this->IsGeneratorProvided; }
  158. void SetIsGeneratorProvided(bool igp) { this->IsGeneratorProvided = igp; }
  159. /**
  160. * Add a utility on which this project depends. A utility is an executable
  161. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  162. * commands. It is not a full path nor does it have an extension.
  163. */
  164. void AddUtility(std::string const& name, cmMakefile* mf = nullptr);
  165. ///! Get the utilities used by this target
  166. std::set<BT<std::string>> const& GetUtilities() const;
  167. ///! Set/Get a property of this target file
  168. void SetProperty(const std::string& prop, const char* value);
  169. void AppendProperty(const std::string& prop, const char* value,
  170. bool asString = false);
  171. ///! Might return a nullptr if the property is not set or invalid
  172. const char* GetProperty(const std::string& prop) const;
  173. ///! Always returns a valid pointer
  174. const char* GetSafeProperty(const std::string& prop) const;
  175. bool GetPropertyAsBool(const std::string& prop) const;
  176. void CheckProperty(const std::string& prop, cmMakefile* context) const;
  177. const char* GetComputedProperty(const std::string& prop,
  178. cmMessenger* messenger,
  179. cmListFileBacktrace const& context) const;
  180. ///! Get all properties
  181. cmPropertyMap const& GetProperties() const;
  182. bool IsImported() const { return this->IsImportedTarget; }
  183. bool IsImportedGloballyVisible() const
  184. {
  185. return this->ImportedGloballyVisible;
  186. }
  187. bool GetMappedConfig(std::string const& desired_config, const char** loc,
  188. const char** imp, std::string& suffix) const;
  189. /** Return whether this target is an executable with symbol exports
  190. enabled. */
  191. bool IsExecutableWithExports() const;
  192. /** Return whether this target is a shared library Framework on
  193. Apple. */
  194. bool IsFrameworkOnApple() const;
  195. /** Return whether this target is an executable Bundle on Apple. */
  196. bool IsAppBundleOnApple() const;
  197. /** Get a backtrace from the creation of the target. */
  198. cmListFileBacktrace const& GetBacktrace() const;
  199. void InsertInclude(std::string const& entry, cmListFileBacktrace const& bt,
  200. bool before = false);
  201. void InsertCompileOption(std::string const& entry,
  202. cmListFileBacktrace const& bt, bool before = false);
  203. void InsertCompileDefinition(std::string const& entry,
  204. cmListFileBacktrace const& bt);
  205. void InsertLinkOption(std::string const& entry,
  206. cmListFileBacktrace const& bt, bool before = false);
  207. void InsertLinkDirectory(std::string const& entry,
  208. cmListFileBacktrace const& bt, bool before = false);
  209. void AppendBuildInterfaceIncludes();
  210. std::string GetDebugGeneratorExpressions(const std::string& value,
  211. cmTargetLinkLibraryType llt) const;
  212. void AddSystemIncludeDirectories(std::set<std::string> const& incs);
  213. std::set<std::string> const& GetSystemIncludeDirectories() const;
  214. cmStringRange GetIncludeDirectoriesEntries() const;
  215. cmBacktraceRange GetIncludeDirectoriesBacktraces() const;
  216. cmStringRange GetCompileOptionsEntries() const;
  217. cmBacktraceRange GetCompileOptionsBacktraces() const;
  218. cmStringRange GetCompileFeaturesEntries() const;
  219. cmBacktraceRange GetCompileFeaturesBacktraces() const;
  220. cmStringRange GetCompileDefinitionsEntries() const;
  221. cmBacktraceRange GetCompileDefinitionsBacktraces() const;
  222. cmStringRange GetSourceEntries() const;
  223. cmBacktraceRange GetSourceBacktraces() const;
  224. cmStringRange GetLinkOptionsEntries() const;
  225. cmBacktraceRange GetLinkOptionsBacktraces() const;
  226. cmStringRange GetLinkDirectoriesEntries() const;
  227. cmBacktraceRange GetLinkDirectoriesBacktraces() const;
  228. cmStringRange GetLinkImplementationEntries() const;
  229. cmBacktraceRange GetLinkImplementationBacktraces() const;
  230. struct StrictTargetComparison
  231. {
  232. bool operator()(cmTarget const* t1, cmTarget const* t2) const;
  233. };
  234. std::string ImportedGetFullPath(const std::string& config,
  235. cmStateEnums::ArtifactType artifact) const;
  236. private:
  237. const char* GetSuffixVariableInternal(
  238. cmStateEnums::ArtifactType artifact) const;
  239. const char* GetPrefixVariableInternal(
  240. cmStateEnums::ArtifactType artifact) const;
  241. // Use a makefile variable to set a default for the given property.
  242. // If the variable is not defined use the given default instead.
  243. void SetPropertyDefault(const std::string& property,
  244. const char* default_value);
  245. bool CheckImportedLibName(std::string const& prop,
  246. std::string const& value) const;
  247. private:
  248. bool IsGeneratorProvided;
  249. cmPolicies::PolicyMap PolicyMap;
  250. std::string Name;
  251. std::string InstallPath;
  252. std::string RuntimeInstallPath;
  253. std::vector<cmCustomCommand> PreBuildCommands;
  254. std::vector<cmCustomCommand> PreLinkCommands;
  255. std::vector<cmCustomCommand> PostBuildCommands;
  256. std::vector<std::pair<TLLSignature, cmListFileContext>> TLLCommands;
  257. LinkLibraryVectorType OriginalLinkLibraries;
  258. cmTargetInternalPointer impl;
  259. bool HaveInstallRule;
  260. bool DLLPlatform;
  261. bool IsAndroid;
  262. bool IsImportedTarget;
  263. bool ImportedGloballyVisible;
  264. bool BuildInterfaceIncludesAppended;
  265. std::string ProcessSourceItemCMP0049(const std::string& s);
  266. /** Return whether or not the target has a DLL import library. */
  267. bool HasImportLibrary() const;
  268. // Internal representation details.
  269. friend class cmTargetInternals;
  270. friend class cmGeneratorTarget;
  271. friend class cmTargetTraceDependencies;
  272. cmListFileBacktrace Backtrace;
  273. };
  274. typedef std::unordered_map<std::string, cmTarget> cmTargets;
  275. #endif