cmTargetLinkLibrariesCommand.h 7.3 KB

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