cmTarget.h 13 KB

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