cmTarget.h 20 KB

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