cmTarget.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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, GLOBAL_TARGET,
  31. INSTALL_FILES, 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. const LinkLibraries &GetOriginalLinkLibraries() {return m_OriginalLinkLibraries;}
  81. /**
  82. * Clear the dependency information recorded for this target, if any.
  83. */
  84. void ClearDependencyInformation(cmMakefile& mf, const char* target);
  85. // Check to see if a library is a framework and treat it different on Mac
  86. bool AddFramework(const std::string& lib, LinkLibraryType llt);
  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();
  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() {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() {return m_RuntimeInstallPath;}
  106. void SetRuntimeInstallPath(const char *name) {m_RuntimeInstallPath = name;}
  107. /**
  108. * Get/Set whether there is an install rule for this target.
  109. */
  110. bool GetHaveInstallRule() { return m_HaveInstallRule; }
  111. void SetHaveInstallRule(bool h) { m_HaveInstallRule = h; }
  112. /**
  113. * Generate the SourceFilesList from the SourceLists. This should only be
  114. * done once to be safe.
  115. */
  116. void GenerateSourceFilesFromSourceLists(cmMakefile &mf);
  117. /** Add a utility on which this project depends. A utility is an executable
  118. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  119. * commands. It is not a full path nor does it have an extension.
  120. */
  121. void AddUtility(const char* u) { m_Utilities.insert(u);}
  122. ///! Get the utilities used by this target
  123. std::set<cmStdString>const& GetUtilities() { return m_Utilities; }
  124. void AnalyzeLibDependencies( const cmMakefile& mf );
  125. ///! Set/Get a property of this target file
  126. void SetProperty(const char *prop, const char *value);
  127. const char *GetProperty(const char *prop);
  128. bool GetPropertyAsBool(const char *prop);
  129. /** Get the directory in which this target will be built. If the
  130. configuration name is given then the generator will add its
  131. subdirectory for that configuration. Otherwise just the canonical
  132. output directory is given. */
  133. const char* GetDirectory(const char* config = 0);
  134. /** Get the location of the target in the build tree for the given
  135. configuration. This location is suitable for use as the LOCATION
  136. target property. */
  137. const char* GetLocation(const char* config);
  138. /**
  139. * Trace through the source files in this target and add al source files
  140. * that they depend on, used by the visual studio generators
  141. */
  142. void TraceVSDependencies(std::string projName, cmMakefile *mf);
  143. ///! Return the prefered linker language for this target
  144. const char* GetLinkerLanguage(cmGlobalGenerator*);
  145. ///! Return the rule variable used to create this type of target,
  146. // need to add CMAKE_(LANG) for full name.
  147. const char* GetCreateRuleVariable();
  148. /** Get the full name of the target according to the settings in its
  149. makefile. */
  150. std::string GetFullName(const char* config=0, bool implib = false);
  151. void GetFullName(std::string& prefix, std::string& base, std::string& suffix,
  152. const char* config=0, bool implib = false);
  153. /** Get the full path to the target according to the settings in its
  154. makefile and the configuration type. */
  155. std::string GetFullPath(const char* config=0, bool implib = false);
  156. /** Get the names of the library needed to generate a build rule
  157. that takes into account shared library version numbers. This
  158. should be called only on a library target. */
  159. void GetLibraryNames(std::string& name, std::string& soName,
  160. std::string& realName, std::string& impName,
  161. const char* config);
  162. /** Get the names of the library used to remove existing copies of
  163. the library from the build tree either before linking or during
  164. a clean step. This should be called only on a library
  165. target. */
  166. void GetLibraryCleanNames(std::string& staticName,
  167. std::string& sharedName,
  168. std::string& sharedSOName,
  169. std::string& sharedRealName,
  170. std::string& importName,
  171. const char* config);
  172. /** Get the names of the executable needed to generate a build rule
  173. that takes into account executable version numbers. This should
  174. be called only on an executable target. */
  175. void GetExecutableNames(std::string& name, std::string& realName,
  176. const char* config);
  177. /** Get the names of the executable used to remove existing copies
  178. of the executable from the build tree either before linking or
  179. during a clean step. This should be called only on an
  180. executable target. */
  181. void GetExecutableCleanNames(std::string& name, std::string& realName,
  182. const char* config);
  183. /**
  184. * Compute whether this target must be relinked before installing.
  185. */
  186. bool NeedRelinkBeforeInstall();
  187. bool HaveBuildTreeRPATH();
  188. bool HaveInstallTreeRPATH();
  189. std::string GetInstallNameDirForBuildTree(const char* config);
  190. std::string GetInstallNameDirForInstallTree(const char* config);
  191. private:
  192. /**
  193. * A list of direct dependencies. Use in conjunction with DependencyMap.
  194. */
  195. typedef std::vector<cmStdString> DependencyList;
  196. /**
  197. * This map holds the dependency graph. map[x] returns a set of
  198. * direct dependencies of x. Note that the direct depenencies are
  199. * ordered. This is necessary to handle direct dependencies that
  200. * themselves have no dependency information.
  201. */
  202. typedef std::map< cmStdString, std::vector< cmStdString > > DependencyMap;
  203. /**
  204. * Maps a library name to its internal structure
  205. */
  206. typedef std::map< cmStdString, std::pair<cmStdString,LinkLibraryType> > LibTypeMap;
  207. /**
  208. * Inserts \a dep at the end of the dependency list of \a lib.
  209. */
  210. void InsertDependency( DependencyMap& depMap,
  211. const cmStdString& lib,
  212. const cmStdString& dep );
  213. /*
  214. * Deletes \a dep from the dependency list of \a lib.
  215. */
  216. void DeleteDependency( DependencyMap& depMap,
  217. const cmStdString& lib,
  218. const cmStdString& dep );
  219. /**
  220. * Emits the library \a lib and all its dependencies into link_line.
  221. * \a emitted keeps track of the libraries that have been emitted to
  222. * avoid duplicates--it is more efficient than searching
  223. * link_line. \a visited is used detect cycles. Note that \a
  224. * link_line is in reverse order, in that the dependencies of a
  225. * library are listed before the library itself.
  226. */
  227. void Emit( const std::string& lib,
  228. const DependencyMap& dep_map,
  229. std::set<cmStdString>& emitted,
  230. std::set<cmStdString>& visited,
  231. std::vector<std::string>& link_line );
  232. /**
  233. * Finds the dependencies for \a lib and inserts them into \a
  234. * dep_map.
  235. */
  236. void GatherDependencies( const cmMakefile& mf, const std::string& lib,
  237. DependencyMap& dep_map );
  238. const char* GetSuffixVariableInternal(TargetType type, bool implib);
  239. const char* GetPrefixVariableInternal(TargetType type, bool implib);
  240. std::string GetFullNameInternal(TargetType type, const char* config,
  241. bool implib);
  242. void GetFullNameInternal(TargetType type, const char* config, bool implib,
  243. std::string& outPrefix, std::string& outBase,
  244. std::string& outSuffix);
  245. void GetLibraryNamesInternal(std::string& name,
  246. std::string& soName,
  247. std::string& realName,
  248. std::string& impName,
  249. TargetType type,
  250. const char* config);
  251. void GetExecutableNamesInternal(std::string& name,
  252. std::string& realName,
  253. TargetType type,
  254. const char* config);
  255. // update the value of the LOCATION var
  256. void UpdateLocation();
  257. // Use a makefile variable to set a default for the given property.
  258. // If the variable is not defined use the given default instead.
  259. void SetPropertyDefault(const char* property, const char* default_value);
  260. private:
  261. std::string m_Name;
  262. std::vector<cmCustomCommand> m_PreBuildCommands;
  263. std::vector<cmCustomCommand> m_PreLinkCommands;
  264. std::vector<cmCustomCommand> m_PostBuildCommands;
  265. std::vector<std::string> m_SourceLists;
  266. TargetType m_TargetType;
  267. std::vector<cmSourceFile*> m_SourceFiles;
  268. LinkLibraries m_LinkLibraries;
  269. LinkLibraries m_PrevLinkedLibraries;
  270. LinkLibraries m_OriginalLinkLibraries;
  271. bool m_LinkLibrariesAnalyzed;
  272. bool m_LinkDirectoriesComputed;
  273. std::vector<std::string> m_Frameworks;
  274. std::vector<std::string> m_LinkDirectories;
  275. std::vector<std::string> m_ExplicitLinkDirectories;
  276. bool m_HaveInstallRule;
  277. std::string m_InstallPath;
  278. std::string m_RuntimeInstallPath;
  279. std::string m_Directory;
  280. std::string m_Location;
  281. std::set<cmStdString> m_Utilities;
  282. bool m_RecordDependencies;
  283. std::map<cmStdString,cmStdString> m_Properties;
  284. // The cmMakefile instance that owns this target. This should
  285. // always be set.
  286. cmMakefile* m_Makefile;
  287. };
  288. typedef std::map<cmStdString,cmTarget> cmTargets;
  289. #endif