cmTarget.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 cmComputeLinkInformation;
  22. /** \class cmTarget
  23. * \brief Represent a library or executable target loaded from a makefile.
  24. *
  25. * cmTarget represents a target loaded from
  26. * a makefile.
  27. */
  28. class cmTarget
  29. {
  30. public:
  31. cmTarget();
  32. ~cmTarget();
  33. enum TargetType { EXECUTABLE, STATIC_LIBRARY,
  34. SHARED_LIBRARY, MODULE_LIBRARY, UTILITY, GLOBAL_TARGET,
  35. INSTALL_FILES, INSTALL_PROGRAMS, INSTALL_DIRECTORY};
  36. static const char* TargetTypeNames[];
  37. enum CustomCommandType { PRE_BUILD, PRE_LINK, POST_BUILD };
  38. /**
  39. * Return the type of target.
  40. */
  41. TargetType GetType() const
  42. {
  43. return this->TargetTypeValue;
  44. }
  45. /**
  46. * Set the target type
  47. */
  48. void SetType(TargetType f, const char* name);
  49. void MarkAsImported();
  50. ///! Set/Get the name of the target
  51. const char* GetName() const {return this->Name.c_str();}
  52. ///! Set the cmMakefile that owns this target
  53. void SetMakefile(cmMakefile *mf);
  54. cmMakefile *GetMakefile() const { return this->Makefile;};
  55. /**
  56. * Get the list of the custom commands for this target
  57. */
  58. std::vector<cmCustomCommand> &GetPreBuildCommands()
  59. {return this->PreBuildCommands;}
  60. std::vector<cmCustomCommand> &GetPreLinkCommands()
  61. {return this->PreLinkCommands;}
  62. std::vector<cmCustomCommand> &GetPostBuildCommands()
  63. {return this->PostBuildCommands;}
  64. ///! Return the list of frameworks being linked to this target
  65. std::vector<std::string> &GetFrameworks() {return this->Frameworks;}
  66. /**
  67. * Get the list of the source files used by this target
  68. */
  69. std::vector<cmSourceFile*> const &GetSourceFiles()
  70. {return this->SourceFiles;}
  71. void AddSourceFile(cmSourceFile* sf) { this->SourceFiles.push_back(sf); }
  72. /**
  73. * Flags for a given source file as used in this target. Typically assigned
  74. * via SET_TARGET_PROPERTIES when the property is a list of source files.
  75. */
  76. struct SourceFileFlags
  77. {
  78. bool PrivateHeader; // source is in "PRIVATE_HEADER" target property
  79. bool PublicHeader; // source is in "PUBLIC_HEADER" target property
  80. bool Resource; // source is in "RESOURCE" target property *or*
  81. // source has MACOSX_PACKAGE_LOCATION=="Resources"
  82. };
  83. /**
  84. * Get the flags for a given source file as used in this target
  85. */
  86. struct SourceFileFlags GetTargetSourceFileFlags(const cmSourceFile* sf);
  87. /**
  88. * Add sources to the target.
  89. */
  90. void AddSources(std::vector<std::string> const& srcs);
  91. cmSourceFile* AddSource(const char* src);
  92. /**
  93. * Get the list of the source files used by this target
  94. */
  95. enum LinkLibraryType {GENERAL, DEBUG, OPTIMIZED};
  96. //* how we identify a library, by name and type
  97. typedef std::pair<cmStdString, LinkLibraryType> LibraryID;
  98. typedef std::vector<LibraryID > LinkLibraryVectorType;
  99. const LinkLibraryVectorType &GetLinkLibraries() const {
  100. return this->LinkLibraries;}
  101. const LinkLibraryVectorType &GetOriginalLinkLibraries() const
  102. {return this->OriginalLinkLibraries;}
  103. /**
  104. * Clear the dependency information recorded for this target, if any.
  105. */
  106. void ClearDependencyInformation(cmMakefile& mf, const char* target);
  107. // Check to see if a library is a framework and treat it different on Mac
  108. bool NameResolvesToFramework(const std::string& libname);
  109. bool AddFramework(const std::string& lib, LinkLibraryType llt);
  110. void AddLinkLibrary(cmMakefile& mf,
  111. const char *target, const char* lib,
  112. LinkLibraryType llt);
  113. void AddLinkLibrary(const std::string& lib,
  114. LinkLibraryType llt);
  115. void MergeLinkLibraries( cmMakefile& mf, const char* selfname,
  116. const LinkLibraryVectorType& libs );
  117. const std::vector<std::string>& GetLinkDirectories();
  118. void AddLinkDirectory(const char* d);
  119. /**
  120. * Set the path where this target should be installed. This is relative to
  121. * INSTALL_PREFIX
  122. */
  123. std::string GetInstallPath() {return this->InstallPath;}
  124. void SetInstallPath(const char *name) {this->InstallPath = name;}
  125. /**
  126. * Set the path where this target (if it has a runtime part) should be
  127. * installed. This is relative to INSTALL_PREFIX
  128. */
  129. std::string GetRuntimeInstallPath() {return this->RuntimeInstallPath;}
  130. void SetRuntimeInstallPath(const char *name) {
  131. this->RuntimeInstallPath = name; }
  132. /**
  133. * Get/Set whether there is an install rule for this target.
  134. */
  135. bool GetHaveInstallRule() { return this->HaveInstallRule; }
  136. void SetHaveInstallRule(bool h) { this->HaveInstallRule = h; }
  137. /**
  138. * Get/Set the path needed for calls to install_name_tool regarding this
  139. * target. Used to support fixing up installed libraries and executables on
  140. * the Mac (including bundles and frameworks). Only used if the target does
  141. * not have an INSTALL_NAME_DIR property.
  142. * See cmInstallTargetGenerator::AddInstallNamePatchRule and callers for
  143. * more information.
  144. */
  145. std::string GetInstallNameFixupPath() { return this->InstallNameFixupPath; }
  146. void SetInstallNameFixupPath(const char *path) {
  147. this->InstallNameFixupPath = path; }
  148. /** Add a utility on which this project depends. A utility is an executable
  149. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  150. * commands. It is not a full path nor does it have an extension.
  151. */
  152. void AddUtility(const char* u) { this->Utilities.insert(u);}
  153. ///! Get the utilities used by this target
  154. std::set<cmStdString>const& GetUtilities() const { return this->Utilities; }
  155. void AnalyzeLibDependencies( const cmMakefile& mf );
  156. ///! Set/Get a property of this target file
  157. void SetProperty(const char *prop, const char *value);
  158. void AppendProperty(const char* prop, const char* value);
  159. const char *GetProperty(const char *prop);
  160. const char *GetProperty(const char *prop, cmProperty::ScopeType scope);
  161. bool GetPropertyAsBool(const char *prop);
  162. bool IsImported() const {return this->IsImportedTarget;}
  163. /** Get link libraries for the given configuration of an imported
  164. target. */
  165. std::vector<std::string> const*
  166. GetImportedLinkLibraries(const char* config);
  167. /** Get the directory in which this target will be built. If the
  168. configuration name is given then the generator will add its
  169. subdirectory for that configuration. Otherwise just the canonical
  170. output directory is given. */
  171. const char* GetDirectory(const char* config = 0, bool implib = false);
  172. /** Get the location of the target in the build tree for the given
  173. configuration. This location is suitable for use as the LOCATION
  174. target property. */
  175. const char* GetLocation(const char* config);
  176. /** Get the target major and minor version numbers interpreted from
  177. the VERSION property. Version 0 is returned if the property is
  178. not set or cannot be parsed. */
  179. void GetTargetVersion(int& major, int& minor);
  180. /**
  181. * Trace through the source files in this target and add al source files
  182. * that they depend on, used by all generators
  183. */
  184. void TraceDependencies(const char* vsProjectFile);
  185. ///! Return the prefered linker language for this target
  186. const char* GetLinkerLanguage(cmGlobalGenerator*);
  187. ///! Return the rule variable used to create this type of target,
  188. // need to add CMAKE_(LANG) for full name.
  189. const char* GetCreateRuleVariable();
  190. /** Get the full name of the target according to the settings in its
  191. makefile. */
  192. std::string GetFullName(const char* config=0, bool implib = false);
  193. void GetFullNameComponents(std::string& prefix,
  194. std::string& base, std::string& suffix,
  195. const char* config=0, bool implib = false);
  196. /** Get the name of the pdb file for the target. */
  197. std::string GetPDBName(const char* config=0);
  198. /** Get the soname of the target. Allowed only for a shared library. */
  199. std::string GetSOName(const char* config);
  200. /** Get the full path to the target according to the settings in its
  201. makefile and the configuration type. */
  202. std::string GetFullPath(const char* config=0, bool implib = false);
  203. /** Get the names of the library needed to generate a build rule
  204. that takes into account shared library version numbers. This
  205. should be called only on a library target. */
  206. void GetLibraryNames(std::string& name, std::string& soName,
  207. std::string& realName, std::string& impName,
  208. std::string& pdbName, const char* config);
  209. /** Get the names of the library used to remove existing copies of
  210. the library from the build tree either before linking or during
  211. a clean step. This should be called only on a library
  212. target. */
  213. void GetLibraryCleanNames(std::string& staticName,
  214. std::string& sharedName,
  215. std::string& sharedSOName,
  216. std::string& sharedRealName,
  217. std::string& importName,
  218. std::string& pdbName,
  219. const char* config);
  220. /** Get the names of the executable needed to generate a build rule
  221. that takes into account executable version numbers. This should
  222. be called only on an executable target. */
  223. void GetExecutableNames(std::string& name, std::string& realName,
  224. std::string& impName,
  225. std::string& pdbName, const char* config);
  226. /** Get the names of the executable used to remove existing copies
  227. of the executable from the build tree either before linking or
  228. during a clean step. This should be called only on an
  229. executable target. */
  230. void GetExecutableCleanNames(std::string& name, std::string& realName,
  231. std::string& impName,
  232. std::string& pdbName, const char* config);
  233. /** Add the target output files to the global generator manifest. */
  234. void GenerateTargetManifest(const char* config);
  235. /**
  236. * Compute whether this target must be relinked before installing.
  237. */
  238. bool NeedRelinkBeforeInstall();
  239. bool HaveBuildTreeRPATH();
  240. bool HaveInstallTreeRPATH();
  241. /** Return true if chrpath might work for this target */
  242. bool IsChrpathUsed();
  243. std::string GetInstallNameDirForBuildTree(const char* config);
  244. std::string GetInstallNameDirForInstallTree(const char* config);
  245. cmComputeLinkInformation* GetLinkInformation(const char* config);
  246. // Get the properties
  247. cmPropertyMap &GetProperties() { return this->Properties; };
  248. // Define the properties
  249. static void DefineProperties(cmake *cm);
  250. // Compute the OBJECT_FILES property only when requested
  251. void ComputeObjectFiles();
  252. /** Get the macro to define when building sources in this target.
  253. If no macro should be defined null is returned. */
  254. const char* GetExportMacro();
  255. // Compute the set of languages compiled by the target. This is
  256. // computed every time it is called because the languages can change
  257. // when source file properties are changed and we do not have enough
  258. // information to forward these property changes to the targets
  259. // until we have per-target object file properties.
  260. void GetLanguages(std::set<cmStdString>& languages) const;
  261. /** Return whether this target is an executable with symbol exports
  262. enabled. */
  263. bool IsExecutableWithExports();
  264. /** Return whether this target is a shared library Framework on
  265. Apple. */
  266. bool IsFrameworkOnApple();
  267. /** Return whether this target is an executable Bundle on Apple. */
  268. bool IsAppBundleOnApple();
  269. private:
  270. /**
  271. * A list of direct dependencies. Use in conjunction with DependencyMap.
  272. */
  273. typedef std::vector< LibraryID > DependencyList;
  274. /**
  275. * This map holds the dependency graph. map[x] returns a set of
  276. * direct dependencies of x. Note that the direct depenencies are
  277. * ordered. This is necessary to handle direct dependencies that
  278. * themselves have no dependency information.
  279. */
  280. typedef std::map< LibraryID, DependencyList > DependencyMap;
  281. /**
  282. * Inserts \a dep at the end of the dependency list of \a lib.
  283. */
  284. void InsertDependency( DependencyMap& depMap,
  285. const LibraryID& lib,
  286. const LibraryID& dep);
  287. /*
  288. * Deletes \a dep from the dependency list of \a lib.
  289. */
  290. void DeleteDependency( DependencyMap& depMap,
  291. const LibraryID& lib,
  292. const LibraryID& dep);
  293. /**
  294. * Emits the library \a lib and all its dependencies into link_line.
  295. * \a emitted keeps track of the libraries that have been emitted to
  296. * avoid duplicates--it is more efficient than searching
  297. * link_line. \a visited is used detect cycles. Note that \a
  298. * link_line is in reverse order, in that the dependencies of a
  299. * library are listed before the library itself.
  300. */
  301. void Emit( const LibraryID lib,
  302. const DependencyMap& dep_map,
  303. std::set<LibraryID>& emitted,
  304. std::set<LibraryID>& visited,
  305. DependencyList& link_line);
  306. /**
  307. * Finds the dependencies for \a lib and inserts them into \a
  308. * dep_map.
  309. */
  310. void GatherDependencies( const cmMakefile& mf,
  311. const LibraryID& lib,
  312. DependencyMap& dep_map);
  313. const char* GetSuffixVariableInternal(TargetType type, bool implib);
  314. const char* GetPrefixVariableInternal(TargetType type, bool implib);
  315. std::string GetFullNameInternal(TargetType type, const char* config,
  316. bool implib);
  317. void GetFullNameInternal(TargetType type, const char* config, bool implib,
  318. std::string& outPrefix, std::string& outBase,
  319. std::string& outSuffix);
  320. void GetLibraryNamesInternal(std::string& name,
  321. std::string& soName,
  322. std::string& realName,
  323. std::string& impName,
  324. std::string& pdbName,
  325. TargetType type,
  326. const char* config);
  327. void GetExecutableNamesInternal(std::string& name,
  328. std::string& realName,
  329. std::string& impName,
  330. std::string& pdbName,
  331. TargetType type,
  332. const char* config);
  333. // Use a makefile variable to set a default for the given property.
  334. // If the variable is not defined use the given default instead.
  335. void SetPropertyDefault(const char* property, const char* default_value);
  336. // Get the full path to the target output directory.
  337. const char* GetAndCreateOutputDir(bool implib, bool create);
  338. // Get the full path to the target output directory.
  339. const char* GetOutputDir(bool implib);
  340. const char* ImportedGetLocation(const char* config);
  341. const char* NormalGetLocation(const char* config);
  342. std::string GetFullNameImported(const char* config, bool implib);
  343. const char* ImportedGetDirectory(const char* config, bool implib);
  344. const char* NormalGetDirectory(const char* config, bool implib);
  345. std::string ImportedGetFullPath(const char* config, bool implib);
  346. std::string NormalGetFullPath(const char* config, bool implib);
  347. private:
  348. std::string Name;
  349. std::vector<cmCustomCommand> PreBuildCommands;
  350. std::vector<cmCustomCommand> PreLinkCommands;
  351. std::vector<cmCustomCommand> PostBuildCommands;
  352. TargetType TargetTypeValue;
  353. std::vector<cmSourceFile*> SourceFiles;
  354. LinkLibraryVectorType LinkLibraries;
  355. LinkLibraryVectorType PrevLinkedLibraries;
  356. bool LinkLibrariesAnalyzed;
  357. std::vector<std::string> Frameworks;
  358. std::vector<std::string> LinkDirectories;
  359. std::set<cmStdString> LinkDirectoriesEmmitted;
  360. bool HaveInstallRule;
  361. std::string InstallNameFixupPath;
  362. std::string InstallPath;
  363. std::string RuntimeInstallPath;
  364. std::string OutputDir;
  365. std::string OutputDirImplib;
  366. std::string Directory;
  367. std::string Location;
  368. std::string ExportMacro;
  369. std::set<cmStdString> Utilities;
  370. bool RecordDependencies;
  371. cmPropertyMap Properties;
  372. LinkLibraryVectorType OriginalLinkLibraries;
  373. bool DLLPlatform;
  374. bool IsImportedTarget;
  375. // Cache import information from properties for each configuration.
  376. struct ImportInfo
  377. {
  378. std::string Location;
  379. std::string SOName;
  380. std::string ImportLibrary;
  381. std::vector<std::string> LinkLibraries;
  382. };
  383. typedef std::map<cmStdString, ImportInfo> ImportInfoMapType;
  384. ImportInfoMapType ImportInfoMap;
  385. ImportInfo const* GetImportInfo(const char* config);
  386. void ComputeImportInfo(std::string const& desired_config, ImportInfo& info);
  387. std::map<cmStdString, cmComputeLinkInformation*> LinkInformation;
  388. // The cmMakefile instance that owns this target. This should
  389. // always be set.
  390. cmMakefile* Makefile;
  391. };
  392. typedef std::map<cmStdString,cmTarget> cmTargets;
  393. class cmTargetSet: public std::set<cmStdString> {};
  394. class cmTargetManifest: public std::map<cmStdString, cmTargetSet> {};
  395. #endif