cmTargetLinkLibrariesCommand.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #include "cmDocumentGeneratorExpressions.h"
  14. /** \class cmTargetLinkLibrariesCommand
  15. * \brief Specify a list of libraries to link into executables.
  16. *
  17. * cmTargetLinkLibrariesCommand is used to specify a list of libraries to link
  18. * into executable(s) or shared objects. The names of the libraries
  19. * should be those defined by the LIBRARY(library) command(s).
  20. */
  21. class cmTargetLinkLibrariesCommand : public cmCommand
  22. {
  23. public:
  24. /**
  25. * This is a virtual constructor for the command.
  26. */
  27. virtual cmCommand* Clone()
  28. {
  29. return new cmTargetLinkLibrariesCommand;
  30. }
  31. /**
  32. * This is called when the command is first encountered in
  33. * the CMakeLists.txt file.
  34. */
  35. virtual bool InitialPass(std::vector<std::string> const& args,
  36. cmExecutionStatus &status);
  37. /**
  38. * The name of the command as specified in CMakeList.txt.
  39. */
  40. virtual const char* GetName() const { return "target_link_libraries";}
  41. /**
  42. * Succinct documentation.
  43. */
  44. virtual const char* GetTerseDocumentation() const
  45. {
  46. return
  47. "Link a target to given libraries.";
  48. }
  49. /**
  50. * More documentation.
  51. */
  52. virtual const char* GetFullDocumentation() const
  53. {
  54. return
  55. " target_link_libraries(<target> [item1 [item2 [...]]]\n"
  56. " [[debug|optimized|general] <item>] ...)\n"
  57. "Specify libraries or flags to use when linking a given target. "
  58. "The named <target> must have been created in the current directory "
  59. "by a command such as add_executable or add_library. "
  60. "The remaining arguments specify library names or flags. "
  61. "Repeated calls for the same <target> append items in the order called."
  62. "\n"
  63. "If a library name matches that of another target in the project "
  64. "a dependency will automatically be added in the build system to make "
  65. "sure the library being linked is up-to-date before the target links. "
  66. "Item names starting with '-', but not '-l' or '-framework', are "
  67. "treated as linker flags."
  68. "\n"
  69. "A \"debug\", \"optimized\", or \"general\" keyword indicates that "
  70. "the library immediately following it is to be used only for the "
  71. "corresponding build configuration. "
  72. "The \"debug\" keyword corresponds to the Debug configuration "
  73. "(or to configurations named in the DEBUG_CONFIGURATIONS global "
  74. "property if it is set). "
  75. "The \"optimized\" keyword corresponds to all other configurations. "
  76. "The \"general\" keyword corresponds to all configurations, and is "
  77. "purely optional (assumed if omitted). "
  78. "Higher granularity may be achieved for per-configuration rules "
  79. "by creating and linking to IMPORTED library targets. "
  80. "See the IMPORTED mode of the add_library command for more "
  81. "information. "
  82. "\n"
  83. "Library dependencies are transitive by default with this signature. "
  84. "When this target is linked into another target then the libraries "
  85. "linked to this target will appear on the link line for the other "
  86. "target too. "
  87. "This transitive \"link interface\" is stored in the "
  88. "INTERFACE_LINK_LIBRARIES target property when policy CMP0022 is set "
  89. "to NEW and may be overridden by setting the property directly. "
  90. "("
  91. "When CMP0022 is not set to NEW, transitive linking is builtin "
  92. "but may be overridden by the LINK_INTERFACE_LIBRARIES property. "
  93. "Calls to other signatures of this command may set the property "
  94. "making any libraries linked exclusively by this signature private."
  95. ")"
  96. "\n"
  97. "CMake will also propagate \"usage requirements\" from linked library "
  98. "targets. "
  99. "Usage requirements affect compilation of sources in the <target>. "
  100. "They are specified by properties defined on linked targets. "
  101. "During generation of the build system, CMake integrates "
  102. "usage requirement property values with the corresponding "
  103. "build properties for <target>:\n"
  104. " INTERFACE_COMPILE_DEFINITONS: Appends to COMPILE_DEFINITONS\n"
  105. " INTERFACE_INCLUDE_DIRECTORIES: Appends to INCLUDE_DIRECTORIES\n"
  106. " INTERFACE_POSITION_INDEPENDENT_CODE: Sets POSITION_INDEPENDENT_CODE\n"
  107. " or checked for consistency with existing value\n"
  108. "\n"
  109. "If an <item> is a library in a Mac OX framework, the Headers "
  110. "directory of the framework will also be processed as a \"usage "
  111. "requirement\". This has the same effect as passing the framework "
  112. "directory as an include directory."
  113. " target_link_libraries(<target>\n"
  114. " <PRIVATE|PUBLIC|INTERFACE> <lib> ...\n"
  115. " [<PRIVATE|PUBLIC|INTERFACE> <lib> ... ] ...])\n"
  116. "The PUBLIC, PRIVATE and INTERFACE keywords can be used to specify "
  117. "both the link dependencies and the link interface in one command. "
  118. "Libraries and targets following PUBLIC are linked to, and are "
  119. "made part of the link interface. Libraries and targets "
  120. "following PRIVATE are linked to, but are not made part of the "
  121. "link interface. Libraries following INTERFACE are appended "
  122. "to the link interface and are not used for linking <target>."
  123. "\n"
  124. " target_link_libraries(<target> LINK_INTERFACE_LIBRARIES\n"
  125. " [[debug|optimized|general] <lib>] ...)\n"
  126. "The LINK_INTERFACE_LIBRARIES mode appends the libraries "
  127. "to the INTERFACE_LINK_LIBRARIES target property instead of using them "
  128. "for linking. If policy CMP0022 is not NEW, then this mode also "
  129. "appends libraries to the LINK_INTERFACE_LIBRARIES and its "
  130. "per-configuration equivalent. This signature "
  131. "is for compatibility only. Prefer the INTERFACE mode instead. "
  132. "Libraries specified as \"debug\" are wrapped in a generator "
  133. "expression to correspond to debug builds. If policy CMP0022 is not "
  134. "NEW, the libraries are also appended to the "
  135. "LINK_INTERFACE_LIBRARIES_DEBUG property (or to the properties "
  136. "corresponding to configurations listed in the DEBUG_CONFIGURATIONS "
  137. "global property if it is set). "
  138. "Libraries specified as \"optimized\" are appended to the "
  139. "INTERFACE_LINK_LIBRARIES property. If policy CMP0022 is not NEW, "
  140. "they are also appended to the LINK_INTERFACE_LIBRARIES property. "
  141. "Libraries specified as \"general\" (or without any keyword) are "
  142. "treated as if specified for both \"debug\" and \"optimized\"."
  143. "\n"
  144. " target_link_libraries(<target>\n"
  145. " <LINK_PRIVATE|LINK_PUBLIC>\n"
  146. " [[debug|optimized|general] <lib>] ...\n"
  147. " [<LINK_PRIVATE|LINK_PUBLIC>\n"
  148. " [[debug|optimized|general] <lib>] ...])\n"
  149. "The LINK_PUBLIC and LINK_PRIVATE modes can be used to specify both "
  150. "the link dependencies and the link interface in one command. This "
  151. "signature is for compatibility only. Prefer the PUBLIC or PRIVATE "
  152. "keywords instead. "
  153. "Libraries and targets following LINK_PUBLIC are linked to, and are "
  154. "made part of the INTERFACE_LINK_LIBRARIES. If policy CMP0022 is not "
  155. "NEW, they are also made part of the LINK_INTERFACE_LIBRARIES. "
  156. "Libraries and targets following LINK_PRIVATE are linked to, but are "
  157. "not made part of the INTERFACE_LINK_LIBRARIES (or "
  158. "LINK_INTERFACE_LIBRARIES)."
  159. "\n"
  160. "The library dependency graph is normally acyclic (a DAG), but in the "
  161. "case of mutually-dependent STATIC libraries CMake allows the graph "
  162. "to contain cycles (strongly connected components). "
  163. "When another target links to one of the libraries CMake repeats "
  164. "the entire connected component. "
  165. "For example, the code\n"
  166. " add_library(A STATIC a.c)\n"
  167. " add_library(B STATIC b.c)\n"
  168. " target_link_libraries(A B)\n"
  169. " target_link_libraries(B A)\n"
  170. " add_executable(main main.c)\n"
  171. " target_link_libraries(main A)\n"
  172. "links 'main' to 'A B A B'. "
  173. "("
  174. "While one repetition is usually sufficient, pathological object "
  175. "file and symbol arrangements can require more. "
  176. "One may handle such cases by manually repeating the component in "
  177. "the last target_link_libraries call. "
  178. "However, if two archives are really so interdependent they should "
  179. "probably be combined into a single archive."
  180. ")"
  181. "\n"
  182. "Arguments to target_link_libraries may use \"generator expressions\" "
  183. "with the syntax \"$<...>\". Note however, that generator expressions "
  184. "will not be used in OLD handling of CMP0003 or CMP0004."
  185. "\n"
  186. CM_DOCUMENT_COMMAND_GENERATOR_EXPRESSIONS
  187. ;
  188. }
  189. cmTypeMacro(cmTargetLinkLibrariesCommand, cmCommand);
  190. private:
  191. void LinkLibraryTypeSpecifierWarning(int left, int right);
  192. static const char* LinkLibraryTypeNames[3];
  193. cmTarget* Target;
  194. enum ProcessingState {
  195. ProcessingLinkLibraries,
  196. ProcessingPlainLinkInterface,
  197. ProcessingKeywordLinkInterface,
  198. ProcessingPlainPublicInterface,
  199. ProcessingKeywordPublicInterface,
  200. ProcessingPlainPrivateInterface,
  201. ProcessingKeywordPrivateInterface
  202. };
  203. ProcessingState CurrentProcessingState;
  204. bool HandleLibrary(const char* lib, cmTarget::LinkLibraryType llt);
  205. };
  206. #endif