cmTarget.h 14 KB

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