1
0

cmTarget.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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() const
  40. {
  41. return this->TargetTypeValue;
  42. }
  43. /**
  44. * Set the target type
  45. */
  46. void SetType(TargetType f, const char* name);
  47. void MarkAsImported();
  48. ///! Set/Get the name of the target
  49. const char* GetName() const {return this->Name.c_str();}
  50. ///! Set the cmMakefile that owns this target
  51. void SetMakefile(cmMakefile *mf);
  52. cmMakefile *GetMakefile() { return this->Makefile;};
  53. /**
  54. * Get the list of the custom commands for this target
  55. */
  56. std::vector<cmCustomCommand> &GetPreBuildCommands()
  57. {return this->PreBuildCommands;}
  58. std::vector<cmCustomCommand> &GetPreLinkCommands()
  59. {return this->PreLinkCommands;}
  60. std::vector<cmCustomCommand> &GetPostBuildCommands()
  61. {return this->PostBuildCommands;}
  62. /**
  63. * Get the list of the source lists used by this target
  64. */
  65. std::vector<std::string> &GetSourceLists() {return this->SourceLists;}
  66. ///! Return the list of frameworks being linked to this target
  67. std::vector<std::string> &GetFrameworks() {return this->Frameworks;}
  68. /**
  69. * Get the list of the source files used by this target
  70. */
  71. std::vector<cmSourceFile*> &GetSourceFiles() {return this->SourceFiles;}
  72. /**
  73. * Get the list of the source files used by this target
  74. */
  75. enum LinkLibraryType {GENERAL, DEBUG, OPTIMIZED};
  76. //* how we identify a library, by name and type
  77. typedef std::pair<cmStdString, LinkLibraryType> LibraryID;
  78. typedef std::vector<LibraryID > LinkLibraryVectorType;
  79. const LinkLibraryVectorType &GetLinkLibraries() {
  80. return this->LinkLibraries;}
  81. const LinkLibraryVectorType &GetOriginalLinkLibraries()
  82. {return this->OriginalLinkLibraries;}
  83. /**
  84. * Clear the dependency information recorded for this target, if any.
  85. */
  86. void ClearDependencyInformation(cmMakefile& mf, const char* target);
  87. // Check to see if a library is a framework and treat it different on Mac
  88. bool AddFramework(const std::string& lib, LinkLibraryType llt);
  89. void AddLinkLibrary(cmMakefile& mf,
  90. const char *target, const char* lib,
  91. LinkLibraryType llt);
  92. void AddLinkLibrary(const std::string& lib,
  93. LinkLibraryType llt);
  94. void MergeLinkLibraries( cmMakefile& mf, const char* selfname,
  95. const LinkLibraryVectorType& libs );
  96. const std::vector<std::string>& GetLinkDirectories();
  97. void AddLinkDirectory(const char* d);
  98. /**
  99. * Set the path where this target should be installed. This is relative to
  100. * INSTALL_PREFIX
  101. */
  102. std::string GetInstallPath() {return this->InstallPath;}
  103. void SetInstallPath(const char *name) {this->InstallPath = name;}
  104. /**
  105. * Set the path where this target (if it has a runtime part) should be
  106. * installed. This is relative to INSTALL_PREFIX
  107. */
  108. std::string GetRuntimeInstallPath() {return this->RuntimeInstallPath;}
  109. void SetRuntimeInstallPath(const char *name) {
  110. this->RuntimeInstallPath = name;}
  111. /**
  112. * Get/Set whether there is an install rule for this target.
  113. */
  114. bool GetHaveInstallRule() { return this->HaveInstallRule; }
  115. void SetHaveInstallRule(bool h) { this->HaveInstallRule = h; }
  116. /**
  117. * Generate the SourceFilesList from the SourceLists. This should only be
  118. * done once to be safe.
  119. */
  120. void GenerateSourceFilesFromSourceLists(cmMakefile &mf);
  121. /** Add a utility on which this project depends. A utility is an executable
  122. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  123. * commands. It is not a full path nor does it have an extension.
  124. */
  125. void AddUtility(const char* u) { this->Utilities.insert(u);}
  126. ///! Get the utilities used by this target
  127. std::set<cmStdString>const& GetUtilities() { return this->Utilities; }
  128. void AnalyzeLibDependencies( const cmMakefile& mf );
  129. ///! Set/Get a property of this target file
  130. void SetProperty(const char *prop, const char *value);
  131. const char *GetProperty(const char *prop);
  132. const char *GetProperty(const char *prop, cmProperty::ScopeType scope);
  133. bool GetPropertyAsBool(const char *prop);
  134. bool IsImported() const {return this->IsImportedTarget;}
  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, bool implib = false);
  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. /** Get the target major and minor version numbers interpreted from
  145. the VERSION property. Version 0 is returned if the property is
  146. not set or cannot be parsed. */
  147. void GetTargetVersion(int& major, int& minor);
  148. /**
  149. * Trace through the source files in this target and add al source files
  150. * that they depend on, used by all generators
  151. */
  152. void TraceVSDependencies(std::string projName, cmMakefile *mf);
  153. ///! Return the prefered linker language for this target
  154. const char* GetLinkerLanguage(cmGlobalGenerator*);
  155. ///! Return the rule variable used to create this type of target,
  156. // need to add CMAKE_(LANG) for full name.
  157. const char* GetCreateRuleVariable();
  158. /** Get the full name of the target according to the settings in its
  159. makefile. */
  160. std::string GetFullName(const char* config=0, bool implib = false);
  161. void GetFullName(std::string& prefix,
  162. std::string& base, std::string& suffix,
  163. const char* config=0, bool implib = false);
  164. /** Get the name of the pdb file for the target. */
  165. std::string GetPDBName(const char* config=0);
  166. /** Get the full path to the target according to the settings in its
  167. makefile and the configuration type. */
  168. std::string GetFullPath(const char* config=0, bool implib = false);
  169. /** Get the names of the library needed to generate a build rule
  170. that takes into account shared library version numbers. This
  171. should be called only on a library target. */
  172. void GetLibraryNames(std::string& name, std::string& soName,
  173. std::string& realName, std::string& impName,
  174. std::string& pdbName, const char* config);
  175. /** Get the names of the library used to remove existing copies of
  176. the library from the build tree either before linking or during
  177. a clean step. This should be called only on a library
  178. target. */
  179. void GetLibraryCleanNames(std::string& staticName,
  180. std::string& sharedName,
  181. std::string& sharedSOName,
  182. std::string& sharedRealName,
  183. std::string& importName,
  184. std::string& pdbName,
  185. const char* config);
  186. /** Get the names of the executable needed to generate a build rule
  187. that takes into account executable version numbers. This should
  188. be called only on an executable target. */
  189. void GetExecutableNames(std::string& name, std::string& realName,
  190. std::string& impName,
  191. std::string& pdbName, const char* config);
  192. /** Get the names of the executable used to remove existing copies
  193. of the executable from the build tree either before linking or
  194. during a clean step. This should be called only on an
  195. executable target. */
  196. void GetExecutableCleanNames(std::string& name, std::string& realName,
  197. std::string& impName,
  198. std::string& pdbName, const char* config);
  199. /**
  200. * Compute whether this target must be relinked before installing.
  201. */
  202. bool NeedRelinkBeforeInstall();
  203. bool HaveBuildTreeRPATH();
  204. bool HaveInstallTreeRPATH();
  205. std::string GetInstallNameDirForBuildTree(const char* config);
  206. std::string GetInstallNameDirForInstallTree(const char* config);
  207. // Get the properties
  208. cmPropertyMap &GetProperties() { return this->Properties; };
  209. // Define the properties
  210. static void DefineProperties(cmake *cm);
  211. // Compute the OBJECT_FILES property only when requested
  212. void ComputeObjectFiles();
  213. /** Get the macro to define when building sources in this target.
  214. If no macro should be defined null is returned. */
  215. const char* GetExportMacro();
  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. * Checks the prebuild, prelink and postbuild custom commands for known
  242. * targets and adds them to the dependencies.
  243. */
  244. void CheckForTargetsAsCommand(const std::vector<cmCustomCommand>& commands);
  245. /**
  246. * Emits the library \a lib and all its dependencies into link_line.
  247. * \a emitted keeps track of the libraries that have been emitted to
  248. * avoid duplicates--it is more efficient than searching
  249. * link_line. \a visited is used detect cycles. Note that \a
  250. * link_line is in reverse order, in that the dependencies of a
  251. * library are listed before the library itself.
  252. */
  253. void Emit( const LibraryID lib,
  254. const DependencyMap& dep_map,
  255. std::set<LibraryID>& emitted,
  256. std::set<LibraryID>& visited,
  257. DependencyList& link_line);
  258. /**
  259. * Finds the dependencies for \a lib and inserts them into \a
  260. * dep_map.
  261. */
  262. void GatherDependencies( const cmMakefile& mf,
  263. const LibraryID& lib,
  264. DependencyMap& dep_map);
  265. const char* GetSuffixVariableInternal(TargetType type, bool implib);
  266. const char* GetPrefixVariableInternal(TargetType type, bool implib);
  267. std::string GetFullNameInternal(TargetType type, const char* config,
  268. bool implib);
  269. void GetFullNameInternal(TargetType type, const char* config, bool implib,
  270. std::string& outPrefix, std::string& outBase,
  271. std::string& outSuffix);
  272. void GetLibraryNamesInternal(std::string& name,
  273. std::string& soName,
  274. std::string& realName,
  275. std::string& impName,
  276. std::string& pdbName,
  277. TargetType type,
  278. const char* config);
  279. void GetExecutableNamesInternal(std::string& name,
  280. std::string& realName,
  281. std::string& impName,
  282. std::string& pdbName,
  283. TargetType type,
  284. const char* config);
  285. // Use a makefile variable to set a default for the given property.
  286. // If the variable is not defined use the given default instead.
  287. void SetPropertyDefault(const char* property, const char* default_value);
  288. // Get the full path to the target output directory.
  289. const char* GetOutputDir(bool implib);
  290. const char* ImportedGetLocation(const char* config);
  291. const char* NormalGetLocation(const char* config);
  292. void NormalGetFullNameInternal(TargetType type, const char* config, bool implib,
  293. std::string& outPrefix, std::string& outBase,
  294. std::string& outSuffix);
  295. void ImportedGetFullNameInternal(TargetType type, const char* config, bool implib,
  296. std::string& outPrefix, std::string& outBase,
  297. std::string& outSuffix);
  298. const char* ImportedGetDirectory(const char* config, bool implib);
  299. const char* NormalGetDirectory(const char* config, bool implib);
  300. private:
  301. std::string Name;
  302. std::vector<cmCustomCommand> PreBuildCommands;
  303. std::vector<cmCustomCommand> PreLinkCommands;
  304. std::vector<cmCustomCommand> PostBuildCommands;
  305. std::vector<std::string> SourceLists;
  306. TargetType TargetTypeValue;
  307. std::vector<cmSourceFile*> SourceFiles;
  308. LinkLibraryVectorType LinkLibraries;
  309. LinkLibraryVectorType PrevLinkedLibraries;
  310. bool LinkLibrariesAnalyzed;
  311. bool LinkDirectoriesComputed;
  312. std::vector<std::string> Frameworks;
  313. std::vector<std::string> LinkDirectories;
  314. std::vector<std::string> ExplicitLinkDirectories;
  315. bool HaveInstallRule;
  316. std::string InstallPath;
  317. std::string RuntimeInstallPath;
  318. std::string OutputDir;
  319. std::string OutputDirImplib;
  320. std::string Directory;
  321. std::string Location;
  322. std::string ExportMacro;
  323. std::set<cmStdString> Utilities;
  324. bool RecordDependencies;
  325. cmPropertyMap Properties;
  326. LinkLibraryVectorType OriginalLinkLibraries;
  327. bool DLLPlatform;
  328. bool IsImportedTarget;
  329. // The cmMakefile instance that owns this target. This should
  330. // always be set.
  331. cmMakefile* Makefile;
  332. };
  333. typedef std::map<cmStdString,cmTarget> cmTargets;
  334. class cmTargetSet: public std::set<cmStdString> {};
  335. class cmTargetManifest: public std::map<cmStdString, cmTargetSet> {};
  336. #endif