cmTarget.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef cmTarget_h
  11. #define cmTarget_h
  12. #include "cmCustomCommand.h"
  13. #include "cmPropertyMap.h"
  14. #include "cmPolicies.h"
  15. #include "cmListFileCache.h"
  16. #include "cmLinkItem.h"
  17. #include <cmsys/auto_ptr.hxx>
  18. #if defined(CMAKE_BUILD_WITH_CMAKE)
  19. # ifdef CMake_HAVE_CXX11_UNORDERED_MAP
  20. # include <unordered_map>
  21. # else
  22. # include <cmsys/hash_map.hxx>
  23. # endif
  24. #endif
  25. #define CM_FOR_EACH_TARGET_POLICY(F) \
  26. F(CMP0003) \
  27. F(CMP0004) \
  28. F(CMP0008) \
  29. F(CMP0020) \
  30. F(CMP0021) \
  31. F(CMP0022) \
  32. F(CMP0027) \
  33. F(CMP0038) \
  34. F(CMP0041) \
  35. F(CMP0042) \
  36. F(CMP0046) \
  37. F(CMP0052) \
  38. F(CMP0060) \
  39. F(CMP0063) \
  40. F(CMP0065)
  41. class cmake;
  42. class cmMakefile;
  43. class cmSourceFile;
  44. class cmGlobalGenerator;
  45. class cmComputeLinkInformation;
  46. class cmListFileBacktrace;
  47. class cmTarget;
  48. class cmGeneratorTarget;
  49. class cmTargetTraceDependencies;
  50. class cmTargetInternals;
  51. class cmTargetInternalPointer
  52. {
  53. public:
  54. cmTargetInternalPointer();
  55. cmTargetInternalPointer(cmTargetInternalPointer const& r);
  56. ~cmTargetInternalPointer();
  57. cmTargetInternalPointer& operator=(cmTargetInternalPointer const& r);
  58. cmTargetInternals* operator->() const { return this->Pointer; }
  59. cmTargetInternals* Get() const { return this->Pointer; }
  60. private:
  61. cmTargetInternals* Pointer;
  62. };
  63. /** \class cmTarget
  64. * \brief Represent a library or executable target loaded from a makefile.
  65. *
  66. * cmTarget represents a target loaded from
  67. * a makefile.
  68. */
  69. class cmTarget
  70. {
  71. public:
  72. cmTarget();
  73. enum TargetType { EXECUTABLE, STATIC_LIBRARY,
  74. SHARED_LIBRARY, MODULE_LIBRARY,
  75. OBJECT_LIBRARY, UTILITY, GLOBAL_TARGET,
  76. INTERFACE_LIBRARY,
  77. UNKNOWN_LIBRARY};
  78. static const char* GetTargetTypeName(TargetType targetType);
  79. enum CustomCommandType { PRE_BUILD, PRE_LINK, POST_BUILD };
  80. /**
  81. * Return the type of target.
  82. */
  83. TargetType GetType() const
  84. {
  85. return this->TargetTypeValue;
  86. }
  87. /**
  88. * Set the target type
  89. */
  90. void SetType(TargetType f, const std::string& name);
  91. void MarkAsImported();
  92. ///! Set/Get the name of the target
  93. const std::string& GetName() const {return this->Name;}
  94. std::string GetExportName() const;
  95. ///! Set the cmMakefile that owns this target
  96. void SetMakefile(cmMakefile *mf);
  97. cmMakefile *GetMakefile() const { return this->Makefile;}
  98. #define DECLARE_TARGET_POLICY(POLICY) \
  99. cmPolicies::PolicyStatus GetPolicyStatus ## POLICY () const \
  100. { return this->PolicyMap.Get(cmPolicies::POLICY); }
  101. CM_FOR_EACH_TARGET_POLICY(DECLARE_TARGET_POLICY)
  102. #undef DECLARE_TARGET_POLICY
  103. /**
  104. * Get the list of the custom commands for this target
  105. */
  106. std::vector<cmCustomCommand> const &GetPreBuildCommands() const
  107. {return this->PreBuildCommands;}
  108. std::vector<cmCustomCommand> const &GetPreLinkCommands() const
  109. {return this->PreLinkCommands;}
  110. std::vector<cmCustomCommand> const &GetPostBuildCommands() const
  111. {return this->PostBuildCommands;}
  112. void AddPreBuildCommand(cmCustomCommand const &cmd)
  113. {this->PreBuildCommands.push_back(cmd);}
  114. void AddPreLinkCommand(cmCustomCommand const &cmd)
  115. {this->PreLinkCommands.push_back(cmd);}
  116. void AddPostBuildCommand(cmCustomCommand const &cmd)
  117. {this->PostBuildCommands.push_back(cmd);}
  118. /**
  119. * Add sources to the target.
  120. */
  121. void AddSources(std::vector<std::string> const& srcs);
  122. void AddTracedSources(std::vector<std::string> const& srcs);
  123. cmSourceFile* AddSourceCMP0049(const std::string& src);
  124. cmSourceFile* AddSource(const std::string& src);
  125. enum LinkLibraryType {GENERAL, DEBUG, OPTIMIZED};
  126. //* how we identify a library, by name and type
  127. typedef std::pair<std::string, LinkLibraryType> LibraryID;
  128. typedef std::vector<LibraryID > LinkLibraryVectorType;
  129. const LinkLibraryVectorType &GetOriginalLinkLibraries() const
  130. {return this->OriginalLinkLibraries;}
  131. /** Compute the link type to use for the given configuration. */
  132. LinkLibraryType ComputeLinkType(const std::string& config) const;
  133. /**
  134. * Clear the dependency information recorded for this target, if any.
  135. */
  136. void ClearDependencyInformation(cmMakefile& mf, const std::string& target);
  137. // Check to see if a library is a framework and treat it different on Mac
  138. bool NameResolvesToFramework(const std::string& libname) const;
  139. void AddLinkLibrary(cmMakefile& mf,
  140. const std::string& target, const std::string& lib,
  141. LinkLibraryType llt);
  142. enum TLLSignature {
  143. KeywordTLLSignature,
  144. PlainTLLSignature
  145. };
  146. bool PushTLLCommandTrace(TLLSignature signature,
  147. cmListFileContext const& lfc);
  148. void GetTllSignatureTraces(std::ostringstream &s, TLLSignature sig) const;
  149. void MergeLinkLibraries( cmMakefile& mf, const std::string& selfname,
  150. const LinkLibraryVectorType& libs );
  151. const std::vector<std::string>& GetLinkDirectories() const;
  152. void AddLinkDirectory(const std::string& d);
  153. /**
  154. * Set the path where this target should be installed. This is relative to
  155. * INSTALL_PREFIX
  156. */
  157. std::string GetInstallPath() const {return this->InstallPath;}
  158. void SetInstallPath(const char *name) {this->InstallPath = name;}
  159. /**
  160. * Set the path where this target (if it has a runtime part) should be
  161. * installed. This is relative to INSTALL_PREFIX
  162. */
  163. std::string GetRuntimeInstallPath() const {return this->RuntimeInstallPath;}
  164. void SetRuntimeInstallPath(const char *name) {
  165. this->RuntimeInstallPath = name; }
  166. /**
  167. * Get/Set whether there is an install rule for this target.
  168. */
  169. bool GetHaveInstallRule() const { return this->HaveInstallRule; }
  170. void SetHaveInstallRule(bool h) { this->HaveInstallRule = h; }
  171. /** Add a utility on which this project depends. A utility is an executable
  172. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  173. * commands. It is not a full path nor does it have an extension.
  174. */
  175. void AddUtility(const std::string& u, cmMakefile *makefile = 0);
  176. ///! Get the utilities used by this target
  177. std::set<std::string>const& GetUtilities() const { return this->Utilities; }
  178. std::set<cmLinkItem>const& GetUtilityItems() const;
  179. cmListFileBacktrace const* GetUtilityBacktrace(const std::string& u) const;
  180. /** Finalize the target at the end of the Configure step. */
  181. void FinishConfigure();
  182. ///! Set/Get a property of this target file
  183. void SetProperty(const std::string& prop, const char *value);
  184. void AppendProperty(const std::string& prop, const char* value,
  185. bool asString=false);
  186. const char *GetProperty(const std::string& prop) const;
  187. const char *GetProperty(const std::string& prop, cmMakefile* context) const;
  188. bool GetPropertyAsBool(const std::string& prop) const;
  189. void CheckProperty(const std::string& prop, cmMakefile* context) const;
  190. bool IsImported() const {return this->IsImportedTarget;}
  191. void GetObjectLibrariesCMP0026(std::vector<cmTarget*>& objlibs) const;
  192. cmTarget const* FindTargetToLink(std::string const& name) const;
  193. /** Strip off leading and trailing whitespace from an item named in
  194. the link dependencies of this target. */
  195. std::string CheckCMP0004(std::string const& item) const;
  196. const char* ImportedGetLocation(const std::string& config) const;
  197. /** Get the target major and minor version numbers interpreted from
  198. the VERSION property. Version 0 is returned if the property is
  199. not set or cannot be parsed. */
  200. void GetTargetVersion(int& major, int& minor) const;
  201. /** Get the target major, minor, and patch version numbers
  202. interpreted from the VERSION or SOVERSION property. Version 0
  203. is returned if the property is not set or cannot be parsed. */
  204. void
  205. GetTargetVersion(bool soversion, int& major, int& minor, int& patch) const;
  206. /** Test for special case of a third-party shared library that has
  207. no soname at all. */
  208. bool IsImportedSharedLibWithoutSOName(const std::string& config) const;
  209. /** Does this target have a GNU implib to convert to MS format? */
  210. bool HasImplibGNUtoMS() const;
  211. /** Convert the given GNU import library name (.dll.a) to a name with a new
  212. extension (.lib or ${CMAKE_IMPORT_LIBRARY_SUFFIX}). */
  213. bool GetImplibGNUtoMS(std::string const& gnuName, std::string& out,
  214. const char* newExt = 0) const;
  215. // Get the properties
  216. cmPropertyMap &GetProperties() const { return this->Properties; }
  217. bool GetMappedConfig(std::string const& desired_config,
  218. const char** loc,
  219. const char** imp,
  220. std::string& suffix) const;
  221. /** Get the macro to define when building sources in this target.
  222. If no macro should be defined null is returned. */
  223. const char* GetExportMacro() const;
  224. /** Return whether this target is an executable with symbol exports
  225. enabled. */
  226. bool IsExecutableWithExports() const;
  227. /** Return whether this target may be used to link another target. */
  228. bool IsLinkable() const;
  229. /** Return whether or not the target is for a DLL platform. */
  230. bool IsDLLPlatform() const { return this->DLLPlatform; }
  231. /** Return whether or not the target has a DLL import library. */
  232. bool HasImportLibrary() const;
  233. /** Return whether this target is a shared library Framework on
  234. Apple. */
  235. bool IsFrameworkOnApple() const;
  236. /** Return whether this target is a CFBundle (plugin) on Apple. */
  237. bool IsCFBundleOnApple() const;
  238. /** Return whether this target is a XCTest on Apple. */
  239. bool IsXCTestOnApple() const;
  240. /** Return whether this target is an executable Bundle on Apple. */
  241. bool IsAppBundleOnApple() const;
  242. /** Return the framework version string. Undefined if
  243. IsFrameworkOnApple returns false. */
  244. std::string GetFrameworkVersion() const;
  245. /** Get a backtrace from the creation of the target. */
  246. cmListFileBacktrace const& GetBacktrace() const;
  247. /** Get a build-tree directory in which to place target support files. */
  248. std::string GetSupportDirectory() const;
  249. /** @return whether this target have a well defined output file name. */
  250. bool HaveWellDefinedOutputFiles() const;
  251. void InsertInclude(std::string const& entry,
  252. cmListFileBacktrace const& bt,
  253. bool before = false);
  254. void InsertCompileOption(std::string const& entry,
  255. cmListFileBacktrace const& bt,
  256. bool before = false);
  257. void InsertCompileDefinition(std::string const& entry,
  258. cmListFileBacktrace const& bt);
  259. void AppendBuildInterfaceIncludes();
  260. std::string GetDebugGeneratorExpressions(const std::string &value,
  261. cmTarget::LinkLibraryType llt) const;
  262. void AddSystemIncludeDirectories(const std::set<std::string> &incs);
  263. std::set<std::string> const & GetSystemIncludeDirectories() const
  264. { return this->SystemIncludeDirectories; }
  265. bool LinkLanguagePropagatesToDependents() const
  266. { return this->TargetTypeValue == STATIC_LIBRARY; }
  267. cmStringRange GetIncludeDirectoriesEntries() const;
  268. cmBacktraceRange GetIncludeDirectoriesBacktraces() const;
  269. cmStringRange GetCompileOptionsEntries() const;
  270. cmBacktraceRange GetCompileOptionsBacktraces() const;
  271. cmStringRange GetCompileFeaturesEntries() const;
  272. cmBacktraceRange GetCompileFeaturesBacktraces() const;
  273. cmStringRange GetCompileDefinitionsEntries() const;
  274. cmBacktraceRange GetCompileDefinitionsBacktraces() const;
  275. cmStringRange GetSourceEntries() const;
  276. cmBacktraceRange GetSourceBacktraces() const;
  277. cmStringRange GetLinkImplementationEntries() const;
  278. cmBacktraceRange GetLinkImplementationBacktraces() const;
  279. #if defined(_WIN32) && !defined(__CYGWIN__)
  280. const LinkLibraryVectorType &GetLinkLibrariesForVS6() const {
  281. return this->LinkLibrariesForVS6;}
  282. #endif
  283. private:
  284. bool HandleLocationPropertyPolicy(cmMakefile* context) const;
  285. #if defined(_WIN32) && !defined(__CYGWIN__)
  286. /**
  287. * A list of direct dependencies. Use in conjunction with DependencyMap.
  288. */
  289. typedef std::vector< LibraryID > DependencyList;
  290. /**
  291. * This map holds the dependency graph. map[x] returns a set of
  292. * direct dependencies of x. Note that the direct depenencies are
  293. * ordered. This is necessary to handle direct dependencies that
  294. * themselves have no dependency information.
  295. */
  296. typedef std::map< LibraryID, DependencyList > DependencyMap;
  297. /**
  298. * Inserts \a dep at the end of the dependency list of \a lib.
  299. */
  300. void InsertDependencyForVS6( DependencyMap& depMap,
  301. const LibraryID& lib,
  302. const LibraryID& dep);
  303. /*
  304. * Deletes \a dep from the dependency list of \a lib.
  305. */
  306. void DeleteDependencyForVS6( DependencyMap& depMap,
  307. const LibraryID& lib,
  308. const LibraryID& dep);
  309. /**
  310. * Emits the library \a lib and all its dependencies into link_line.
  311. * \a emitted keeps track of the libraries that have been emitted to
  312. * avoid duplicates--it is more efficient than searching
  313. * link_line. \a visited is used detect cycles. Note that \a
  314. * link_line is in reverse order, in that the dependencies of a
  315. * library are listed before the library itself.
  316. */
  317. void EmitForVS6( const LibraryID lib,
  318. const DependencyMap& dep_map,
  319. std::set<LibraryID>& emitted,
  320. std::set<LibraryID>& visited,
  321. DependencyList& link_line);
  322. /**
  323. * Finds the dependencies for \a lib and inserts them into \a
  324. * dep_map.
  325. */
  326. void GatherDependenciesForVS6( const cmMakefile& mf,
  327. const LibraryID& lib,
  328. DependencyMap& dep_map);
  329. void AnalyzeLibDependenciesForVS6( const cmMakefile& mf );
  330. #endif
  331. const char* GetSuffixVariableInternal(bool implib) const;
  332. const char* GetPrefixVariableInternal(bool implib) const;
  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 std::string& property,
  336. const char* default_value);
  337. std::string GetFullNameImported(const std::string& config,
  338. bool implib) const;
  339. std::string ImportedGetFullPath(const std::string& config,
  340. bool implib) const;
  341. private:
  342. mutable cmPropertyMap Properties;
  343. std::set<std::string> SystemIncludeDirectories;
  344. std::set<std::string> LinkDirectoriesEmmitted;
  345. std::set<std::string> Utilities;
  346. std::map<std::string, cmListFileBacktrace> UtilityBacktraces;
  347. cmPolicies::PolicyMap PolicyMap;
  348. std::string Name;
  349. std::string InstallPath;
  350. std::string RuntimeInstallPath;
  351. mutable std::string ExportMacro;
  352. std::vector<std::string> LinkDirectories;
  353. std::vector<cmCustomCommand> PreBuildCommands;
  354. std::vector<cmCustomCommand> PreLinkCommands;
  355. std::vector<cmCustomCommand> PostBuildCommands;
  356. std::vector<std::pair<TLLSignature, cmListFileContext> > TLLCommands;
  357. LinkLibraryVectorType PrevLinkedLibraries;
  358. LinkLibraryVectorType OriginalLinkLibraries;
  359. #if defined(_WIN32) && !defined(__CYGWIN__)
  360. LinkLibraryVectorType LinkLibrariesForVS6;
  361. #endif
  362. cmMakefile* Makefile;
  363. cmTargetInternalPointer Internal;
  364. TargetType TargetTypeValue;
  365. bool HaveInstallRule;
  366. bool RecordDependencies;
  367. bool DLLPlatform;
  368. bool IsAndroid;
  369. bool IsApple;
  370. bool IsImportedTarget;
  371. bool BuildInterfaceIncludesAppended;
  372. #if defined(_WIN32) && !defined(__CYGWIN__)
  373. bool LinkLibrariesForVS6Analyzed;
  374. #endif
  375. // Cache import information from properties for each configuration.
  376. struct ImportInfo
  377. {
  378. ImportInfo(): NoSOName(false), Multiplicity(0) {}
  379. bool NoSOName;
  380. int Multiplicity;
  381. std::string Location;
  382. std::string SOName;
  383. std::string ImportLibrary;
  384. std::string Languages;
  385. std::string Libraries;
  386. std::string LibrariesProp;
  387. std::string SharedDeps;
  388. };
  389. ImportInfo const* GetImportInfo(const std::string& config) const;
  390. void ComputeImportInfo(std::string const& desired_config,
  391. ImportInfo& info) const;
  392. std::string ProcessSourceItemCMP0049(const std::string& s);
  393. void MaybeInvalidatePropertyCache(const std::string& prop);
  394. // Internal representation details.
  395. friend class cmTargetInternals;
  396. friend class cmGeneratorTarget;
  397. friend class cmTargetTraceDependencies;
  398. void ComputeVersionedName(std::string& vName,
  399. std::string const& prefix,
  400. std::string const& base,
  401. std::string const& suffix,
  402. std::string const& name,
  403. const char* version) const;
  404. };
  405. #ifdef CMAKE_BUILD_WITH_CMAKE
  406. #ifdef CMake_HAVE_CXX11_UNORDERED_MAP
  407. typedef std::unordered_map<std::string, cmTarget> cmTargets;
  408. #else
  409. typedef cmsys::hash_map<std::string, cmTarget> cmTargets;
  410. #endif
  411. #else
  412. typedef std::map<std::string,cmTarget> cmTargets;
  413. #endif
  414. class cmTargetSet: public std::set<std::string> {};
  415. class cmTargetManifest: public std::map<std::string, cmTargetSet> {};
  416. #endif