cmTarget.h 11 KB

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