cmTarget.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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> const& GetSourceLists() {return this->SourceLists;}
  66. void AddSourceListEntry(const char* src)
  67. { this->SourceLists.push_back(src); }
  68. void SetSourceList(std::vector<std::string> const& srcs)
  69. { this->SourceLists = srcs; }
  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*> const &GetSourceFiles()
  76. {return this->SourceFiles;}
  77. void AddSourceFile(cmSourceFile* sf) { this->SourceFiles.push_back(sf); }
  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. bool IsImported() const {return this->IsImportedTarget;}
  141. /** Get the directory in which this target will be built. If the
  142. configuration name is given then the generator will add its
  143. subdirectory for that configuration. Otherwise just the canonical
  144. output directory is given. */
  145. const char* GetDirectory(const char* config = 0, bool implib = false);
  146. /** Get the location of the target in the build tree for the given
  147. configuration. This location is suitable for use as the LOCATION
  148. target property. */
  149. const char* GetLocation(const char* config);
  150. /** Get the target major and minor version numbers interpreted from
  151. the VERSION property. Version 0 is returned if the property is
  152. not set or cannot be parsed. */
  153. void GetTargetVersion(int& major, int& minor);
  154. /**
  155. * Trace through the source files in this target and add al source files
  156. * that they depend on, used by all generators
  157. */
  158. void TraceVSDependencies(std::string projName, cmMakefile *mf);
  159. ///! Return the prefered linker language for this target
  160. const char* GetLinkerLanguage(cmGlobalGenerator*);
  161. ///! Return the rule variable used to create this type of target,
  162. // need to add CMAKE_(LANG) for full name.
  163. const char* GetCreateRuleVariable();
  164. /** Get the full name of the target according to the settings in its
  165. makefile. */
  166. std::string GetFullName(const char* config=0, bool implib = false);
  167. void GetFullName(std::string& prefix,
  168. std::string& base, std::string& suffix,
  169. const char* config=0, bool implib = false);
  170. /** Get the name of the pdb file for the target. */
  171. std::string GetPDBName(const char* config=0);
  172. /** Get the full path to the target according to the settings in its
  173. makefile and the configuration type. */
  174. std::string GetFullPath(const char* config=0, bool implib = false);
  175. /** Get the names of the library needed to generate a build rule
  176. that takes into account shared library version numbers. This
  177. should be called only on a library target. */
  178. void GetLibraryNames(std::string& name, std::string& soName,
  179. std::string& realName, std::string& impName,
  180. std::string& pdbName, const char* config);
  181. /** Get the names of the library used to remove existing copies of
  182. the library from the build tree either before linking or during
  183. a clean step. This should be called only on a library
  184. target. */
  185. void GetLibraryCleanNames(std::string& staticName,
  186. std::string& sharedName,
  187. std::string& sharedSOName,
  188. std::string& sharedRealName,
  189. std::string& importName,
  190. std::string& pdbName,
  191. const char* config);
  192. /** Get the names of the executable needed to generate a build rule
  193. that takes into account executable version numbers. This should
  194. be called only on an executable target. */
  195. void GetExecutableNames(std::string& name, std::string& realName,
  196. std::string& impName,
  197. std::string& pdbName, const char* config);
  198. /** Get the names of the executable used to remove existing copies
  199. of the executable from the build tree either before linking or
  200. during a clean step. This should be called only on an
  201. executable target. */
  202. void GetExecutableCleanNames(std::string& name, std::string& realName,
  203. std::string& impName,
  204. std::string& pdbName, const char* config);
  205. /**
  206. * Compute whether this target must be relinked before installing.
  207. */
  208. bool NeedRelinkBeforeInstall();
  209. bool HaveBuildTreeRPATH();
  210. bool HaveInstallTreeRPATH();
  211. std::string GetInstallNameDirForBuildTree(const char* config);
  212. std::string GetInstallNameDirForInstallTree(const char* config);
  213. // Get the properties
  214. cmPropertyMap &GetProperties() { return this->Properties; };
  215. // Define the properties
  216. static void DefineProperties(cmake *cm);
  217. // Compute the OBJECT_FILES property only when requested
  218. void ComputeObjectFiles();
  219. /** Get the macro to define when building sources in this target.
  220. If no macro should be defined null is returned. */
  221. const char* GetExportMacro();
  222. private:
  223. /**
  224. * A list of direct dependencies. Use in conjunction with DependencyMap.
  225. */
  226. typedef std::vector< LibraryID > DependencyList;
  227. /**
  228. * This map holds the dependency graph. map[x] returns a set of
  229. * direct dependencies of x. Note that the direct depenencies are
  230. * ordered. This is necessary to handle direct dependencies that
  231. * themselves have no dependency information.
  232. */
  233. typedef std::map< LibraryID, DependencyList > DependencyMap;
  234. /**
  235. * Inserts \a dep at the end of the dependency list of \a lib.
  236. */
  237. void InsertDependency( DependencyMap& depMap,
  238. const LibraryID& lib,
  239. const LibraryID& dep);
  240. /*
  241. * Deletes \a dep from the dependency list of \a lib.
  242. */
  243. void DeleteDependency( DependencyMap& depMap,
  244. const LibraryID& lib,
  245. const LibraryID& dep);
  246. /*
  247. * Check custom commands for known targets and add a target-level
  248. * dependency.
  249. */
  250. void CheckForTargetsAsCommand(const cmCustomCommand& cc);
  251. void CheckForTargetsAsCommand(const std::vector<cmCustomCommand>& commands);
  252. /**
  253. * Emits the library \a lib and all its dependencies into link_line.
  254. * \a emitted keeps track of the libraries that have been emitted to
  255. * avoid duplicates--it is more efficient than searching
  256. * link_line. \a visited is used detect cycles. Note that \a
  257. * link_line is in reverse order, in that the dependencies of a
  258. * library are listed before the library itself.
  259. */
  260. void Emit( const LibraryID lib,
  261. const DependencyMap& dep_map,
  262. std::set<LibraryID>& emitted,
  263. std::set<LibraryID>& visited,
  264. DependencyList& link_line);
  265. /**
  266. * Finds the dependencies for \a lib and inserts them into \a
  267. * dep_map.
  268. */
  269. void GatherDependencies( const cmMakefile& mf,
  270. const LibraryID& lib,
  271. DependencyMap& dep_map);
  272. const char* GetSuffixVariableInternal(TargetType type, bool implib);
  273. const char* GetPrefixVariableInternal(TargetType type, bool implib);
  274. std::string GetFullNameInternal(TargetType type, const char* config,
  275. bool implib);
  276. void GetFullNameInternal(TargetType type, const char* config, bool implib,
  277. std::string& outPrefix, std::string& outBase,
  278. std::string& outSuffix);
  279. void GetLibraryNamesInternal(std::string& name,
  280. std::string& soName,
  281. std::string& realName,
  282. std::string& impName,
  283. std::string& pdbName,
  284. TargetType type,
  285. const char* config);
  286. void GetExecutableNamesInternal(std::string& name,
  287. std::string& realName,
  288. std::string& impName,
  289. std::string& pdbName,
  290. TargetType type,
  291. const char* config);
  292. // Use a makefile variable to set a default for the given property.
  293. // If the variable is not defined use the given default instead.
  294. void SetPropertyDefault(const char* property, const char* default_value);
  295. // Get the full path to the target output directory.
  296. const char* GetOutputDir(bool implib);
  297. const char* ImportedGetLocation(const char* config);
  298. const char* NormalGetLocation(const char* config);
  299. void NormalGetFullNameInternal(TargetType type, const char* config, bool implib,
  300. std::string& outPrefix, std::string& outBase,
  301. std::string& outSuffix);
  302. void ImportedGetFullNameInternal(TargetType type, const char* config, bool implib,
  303. std::string& outPrefix, std::string& outBase,
  304. std::string& outSuffix);
  305. const char* ImportedGetDirectory(const char* config, bool implib);
  306. const char* NormalGetDirectory(const char* config, bool implib);
  307. private:
  308. std::string Name;
  309. std::vector<cmCustomCommand> PreBuildCommands;
  310. std::vector<cmCustomCommand> PreLinkCommands;
  311. std::vector<cmCustomCommand> PostBuildCommands;
  312. std::vector<std::string> SourceLists;
  313. TargetType TargetTypeValue;
  314. std::vector<cmSourceFile*> SourceFiles;
  315. LinkLibraryVectorType LinkLibraries;
  316. LinkLibraryVectorType PrevLinkedLibraries;
  317. bool LinkLibrariesAnalyzed;
  318. bool LinkDirectoriesComputed;
  319. std::vector<std::string> Frameworks;
  320. std::vector<std::string> LinkDirectories;
  321. std::vector<std::string> ExplicitLinkDirectories;
  322. bool HaveInstallRule;
  323. std::string InstallPath;
  324. std::string RuntimeInstallPath;
  325. std::string OutputDir;
  326. std::string OutputDirImplib;
  327. std::string Directory;
  328. std::string Location;
  329. std::string ExportMacro;
  330. std::set<cmStdString> Utilities;
  331. bool RecordDependencies;
  332. cmPropertyMap Properties;
  333. LinkLibraryVectorType OriginalLinkLibraries;
  334. bool DLLPlatform;
  335. bool IsImportedTarget;
  336. // The cmMakefile instance that owns this target. This should
  337. // always be set.
  338. cmMakefile* Makefile;
  339. };
  340. typedef std::map<cmStdString,cmTarget> cmTargets;
  341. class cmTargetSet: public std::set<cmStdString> {};
  342. class cmTargetManifest: public std::map<cmStdString, cmTargetSet> {};
  343. #endif