cmTarget.h 19 KB

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