cmTarget.h 17 KB

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