cmTarget.h 11 KB

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