cmTarget.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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()
  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() {return m_Name.c_str();}
  45. /**
  46. * Indicate whether the target is part of the all target
  47. */
  48. bool IsInAll() { return this->GetPropertyAsBool("IN_ALL"); }
  49. bool GetInAll() { return this->GetPropertyAsBool("IN_ALL"); }
  50. void SetInAll(bool f) { this->SetProperty("IN_ALL", (f) ?"TRUE" : "FALSE"); }
  51. ///! Set the cmMakefile that owns this target
  52. void SetMakefile(cmMakefile *mf) { m_Makefile = mf; };
  53. /**
  54. * Get the list of the custom commands for this target
  55. */
  56. std::vector<cmCustomCommand> &GetPreBuildCommands()
  57. {return m_PreBuildCommands;}
  58. std::vector<cmCustomCommand> &GetPreLinkCommands()
  59. {return m_PreLinkCommands;}
  60. std::vector<cmCustomCommand> &GetPostBuildCommands()
  61. {return m_PostBuildCommands;}
  62. /**
  63. * Get the list of the source lists used by this target
  64. */
  65. std::vector<std::string> &GetSourceLists() {return m_SourceLists;}
  66. /**
  67. * Get the list of the source files used by this target
  68. */
  69. std::vector<cmSourceFile*> &GetSourceFiles() {return m_SourceFiles;}
  70. /**
  71. * Get the list of the source files used by this target
  72. */
  73. enum LinkLibraryType {GENERAL, DEBUG, OPTIMIZED};
  74. typedef std::vector<std::pair<std::string,LinkLibraryType> > LinkLibraries;
  75. const LinkLibraries &GetLinkLibraries() {return m_LinkLibraries;}
  76. /**
  77. * Clear the dependency information recorded for this target, if any.
  78. */
  79. void ClearDependencyInformation(cmMakefile& mf, const char* target);
  80. void AddLinkLibrary(cmMakefile& mf,
  81. const char *target, const char* lib,
  82. LinkLibraryType llt);
  83. void AddLinkLibrary(const std::string& lib,
  84. LinkLibraryType llt);
  85. void MergeLinkLibraries( cmMakefile& mf, const char* selfname, const LinkLibraries& libs );
  86. const std::vector<std::string>& GetLinkDirectories()
  87. {return m_LinkDirectories;}
  88. void AddLinkDirectory(const char* d);
  89. /**
  90. * Set the path where this target should be installed. This is relative to
  91. * INSTALL_PREFIX
  92. */
  93. std::string GetInstallPath() {return m_InstallPath;}
  94. void SetInstallPath(const char *name) {m_InstallPath = name;}
  95. /**
  96. * Set the path where this target (if it has a runtime part) should be
  97. * installed. This is relative to INSTALL_PREFIX
  98. */
  99. std::string GetRuntimeInstallPath() {return m_RuntimeInstallPath;}
  100. void SetRuntimeInstallPath(const char *name) {m_RuntimeInstallPath = name;}
  101. /**
  102. * Generate the SourceFilesList from the SourceLists. This should only be
  103. * done once to be safe.
  104. */
  105. void GenerateSourceFilesFromSourceLists(cmMakefile &mf);
  106. /** Add a utility on which this project depends. A utility is an executable
  107. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  108. * commands. It is not a full path nor does it have an extension.
  109. */
  110. void AddUtility(const char* u) { m_Utilities.insert(u);}
  111. ///! Get the utilities used by this target
  112. std::set<cmStdString>const& GetUtilities() { return m_Utilities; }
  113. void AnalyzeLibDependencies( const cmMakefile& mf );
  114. ///! Set/Get a property of this target file
  115. void SetProperty(const char *prop, const char *value);
  116. const char *GetProperty(const char *prop);
  117. bool GetPropertyAsBool(const char *prop);
  118. /**
  119. * Trace through the source files in this target and add al source files
  120. * that they depend on, used by the visual studio generators
  121. */
  122. void TraceVSDependencies(std::string projName, cmMakefile *mf);
  123. ///! Return the prefered linker language for this target
  124. const char* GetLinkerLanguage(cmGlobalGenerator*);
  125. ///! Return the rule variable used to create this type of target,
  126. // need to add CMAKE_(LANG) for full name.
  127. const char* GetCreateRuleVariable();
  128. ///! Return the name of the variable to look up the target suffix
  129. const char* GetSuffixVariable();
  130. ///! Return the name of the variable to look up the target suffix
  131. const char* GetPrefixVariable();
  132. /** Get the full name of the target according to the settings in the
  133. given makefile. */
  134. std::string GetFullName(cmMakefile* mf);
  135. /** Get the base name (no suffix) of the target according to the
  136. settings in the given makefile. */
  137. std::string GetBaseName(cmMakefile* mf);
  138. /** Get the names of the library needed to generate a build rule
  139. that takes into account shared library version numbers. This
  140. should be called only on a library target. */
  141. void GetLibraryNames(cmMakefile* mf, std::string& name,
  142. std::string& soName, std::string& realName,
  143. std::string& baseName);
  144. /** Get the names of the library used to remove existing copies of
  145. the library from the build tree either before linking or during
  146. a clean step. This should be called only on a library
  147. target. */
  148. void GetLibraryCleanNames(cmMakefile* mf,
  149. std::string& staticName,
  150. std::string& sharedName,
  151. std::string& sharedSOName,
  152. std::string& sharedRealName);
  153. private:
  154. /**
  155. * A list of direct dependencies. Use in conjunction with DependencyMap.
  156. */
  157. typedef std::vector<cmStdString> DependencyList;
  158. /**
  159. * This map holds the dependency graph. map[x] returns a set of
  160. * direct dependencies of x. Note that the direct depenencies are
  161. * ordered. This is necessary to handle direct dependencies that
  162. * themselves have no dependency information.
  163. */
  164. typedef std::map< cmStdString, std::vector< cmStdString > > DependencyMap;
  165. /**
  166. * Maps a library name to its internal structure
  167. */
  168. typedef std::map< cmStdString, std::pair<cmStdString,LinkLibraryType> > LibTypeMap;
  169. /**
  170. * Inserts \a dep at the end of the dependency list of \a lib.
  171. */
  172. void InsertDependency( DependencyMap& depMap,
  173. const cmStdString& lib,
  174. const cmStdString& dep );
  175. /*
  176. * Deletes \a dep from the dependency list of \a lib.
  177. */
  178. void DeleteDependency( DependencyMap& depMap,
  179. const cmStdString& lib,
  180. const cmStdString& dep );
  181. /**
  182. * Emits the library \a lib and all its dependencies into link_line.
  183. * \a emitted keeps track of the libraries that have been emitted to
  184. * avoid duplicates--it is more efficient than searching
  185. * link_line. \a visited is used detect cycles. Note that \a
  186. * link_line is in reverse order, in that the dependencies of a
  187. * library are listed before the library itself.
  188. */
  189. void Emit( const std::string& lib,
  190. const DependencyMap& dep_map,
  191. std::set<cmStdString>& emitted,
  192. std::set<cmStdString>& visited,
  193. std::vector<std::string>& link_line );
  194. /**
  195. * Finds the dependencies for \a lib and inserts them into \a
  196. * dep_map.
  197. */
  198. void GatherDependencies( const cmMakefile& mf, const std::string& lib,
  199. DependencyMap& dep_map );
  200. const char* GetSuffixVariableInternal(TargetType type);
  201. const char* GetPrefixVariableInternal(TargetType type);
  202. std::string GetFullNameInternal(cmMakefile* mf, TargetType type);
  203. std::string GetBaseNameInternal(cmMakefile* mf, TargetType type);
  204. void GetLibraryNamesInternal(cmMakefile* mf,
  205. std::string& name,
  206. std::string& soName,
  207. std::string& realName,
  208. TargetType type);
  209. // update the value of the LOCATION var
  210. void UpdateLocation();
  211. private:
  212. std::string m_Name;
  213. std::vector<cmCustomCommand> m_PreBuildCommands;
  214. std::vector<cmCustomCommand> m_PreLinkCommands;
  215. std::vector<cmCustomCommand> m_PostBuildCommands;
  216. std::vector<std::string> m_SourceLists;
  217. TargetType m_TargetType;
  218. std::vector<cmSourceFile*> m_SourceFiles;
  219. LinkLibraries m_LinkLibraries;
  220. LinkLibraries m_PrevLinkedLibraries;
  221. std::vector<std::string> m_LinkDirectories;
  222. std::string m_InstallPath;
  223. std::string m_RuntimeInstallPath;
  224. std::set<cmStdString> m_Utilities;
  225. bool m_RecordDependencies;
  226. std::map<cmStdString,cmStdString> m_Properties;
  227. // the Makefile that owns this target
  228. cmMakefile *m_Makefile;
  229. };
  230. typedef std::map<cmStdString,cmTarget> cmTargets;
  231. #endif