cmTarget.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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, INSTALL_DIRECTORY};
  32. enum CustomCommandType { PRE_BUILD, PRE_LINK, POST_BUILD };
  33. /**
  34. * Return the type of target.
  35. */
  36. TargetType GetType()
  37. {
  38. return this->TargetTypeValue;
  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 this->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) {
  52. this->SetProperty("IN_ALL", (f) ?"TRUE" : "FALSE"); }
  53. ///! Set the cmMakefile that owns this target
  54. void SetMakefile(cmMakefile *mf);
  55. cmMakefile *GetMakefile() { return this->Makefile;};
  56. /**
  57. * Get the list of the custom commands for this target
  58. */
  59. std::vector<cmCustomCommand> &GetPreBuildCommands()
  60. {return this->PreBuildCommands;}
  61. std::vector<cmCustomCommand> &GetPreLinkCommands()
  62. {return this->PreLinkCommands;}
  63. std::vector<cmCustomCommand> &GetPostBuildCommands()
  64. {return this->PostBuildCommands;}
  65. /**
  66. * Get the list of the source lists used by this target
  67. */
  68. std::vector<std::string> &GetSourceLists() {return this->SourceLists;}
  69. ///! Return the list of frameworks being linked to this target
  70. std::vector<std::string> &GetFrameworks() {return this->Frameworks;}
  71. /**
  72. * Get the list of the source files used by this target
  73. */
  74. std::vector<cmSourceFile*> &GetSourceFiles() {return this->SourceFiles;}
  75. /**
  76. * Get the list of the source files used by this target
  77. */
  78. enum LinkLibraryType {GENERAL, DEBUG, OPTIMIZED};
  79. typedef std::vector<std::pair<std::string,LinkLibraryType> >
  80. LinkLibraryVectorType;
  81. const LinkLibraryVectorType &GetLinkLibraries() {
  82. return this->LinkLibraries;}
  83. const LinkLibraryVectorType &GetOriginalLinkLibraries()
  84. {return this->OriginalLinkLibraries;}
  85. /**
  86. * Clear the dependency information recorded for this target, if any.
  87. */
  88. void ClearDependencyInformation(cmMakefile& mf, const char* target);
  89. // Check to see if a library is a framework and treat it different on Mac
  90. bool AddFramework(const std::string& lib, LinkLibraryType llt);
  91. void AddLinkLibrary(cmMakefile& mf,
  92. const char *target, const char* lib,
  93. LinkLibraryType llt);
  94. void AddLinkLibrary(const std::string& lib,
  95. LinkLibraryType llt);
  96. void MergeLinkLibraries( cmMakefile& mf, const char* selfname,
  97. const LinkLibraryVectorType& libs );
  98. const std::vector<std::string>& GetLinkDirectories();
  99. void AddLinkDirectory(const char* d);
  100. /**
  101. * Set the path where this target should be installed. This is relative to
  102. * INSTALL_PREFIX
  103. */
  104. std::string GetInstallPath() {return this->InstallPath;}
  105. void SetInstallPath(const char *name) {this->InstallPath = name;}
  106. /**
  107. * Set the path where this target (if it has a runtime part) should be
  108. * installed. This is relative to INSTALL_PREFIX
  109. */
  110. std::string GetRuntimeInstallPath() {return this->RuntimeInstallPath;}
  111. void SetRuntimeInstallPath(const char *name) {
  112. this->RuntimeInstallPath = name;}
  113. /**
  114. * Get/Set whether there is an install rule for this target.
  115. */
  116. bool GetHaveInstallRule() { return this->HaveInstallRule; }
  117. void SetHaveInstallRule(bool h) { this->HaveInstallRule = h; }
  118. /**
  119. * Generate the SourceFilesList from the SourceLists. This should only be
  120. * done once to be safe.
  121. */
  122. void GenerateSourceFilesFromSourceLists(cmMakefile &mf);
  123. /** Add a utility on which this project depends. A utility is an executable
  124. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  125. * commands. It is not a full path nor does it have an extension.
  126. */
  127. void AddUtility(const char* u) { this->Utilities.insert(u);}
  128. ///! Get the utilities used by this target
  129. std::set<cmStdString>const& GetUtilities() { return this->Utilities; }
  130. void AnalyzeLibDependencies( const cmMakefile& mf );
  131. ///! Set/Get a property of this target file
  132. void SetProperty(const char *prop, const char *value);
  133. const char *GetProperty(const char *prop);
  134. bool GetPropertyAsBool(const char *prop);
  135. /** Get the directory in which this target will be built. If the
  136. configuration name is given then the generator will add its
  137. subdirectory for that configuration. Otherwise just the canonical
  138. output directory is given. */
  139. const char* GetDirectory(const char* config = 0);
  140. /** Get the location of the target in the build tree for the given
  141. configuration. This location is suitable for use as the LOCATION
  142. target property. */
  143. const char* GetLocation(const char* config);
  144. /**
  145. * Trace through the source files in this target and add al source files
  146. * that they depend on, used by the visual studio generators
  147. */
  148. void TraceVSDependencies(std::string projName, cmMakefile *mf);
  149. ///! Return the prefered linker language for this target
  150. const char* GetLinkerLanguage(cmGlobalGenerator*);
  151. ///! Return the rule variable used to create this type of target,
  152. // need to add CMAKE_(LANG) for full name.
  153. const char* GetCreateRuleVariable();
  154. /** Get the full name of the target according to the settings in its
  155. makefile. */
  156. std::string GetFullName(const char* config=0, bool implib = false);
  157. void GetFullName(std::string& prefix,
  158. std::string& base, std::string& suffix,
  159. const char* config=0, bool implib = false);
  160. /** Get the full path to the target according to the settings in its
  161. makefile and the configuration type. */
  162. std::string GetFullPath(const char* config=0, bool implib = false);
  163. /** Get the names of the library needed to generate a build rule
  164. that takes into account shared library version numbers. This
  165. should be called only on a library target. */
  166. void GetLibraryNames(std::string& name, std::string& soName,
  167. std::string& realName, std::string& impName,
  168. const char* config);
  169. /** Get the names of the library used to remove existing copies of
  170. the library from the build tree either before linking or during
  171. a clean step. This should be called only on a library
  172. target. */
  173. void GetLibraryCleanNames(std::string& staticName,
  174. std::string& sharedName,
  175. std::string& sharedSOName,
  176. std::string& sharedRealName,
  177. std::string& importName,
  178. const char* config);
  179. /** Get the names of the executable needed to generate a build rule
  180. that takes into account executable version numbers. This should
  181. be called only on an executable target. */
  182. void GetExecutableNames(std::string& name, std::string& realName,
  183. const char* config);
  184. /** Get the names of the executable used to remove existing copies
  185. of the executable from the build tree either before linking or
  186. during a clean step. This should be called only on an
  187. executable target. */
  188. void GetExecutableCleanNames(std::string& name, std::string& realName,
  189. const char* config);
  190. /**
  191. * Compute whether this target must be relinked before installing.
  192. */
  193. bool NeedRelinkBeforeInstall();
  194. bool HaveBuildTreeRPATH();
  195. bool HaveInstallTreeRPATH();
  196. std::string GetInstallNameDirForBuildTree(const char* config);
  197. std::string GetInstallNameDirForInstallTree(const char* config);
  198. private:
  199. /**
  200. * A list of direct dependencies. Use in conjunction with DependencyMap.
  201. */
  202. typedef std::vector<cmStdString> DependencyList;
  203. /**
  204. * This map holds the dependency graph. map[x] returns a set of
  205. * direct dependencies of x. Note that the direct depenencies are
  206. * ordered. This is necessary to handle direct dependencies that
  207. * themselves have no dependency information.
  208. */
  209. typedef std::map< cmStdString, std::vector< cmStdString > > DependencyMap;
  210. /**
  211. * Maps a library name to its internal structure
  212. */
  213. typedef std::map< cmStdString, std::pair<cmStdString,LinkLibraryType> >
  214. LibTypeMap;
  215. /**
  216. * Inserts \a dep at the end of the dependency list of \a lib.
  217. */
  218. void InsertDependency( DependencyMap& depMap,
  219. const cmStdString& lib,
  220. const cmStdString& dep );
  221. /*
  222. * Deletes \a dep from the dependency list of \a lib.
  223. */
  224. void DeleteDependency( DependencyMap& depMap,
  225. const cmStdString& lib,
  226. const cmStdString& dep );
  227. /**
  228. * Emits the library \a lib and all its dependencies into link_line.
  229. * \a emitted keeps track of the libraries that have been emitted to
  230. * avoid duplicates--it is more efficient than searching
  231. * link_line. \a visited is used detect cycles. Note that \a
  232. * link_line is in reverse order, in that the dependencies of a
  233. * library are listed before the library itself.
  234. */
  235. void Emit( const std::string& lib,
  236. const DependencyMap& dep_map,
  237. std::set<cmStdString>& emitted,
  238. std::set<cmStdString>& visited,
  239. std::vector<std::string>& link_line );
  240. /**
  241. * Finds the dependencies for \a lib and inserts them into \a
  242. * dep_map.
  243. */
  244. void GatherDependencies( const cmMakefile& mf, const std::string& lib,
  245. DependencyMap& dep_map );
  246. const char* GetSuffixVariableInternal(TargetType type, bool implib);
  247. const char* GetPrefixVariableInternal(TargetType type, bool implib);
  248. std::string GetFullNameInternal(TargetType type, const char* config,
  249. bool implib);
  250. void GetFullNameInternal(TargetType type, const char* config, bool implib,
  251. std::string& outPrefix, std::string& outBase,
  252. std::string& outSuffix);
  253. void GetLibraryNamesInternal(std::string& name,
  254. std::string& soName,
  255. std::string& realName,
  256. std::string& impName,
  257. TargetType type,
  258. const char* config);
  259. void GetExecutableNamesInternal(std::string& name,
  260. std::string& realName,
  261. TargetType type,
  262. const char* config);
  263. // Use a makefile variable to set a default for the given property.
  264. // If the variable is not defined use the given default instead.
  265. void SetPropertyDefault(const char* property, const char* default_value);
  266. private:
  267. std::string Name;
  268. std::vector<cmCustomCommand> PreBuildCommands;
  269. std::vector<cmCustomCommand> PreLinkCommands;
  270. std::vector<cmCustomCommand> PostBuildCommands;
  271. std::vector<std::string> SourceLists;
  272. TargetType TargetTypeValue;
  273. std::vector<cmSourceFile*> SourceFiles;
  274. LinkLibraryVectorType LinkLibraries;
  275. LinkLibraryVectorType PrevLinkedLibraries;
  276. bool LinkLibrariesAnalyzed;
  277. bool LinkDirectoriesComputed;
  278. std::vector<std::string> Frameworks;
  279. std::vector<std::string> LinkDirectories;
  280. std::vector<std::string> ExplicitLinkDirectories;
  281. bool HaveInstallRule;
  282. std::string InstallPath;
  283. std::string RuntimeInstallPath;
  284. std::string Directory;
  285. std::string Location;
  286. std::set<cmStdString> Utilities;
  287. bool RecordDependencies;
  288. std::map<cmStdString,cmStdString> Properties;
  289. LinkLibraryVectorType OriginalLinkLibraries;
  290. // The cmMakefile instance that owns this target. This should
  291. // always be set.
  292. cmMakefile* Makefile;
  293. };
  294. typedef std::map<cmStdString,cmTarget> cmTargets;
  295. class cmTargetSet: public std::set<cmStdString> {};
  296. class cmTargetManifest: public std::map<cmStdString, cmTargetSet> {};
  297. #endif