cmTarget.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef cmTarget_h
  11. #define cmTarget_h
  12. #include <cmConfigure.h> // IWYU pragma: keep
  13. #include "cmAlgorithms.h"
  14. #include "cmCustomCommand.h"
  15. #include "cmListFileCache.h"
  16. #include "cmPolicies.h"
  17. #include "cmPropertyMap.h"
  18. #include "cmState.h"
  19. #include "cmTargetLinkLibraryType.h"
  20. #include <iosfwd>
  21. #include <map>
  22. #include <set>
  23. #include <string>
  24. #include <utility>
  25. #include <vector>
  26. #if defined(CMAKE_BUILD_WITH_CMAKE)
  27. #ifdef CMake_HAVE_CXX_UNORDERED_MAP
  28. #include <unordered_map>
  29. #else
  30. #include <cmsys/hash_map.hxx>
  31. #endif
  32. #endif
  33. class cmMakefile;
  34. class cmSourceFile;
  35. class cmTargetInternals;
  36. class cmTargetInternalPointer
  37. {
  38. public:
  39. cmTargetInternalPointer();
  40. cmTargetInternalPointer(cmTargetInternalPointer const& r);
  41. ~cmTargetInternalPointer();
  42. cmTargetInternalPointer& operator=(cmTargetInternalPointer const& r);
  43. cmTargetInternals* operator->() const { return this->Pointer; }
  44. cmTargetInternals* Get() const { return this->Pointer; }
  45. private:
  46. cmTargetInternals* Pointer;
  47. };
  48. /** \class cmTarget
  49. * \brief Represent a library or executable target loaded from a makefile.
  50. *
  51. * cmTarget represents a target loaded from
  52. * a makefile.
  53. */
  54. class cmTarget
  55. {
  56. public:
  57. cmTarget();
  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. /**
  69. * Set the target type
  70. */
  71. void SetType(cmState::TargetType f, const std::string& name);
  72. void MarkAsImported(bool global = false);
  73. ///! Set/Get the name of the target
  74. const std::string& GetName() const { return this->Name; }
  75. /** Get a copy of this target adapted for the given directory. */
  76. cmTarget CopyForDirectory(cmMakefile* mf) const;
  77. ///! Set the cmMakefile that owns this target
  78. void SetMakefile(cmMakefile* mf);
  79. cmMakefile* GetMakefile() const { return this->Makefile; }
  80. #define DECLARE_TARGET_POLICY(POLICY) \
  81. cmPolicies::PolicyStatus GetPolicyStatus##POLICY() const \
  82. { \
  83. return this->PolicyMap.Get(cmPolicies::POLICY); \
  84. }
  85. CM_FOR_EACH_TARGET_POLICY(DECLARE_TARGET_POLICY)
  86. #undef DECLARE_TARGET_POLICY
  87. /**
  88. * Get the list of the custom commands for this target
  89. */
  90. std::vector<cmCustomCommand> const& GetPreBuildCommands() const
  91. {
  92. return this->PreBuildCommands;
  93. }
  94. std::vector<cmCustomCommand> const& GetPreLinkCommands() const
  95. {
  96. return this->PreLinkCommands;
  97. }
  98. std::vector<cmCustomCommand> const& GetPostBuildCommands() const
  99. {
  100. return this->PostBuildCommands;
  101. }
  102. void AddPreBuildCommand(cmCustomCommand const& cmd)
  103. {
  104. this->PreBuildCommands.push_back(cmd);
  105. }
  106. void AddPreLinkCommand(cmCustomCommand const& cmd)
  107. {
  108. this->PreLinkCommands.push_back(cmd);
  109. }
  110. void AddPostBuildCommand(cmCustomCommand const& cmd)
  111. {
  112. this->PostBuildCommands.push_back(cmd);
  113. }
  114. /**
  115. * Add sources to the target.
  116. */
  117. void AddSources(std::vector<std::string> const& srcs);
  118. void AddTracedSources(std::vector<std::string> const& srcs);
  119. cmSourceFile* AddSourceCMP0049(const std::string& src);
  120. cmSourceFile* AddSource(const std::string& src);
  121. //* how we identify a library, by name and type
  122. typedef std::pair<std::string, cmTargetLinkLibraryType> LibraryID;
  123. typedef std::vector<LibraryID> LinkLibraryVectorType;
  124. const LinkLibraryVectorType& GetOriginalLinkLibraries() const
  125. {
  126. return this->OriginalLinkLibraries;
  127. }
  128. /**
  129. * Clear the dependency information recorded for this target, if any.
  130. */
  131. void ClearDependencyInformation(cmMakefile& mf, const std::string& target);
  132. void AddLinkLibrary(cmMakefile& mf, const std::string& target,
  133. const std::string& lib, cmTargetLinkLibraryType llt);
  134. enum TLLSignature
  135. {
  136. KeywordTLLSignature,
  137. PlainTLLSignature
  138. };
  139. bool PushTLLCommandTrace(TLLSignature signature,
  140. cmListFileContext const& lfc);
  141. void GetTllSignatureTraces(std::ostream& s, TLLSignature sig) const;
  142. void MergeLinkLibraries(cmMakefile& mf, const std::string& selfname,
  143. const LinkLibraryVectorType& libs);
  144. const std::vector<std::string>& GetLinkDirectories() const;
  145. void AddLinkDirectory(const std::string& d);
  146. /**
  147. * Set the path where this target should be installed. This is relative to
  148. * INSTALL_PREFIX
  149. */
  150. std::string GetInstallPath() const { return this->InstallPath; }
  151. void SetInstallPath(const char* name) { this->InstallPath = name; }
  152. /**
  153. * Set the path where this target (if it has a runtime part) should be
  154. * installed. This is relative to INSTALL_PREFIX
  155. */
  156. std::string GetRuntimeInstallPath() const
  157. {
  158. return this->RuntimeInstallPath;
  159. }
  160. void SetRuntimeInstallPath(const char* name)
  161. {
  162. this->RuntimeInstallPath = name;
  163. }
  164. /**
  165. * Get/Set whether there is an install rule for this target.
  166. */
  167. bool GetHaveInstallRule() const { return this->HaveInstallRule; }
  168. void SetHaveInstallRule(bool h) { this->HaveInstallRule = h; }
  169. /** Add a utility on which this project depends. A utility is an executable
  170. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  171. * commands. It is not a full path nor does it have an extension.
  172. */
  173. void AddUtility(const std::string& u, cmMakefile* makefile = CM_NULLPTR);
  174. ///! Get the utilities used by this target
  175. std::set<std::string> const& GetUtilities() const { return this->Utilities; }
  176. cmListFileBacktrace const* GetUtilityBacktrace(const std::string& u) const;
  177. ///! Set/Get a property of this target file
  178. void SetProperty(const std::string& prop, const char* value);
  179. void AppendProperty(const std::string& prop, const char* value,
  180. bool asString = false);
  181. const char* GetProperty(const std::string& prop) const;
  182. const char* GetProperty(const std::string& prop, cmMakefile* context) const;
  183. bool GetPropertyAsBool(const std::string& prop) const;
  184. void CheckProperty(const std::string& prop, cmMakefile* context) const;
  185. bool IsImported() const { return this->IsImportedTarget; }
  186. bool IsImportedGloballyVisible() const
  187. {
  188. return this->ImportedGloballyVisible;
  189. }
  190. // Get the properties
  191. cmPropertyMap& GetProperties() const { return this->Properties; }
  192. bool GetMappedConfig(std::string const& desired_config, const char** loc,
  193. const char** imp, std::string& suffix) const;
  194. /** Return whether this target is an executable with symbol exports
  195. enabled. */
  196. bool IsExecutableWithExports() const;
  197. /** Return whether this target is a shared library Framework on
  198. Apple. */
  199. bool IsFrameworkOnApple() const;
  200. /** Return whether this target is an executable Bundle on Apple. */
  201. bool IsAppBundleOnApple() const;
  202. /** Get a backtrace from the creation of the target. */
  203. cmListFileBacktrace const& GetBacktrace() const;
  204. void InsertInclude(std::string const& entry, cmListFileBacktrace const& bt,
  205. bool before = false);
  206. void InsertCompileOption(std::string const& entry,
  207. cmListFileBacktrace const& bt, bool before = false);
  208. void InsertCompileDefinition(std::string const& entry,
  209. cmListFileBacktrace const& bt);
  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 GetLinkImplementationEntries() const;
  229. cmBacktraceRange GetLinkImplementationBacktraces() const;
  230. struct StrictTargetComparison
  231. {
  232. bool operator()(cmTarget const* t1, cmTarget const* t2) const;
  233. };
  234. private:
  235. bool HandleLocationPropertyPolicy(cmMakefile* context) const;
  236. const char* GetSuffixVariableInternal(bool implib) const;
  237. const char* GetPrefixVariableInternal(bool implib) const;
  238. // Use a makefile variable to set a default for the given property.
  239. // If the variable is not defined use the given default instead.
  240. void SetPropertyDefault(const std::string& property,
  241. const char* default_value);
  242. std::string ImportedGetFullPath(const std::string& config,
  243. bool implib) const;
  244. private:
  245. mutable cmPropertyMap Properties;
  246. std::set<std::string> SystemIncludeDirectories;
  247. std::set<std::string> LinkDirectoriesEmmitted;
  248. std::set<std::string> Utilities;
  249. std::map<std::string, cmListFileBacktrace> UtilityBacktraces;
  250. cmPolicies::PolicyMap PolicyMap;
  251. std::string Name;
  252. std::string InstallPath;
  253. std::string RuntimeInstallPath;
  254. std::vector<std::string> LinkDirectories;
  255. std::vector<cmCustomCommand> PreBuildCommands;
  256. std::vector<cmCustomCommand> PreLinkCommands;
  257. std::vector<cmCustomCommand> PostBuildCommands;
  258. std::vector<std::pair<TLLSignature, cmListFileContext> > TLLCommands;
  259. LinkLibraryVectorType PrevLinkedLibraries;
  260. LinkLibraryVectorType OriginalLinkLibraries;
  261. cmMakefile* Makefile;
  262. cmTargetInternalPointer Internal;
  263. cmState::TargetType TargetTypeValue;
  264. bool HaveInstallRule;
  265. bool RecordDependencies;
  266. bool DLLPlatform;
  267. bool IsAndroid;
  268. bool IsImportedTarget;
  269. bool ImportedGloballyVisible;
  270. bool BuildInterfaceIncludesAppended;
  271. std::string ProcessSourceItemCMP0049(const std::string& s);
  272. /** Return whether or not the target has a DLL import library. */
  273. bool HasImportLibrary() const;
  274. // Internal representation details.
  275. friend class cmTargetInternals;
  276. friend class cmGeneratorTarget;
  277. friend class cmTargetTraceDependencies;
  278. cmListFileBacktrace Backtrace;
  279. };
  280. #ifdef CMAKE_BUILD_WITH_CMAKE
  281. #ifdef CMake_HAVE_CXX_UNORDERED_MAP
  282. typedef std::unordered_map<std::string, cmTarget> cmTargets;
  283. #else
  284. typedef cmsys::hash_map<std::string, cmTarget> cmTargets;
  285. #endif
  286. #else
  287. typedef std::map<std::string, cmTarget> cmTargets;
  288. #endif
  289. class cmTargetSet : public std::set<std::string>
  290. {
  291. };
  292. class cmTargetManifest : public std::map<std::string, cmTargetSet>
  293. {
  294. };
  295. #endif