cmTargetLinkLibrariesCommand.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 cmTargetLinkLibrariesCommand_h
  11. #define cmTargetLinkLibrariesCommand_h
  12. #include "cmCommand.h"
  13. /** \class cmTargetLinkLibrariesCommand
  14. * \brief Specify a list of libraries to link into executables.
  15. *
  16. * cmTargetLinkLibrariesCommand is used to specify a list of libraries to link
  17. * into executable(s) or shared objects. The names of the libraries
  18. * should be those defined by the LIBRARY(library) command(s).
  19. */
  20. class cmTargetLinkLibrariesCommand : public cmCommand
  21. {
  22. public:
  23. /**
  24. * This is a virtual constructor for the command.
  25. */
  26. virtual cmCommand* Clone()
  27. {
  28. return new cmTargetLinkLibrariesCommand;
  29. }
  30. /**
  31. * This is called when the command is first encountered in
  32. * the CMakeLists.txt file.
  33. */
  34. virtual bool InitialPass(std::vector<std::string> const& args,
  35. cmExecutionStatus &status);
  36. /**
  37. * The name of the command as specified in CMakeList.txt.
  38. */
  39. virtual const char* GetName() const { return "target_link_libraries";}
  40. /**
  41. * Succinct documentation.
  42. */
  43. virtual const char* GetTerseDocumentation() const
  44. {
  45. return
  46. "Link a target to given libraries.";
  47. }
  48. /**
  49. * More documentation.
  50. */
  51. virtual const char* GetFullDocumentation() const
  52. {
  53. return
  54. " target_link_libraries(<target> [item1 [item2 [...]]]\n"
  55. " [[debug|optimized|general] <item>] ...)\n"
  56. "Specify libraries or flags to use when linking a given target. "
  57. "The named <target> must have been created in the current directory "
  58. "by a command such as add_executable or add_library. "
  59. "The remaining arguments specify library names or flags. "
  60. "Repeated calls for the same <target> append items in the order called."
  61. "\n"
  62. "If a library name matches that of another target in the project "
  63. "a dependency will automatically be added in the build system to make "
  64. "sure the library being linked is up-to-date before the target links. "
  65. "Item names starting with '-', but not '-l' or '-framework', are "
  66. "treated as linker flags."
  67. "\n"
  68. "A \"debug\", \"optimized\", or \"general\" keyword indicates that "
  69. "the library immediately following it is to be used only for the "
  70. "corresponding build configuration. "
  71. "The \"debug\" keyword corresponds to the Debug configuration "
  72. "(or to configurations named in the DEBUG_CONFIGURATIONS global "
  73. "property if it is set). "
  74. "The \"optimized\" keyword corresponds to all other configurations. "
  75. "The \"general\" keyword corresponds to all configurations, and is "
  76. "purely optional (assumed if omitted). "
  77. "Higher granularity may be achieved for per-configuration rules "
  78. "by creating and linking to IMPORTED library targets. "
  79. "See the IMPORTED mode of the add_library command for more "
  80. "information. "
  81. "\n"
  82. "Library dependencies are transitive by default. "
  83. "When this target is linked into another target then the libraries "
  84. "linked to this target will appear on the link line for the other "
  85. "target too. "
  86. "See the LINK_INTERFACE_LIBRARIES target property to override the "
  87. "set of transitive link dependencies for a target."
  88. "\n"
  89. " target_link_libraries(<target> LINK_INTERFACE_LIBRARIES\n"
  90. " [[debug|optimized|general] <lib>] ...)\n"
  91. "The LINK_INTERFACE_LIBRARIES mode appends the libraries "
  92. "to the LINK_INTERFACE_LIBRARIES and its per-configuration equivalent "
  93. "target properties instead of using them for linking. "
  94. "Libraries specified as \"debug\" are appended to the "
  95. "LINK_INTERFACE_LIBRARIES_DEBUG property (or to the properties "
  96. "corresponding to configurations listed in the DEBUG_CONFIGURATIONS "
  97. "global property if it is set). "
  98. "Libraries specified as \"optimized\" are appended to the "
  99. "LINK_INTERFACE_LIBRARIES property. "
  100. "Libraries specified as \"general\" (or without any keyword) are "
  101. "treated as if specified for both \"debug\" and \"optimized\"."
  102. "\n"
  103. " target_link_libraries(<target>\n"
  104. " <LINK_PRIVATE|LINK_PUBLIC>\n"
  105. " [[debug|optimized|general] <lib>] ...\n"
  106. " [<LINK_PRIVATE|LINK_PUBLIC>\n"
  107. " [[debug|optimized|general] <lib>] ...])\n"
  108. "The LINK_PUBLIC and LINK_PRIVATE modes can be used to specify both "
  109. "the link dependencies and the link interface in one command. "
  110. "Libraries and targets following LINK_PUBLIC are linked to, and are "
  111. "made part of the LINK_INTERFACE_LIBRARIES. Libraries and targets "
  112. "following LINK_PRIVATE are linked to, but are not made part of the "
  113. "LINK_INTERFACE_LIBRARIES. "
  114. "\n"
  115. "The library dependency graph is normally acyclic (a DAG), but in the "
  116. "case of mutually-dependent STATIC libraries CMake allows the graph "
  117. "to contain cycles (strongly connected components). "
  118. "When another target links to one of the libraries CMake repeats "
  119. "the entire connected component. "
  120. "For example, the code\n"
  121. " add_library(A STATIC a.c)\n"
  122. " add_library(B STATIC b.c)\n"
  123. " target_link_libraries(A B)\n"
  124. " target_link_libraries(B A)\n"
  125. " add_executable(main main.c)\n"
  126. " target_link_libraries(main A)\n"
  127. "links 'main' to 'A B A B'. "
  128. "("
  129. "While one repetition is usually sufficient, pathological object "
  130. "file and symbol arrangements can require more. "
  131. "One may handle such cases by manually repeating the component in "
  132. "the last target_link_libraries call. "
  133. "However, if two archives are really so interdependent they should "
  134. "probably be combined into a single archive."
  135. ")"
  136. ;
  137. }
  138. cmTypeMacro(cmTargetLinkLibrariesCommand, cmCommand);
  139. private:
  140. void LinkLibraryTypeSpecifierWarning(int left, int right);
  141. static const char* LinkLibraryTypeNames[3];
  142. cmTarget* Target;
  143. enum ProcessingState {
  144. ProcessingLinkLibraries,
  145. ProcessingLinkInterface,
  146. ProcessingPublicInterface,
  147. ProcessingPrivateInterface
  148. };
  149. ProcessingState CurrentProcessingState;
  150. void HandleLibrary(const char* lib, cmTarget::LinkLibraryType llt);
  151. };
  152. #endif