cmTarget.h 10 KB

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