cmTarget.h 14 KB

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