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