cmTarget.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 cmSourceFile;
  17. class cmGlobalGenerator;
  18. /** \class cmTarget
  19. * \brief Represent a library or executable target loaded from a makefile.
  20. *
  21. * cmTarget represents a target loaded from
  22. * a makefile.
  23. */
  24. class cmTarget
  25. {
  26. public:
  27. enum TargetType { EXECUTABLE, STATIC_LIBRARY,
  28. SHARED_LIBRARY, MODULE_LIBRARY, UTILITY, INSTALL_FILES,
  29. INSTALL_PROGRAMS };
  30. enum CustomCommandType { PRE_BUILD, PRE_LINK, POST_BUILD };
  31. /**
  32. * Return the type of target.
  33. */
  34. TargetType GetType() const
  35. {
  36. return m_TargetType;
  37. }
  38. /**
  39. * Set the target type
  40. */
  41. void SetType(TargetType f, const char* name);
  42. ///! Set/Get the name of the target
  43. const char* GetName() const {return m_Name.c_str();}
  44. /**
  45. * Indicate whether the target is part of the all target
  46. */
  47. bool IsInAll() const { return m_InAll; }
  48. bool GetInAll() const { return m_InAll; }
  49. void SetInAll(bool f) { m_InAll = f; }
  50. /**
  51. * Get the list of the custom commands for this target
  52. */
  53. const std::vector<cmCustomCommand> &GetPreBuildCommands() const
  54. {return m_PreBuildCommands;}
  55. std::vector<cmCustomCommand> &GetPreBuildCommands()
  56. {return m_PreBuildCommands;}
  57. const std::vector<cmCustomCommand> &GetPreLinkCommands() const
  58. {return m_PreLinkCommands;}
  59. std::vector<cmCustomCommand> &GetPreLinkCommands()
  60. {return m_PreLinkCommands;}
  61. const std::vector<cmCustomCommand> &GetPostBuildCommands() const
  62. {return m_PostBuildCommands;}
  63. std::vector<cmCustomCommand> &GetPostBuildCommands()
  64. {return m_PostBuildCommands;}
  65. /**
  66. * Get the list of the source lists used by this target
  67. */
  68. const std::vector<std::string> &GetSourceLists() const
  69. {return m_SourceLists;}
  70. std::vector<std::string> &GetSourceLists() {return m_SourceLists;}
  71. /**
  72. * Get the list of the source files used by this target
  73. */
  74. const std::vector<cmSourceFile*> &GetSourceFiles() const
  75. {return m_SourceFiles;}
  76. std::vector<cmSourceFile*> &GetSourceFiles() {return m_SourceFiles;}
  77. /**
  78. * Get the list of the source files used by this target
  79. */
  80. enum LinkLibraryType {GENERAL, DEBUG, OPTIMIZED};
  81. typedef std::vector<std::pair<std::string,LinkLibraryType> > LinkLibraries;
  82. const LinkLibraries &GetLinkLibraries() const {return m_LinkLibraries;}
  83. /**
  84. * Clear the dependency information recorded for this target, if any.
  85. */
  86. void ClearDependencyInformation(cmMakefile& mf, const char* target);
  87. void AddLinkLibrary(cmMakefile& mf,
  88. const char *target, const char* lib,
  89. LinkLibraryType llt);
  90. void AddLinkLibrary(const std::string& lib,
  91. LinkLibraryType llt);
  92. void MergeLinkLibraries( cmMakefile& mf, const char* selfname, const LinkLibraries& libs );
  93. const std::vector<std::string>& GetLinkDirectories() const {return m_LinkDirectories;}
  94. void AddLinkDirectory(const char* d);
  95. /**
  96. * Set the path where this target should be installed. This is relative to
  97. * INSTALL_PREFIX
  98. */
  99. std::string GetInstallPath() const {return m_InstallPath;}
  100. void SetInstallPath(const char *name) {m_InstallPath = name;}
  101. /**
  102. * Set the path where this target (if it has a runtime part) should be
  103. * installed. This is relative to INSTALL_PREFIX
  104. */
  105. std::string GetRuntimeInstallPath() const {return m_RuntimeInstallPath;}
  106. void SetRuntimeInstallPath(const char *name) {m_RuntimeInstallPath = name;}
  107. /**
  108. * Generate the SourceFilesList from the SourceLists. This should only be
  109. * done once to be safe.
  110. */
  111. void GenerateSourceFilesFromSourceLists(cmMakefile &mf);
  112. /** Add a utility on which this project depends. A utility is an executable
  113. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  114. * commands. It is not a full path nor does it have an extension.
  115. */
  116. void AddUtility(const char* u) { m_Utilities.insert(u);}
  117. ///! Get the utilities used by this target
  118. std::set<cmStdString>const& GetUtilities() const { return m_Utilities; }
  119. void AnalyzeLibDependencies( const cmMakefile& mf );
  120. ///! Set/Get a property of this target file
  121. void SetProperty(const char *prop, const char *value);
  122. const char *GetProperty(const char *prop) const;
  123. bool GetPropertyAsBool(const char *prop) const;
  124. /**
  125. * Trace through the source files in this target and add al source files
  126. * that they depend on, used by the visual studio generators
  127. */
  128. void TraceVSDependencies(std::string projName, cmMakefile *mf);
  129. ///! Return the prefered linker language for this target
  130. const char* GetLinkerLanguage(cmGlobalGenerator*) const;
  131. private:
  132. /**
  133. * A list of direct dependencies. Use in conjunction with DependencyMap.
  134. */
  135. typedef std::vector<cmStdString> DependencyList;
  136. /**
  137. * This map holds the dependency graph. map[x] returns a set of
  138. * direct dependencies of x. Note that the direct depenencies are
  139. * ordered. This is necessary to handle direct dependencies that
  140. * themselves have no dependency information.
  141. */
  142. typedef std::map< cmStdString, std::vector< cmStdString > > DependencyMap;
  143. /**
  144. * Maps a library name to its internal structure
  145. */
  146. typedef std::map< cmStdString, std::pair<cmStdString,LinkLibraryType> > LibTypeMap;
  147. /**
  148. * Inserts \a dep at the end of the dependency list of \a lib.
  149. */
  150. void InsertDependency( DependencyMap& depMap,
  151. const cmStdString& lib,
  152. const cmStdString& dep ) const;
  153. /*
  154. * Deletes \a dep from the dependency list of \a lib.
  155. */
  156. void DeleteDependency( DependencyMap& depMap,
  157. const cmStdString& lib,
  158. const cmStdString& dep ) const;
  159. /**
  160. * Emits the library \a lib and all its dependencies into link_line.
  161. * \a emitted keeps track of the libraries that have been emitted to
  162. * avoid duplicates--it is more efficient than searching
  163. * link_line. \a visited is used detect cycles. Note that \a
  164. * link_line is in reverse order, in that the dependencies of a
  165. * library are listed before the library itself.
  166. */
  167. void Emit( const std::string& lib,
  168. const DependencyMap& dep_map,
  169. std::set<cmStdString>& emitted,
  170. std::set<cmStdString>& visited,
  171. std::vector<std::string>& link_line ) const;
  172. /**
  173. * Finds the dependencies for \a lib and inserts them into \a
  174. * dep_map.
  175. */
  176. void GatherDependencies( const cmMakefile& mf, const std::string& lib,
  177. DependencyMap& dep_map );
  178. private:
  179. std::string m_Name;
  180. std::vector<cmCustomCommand> m_PreBuildCommands;
  181. std::vector<cmCustomCommand> m_PreLinkCommands;
  182. std::vector<cmCustomCommand> m_PostBuildCommands;
  183. std::vector<std::string> m_SourceLists;
  184. TargetType m_TargetType;
  185. std::vector<cmSourceFile*> m_SourceFiles;
  186. LinkLibraries m_LinkLibraries;
  187. LinkLibraries m_PrevLinkedLibraries;
  188. std::vector<std::string> m_LinkDirectories;
  189. bool m_InAll;
  190. std::string m_InstallPath;
  191. std::string m_RuntimeInstallPath;
  192. std::set<cmStdString> m_Utilities;
  193. bool m_RecordDependencies;
  194. std::map<cmStdString,cmStdString> m_Properties;
  195. };
  196. typedef std::map<cmStdString,cmTarget> cmTargets;
  197. #endif