cmTarget.h 20 KB

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