cmTargetLinkLibrariesCommand.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. "
  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. "See the INTERFACE_LINK_LIBRARIES target property to override the "
  88. "set of transitive link dependencies for a target. "
  89. "Calls to other signatures of this command may set the property "
  90. "making any libraries linked exclusively by this signature private."
  91. "\n"
  92. "CMake will also propagate \"usage requirements\" from linked library "
  93. "targets. "
  94. "Usage requirements affect compilation of sources in the <target>. "
  95. "They are specified by properties defined on linked targets. "
  96. "During generation of the build system, CMake integrates "
  97. "usage requirement property values with the corresponding "
  98. "build properties for <target>:\n"
  99. " INTERFACE_COMPILE_DEFINITONS: Appends to COMPILE_DEFINITONS\n"
  100. " INTERFACE_INCLUDE_DIRECTORIES: Appends to INCLUDE_DIRECTORIES\n"
  101. " INTERFACE_POSITION_INDEPENDENT_CODE: Sets POSITION_INDEPENDENT_CODE\n"
  102. " or checked for consistency with existing value\n"
  103. "\n"
  104. "If an <item> is a library in a Mac OX framework, the Headers "
  105. "directory of the framework will also be processed as a \"usage "
  106. "requirement\". This has the same effect as passing the framework "
  107. "directory as an include directory."
  108. " target_link_libraries(<target>\n"
  109. " <PRIVATE|PUBLIC|INTERFACE> <lib> ...\n"
  110. " [<PRIVATE|PUBLIC|INTERFACE> <lib> ... ] ...])\n"
  111. "The PUBLIC, PRIVATE and INTERFACE keywords can be used to specify "
  112. "both the link dependencies and the link interface in one command. "
  113. "Libraries and targets following PUBLIC are linked to, and are "
  114. "made part of the link interface. Libraries and targets "
  115. "following PRIVATE are linked to, but are not made part of the "
  116. "link interface. Libraries following INTERFACE are appended "
  117. "to the link interface and are not used for linking <target>."
  118. "\n"
  119. " target_link_libraries(<target> LINK_INTERFACE_LIBRARIES\n"
  120. " [[debug|optimized|general] <lib>] ...)\n"
  121. "The LINK_INTERFACE_LIBRARIES mode appends the libraries "
  122. "to the INTERFACE_LINK_LIBRARIES target property instead of using them "
  123. "for linking. If policy CMP0022 is not NEW, then this mode also "
  124. "appends libraries to the LINK_INTERFACE_LIBRARIES and its "
  125. "per-configuration equivalent. This signature "
  126. "is for compatibility only. Prefer the INTERFACE mode instead. "
  127. "Libraries specified as \"debug\" are wrapped in a generator "
  128. "expression to correspond to debug builds. If policy CMP0022 is not "
  129. "NEW, the libraries are also appended to the "
  130. "LINK_INTERFACE_LIBRARIES_DEBUG property (or to the properties "
  131. "corresponding to configurations listed in the DEBUG_CONFIGURATIONS "
  132. "global property if it is set). "
  133. "Libraries specified as \"optimized\" are appended to the "
  134. "INTERFACE_LINK_LIBRARIES property. If policy CMP0022 is not NEW, "
  135. "they are also appended to the LINK_INTERFACE_LIBRARIES property. "
  136. "Libraries specified as \"general\" (or without any keyword) are "
  137. "treated as if specified for both \"debug\" and \"optimized\"."
  138. "\n"
  139. " target_link_libraries(<target>\n"
  140. " <LINK_PRIVATE|LINK_PUBLIC>\n"
  141. " [[debug|optimized|general] <lib>] ...\n"
  142. " [<LINK_PRIVATE|LINK_PUBLIC>\n"
  143. " [[debug|optimized|general] <lib>] ...])\n"
  144. "The LINK_PUBLIC and LINK_PRIVATE modes can be used to specify both "
  145. "the link dependencies and the link interface in one command. This "
  146. "signature is for compatibility only. Prefer the PUBLIC or PRIVATE "
  147. "keywords instead. "
  148. "Libraries and targets following LINK_PUBLIC are linked to, and are "
  149. "made part of the INTERFACE_LINK_LIBRARIES. If policy CMP0022 is not "
  150. "NEW, they are also made part of the LINK_INTERFACE_LIBRARIES. "
  151. "Libraries and targets following LINK_PRIVATE are linked to, but are "
  152. "not made part of the INTERFACE_LINK_LIBRARIES (or "
  153. "LINK_INTERFACE_LIBRARIES)."
  154. "\n"
  155. "The library dependency graph is normally acyclic (a DAG), but in the "
  156. "case of mutually-dependent STATIC libraries CMake allows the graph "
  157. "to contain cycles (strongly connected components). "
  158. "When another target links to one of the libraries CMake repeats "
  159. "the entire connected component. "
  160. "For example, the code\n"
  161. " add_library(A STATIC a.c)\n"
  162. " add_library(B STATIC b.c)\n"
  163. " target_link_libraries(A B)\n"
  164. " target_link_libraries(B A)\n"
  165. " add_executable(main main.c)\n"
  166. " target_link_libraries(main A)\n"
  167. "links 'main' to 'A B A B'. "
  168. "("
  169. "While one repetition is usually sufficient, pathological object "
  170. "file and symbol arrangements can require more. "
  171. "One may handle such cases by manually repeating the component in "
  172. "the last target_link_libraries call. "
  173. "However, if two archives are really so interdependent they should "
  174. "probably be combined into a single archive."
  175. ")"
  176. "\n"
  177. "Arguments to target_link_libraries may use \"generator expressions\" "
  178. "with the syntax \"$<...>\". Note however, that generator expressions "
  179. "will not be used in OLD handling of CMP0003 or CMP0004."
  180. "\n"
  181. CM_DOCUMENT_COMMAND_GENERATOR_EXPRESSIONS
  182. ;
  183. }
  184. cmTypeMacro(cmTargetLinkLibrariesCommand, cmCommand);
  185. private:
  186. void LinkLibraryTypeSpecifierWarning(int left, int right);
  187. static const char* LinkLibraryTypeNames[3];
  188. cmTarget* Target;
  189. enum ProcessingState {
  190. ProcessingLinkLibraries,
  191. ProcessingPlainLinkInterface,
  192. ProcessingKeywordLinkInterface,
  193. ProcessingPlainPublicInterface,
  194. ProcessingKeywordPublicInterface,
  195. ProcessingPlainPrivateInterface,
  196. ProcessingKeywordPrivateInterface
  197. };
  198. ProcessingState CurrentProcessingState;
  199. bool HandleLibrary(const char* lib, cmTarget::LinkLibraryType llt);
  200. };
  201. #endif