cmTarget.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. cmTarget();
  29. enum TargetType { EXECUTABLE, STATIC_LIBRARY,
  30. SHARED_LIBRARY, MODULE_LIBRARY, UTILITY, INSTALL_FILES,
  31. INSTALL_PROGRAMS };
  32. enum CustomCommandType { PRE_BUILD, PRE_LINK, POST_BUILD };
  33. /**
  34. * Return the type of target.
  35. */
  36. TargetType GetType()
  37. {
  38. return m_TargetType;
  39. }
  40. /**
  41. * Set the target type
  42. */
  43. void SetType(TargetType f, const char* name);
  44. ///! Set/Get the name of the target
  45. const char* GetName() {return m_Name.c_str();}
  46. /**
  47. * Indicate whether the target is part of the all target
  48. */
  49. bool IsInAll() { return this->GetPropertyAsBool("IN_ALL"); }
  50. bool GetInAll() { return this->GetPropertyAsBool("IN_ALL"); }
  51. void SetInAll(bool f) { this->SetProperty("IN_ALL", (f) ?"TRUE" : "FALSE"); }
  52. ///! Set the cmMakefile that owns this target
  53. void SetMakefile(cmMakefile *mf);
  54. cmMakefile *GetMakefile() { return m_Makefile;};
  55. /**
  56. * Get the list of the custom commands for this target
  57. */
  58. std::vector<cmCustomCommand> &GetPreBuildCommands()
  59. {return m_PreBuildCommands;}
  60. std::vector<cmCustomCommand> &GetPreLinkCommands()
  61. {return m_PreLinkCommands;}
  62. std::vector<cmCustomCommand> &GetPostBuildCommands()
  63. {return m_PostBuildCommands;}
  64. /**
  65. * Get the list of the source lists used by this target
  66. */
  67. std::vector<std::string> &GetSourceLists() {return m_SourceLists;}
  68. ///! Return the list of frameworks being linked to this target
  69. std::vector<std::string> &GetFrameworks() {return m_Frameworks;}
  70. /**
  71. * Get the list of the source files used by this target
  72. */
  73. std::vector<cmSourceFile*> &GetSourceFiles() {return m_SourceFiles;}
  74. /**
  75. * Get the list of the source files used by this target
  76. */
  77. enum LinkLibraryType {GENERAL, DEBUG, OPTIMIZED};
  78. typedef std::vector<std::pair<std::string,LinkLibraryType> > LinkLibraries;
  79. const LinkLibraries &GetLinkLibraries() {return m_LinkLibraries;}
  80. /**
  81. * Clear the dependency information recorded for this target, if any.
  82. */
  83. void ClearDependencyInformation(cmMakefile& mf, const char* target);
  84. // Check to see if a library is a framework and treat it different on Mac
  85. bool AddFramework(const std::string& lib, LinkLibraryType llt);
  86. void AddLinkLibrary(cmMakefile& mf,
  87. const char *target, const char* lib,
  88. LinkLibraryType llt);
  89. void AddLinkLibrary(const std::string& lib,
  90. LinkLibraryType llt);
  91. void MergeLinkLibraries( cmMakefile& mf, const char* selfname, const LinkLibraries& libs );
  92. const std::vector<std::string>& GetLinkDirectories();
  93. void AddLinkDirectory(const char* d);
  94. /**
  95. * Set the path where this target should be installed. This is relative to
  96. * INSTALL_PREFIX
  97. */
  98. std::string GetInstallPath() {return m_InstallPath;}
  99. void SetInstallPath(const char *name) {m_InstallPath = name;}
  100. /**
  101. * Set the path where this target (if it has a runtime part) should be
  102. * installed. This is relative to INSTALL_PREFIX
  103. */
  104. std::string GetRuntimeInstallPath() {return m_RuntimeInstallPath;}
  105. void SetRuntimeInstallPath(const char *name) {m_RuntimeInstallPath = name;}
  106. /**
  107. * Generate the SourceFilesList from the SourceLists. This should only be
  108. * done once to be safe.
  109. */
  110. void GenerateSourceFilesFromSourceLists(cmMakefile &mf);
  111. /** Add a utility on which this project depends. A utility is an executable
  112. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  113. * commands. It is not a full path nor does it have an extension.
  114. */
  115. void AddUtility(const char* u) { m_Utilities.insert(u);}
  116. ///! Get the utilities used by this target
  117. std::set<cmStdString>const& GetUtilities() { return m_Utilities; }
  118. void AnalyzeLibDependencies( const cmMakefile& mf );
  119. ///! Set/Get a property of this target file
  120. void SetProperty(const char *prop, const char *value);
  121. const char *GetProperty(const char *prop);
  122. bool GetPropertyAsBool(const char *prop);
  123. const char* GetDirectory();
  124. const char* GetLocation(const char* config);
  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*);
  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();
  137. ///! Return the name of the variable to look up the target suffix
  138. const char* GetPrefixVariable();
  139. /** Get the full name of the target according to the settings in its
  140. makefile. */
  141. std::string GetFullName(const char* config=0);
  142. void GetFullName(std::string& prefix, std::string& base, std::string& suffix,
  143. const char* config=0);
  144. /** Get the full path to the target according to the settings in its
  145. makefile and the configuration type. */
  146. std::string GetFullPath(const char* config=0);
  147. /** Get the names of the library needed to generate a build rule
  148. that takes into account shared library version numbers. This
  149. should be called only on a library target. */
  150. void GetLibraryNames(std::string& name, std::string& soName,
  151. std::string& realName, const char* config);
  152. /** Get the names of the library used to remove existing copies of
  153. the library from the build tree either before linking or during
  154. a clean step. This should be called only on a library
  155. target. */
  156. void GetLibraryCleanNames(std::string& staticName,
  157. std::string& sharedName,
  158. std::string& sharedSOName,
  159. std::string& sharedRealName,
  160. const char* config);
  161. /** Get the names of the executable needed to generate a build rule
  162. that takes into account executable version numbers. This should
  163. be called only on an executable target. */
  164. void GetExecutableNames(std::string& name, std::string& realName,
  165. const char* config);
  166. /** Get the names of the executable used to remove existing copies
  167. of the executable from the build tree either before linking or
  168. during a clean step. This should be called only on an
  169. executable target. */
  170. void GetExecutableCleanNames(std::string& name, std::string& realName,
  171. const char* config);
  172. /**
  173. * Compute whether this target must be relinked before installing.
  174. */
  175. bool NeedRelinkBeforeInstall();
  176. bool HaveBuildTreeRPATH();
  177. bool HaveInstallTreeRPATH();
  178. private:
  179. /**
  180. * A list of direct dependencies. Use in conjunction with DependencyMap.
  181. */
  182. typedef std::vector<cmStdString> DependencyList;
  183. /**
  184. * This map holds the dependency graph. map[x] returns a set of
  185. * direct dependencies of x. Note that the direct depenencies are
  186. * ordered. This is necessary to handle direct dependencies that
  187. * themselves have no dependency information.
  188. */
  189. typedef std::map< cmStdString, std::vector< cmStdString > > DependencyMap;
  190. /**
  191. * Maps a library name to its internal structure
  192. */
  193. typedef std::map< cmStdString, std::pair<cmStdString,LinkLibraryType> > LibTypeMap;
  194. /**
  195. * Inserts \a dep at the end of the dependency list of \a lib.
  196. */
  197. void InsertDependency( DependencyMap& depMap,
  198. const cmStdString& lib,
  199. const cmStdString& dep );
  200. /*
  201. * Deletes \a dep from the dependency list of \a lib.
  202. */
  203. void DeleteDependency( DependencyMap& depMap,
  204. const cmStdString& lib,
  205. const cmStdString& dep );
  206. /**
  207. * Emits the library \a lib and all its dependencies into link_line.
  208. * \a emitted keeps track of the libraries that have been emitted to
  209. * avoid duplicates--it is more efficient than searching
  210. * link_line. \a visited is used detect cycles. Note that \a
  211. * link_line is in reverse order, in that the dependencies of a
  212. * library are listed before the library itself.
  213. */
  214. void Emit( const std::string& lib,
  215. const DependencyMap& dep_map,
  216. std::set<cmStdString>& emitted,
  217. std::set<cmStdString>& visited,
  218. std::vector<std::string>& link_line );
  219. /**
  220. * Finds the dependencies for \a lib and inserts them into \a
  221. * dep_map.
  222. */
  223. void GatherDependencies( const cmMakefile& mf, const std::string& lib,
  224. DependencyMap& dep_map );
  225. const char* GetSuffixVariableInternal(TargetType type);
  226. const char* GetPrefixVariableInternal(TargetType type);
  227. std::string GetFullNameInternal(TargetType type, const char* config);
  228. void GetFullNameInternal(TargetType type, const char* config,
  229. std::string& outPrefix, std::string& outBase,
  230. std::string& outSuffix);
  231. void GetLibraryNamesInternal(std::string& name,
  232. std::string& soName,
  233. std::string& realName,
  234. TargetType type,
  235. const char* config);
  236. void GetExecutableNamesInternal(std::string& name,
  237. std::string& realName,
  238. TargetType type,
  239. const char* config);
  240. // update the value of the LOCATION var
  241. void UpdateLocation();
  242. // Use a makefile variable to set a default for the given property.
  243. // If the variable is not defined use the given default instead.
  244. void SetPropertyDefault(const char* property, const char* default_value);
  245. private:
  246. std::string m_Name;
  247. std::vector<cmCustomCommand> m_PreBuildCommands;
  248. std::vector<cmCustomCommand> m_PreLinkCommands;
  249. std::vector<cmCustomCommand> m_PostBuildCommands;
  250. std::vector<std::string> m_SourceLists;
  251. TargetType m_TargetType;
  252. std::vector<cmSourceFile*> m_SourceFiles;
  253. LinkLibraries m_LinkLibraries;
  254. LinkLibraries m_PrevLinkedLibraries;
  255. bool m_LinkLibrariesAnalyzed;
  256. bool m_LinkDirectoriesComputed;
  257. std::vector<std::string> m_Frameworks;
  258. std::vector<std::string> m_LinkDirectories;
  259. std::vector<std::string> m_ExplicitLinkDirectories;
  260. std::string m_InstallPath;
  261. std::string m_RuntimeInstallPath;
  262. std::string m_Directory;
  263. std::string m_Location;
  264. std::set<cmStdString> m_Utilities;
  265. bool m_RecordDependencies;
  266. std::map<cmStdString,cmStdString> m_Properties;
  267. // The cmMakefile instance that owns this target. This should
  268. // always be set.
  269. cmMakefile* m_Makefile;
  270. };
  271. typedef std::map<cmStdString,cmTarget> cmTargets;
  272. #endif