cmTargetLinkLibrariesCommand.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. " target_link_libraries(<target> LINK_INTERFACE_LIBRARIES\n"
  105. " [[debug|optimized|general] <lib>] ...)\n"
  106. "The LINK_INTERFACE_LIBRARIES mode appends the libraries "
  107. "to the INTERFACE_LINK_LIBRARIES target property instead of using them "
  108. "for linking. If policy CMP0022 is not NEW, then this mode also "
  109. "appends libraries to the LINK_INTERFACE_LIBRARIES and its "
  110. "per-configuration equivalent. "
  111. "Libraries specified as \"debug\" are wrapped in a generator "
  112. "expression to correspond to debug builds. If policy CMP0022 is not "
  113. "NEW, the libraries are also appended to the "
  114. "LINK_INTERFACE_LIBRARIES_DEBUG property (or to the properties "
  115. "corresponding to configurations listed in the DEBUG_CONFIGURATIONS "
  116. "global property if it is set). "
  117. "Libraries specified as \"optimized\" are appended to the "
  118. "INTERFACE_LINK_LIBRARIES property. If policy CMP0022 is not NEW, "
  119. "they are also appended to the LINK_INTERFACE_LIBRARIES property. "
  120. "Libraries specified as \"general\" (or without any keyword) are "
  121. "treated as if specified for both \"debug\" and \"optimized\"."
  122. "\n"
  123. " target_link_libraries(<target>\n"
  124. " <LINK_PRIVATE|LINK_PUBLIC>\n"
  125. " [[debug|optimized|general] <lib>] ...\n"
  126. " [<LINK_PRIVATE|LINK_PUBLIC>\n"
  127. " [[debug|optimized|general] <lib>] ...])\n"
  128. "The LINK_PUBLIC and LINK_PRIVATE modes can be used to specify both "
  129. "the link dependencies and the link interface in one command. "
  130. "Libraries and targets following LINK_PUBLIC are linked to, and are "
  131. "made part of the INTERFACE_LINK_LIBRARIES. If policy CMP0022 is not "
  132. "NEW, they are also made part of the LINK_INTERFACE_LIBRARIES. "
  133. "Libraries and targets following LINK_PRIVATE are linked to, but are "
  134. "not made part of the INTERFACE_LINK_LIBRARIES (or "
  135. "LINK_INTERFACE_LIBRARIES)."
  136. "\n"
  137. "The library dependency graph is normally acyclic (a DAG), but in the "
  138. "case of mutually-dependent STATIC libraries CMake allows the graph "
  139. "to contain cycles (strongly connected components). "
  140. "When another target links to one of the libraries CMake repeats "
  141. "the entire connected component. "
  142. "For example, the code\n"
  143. " add_library(A STATIC a.c)\n"
  144. " add_library(B STATIC b.c)\n"
  145. " target_link_libraries(A B)\n"
  146. " target_link_libraries(B A)\n"
  147. " add_executable(main main.c)\n"
  148. " target_link_libraries(main A)\n"
  149. "links 'main' to 'A B A B'. "
  150. "("
  151. "While one repetition is usually sufficient, pathological object "
  152. "file and symbol arrangements can require more. "
  153. "One may handle such cases by manually repeating the component in "
  154. "the last target_link_libraries call. "
  155. "However, if two archives are really so interdependent they should "
  156. "probably be combined into a single archive."
  157. ")"
  158. "\n"
  159. "Arguments to target_link_libraries may use \"generator expressions\" "
  160. "with the syntax \"$<...>\". Note however, that generator expressions "
  161. "will not be used in OLD handling of CMP0003 or CMP0004."
  162. "\n"
  163. CM_DOCUMENT_COMMAND_GENERATOR_EXPRESSIONS
  164. ;
  165. }
  166. cmTypeMacro(cmTargetLinkLibrariesCommand, cmCommand);
  167. private:
  168. void LinkLibraryTypeSpecifierWarning(int left, int right);
  169. static const char* LinkLibraryTypeNames[3];
  170. cmTarget* Target;
  171. enum ProcessingState {
  172. ProcessingLinkLibraries,
  173. ProcessingLinkInterface,
  174. ProcessingPublicInterface,
  175. ProcessingPrivateInterface
  176. };
  177. ProcessingState CurrentProcessingState;
  178. void HandleLibrary(const char* lib, cmTarget::LinkLibraryType llt);
  179. };
  180. #endif