cmTarget.h 14 KB

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