cmTarget.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 "cmCustomCommand.h"
  13. #include "cmPropertyMap.h"
  14. #include "cmPolicies.h"
  15. #include "cmListFileCache.h"
  16. #include <cmsys/auto_ptr.hxx>
  17. #if defined(CMAKE_BUILD_WITH_CMAKE)
  18. # ifdef CMake_HAVE_CXX11_UNORDERED_MAP
  19. # include <unordered_map>
  20. # else
  21. # include <cmsys/hash_map.hxx>
  22. # endif
  23. #endif
  24. #define CM_FOR_EACH_TARGET_POLICY(F) \
  25. F(CMP0003) \
  26. F(CMP0004) \
  27. F(CMP0008) \
  28. F(CMP0020) \
  29. F(CMP0021) \
  30. F(CMP0022) \
  31. F(CMP0027) \
  32. F(CMP0038) \
  33. F(CMP0041) \
  34. F(CMP0042) \
  35. F(CMP0046) \
  36. F(CMP0052) \
  37. F(CMP0060) \
  38. F(CMP0063) \
  39. F(CMP0065)
  40. class cmake;
  41. class cmMakefile;
  42. class cmSourceFile;
  43. class cmGlobalGenerator;
  44. class cmComputeLinkInformation;
  45. class cmListFileBacktrace;
  46. class cmTarget;
  47. class cmGeneratorTarget;
  48. class cmTargetTraceDependencies;
  49. class cmTargetInternals;
  50. class cmTargetInternalPointer
  51. {
  52. public:
  53. cmTargetInternalPointer();
  54. cmTargetInternalPointer(cmTargetInternalPointer const& r);
  55. ~cmTargetInternalPointer();
  56. cmTargetInternalPointer& operator=(cmTargetInternalPointer const& r);
  57. cmTargetInternals* operator->() const { return this->Pointer; }
  58. cmTargetInternals* Get() const { return this->Pointer; }
  59. private:
  60. cmTargetInternals* Pointer;
  61. };
  62. /** \class cmTarget
  63. * \brief Represent a library or executable target loaded from a makefile.
  64. *
  65. * cmTarget represents a target loaded from
  66. * a makefile.
  67. */
  68. class cmTarget
  69. {
  70. public:
  71. cmTarget();
  72. enum CustomCommandType { PRE_BUILD, PRE_LINK, POST_BUILD };
  73. /**
  74. * Return the type of target.
  75. */
  76. cmState::TargetType GetType() const
  77. {
  78. return this->TargetTypeValue;
  79. }
  80. /**
  81. * Set the target type
  82. */
  83. void SetType(cmState::TargetType f, const std::string& name);
  84. void MarkAsImported();
  85. ///! Set/Get the name of the target
  86. const std::string& GetName() const {return this->Name;}
  87. ///! Set the cmMakefile that owns this target
  88. void SetMakefile(cmMakefile *mf);
  89. cmMakefile *GetMakefile() const { return this->Makefile;}
  90. #define DECLARE_TARGET_POLICY(POLICY) \
  91. cmPolicies::PolicyStatus GetPolicyStatus ## POLICY () const \
  92. { return this->PolicyMap.Get(cmPolicies::POLICY); }
  93. CM_FOR_EACH_TARGET_POLICY(DECLARE_TARGET_POLICY)
  94. #undef DECLARE_TARGET_POLICY
  95. /**
  96. * Get the list of the custom commands for this target
  97. */
  98. std::vector<cmCustomCommand> const &GetPreBuildCommands() const
  99. {return this->PreBuildCommands;}
  100. std::vector<cmCustomCommand> const &GetPreLinkCommands() const
  101. {return this->PreLinkCommands;}
  102. std::vector<cmCustomCommand> const &GetPostBuildCommands() const
  103. {return this->PostBuildCommands;}
  104. void AddPreBuildCommand(cmCustomCommand const &cmd)
  105. {this->PreBuildCommands.push_back(cmd);}
  106. void AddPreLinkCommand(cmCustomCommand const &cmd)
  107. {this->PreLinkCommands.push_back(cmd);}
  108. void AddPostBuildCommand(cmCustomCommand const &cmd)
  109. {this->PostBuildCommands.push_back(cmd);}
  110. /**
  111. * Add sources to the target.
  112. */
  113. void AddSources(std::vector<std::string> const& srcs);
  114. void AddTracedSources(std::vector<std::string> const& srcs);
  115. cmSourceFile* AddSourceCMP0049(const std::string& src);
  116. cmSourceFile* AddSource(const std::string& src);
  117. //* how we identify a library, by name and type
  118. typedef std::pair<std::string, cmTargetLinkLibraryType> LibraryID;
  119. typedef std::vector<LibraryID > LinkLibraryVectorType;
  120. const LinkLibraryVectorType &GetOriginalLinkLibraries() const
  121. {return this->OriginalLinkLibraries;}
  122. /** Compute the link type to use for the given configuration. */
  123. cmTargetLinkLibraryType ComputeLinkType(const std::string& config) const;
  124. /**
  125. * Clear the dependency information recorded for this target, if any.
  126. */
  127. void ClearDependencyInformation(cmMakefile& mf, const std::string& target);
  128. // Check to see if a library is a framework and treat it different on Mac
  129. bool NameResolvesToFramework(const std::string& libname) const;
  130. void AddLinkLibrary(cmMakefile& mf,
  131. const std::string& target, const std::string& lib,
  132. cmTargetLinkLibraryType llt);
  133. enum TLLSignature {
  134. KeywordTLLSignature,
  135. PlainTLLSignature
  136. };
  137. bool PushTLLCommandTrace(TLLSignature signature,
  138. cmListFileContext const& lfc);
  139. void GetTllSignatureTraces(std::ostringstream &s, TLLSignature sig) const;
  140. void MergeLinkLibraries( cmMakefile& mf, const std::string& selfname,
  141. const LinkLibraryVectorType& libs );
  142. const std::vector<std::string>& GetLinkDirectories() const;
  143. void AddLinkDirectory(const std::string& d);
  144. /**
  145. * Set the path where this target should be installed. This is relative to
  146. * INSTALL_PREFIX
  147. */
  148. std::string GetInstallPath() const {return this->InstallPath;}
  149. void SetInstallPath(const char *name) {this->InstallPath = name;}
  150. /**
  151. * Set the path where this target (if it has a runtime part) should be
  152. * installed. This is relative to INSTALL_PREFIX
  153. */
  154. std::string GetRuntimeInstallPath() const {return this->RuntimeInstallPath;}
  155. void SetRuntimeInstallPath(const char *name) {
  156. this->RuntimeInstallPath = name; }
  157. /**
  158. * Get/Set whether there is an install rule for this target.
  159. */
  160. bool GetHaveInstallRule() const { return this->HaveInstallRule; }
  161. void SetHaveInstallRule(bool h) { this->HaveInstallRule = h; }
  162. /** Add a utility on which this project depends. A utility is an executable
  163. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  164. * commands. It is not a full path nor does it have an extension.
  165. */
  166. void AddUtility(const std::string& u, cmMakefile *makefile = 0);
  167. ///! Get the utilities used by this target
  168. std::set<std::string>const& GetUtilities() const { return this->Utilities; }
  169. cmListFileBacktrace const* GetUtilityBacktrace(const std::string& u) const;
  170. /** Finalize the target at the end of the Configure step. */
  171. void FinishConfigure();
  172. ///! Set/Get a property of this target file
  173. void SetProperty(const std::string& prop, const char *value);
  174. void AppendProperty(const std::string& prop, const char* value,
  175. bool asString=false);
  176. const char *GetProperty(const std::string& prop) const;
  177. const char *GetProperty(const std::string& prop, cmMakefile* context) const;
  178. bool GetPropertyAsBool(const std::string& prop) const;
  179. void CheckProperty(const std::string& prop, cmMakefile* context) const;
  180. bool IsImported() const {return this->IsImportedTarget;}
  181. // Get the properties
  182. cmPropertyMap &GetProperties() const { return this->Properties; }
  183. bool GetMappedConfig(std::string const& desired_config,
  184. const char** loc,
  185. const char** imp,
  186. std::string& suffix) const;
  187. /** Get the macro to define when building sources in this target.
  188. If no macro should be defined null is returned. */
  189. const char* GetExportMacro() const;
  190. /** Return whether this target is an executable with symbol exports
  191. enabled. */
  192. bool IsExecutableWithExports() const;
  193. /** Return whether or not the target is for a DLL platform. */
  194. bool IsDLLPlatform() const { return this->DLLPlatform; }
  195. /** Return whether this target is a shared library Framework on
  196. Apple. */
  197. bool IsFrameworkOnApple() const;
  198. /** Return whether this target is a CFBundle (plugin) on Apple. */
  199. bool IsCFBundleOnApple() const;
  200. /** Return whether this target is an executable Bundle on Apple. */
  201. bool IsAppBundleOnApple() const;
  202. /** Return the framework version string. Undefined if
  203. IsFrameworkOnApple returns false. */
  204. std::string GetFrameworkVersion() const;
  205. /** Get a backtrace from the creation of the target. */
  206. cmListFileBacktrace const& GetBacktrace() const;
  207. /** @return whether this target have a well defined output file name. */
  208. bool HaveWellDefinedOutputFiles() const;
  209. void InsertInclude(std::string const& entry,
  210. cmListFileBacktrace const& bt,
  211. bool before = false);
  212. void InsertCompileOption(std::string const& entry,
  213. cmListFileBacktrace const& bt,
  214. bool before = false);
  215. void InsertCompileDefinition(std::string const& entry,
  216. cmListFileBacktrace const& bt);
  217. void AppendBuildInterfaceIncludes();
  218. std::string GetDebugGeneratorExpressions(const std::string &value,
  219. cmTargetLinkLibraryType llt) const;
  220. void AddSystemIncludeDirectories(const std::set<std::string> &incs);
  221. std::set<std::string> const & GetSystemIncludeDirectories() const
  222. { return this->SystemIncludeDirectories; }
  223. bool LinkLanguagePropagatesToDependents() const
  224. { return this->TargetTypeValue == cmState::STATIC_LIBRARY; }
  225. cmStringRange GetIncludeDirectoriesEntries() const;
  226. cmBacktraceRange GetIncludeDirectoriesBacktraces() const;
  227. cmStringRange GetCompileOptionsEntries() const;
  228. cmBacktraceRange GetCompileOptionsBacktraces() const;
  229. cmStringRange GetCompileFeaturesEntries() const;
  230. cmBacktraceRange GetCompileFeaturesBacktraces() const;
  231. cmStringRange GetCompileDefinitionsEntries() const;
  232. cmBacktraceRange GetCompileDefinitionsBacktraces() const;
  233. cmStringRange GetSourceEntries() const;
  234. cmBacktraceRange GetSourceBacktraces() const;
  235. cmStringRange GetLinkImplementationEntries() const;
  236. cmBacktraceRange GetLinkImplementationBacktraces() const;
  237. #if defined(_WIN32) && !defined(__CYGWIN__)
  238. const LinkLibraryVectorType &GetLinkLibrariesForVS6() const {
  239. return this->LinkLibrariesForVS6;}
  240. #endif
  241. private:
  242. bool HandleLocationPropertyPolicy(cmMakefile* context) const;
  243. #if defined(_WIN32) && !defined(__CYGWIN__)
  244. /**
  245. * A list of direct dependencies. Use in conjunction with DependencyMap.
  246. */
  247. typedef std::vector< LibraryID > DependencyList;
  248. /**
  249. * This map holds the dependency graph. map[x] returns a set of
  250. * direct dependencies of x. Note that the direct depenencies are
  251. * ordered. This is necessary to handle direct dependencies that
  252. * themselves have no dependency information.
  253. */
  254. typedef std::map< LibraryID, DependencyList > DependencyMap;
  255. /**
  256. * Inserts \a dep at the end of the dependency list of \a lib.
  257. */
  258. void InsertDependencyForVS6( DependencyMap& depMap,
  259. const LibraryID& lib,
  260. const LibraryID& dep);
  261. /*
  262. * Deletes \a dep from the dependency list of \a lib.
  263. */
  264. void DeleteDependencyForVS6( DependencyMap& depMap,
  265. const LibraryID& lib,
  266. const LibraryID& dep);
  267. /**
  268. * Emits the library \a lib and all its dependencies into link_line.
  269. * \a emitted keeps track of the libraries that have been emitted to
  270. * avoid duplicates--it is more efficient than searching
  271. * link_line. \a visited is used detect cycles. Note that \a
  272. * link_line is in reverse order, in that the dependencies of a
  273. * library are listed before the library itself.
  274. */
  275. void EmitForVS6( const LibraryID lib,
  276. const DependencyMap& dep_map,
  277. std::set<LibraryID>& emitted,
  278. std::set<LibraryID>& visited,
  279. DependencyList& link_line);
  280. /**
  281. * Finds the dependencies for \a lib and inserts them into \a
  282. * dep_map.
  283. */
  284. void GatherDependenciesForVS6( const cmMakefile& mf,
  285. const LibraryID& lib,
  286. DependencyMap& dep_map);
  287. void AnalyzeLibDependenciesForVS6( const cmMakefile& mf );
  288. #endif
  289. const char* GetSuffixVariableInternal(bool implib) const;
  290. const char* GetPrefixVariableInternal(bool implib) const;
  291. // Use a makefile variable to set a default for the given property.
  292. // If the variable is not defined use the given default instead.
  293. void SetPropertyDefault(const std::string& property,
  294. const char* default_value);
  295. std::string ImportedGetFullPath(const std::string& config,
  296. bool implib) const;
  297. private:
  298. mutable cmPropertyMap Properties;
  299. std::set<std::string> SystemIncludeDirectories;
  300. std::set<std::string> LinkDirectoriesEmmitted;
  301. std::set<std::string> Utilities;
  302. std::map<std::string, cmListFileBacktrace> UtilityBacktraces;
  303. cmPolicies::PolicyMap PolicyMap;
  304. std::string Name;
  305. std::string InstallPath;
  306. std::string RuntimeInstallPath;
  307. mutable std::string ExportMacro;
  308. std::vector<std::string> LinkDirectories;
  309. std::vector<cmCustomCommand> PreBuildCommands;
  310. std::vector<cmCustomCommand> PreLinkCommands;
  311. std::vector<cmCustomCommand> PostBuildCommands;
  312. std::vector<std::pair<TLLSignature, cmListFileContext> > TLLCommands;
  313. LinkLibraryVectorType PrevLinkedLibraries;
  314. LinkLibraryVectorType OriginalLinkLibraries;
  315. #if defined(_WIN32) && !defined(__CYGWIN__)
  316. LinkLibraryVectorType LinkLibrariesForVS6;
  317. #endif
  318. cmMakefile* Makefile;
  319. cmTargetInternalPointer Internal;
  320. cmState::TargetType TargetTypeValue;
  321. bool HaveInstallRule;
  322. bool RecordDependencies;
  323. bool DLLPlatform;
  324. bool IsAndroid;
  325. bool IsApple;
  326. bool IsImportedTarget;
  327. bool BuildInterfaceIncludesAppended;
  328. #if defined(_WIN32) && !defined(__CYGWIN__)
  329. bool LinkLibrariesForVS6Analyzed;
  330. #endif
  331. std::string ProcessSourceItemCMP0049(const std::string& s);
  332. /** Return whether or not the target has a DLL import library. */
  333. bool HasImportLibrary() const;
  334. // Internal representation details.
  335. friend class cmTargetInternals;
  336. friend class cmGeneratorTarget;
  337. friend class cmTargetTraceDependencies;
  338. cmListFileBacktrace Backtrace;
  339. };
  340. #ifdef CMAKE_BUILD_WITH_CMAKE
  341. #ifdef CMake_HAVE_CXX11_UNORDERED_MAP
  342. typedef std::unordered_map<std::string, cmTarget> cmTargets;
  343. #else
  344. typedef cmsys::hash_map<std::string, cmTarget> cmTargets;
  345. #endif
  346. #else
  347. typedef std::map<std::string,cmTarget> cmTargets;
  348. #endif
  349. class cmTargetSet: public std::set<std::string> {};
  350. class cmTargetManifest: public std::map<std::string, cmTargetSet> {};
  351. #endif