cmTarget.h 17 KB

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