cmTarget.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #ifndef cmTarget_h
  14. #define cmTarget_h
  15. #include "cmCustomCommand.h"
  16. class cmMakefile;
  17. class cmSourceFile;
  18. class cmGlobalGenerator;
  19. /** \class cmTarget
  20. * \brief Represent a library or executable target loaded from a makefile.
  21. *
  22. * cmTarget represents a target loaded from
  23. * a makefile.
  24. */
  25. class cmTarget
  26. {
  27. public:
  28. enum TargetType { EXECUTABLE, STATIC_LIBRARY,
  29. SHARED_LIBRARY, MODULE_LIBRARY, UTILITY, INSTALL_FILES,
  30. INSTALL_PROGRAMS };
  31. enum CustomCommandType { PRE_BUILD, PRE_LINK, POST_BUILD };
  32. /**
  33. * Return the type of target.
  34. */
  35. TargetType GetType() const
  36. {
  37. return m_TargetType;
  38. }
  39. /**
  40. * Set the target type
  41. */
  42. void SetType(TargetType f, const char* name);
  43. ///! Set/Get the name of the target
  44. const char* GetName() const {return m_Name.c_str();}
  45. /**
  46. * Indicate whether the target is part of the all target
  47. */
  48. bool IsInAll() const { return this->GetPropertyAsBool("IN_ALL"); }
  49. bool GetInAll() const { return this->GetPropertyAsBool("IN_ALL"); }
  50. void SetInAll(bool f) { this->SetProperty("IN_ALL", (f) ? "TRUE" : "FALSE"); }
  51. /**
  52. * Get the list of the custom commands for this target
  53. */
  54. const std::vector<cmCustomCommand> &GetPreBuildCommands() const
  55. {return m_PreBuildCommands;}
  56. std::vector<cmCustomCommand> &GetPreBuildCommands()
  57. {return m_PreBuildCommands;}
  58. const std::vector<cmCustomCommand> &GetPreLinkCommands() const
  59. {return m_PreLinkCommands;}
  60. std::vector<cmCustomCommand> &GetPreLinkCommands()
  61. {return m_PreLinkCommands;}
  62. const std::vector<cmCustomCommand> &GetPostBuildCommands() const
  63. {return m_PostBuildCommands;}
  64. std::vector<cmCustomCommand> &GetPostBuildCommands()
  65. {return m_PostBuildCommands;}
  66. /**
  67. * Get the list of the source lists used by this target
  68. */
  69. const std::vector<std::string> &GetSourceLists() const
  70. {return m_SourceLists;}
  71. std::vector<std::string> &GetSourceLists() {return m_SourceLists;}
  72. /**
  73. * Get the list of the source files used by this target
  74. */
  75. const std::vector<cmSourceFile*> &GetSourceFiles() const
  76. {return m_SourceFiles;}
  77. std::vector<cmSourceFile*> &GetSourceFiles() {return m_SourceFiles;}
  78. /**
  79. * Get the list of the source files used by this target
  80. */
  81. enum LinkLibraryType {GENERAL, DEBUG, OPTIMIZED};
  82. typedef std::vector<std::pair<std::string,LinkLibraryType> > LinkLibraries;
  83. const LinkLibraries &GetLinkLibraries() const {return m_LinkLibraries;}
  84. /**
  85. * Clear the dependency information recorded for this target, if any.
  86. */
  87. void ClearDependencyInformation(cmMakefile& mf, const char* target);
  88. void AddLinkLibrary(cmMakefile& mf,
  89. const char *target, const char* lib,
  90. LinkLibraryType llt);
  91. void AddLinkLibrary(const std::string& lib,
  92. LinkLibraryType llt);
  93. void MergeLinkLibraries( cmMakefile& mf, const char* selfname, const LinkLibraries& libs );
  94. const std::vector<std::string>& GetLinkDirectories() const {return m_LinkDirectories;}
  95. void AddLinkDirectory(const char* d);
  96. /**
  97. * Set the path where this target should be installed. This is relative to
  98. * INSTALL_PREFIX
  99. */
  100. std::string GetInstallPath() const {return m_InstallPath;}
  101. void SetInstallPath(const char *name) {m_InstallPath = name;}
  102. /**
  103. * Set the path where this target (if it has a runtime part) should be
  104. * installed. This is relative to INSTALL_PREFIX
  105. */
  106. std::string GetRuntimeInstallPath() const {return m_RuntimeInstallPath;}
  107. void SetRuntimeInstallPath(const char *name) {m_RuntimeInstallPath = name;}
  108. /**
  109. * Generate the SourceFilesList from the SourceLists. This should only be
  110. * done once to be safe.
  111. */
  112. void GenerateSourceFilesFromSourceLists(cmMakefile &mf);
  113. /** Add a utility on which this project depends. A utility is an executable
  114. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  115. * commands. It is not a full path nor does it have an extension.
  116. */
  117. void AddUtility(const char* u) { m_Utilities.insert(u);}
  118. ///! Get the utilities used by this target
  119. std::set<cmStdString>const& GetUtilities() const { return m_Utilities; }
  120. void AnalyzeLibDependencies( const cmMakefile& mf );
  121. ///! Set/Get a property of this target file
  122. void SetProperty(const char *prop, const char *value);
  123. const char *GetProperty(const char *prop) const;
  124. bool GetPropertyAsBool(const char *prop) const;
  125. /**
  126. * Trace through the source files in this target and add al source files
  127. * that they depend on, used by the visual studio generators
  128. */
  129. void TraceVSDependencies(std::string projName, cmMakefile *mf);
  130. ///! Return the prefered linker language for this target
  131. const char* GetLinkerLanguage(cmGlobalGenerator*) const;
  132. ///! Return the rule variable used to create this type of target,
  133. // need to add CMAKE_(LANG) for full name.
  134. const char* GetCreateRuleVariable();
  135. ///! Return the name of the variable to look up the target suffix
  136. const char* GetSuffixVariable() const;
  137. ///! Return the name of the variable to look up the target suffix
  138. const char* GetPrefixVariable() const;
  139. // Get the full name of the target according to the settings in the
  140. // given makefile.
  141. std::string GetFullName(cmMakefile* mf) const;
  142. // Get the baes name (no suffix) of the target according to the
  143. // settings in the given makefile.
  144. std::string GetBaseName(cmMakefile* mf) const;
  145. private:
  146. /**
  147. * A list of direct dependencies. Use in conjunction with DependencyMap.
  148. */
  149. typedef std::vector<cmStdString> DependencyList;
  150. /**
  151. * This map holds the dependency graph. map[x] returns a set of
  152. * direct dependencies of x. Note that the direct depenencies are
  153. * ordered. This is necessary to handle direct dependencies that
  154. * themselves have no dependency information.
  155. */
  156. typedef std::map< cmStdString, std::vector< cmStdString > > DependencyMap;
  157. /**
  158. * Maps a library name to its internal structure
  159. */
  160. typedef std::map< cmStdString, std::pair<cmStdString,LinkLibraryType> > LibTypeMap;
  161. /**
  162. * Inserts \a dep at the end of the dependency list of \a lib.
  163. */
  164. void InsertDependency( DependencyMap& depMap,
  165. const cmStdString& lib,
  166. const cmStdString& dep ) const;
  167. /*
  168. * Deletes \a dep from the dependency list of \a lib.
  169. */
  170. void DeleteDependency( DependencyMap& depMap,
  171. const cmStdString& lib,
  172. const cmStdString& dep ) const;
  173. /**
  174. * Emits the library \a lib and all its dependencies into link_line.
  175. * \a emitted keeps track of the libraries that have been emitted to
  176. * avoid duplicates--it is more efficient than searching
  177. * link_line. \a visited is used detect cycles. Note that \a
  178. * link_line is in reverse order, in that the dependencies of a
  179. * library are listed before the library itself.
  180. */
  181. void Emit( const std::string& lib,
  182. const DependencyMap& dep_map,
  183. std::set<cmStdString>& emitted,
  184. std::set<cmStdString>& visited,
  185. std::vector<std::string>& link_line ) const;
  186. /**
  187. * Finds the dependencies for \a lib and inserts them into \a
  188. * dep_map.
  189. */
  190. void GatherDependencies( const cmMakefile& mf, const std::string& lib,
  191. DependencyMap& dep_map );
  192. const char* GetSuffixVariableInternal(TargetType type) const;
  193. const char* GetPrefixVariableInternal(TargetType type) const;
  194. std::string GetFullNameInternal(cmMakefile* mf, TargetType type) const;
  195. std::string GetBaseNameInternal(cmMakefile* mf, TargetType type) const;
  196. private:
  197. std::string m_Name;
  198. std::vector<cmCustomCommand> m_PreBuildCommands;
  199. std::vector<cmCustomCommand> m_PreLinkCommands;
  200. std::vector<cmCustomCommand> m_PostBuildCommands;
  201. std::vector<std::string> m_SourceLists;
  202. TargetType m_TargetType;
  203. std::vector<cmSourceFile*> m_SourceFiles;
  204. LinkLibraries m_LinkLibraries;
  205. LinkLibraries m_PrevLinkedLibraries;
  206. std::vector<std::string> m_LinkDirectories;
  207. std::string m_InstallPath;
  208. std::string m_RuntimeInstallPath;
  209. std::set<cmStdString> m_Utilities;
  210. bool m_RecordDependencies;
  211. std::map<cmStdString,cmStdString> m_Properties;
  212. };
  213. typedef std::map<cmStdString,cmTarget> cmTargets;
  214. #endif