cmTarget.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. enum Visibility
  58. {
  59. VisibilityNormal,
  60. VisibilityImported,
  61. VisibilityImportedGlobally
  62. };
  63. cmTarget(std::string const& name, cmState::TargetType type, Visibility vis,
  64. cmMakefile* mf);
  65. enum CustomCommandType
  66. {
  67. PRE_BUILD,
  68. PRE_LINK,
  69. POST_BUILD
  70. };
  71. /**
  72. * Return the type of target.
  73. */
  74. cmState::TargetType GetType() const { return this->TargetTypeValue; }
  75. ///! Set/Get the name of the target
  76. const std::string& GetName() const { return this->Name; }
  77. /** Get the cmMakefile that owns this target. */
  78. cmMakefile* GetMakefile() const { return this->Makefile; }
  79. #define DECLARE_TARGET_POLICY(POLICY) \
  80. cmPolicies::PolicyStatus GetPolicyStatus##POLICY() const \
  81. { \
  82. return this->PolicyMap.Get(cmPolicies::POLICY); \
  83. }
  84. CM_FOR_EACH_TARGET_POLICY(DECLARE_TARGET_POLICY)
  85. #undef DECLARE_TARGET_POLICY
  86. /**
  87. * Get the list of the custom commands for this target
  88. */
  89. std::vector<cmCustomCommand> const& GetPreBuildCommands() const
  90. {
  91. return this->PreBuildCommands;
  92. }
  93. std::vector<cmCustomCommand> const& GetPreLinkCommands() const
  94. {
  95. return this->PreLinkCommands;
  96. }
  97. std::vector<cmCustomCommand> const& GetPostBuildCommands() const
  98. {
  99. return this->PostBuildCommands;
  100. }
  101. void AddPreBuildCommand(cmCustomCommand const& cmd)
  102. {
  103. this->PreBuildCommands.push_back(cmd);
  104. }
  105. void AddPreLinkCommand(cmCustomCommand const& cmd)
  106. {
  107. this->PreLinkCommands.push_back(cmd);
  108. }
  109. void AddPostBuildCommand(cmCustomCommand const& cmd)
  110. {
  111. this->PostBuildCommands.push_back(cmd);
  112. }
  113. /**
  114. * Add sources to the target.
  115. */
  116. void AddSources(std::vector<std::string> const& srcs);
  117. void AddTracedSources(std::vector<std::string> const& srcs);
  118. cmSourceFile* AddSourceCMP0049(const std::string& src);
  119. cmSourceFile* AddSource(const std::string& src);
  120. //* how we identify a library, by name and type
  121. typedef std::pair<std::string, cmTargetLinkLibraryType> LibraryID;
  122. typedef std::vector<LibraryID> LinkLibraryVectorType;
  123. const LinkLibraryVectorType& GetOriginalLinkLibraries() const
  124. {
  125. return this->OriginalLinkLibraries;
  126. }
  127. /**
  128. * Clear the dependency information recorded for this target, if any.
  129. */
  130. void ClearDependencyInformation(cmMakefile& mf, const std::string& target);
  131. void AddLinkLibrary(cmMakefile& mf, const std::string& target,
  132. const std::string& lib, cmTargetLinkLibraryType llt);
  133. enum TLLSignature
  134. {
  135. KeywordTLLSignature,
  136. PlainTLLSignature
  137. };
  138. bool PushTLLCommandTrace(TLLSignature signature,
  139. cmListFileContext const& lfc);
  140. void GetTllSignatureTraces(std::ostream& s, TLLSignature sig) const;
  141. void MergeLinkLibraries(cmMakefile& mf, const std::string& selfname,
  142. const LinkLibraryVectorType& libs);
  143. const std::vector<std::string>& GetLinkDirectories() const;
  144. void AddLinkDirectory(const std::string& d);
  145. /**
  146. * Set the path where this target should be installed. This is relative to
  147. * INSTALL_PREFIX
  148. */
  149. std::string GetInstallPath() const { return this->InstallPath; }
  150. void SetInstallPath(const char* name) { this->InstallPath = name; }
  151. /**
  152. * Set the path where this target (if it has a runtime part) should be
  153. * installed. This is relative to INSTALL_PREFIX
  154. */
  155. std::string GetRuntimeInstallPath() const
  156. {
  157. return this->RuntimeInstallPath;
  158. }
  159. void SetRuntimeInstallPath(const char* name)
  160. {
  161. this->RuntimeInstallPath = name;
  162. }
  163. /**
  164. * Get/Set whether there is an install rule for this target.
  165. */
  166. bool GetHaveInstallRule() const { return this->HaveInstallRule; }
  167. void SetHaveInstallRule(bool h) { this->HaveInstallRule = h; }
  168. /** Add a utility on which this project depends. A utility is an executable
  169. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  170. * commands. It is not a full path nor does it have an extension.
  171. */
  172. void AddUtility(const std::string& u, cmMakefile* makefile = CM_NULLPTR);
  173. ///! Get the utilities used by this target
  174. std::set<std::string> const& GetUtilities() const { return this->Utilities; }
  175. cmListFileBacktrace const* GetUtilityBacktrace(const std::string& u) const;
  176. ///! Set/Get a property of this target file
  177. void SetProperty(const std::string& prop, const char* value);
  178. void AppendProperty(const std::string& prop, const char* value,
  179. bool asString = false);
  180. const char* GetProperty(const std::string& prop) const;
  181. const char* GetProperty(const std::string& prop, cmMakefile* context) const;
  182. bool GetPropertyAsBool(const std::string& prop) const;
  183. void CheckProperty(const std::string& prop, cmMakefile* 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& 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 AppendBuildInterfaceIncludes();
  210. std::string GetDebugGeneratorExpressions(const std::string& value,
  211. cmTargetLinkLibraryType llt) const;
  212. void AddSystemIncludeDirectories(const std::set<std::string>& incs);
  213. std::set<std::string> const& GetSystemIncludeDirectories() const
  214. {
  215. return this->SystemIncludeDirectories;
  216. }
  217. cmStringRange GetIncludeDirectoriesEntries() const;
  218. cmBacktraceRange GetIncludeDirectoriesBacktraces() const;
  219. cmStringRange GetCompileOptionsEntries() const;
  220. cmBacktraceRange GetCompileOptionsBacktraces() const;
  221. cmStringRange GetCompileFeaturesEntries() const;
  222. cmBacktraceRange GetCompileFeaturesBacktraces() const;
  223. cmStringRange GetCompileDefinitionsEntries() const;
  224. cmBacktraceRange GetCompileDefinitionsBacktraces() const;
  225. cmStringRange GetSourceEntries() const;
  226. cmBacktraceRange GetSourceBacktraces() const;
  227. cmStringRange GetLinkImplementationEntries() const;
  228. cmBacktraceRange GetLinkImplementationBacktraces() const;
  229. struct StrictTargetComparison
  230. {
  231. bool operator()(cmTarget const* t1, cmTarget const* t2) const;
  232. };
  233. private:
  234. bool HandleLocationPropertyPolicy(cmMakefile* context) const;
  235. const char* GetSuffixVariableInternal(bool implib) const;
  236. const char* GetPrefixVariableInternal(bool implib) const;
  237. // Use a makefile variable to set a default for the given property.
  238. // If the variable is not defined use the given default instead.
  239. void SetPropertyDefault(const std::string& property,
  240. const char* default_value);
  241. std::string ImportedGetFullPath(const std::string& config,
  242. bool implib) const;
  243. private:
  244. mutable cmPropertyMap Properties;
  245. std::set<std::string> SystemIncludeDirectories;
  246. std::set<std::string> LinkDirectoriesEmmitted;
  247. std::set<std::string> Utilities;
  248. std::map<std::string, cmListFileBacktrace> UtilityBacktraces;
  249. cmPolicies::PolicyMap PolicyMap;
  250. std::string Name;
  251. std::string InstallPath;
  252. std::string RuntimeInstallPath;
  253. std::vector<std::string> LinkDirectories;
  254. std::vector<cmCustomCommand> PreBuildCommands;
  255. std::vector<cmCustomCommand> PreLinkCommands;
  256. std::vector<cmCustomCommand> PostBuildCommands;
  257. std::vector<std::pair<TLLSignature, cmListFileContext> > TLLCommands;
  258. LinkLibraryVectorType PrevLinkedLibraries;
  259. LinkLibraryVectorType OriginalLinkLibraries;
  260. cmMakefile* Makefile;
  261. cmTargetInternalPointer Internal;
  262. cmState::TargetType TargetTypeValue;
  263. bool HaveInstallRule;
  264. bool RecordDependencies;
  265. bool DLLPlatform;
  266. bool IsAndroid;
  267. bool IsImportedTarget;
  268. bool ImportedGloballyVisible;
  269. bool BuildInterfaceIncludesAppended;
  270. std::string ProcessSourceItemCMP0049(const std::string& s);
  271. /** Return whether or not the target has a DLL import library. */
  272. bool HasImportLibrary() const;
  273. // Internal representation details.
  274. friend class cmTargetInternals;
  275. friend class cmGeneratorTarget;
  276. friend class cmTargetTraceDependencies;
  277. cmListFileBacktrace Backtrace;
  278. };
  279. #ifdef CMAKE_BUILD_WITH_CMAKE
  280. #ifdef CMake_HAVE_CXX_UNORDERED_MAP
  281. typedef std::unordered_map<std::string, cmTarget> cmTargets;
  282. #else
  283. typedef cmsys::hash_map<std::string, cmTarget> cmTargets;
  284. #endif
  285. #else
  286. typedef std::map<std::string, cmTarget> cmTargets;
  287. #endif
  288. class cmTargetSet : public std::set<std::string>
  289. {
  290. };
  291. class cmTargetManifest : public std::map<std::string, cmTargetSet>
  292. {
  293. };
  294. #endif