cmTarget.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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() const { 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. ///! Return the list of frameworks being linked to this target
  63. std::vector<std::string> &GetFrameworks() {return this->Frameworks;}
  64. /**
  65. * Get the list of the source files used by this target
  66. */
  67. std::vector<cmSourceFile*> const &GetSourceFiles()
  68. {return this->SourceFiles;}
  69. void AddSourceFile(cmSourceFile* sf) { this->SourceFiles.push_back(sf); }
  70. /**
  71. * Add sources to the target.
  72. */
  73. void AddSources(std::vector<std::string> const& srcs);
  74. cmSourceFile* AddSource(const char* src);
  75. /**
  76. * Get the list of the source files used by this target
  77. */
  78. enum LinkLibraryType {GENERAL, DEBUG, OPTIMIZED};
  79. //* how we identify a library, by name and type
  80. typedef std::pair<cmStdString, LinkLibraryType> LibraryID;
  81. typedef std::vector<LibraryID > LinkLibraryVectorType;
  82. const LinkLibraryVectorType &GetLinkLibraries() const {
  83. return this->LinkLibraries;}
  84. const LinkLibraryVectorType &GetOriginalLinkLibraries() const
  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. * Get/Set the path needed for calls to install_name_tool regarding this
  121. * target. Used to support fixing up installed libraries and executables on
  122. * the Mac (including bundles and frameworks). Only used if the target does
  123. * not have an INSTALL_NAME_DIR property.
  124. * See cmInstallTargetGenerator::AddInstallNamePatchRule and callers for
  125. * more information.
  126. */
  127. std::string GetInstallNameFixupPath() { return this->InstallNameFixupPath; }
  128. void SetInstallNameFixupPath(const char *path) {
  129. this->InstallNameFixupPath = path; }
  130. /** Add a utility on which this project depends. A utility is an executable
  131. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  132. * commands. It is not a full path nor does it have an extension.
  133. */
  134. void AddUtility(const char* u) { this->Utilities.insert(u);}
  135. ///! Get the utilities used by this target
  136. std::set<cmStdString>const& GetUtilities() { return this->Utilities; }
  137. void AnalyzeLibDependencies( const cmMakefile& mf );
  138. ///! Set/Get a property of this target file
  139. void SetProperty(const char *prop, const char *value);
  140. const char *GetProperty(const char *prop);
  141. const char *GetProperty(const char *prop, cmProperty::ScopeType scope);
  142. bool GetPropertyAsBool(const char *prop);
  143. bool IsImported() const {return this->IsImportedTarget;}
  144. /** Get the directory in which this target will be built. If the
  145. configuration name is given then the generator will add its
  146. subdirectory for that configuration. Otherwise just the canonical
  147. output directory is given. */
  148. const char* GetDirectory(const char* config = 0, bool implib = false);
  149. /** Get the location of the target in the build tree for the given
  150. configuration. This location is suitable for use as the LOCATION
  151. target property. */
  152. const char* GetLocation(const char* config);
  153. /** Get the target major and minor version numbers interpreted from
  154. the VERSION property. Version 0 is returned if the property is
  155. not set or cannot be parsed. */
  156. void GetTargetVersion(int& major, int& minor);
  157. /**
  158. * Trace through the source files in this target and add al source files
  159. * that they depend on, used by all generators
  160. */
  161. void TraceDependencies(const char* vsProjectFile);
  162. ///! Return the prefered linker language for this target
  163. const char* GetLinkerLanguage(cmGlobalGenerator*);
  164. ///! Return the rule variable used to create this type of target,
  165. // need to add CMAKE_(LANG) for full name.
  166. const char* GetCreateRuleVariable();
  167. /** Get the full name of the target according to the settings in its
  168. makefile. */
  169. std::string GetFullName(const char* config=0, bool implib = false);
  170. void GetFullName(std::string& prefix,
  171. std::string& base, std::string& suffix,
  172. const char* config=0, bool implib = false);
  173. /** Get the name of the pdb file for the target. */
  174. std::string GetPDBName(const char* config=0);
  175. /** Get the full path to the target according to the settings in its
  176. makefile and the configuration type. */
  177. std::string GetFullPath(const char* config=0, bool implib = false);
  178. /** Get the names of the library needed to generate a build rule
  179. that takes into account shared library version numbers. This
  180. should be called only on a library target. */
  181. void GetLibraryNames(std::string& name, std::string& soName,
  182. std::string& realName, std::string& impName,
  183. std::string& pdbName, const char* config);
  184. /** Get the names of the library used to remove existing copies of
  185. the library from the build tree either before linking or during
  186. a clean step. This should be called only on a library
  187. target. */
  188. void GetLibraryCleanNames(std::string& staticName,
  189. std::string& sharedName,
  190. std::string& sharedSOName,
  191. std::string& sharedRealName,
  192. std::string& importName,
  193. std::string& pdbName,
  194. const char* config);
  195. /** Get the names of the executable needed to generate a build rule
  196. that takes into account executable version numbers. This should
  197. be called only on an executable target. */
  198. void GetExecutableNames(std::string& name, std::string& realName,
  199. std::string& impName,
  200. std::string& pdbName, const char* config);
  201. /** Get the names of the executable used to remove existing copies
  202. of the executable from the build tree either before linking or
  203. during a clean step. This should be called only on an
  204. executable target. */
  205. void GetExecutableCleanNames(std::string& name, std::string& realName,
  206. std::string& impName,
  207. std::string& pdbName, const char* config);
  208. /**
  209. * Compute whether this target must be relinked before installing.
  210. */
  211. bool NeedRelinkBeforeInstall();
  212. bool HaveBuildTreeRPATH();
  213. bool HaveInstallTreeRPATH();
  214. std::string GetInstallNameDirForBuildTree(const char* config);
  215. std::string GetInstallNameDirForInstallTree(const char* config);
  216. // Get the properties
  217. cmPropertyMap &GetProperties() { return this->Properties; };
  218. // Define the properties
  219. static void DefineProperties(cmake *cm);
  220. // Compute the OBJECT_FILES property only when requested
  221. void ComputeObjectFiles();
  222. /** Get the macro to define when building sources in this target.
  223. If no macro should be defined null is returned. */
  224. const char* GetExportMacro();
  225. // Compute the set of languages compiled by the target. This is
  226. // computed every time it is called because the languages can change
  227. // when source file properties are changed and we do not have enough
  228. // information to forward these property changes to the targets
  229. // until we have per-target object file properties.
  230. void GetLanguages(std::set<cmStdString>& languages) const;
  231. private:
  232. /**
  233. * A list of direct dependencies. Use in conjunction with DependencyMap.
  234. */
  235. typedef std::vector< LibraryID > DependencyList;
  236. /**
  237. * This map holds the dependency graph. map[x] returns a set of
  238. * direct dependencies of x. Note that the direct depenencies are
  239. * ordered. This is necessary to handle direct dependencies that
  240. * themselves have no dependency information.
  241. */
  242. typedef std::map< LibraryID, DependencyList > DependencyMap;
  243. /**
  244. * Inserts \a dep at the end of the dependency list of \a lib.
  245. */
  246. void InsertDependency( DependencyMap& depMap,
  247. const LibraryID& lib,
  248. const LibraryID& dep);
  249. /*
  250. * Deletes \a dep from the dependency list of \a lib.
  251. */
  252. void DeleteDependency( DependencyMap& depMap,
  253. const LibraryID& lib,
  254. const LibraryID& dep);
  255. /**
  256. * Emits the library \a lib and all its dependencies into link_line.
  257. * \a emitted keeps track of the libraries that have been emitted to
  258. * avoid duplicates--it is more efficient than searching
  259. * link_line. \a visited is used detect cycles. Note that \a
  260. * link_line is in reverse order, in that the dependencies of a
  261. * library are listed before the library itself.
  262. */
  263. void Emit( const LibraryID lib,
  264. const DependencyMap& dep_map,
  265. std::set<LibraryID>& emitted,
  266. std::set<LibraryID>& visited,
  267. DependencyList& link_line);
  268. /**
  269. * Finds the dependencies for \a lib and inserts them into \a
  270. * dep_map.
  271. */
  272. void GatherDependencies( const cmMakefile& mf,
  273. const LibraryID& lib,
  274. DependencyMap& dep_map);
  275. const char* GetSuffixVariableInternal(TargetType type, bool implib);
  276. const char* GetPrefixVariableInternal(TargetType type, bool implib);
  277. std::string GetFullNameInternal(TargetType type, const char* config,
  278. bool implib);
  279. void GetFullNameInternal(TargetType type, const char* config, bool implib,
  280. std::string& outPrefix, std::string& outBase,
  281. std::string& outSuffix);
  282. void GetLibraryNamesInternal(std::string& name,
  283. std::string& soName,
  284. std::string& realName,
  285. std::string& impName,
  286. std::string& pdbName,
  287. TargetType type,
  288. const char* config);
  289. void GetExecutableNamesInternal(std::string& name,
  290. std::string& realName,
  291. std::string& impName,
  292. std::string& pdbName,
  293. TargetType type,
  294. const char* config);
  295. // Use a makefile variable to set a default for the given property.
  296. // If the variable is not defined use the given default instead.
  297. void SetPropertyDefault(const char* property, const char* default_value);
  298. // Get the full path to the target output directory.
  299. const char* GetOutputDir(bool implib);
  300. const char* ImportedGetLocation(const char* config);
  301. const char* NormalGetLocation(const char* config);
  302. void NormalGetFullNameInternal(TargetType type, const char* config,
  303. bool implib,
  304. std::string& outPrefix,
  305. std::string& outBase,
  306. std::string& outSuffix);
  307. void ImportedGetFullNameInternal(TargetType type, const char* config,
  308. bool implib,
  309. std::string& outPrefix,
  310. std::string& outBase,
  311. std::string& outSuffix);
  312. const char* ImportedGetDirectory(const char* config, bool implib);
  313. const char* NormalGetDirectory(const char* config, bool implib);
  314. private:
  315. std::string Name;
  316. std::vector<cmCustomCommand> PreBuildCommands;
  317. std::vector<cmCustomCommand> PreLinkCommands;
  318. std::vector<cmCustomCommand> PostBuildCommands;
  319. TargetType TargetTypeValue;
  320. std::vector<cmSourceFile*> SourceFiles;
  321. LinkLibraryVectorType LinkLibraries;
  322. LinkLibraryVectorType PrevLinkedLibraries;
  323. bool LinkLibrariesAnalyzed;
  324. bool LinkDirectoriesComputed;
  325. std::vector<std::string> Frameworks;
  326. std::vector<std::string> LinkDirectories;
  327. std::vector<std::string> ExplicitLinkDirectories;
  328. bool HaveInstallRule;
  329. std::string InstallNameFixupPath;
  330. std::string InstallPath;
  331. std::string RuntimeInstallPath;
  332. std::string OutputDir;
  333. std::string OutputDirImplib;
  334. std::string Directory;
  335. std::string Location;
  336. std::string ExportMacro;
  337. std::set<cmStdString> Utilities;
  338. bool RecordDependencies;
  339. cmPropertyMap Properties;
  340. LinkLibraryVectorType OriginalLinkLibraries;
  341. bool DLLPlatform;
  342. bool IsImportedTarget;
  343. // The cmMakefile instance that owns this target. This should
  344. // always be set.
  345. cmMakefile* Makefile;
  346. };
  347. typedef std::map<cmStdString,cmTarget> cmTargets;
  348. class cmTargetSet: public std::set<cmStdString> {};
  349. class cmTargetManifest: public std::map<cmStdString, cmTargetSet> {};
  350. #endif