cmTarget.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. /** Whether this library has \@rpath and platform supports it. */
  207. bool HasMacOSXRpathInstallNameDir(const std::string& config) const;
  208. /** Whether this library defaults to \@rpath. */
  209. bool MacOSXRpathInstallNameDirDefault() const;
  210. /** Test for special case of a third-party shared library that has
  211. no soname at all. */
  212. bool IsImportedSharedLibWithoutSOName(const std::string& config) const;
  213. /** Does this target have a GNU implib to convert to MS format? */
  214. bool HasImplibGNUtoMS() const;
  215. /** Convert the given GNU import library name (.dll.a) to a name with a new
  216. extension (.lib or ${CMAKE_IMPORT_LIBRARY_SUFFIX}). */
  217. bool GetImplibGNUtoMS(std::string const& gnuName, std::string& out,
  218. const char* newExt = 0) const;
  219. // Get the properties
  220. cmPropertyMap &GetProperties() const { return this->Properties; }
  221. bool GetMappedConfig(std::string const& desired_config,
  222. const char** loc,
  223. const char** imp,
  224. std::string& suffix) const;
  225. /** Get the macro to define when building sources in this target.
  226. If no macro should be defined null is returned. */
  227. const char* GetExportMacro() const;
  228. /** Return whether this target is an executable with symbol exports
  229. enabled. */
  230. bool IsExecutableWithExports() const;
  231. /** Return whether this target may be used to link another target. */
  232. bool IsLinkable() const;
  233. /** Return whether or not the target is for a DLL platform. */
  234. bool IsDLLPlatform() const { return this->DLLPlatform; }
  235. /** Return whether or not the target has a DLL import library. */
  236. bool HasImportLibrary() const;
  237. /** Return whether this target is a shared library Framework on
  238. Apple. */
  239. bool IsFrameworkOnApple() const;
  240. /** Return whether this target is a CFBundle (plugin) on Apple. */
  241. bool IsCFBundleOnApple() const;
  242. /** Return whether this target is a XCTest on Apple. */
  243. bool IsXCTestOnApple() const;
  244. /** Return whether this target is an executable Bundle on Apple. */
  245. bool IsAppBundleOnApple() const;
  246. /** Return the framework version string. Undefined if
  247. IsFrameworkOnApple returns false. */
  248. std::string GetFrameworkVersion() const;
  249. /** Get a backtrace from the creation of the target. */
  250. cmListFileBacktrace const& GetBacktrace() const;
  251. /** Get a build-tree directory in which to place target support files. */
  252. std::string GetSupportDirectory() const;
  253. /** @return whether this target have a well defined output file name. */
  254. bool HaveWellDefinedOutputFiles() const;
  255. void InsertInclude(std::string const& entry,
  256. cmListFileBacktrace const& bt,
  257. bool before = false);
  258. void InsertCompileOption(std::string const& entry,
  259. cmListFileBacktrace const& bt,
  260. bool before = false);
  261. void InsertCompileDefinition(std::string const& entry,
  262. cmListFileBacktrace const& bt);
  263. void AppendBuildInterfaceIncludes();
  264. std::string GetDebugGeneratorExpressions(const std::string &value,
  265. cmTarget::LinkLibraryType llt) const;
  266. void AddSystemIncludeDirectories(const std::set<std::string> &incs);
  267. std::set<std::string> const & GetSystemIncludeDirectories() const
  268. { return this->SystemIncludeDirectories; }
  269. bool LinkLanguagePropagatesToDependents() const
  270. { return this->TargetTypeValue == STATIC_LIBRARY; }
  271. cmStringRange GetIncludeDirectoriesEntries() const;
  272. cmBacktraceRange GetIncludeDirectoriesBacktraces() const;
  273. cmStringRange GetCompileOptionsEntries() const;
  274. cmBacktraceRange GetCompileOptionsBacktraces() const;
  275. cmStringRange GetCompileFeaturesEntries() const;
  276. cmBacktraceRange GetCompileFeaturesBacktraces() const;
  277. cmStringRange GetCompileDefinitionsEntries() const;
  278. cmBacktraceRange GetCompileDefinitionsBacktraces() const;
  279. cmStringRange GetSourceEntries() const;
  280. cmBacktraceRange GetSourceBacktraces() const;
  281. cmStringRange GetLinkImplementationEntries() const;
  282. cmBacktraceRange GetLinkImplementationBacktraces() const;
  283. #if defined(_WIN32) && !defined(__CYGWIN__)
  284. const LinkLibraryVectorType &GetLinkLibrariesForVS6() const {
  285. return this->LinkLibrariesForVS6;}
  286. #endif
  287. private:
  288. bool HandleLocationPropertyPolicy(cmMakefile* context) const;
  289. #if defined(_WIN32) && !defined(__CYGWIN__)
  290. /**
  291. * A list of direct dependencies. Use in conjunction with DependencyMap.
  292. */
  293. typedef std::vector< LibraryID > DependencyList;
  294. /**
  295. * This map holds the dependency graph. map[x] returns a set of
  296. * direct dependencies of x. Note that the direct depenencies are
  297. * ordered. This is necessary to handle direct dependencies that
  298. * themselves have no dependency information.
  299. */
  300. typedef std::map< LibraryID, DependencyList > DependencyMap;
  301. /**
  302. * Inserts \a dep at the end of the dependency list of \a lib.
  303. */
  304. void InsertDependencyForVS6( DependencyMap& depMap,
  305. const LibraryID& lib,
  306. const LibraryID& dep);
  307. /*
  308. * Deletes \a dep from the dependency list of \a lib.
  309. */
  310. void DeleteDependencyForVS6( DependencyMap& depMap,
  311. const LibraryID& lib,
  312. const LibraryID& dep);
  313. /**
  314. * Emits the library \a lib and all its dependencies into link_line.
  315. * \a emitted keeps track of the libraries that have been emitted to
  316. * avoid duplicates--it is more efficient than searching
  317. * link_line. \a visited is used detect cycles. Note that \a
  318. * link_line is in reverse order, in that the dependencies of a
  319. * library are listed before the library itself.
  320. */
  321. void EmitForVS6( const LibraryID lib,
  322. const DependencyMap& dep_map,
  323. std::set<LibraryID>& emitted,
  324. std::set<LibraryID>& visited,
  325. DependencyList& link_line);
  326. /**
  327. * Finds the dependencies for \a lib and inserts them into \a
  328. * dep_map.
  329. */
  330. void GatherDependenciesForVS6( const cmMakefile& mf,
  331. const LibraryID& lib,
  332. DependencyMap& dep_map);
  333. void AnalyzeLibDependenciesForVS6( const cmMakefile& mf );
  334. #endif
  335. const char* GetSuffixVariableInternal(bool implib) const;
  336. const char* GetPrefixVariableInternal(bool implib) const;
  337. // Use a makefile variable to set a default for the given property.
  338. // If the variable is not defined use the given default instead.
  339. void SetPropertyDefault(const std::string& property,
  340. const char* default_value);
  341. // Returns ARCHIVE, LIBRARY, or RUNTIME based on platform and type.
  342. const char* GetOutputTargetType(bool implib) const;
  343. std::string GetFullNameImported(const std::string& config,
  344. bool implib) const;
  345. std::string ImportedGetFullPath(const std::string& config,
  346. bool implib) const;
  347. private:
  348. mutable cmPropertyMap Properties;
  349. std::set<std::string> SystemIncludeDirectories;
  350. std::set<std::string> LinkDirectoriesEmmitted;
  351. std::set<std::string> Utilities;
  352. std::map<std::string, cmListFileBacktrace> UtilityBacktraces;
  353. cmPolicies::PolicyMap PolicyMap;
  354. std::string Name;
  355. std::string InstallPath;
  356. std::string RuntimeInstallPath;
  357. mutable std::string ExportMacro;
  358. std::vector<std::string> LinkDirectories;
  359. std::vector<cmCustomCommand> PreBuildCommands;
  360. std::vector<cmCustomCommand> PreLinkCommands;
  361. std::vector<cmCustomCommand> PostBuildCommands;
  362. std::vector<std::pair<TLLSignature, cmListFileContext> > TLLCommands;
  363. LinkLibraryVectorType PrevLinkedLibraries;
  364. LinkLibraryVectorType OriginalLinkLibraries;
  365. #if defined(_WIN32) && !defined(__CYGWIN__)
  366. LinkLibraryVectorType LinkLibrariesForVS6;
  367. #endif
  368. cmMakefile* Makefile;
  369. cmTargetInternalPointer Internal;
  370. TargetType TargetTypeValue;
  371. bool HaveInstallRule;
  372. bool RecordDependencies;
  373. bool DLLPlatform;
  374. bool IsAndroid;
  375. bool IsApple;
  376. bool IsImportedTarget;
  377. bool BuildInterfaceIncludesAppended;
  378. #if defined(_WIN32) && !defined(__CYGWIN__)
  379. bool LinkLibrariesForVS6Analyzed;
  380. #endif
  381. // Cache import information from properties for each configuration.
  382. struct ImportInfo
  383. {
  384. ImportInfo(): NoSOName(false), Multiplicity(0) {}
  385. bool NoSOName;
  386. int Multiplicity;
  387. std::string Location;
  388. std::string SOName;
  389. std::string ImportLibrary;
  390. std::string Languages;
  391. std::string Libraries;
  392. std::string LibrariesProp;
  393. std::string SharedDeps;
  394. };
  395. ImportInfo const* GetImportInfo(const std::string& config) const;
  396. void ComputeImportInfo(std::string const& desired_config,
  397. ImportInfo& info) const;
  398. std::string ProcessSourceItemCMP0049(const std::string& s);
  399. void MaybeInvalidatePropertyCache(const std::string& prop);
  400. // Internal representation details.
  401. friend class cmTargetInternals;
  402. friend class cmGeneratorTarget;
  403. friend class cmTargetTraceDependencies;
  404. void ComputeVersionedName(std::string& vName,
  405. std::string const& prefix,
  406. std::string const& base,
  407. std::string const& suffix,
  408. std::string const& name,
  409. const char* version) const;
  410. };
  411. #ifdef CMAKE_BUILD_WITH_CMAKE
  412. #ifdef CMake_HAVE_CXX11_UNORDERED_MAP
  413. typedef std::unordered_map<std::string, cmTarget> cmTargets;
  414. #else
  415. typedef cmsys::hash_map<std::string, cmTarget> cmTargets;
  416. #endif
  417. #else
  418. typedef std::map<std::string,cmTarget> cmTargets;
  419. #endif
  420. class cmTargetSet: public std::set<std::string> {};
  421. class cmTargetManifest: public std::map<std::string, cmTargetSet> {};
  422. #endif