cmTargetLinkLibrariesCommand.h 7.2 KB

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