cmTarget.h 11 KB

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