index.rst 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. Using Dependencies Guide
  2. ************************
  3. .. only:: html
  4. .. contents::
  5. Introduction
  6. ============
  7. For developers wishing to use CMake to consume a third
  8. party binary package, there are multiple possibilities
  9. regarding how to optimally do so, depending on how
  10. CMake-aware the third-party library is.
  11. CMake files provided with a software package contain
  12. instructions for finding each build dependency. Some
  13. build dependencies are optional in that the build may
  14. succeed with a different feature set if the dependency
  15. is missing, and some dependencies are required. CMake
  16. searches well-known locations for each dependency, and
  17. the provided software may supply additional hints or
  18. locations to CMake to find each dependency.
  19. If a required dependency is not found by
  20. :manual:`cmake(1)`, the cache is populated with an entry
  21. which contains a ``NOTFOUND`` value. This value can be
  22. replaced by specifying it on the command line, or in
  23. the :manual:`ccmake(1)` or :manual:`cmake-gui(1)` tool.
  24. See the :guide:`User Interaction Guide` for
  25. more about setting cache entries.
  26. Libraries providing Config-file packages
  27. ----------------------------------------
  28. The most convenient way for a third-party to provide library
  29. binaries for use with CMake is to provide
  30. :ref:`Config File Packages`. These packages are text files
  31. shipped with the library which instruct CMake how to use the
  32. library binaries and associated headers, helper tools and
  33. CMake macros provided by the library.
  34. The config files can usually be found in a directory whose
  35. name matches the pattern ``lib/cmake/<PackageName>``, though
  36. they may be in other locations instead. The
  37. ``<PackageName>`` corresponds to use in CMake code with the
  38. :command:`find_package` command such as
  39. ``find_package(PackageName REQUIRED)``.
  40. The ``lib/cmake/<PackageName>`` directory will contain a
  41. file which is either named ``<PackageName>Config.cmake``
  42. or ``<PackageName>-config.cmake``. This is the entry point
  43. to the package for CMake. A separate optional file named
  44. ``<PackageName>ConfigVersion.cmake`` may also exist in the
  45. directory. This file is used by CMake to determine whether
  46. the version of the third party package satisfies uses of the
  47. :command:`find_package` command which specify version
  48. constraints. It is optional to specify a version when using
  49. :command:`find_package`, even if a ``ConfigVersion`` file is
  50. present.
  51. If the ``Config.cmake`` file is found and the
  52. optionally-specified version is satisfied, then the CMake
  53. :command:`find_package` command considers the package to be
  54. found and the entire library package is assumed to be
  55. complete as designed.
  56. There may be additional files providing CMake macros or
  57. :ref:`imported targets` for you to use. CMake does not
  58. enforce any naming convention for these
  59. files. They are related to the primary ``Config`` file by
  60. use of the CMake :command:`include` command.
  61. :guide:`Invoking CMake <User Interaction Guide>` with the
  62. intent of using a package of third party binaries requires
  63. that cmake :command:`find_package` commands succeed in finding
  64. the package. If the location of the package is in a directory
  65. known to CMake, the :command:`find_package` call should
  66. succeed. The directories known to cmake are platform-specific.
  67. For example, packages installed on Linux with a standard
  68. system package manager will be found in the ``/usr`` prefix
  69. automatically. Packages installed in ``Program Files`` on
  70. Windows will similarly be found automatically.
  71. Packages which are not found automatically are in locations
  72. not predictable to CMake such as ``/opt/mylib`` or
  73. ``$HOME/dev/prefix``. This is a normal situation and CMake
  74. provides several ways for users to specify where to find
  75. such libraries.
  76. The :variable:`CMAKE_PREFIX_PATH` variable may be
  77. :ref:`set when invoking CMake <Setting Build Variables>`.
  78. It is treated as a list of paths to search for
  79. :ref:`Config File Packages`. A package installed in
  80. ``/opt/somepackage`` will typically install config files
  81. such as
  82. ``/opt/somepackage/lib/cmake/somePackage/SomePackageConfig.cmake``.
  83. In that case, ``/opt/somepackage`` should be added to
  84. :variable:`CMAKE_PREFIX_PATH`.
  85. The environment variable ``CMAKE_PREFIX_PATH`` may also be
  86. populated with prefixes to search for packages. Like the
  87. ``PATH`` environment variable, this is a list and needs to use
  88. the platform-specific environment variable list item separator
  89. (``:`` on Unix and ``;`` on Windows).
  90. The :variable:`CMAKE_PREFIX_PATH` variable provides convenience
  91. in cases where multiple prefixes need to be specified, or when
  92. multiple different package binaries are available in the same
  93. prefix. Paths to packages may also be specified by setting
  94. variables matching ``<PackageName>_DIR``, such as
  95. ``SomePackage_DIR``. Note that this is not a prefix but should
  96. be a full path to a directory containing a config-style package
  97. file, such as ``/opt/somepackage/lib/cmake/SomePackage/`` in
  98. the above example.
  99. Imported Targets from Packages
  100. ------------------------------
  101. A third-party package which provides config-file packages may
  102. also provide :ref:`Imported targets`. These will be
  103. specified in files containing configuration-specific file
  104. paths relevant to the package, such as debug and release
  105. versions of libraries.
  106. Often the third-party package documentation will point out the
  107. names of imported targets available after a successful
  108. ``find_package`` for a library. Those imported target names
  109. can be used with the :command:`target_link_libraries` command.
  110. A complete example which makes a simple use of a third party
  111. library might look like:
  112. .. code-block:: cmake
  113. cmake_minimum_required(VERSION 3.10)
  114. project(MyExeProject VERSION 1.0.0)
  115. find_package(SomePackage REQUIRED)
  116. add_executable(MyExe main.cpp)
  117. target_link_libraries(MyExe PRIVATE SomePrefix::LibName)
  118. See :manual:`cmake-buildsystem(7)` for further information
  119. about developing a CMake buildsystem.
  120. Libraries not Providing Config-file Packages
  121. --------------------------------------------
  122. Third-party libraries which do not provide config-file packages
  123. can still be found with the :command:`find_package` command, if
  124. a ``FindSomePackage.cmake`` file is available.
  125. These module-file packages are different to config-file packages
  126. in that:
  127. #. They should not be provided by the third party, except
  128. perhaps in the form of documentation
  129. #. The availability of a ``Find<PackageName>.cmake`` file does
  130. not indicate the availability of the binaries themselves.
  131. #. CMake does not search the :variable:`CMAKE_PREFIX_PATH` for
  132. ``Find<PackageName>.cmake`` files. Instead CMake searches
  133. for such files in the :variable:`CMAKE_MODULE_PATH`
  134. variable. It is common for users to set the
  135. :variable:`CMAKE_MODULE_PATH` when running CMake, and it is
  136. common for CMake projects to append to
  137. :variable:`CMAKE_MODULE_PATH` to allow use of local
  138. module-file packages.
  139. #. CMake ships ``Find<PackageName>.cmake`` files for some
  140. :manual:`third party packages <cmake-modules(7)>`
  141. for convenience in cases where the third party does
  142. not provide config-file packages directly. These files are
  143. a maintenance burden for CMake, so new Find modules are
  144. generally not added to CMake anymore. Third-parties should
  145. provide config file packages instead of relying on a Find
  146. module to be provided by CMake.
  147. Module-file packages may also provide :ref:`Imported targets`.
  148. A complete example which finds such a package might look
  149. like:
  150. .. code-block:: cmake
  151. cmake_minimum_required(VERSION 3.10)
  152. project(MyExeProject VERSION 1.0.0)
  153. find_package(PNG REQUIRED)
  154. # Add path to a FindSomePackage.cmake file
  155. list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
  156. find_package(SomePackage REQUIRED)
  157. add_executable(MyExe main.cpp)
  158. target_link_libraries(MyExe PRIVATE
  159. PNG::PNG
  160. SomePrefix::LibName
  161. )
  162. The :variable:`<PackageName>_ROOT` variable is also
  163. searched as a prefix for :command:`find_package` calls using
  164. module-file packages such as ``FindSomePackage``.