UsePkgConfig.cmake 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. UsePkgConfig
  5. ------------
  6. .. deprecated:: 3.0
  7. This module should no longer be used. Instead, use the
  8. :module:`FindPkgConfig` module or the :command:`cmake_pkg_config` command.
  9. This module provided a command for finding external packages using
  10. ``pkg-config`` command-line utility. It has been replaced by the more
  11. convenient ``FindPkgConfig`` module, which is commonly used in
  12. :ref:`Find Modules`.
  13. As of CMake 3.31, the built-in :command:`cmake_pkg_config` command provides
  14. even more features to extract package information.
  15. Load this module in a CMake project with:
  16. .. code-block:: cmake
  17. include(UsePkgConfig)
  18. Commands
  19. ^^^^^^^^
  20. This module provides the following command:
  21. .. command:: pkgconfig
  22. Finds external package using ``pkg-config`` and sets result variables:
  23. .. code-block:: cmake
  24. pkgconfig(<package> <includedir> <libdir> <linkflags> <cflags>)
  25. This command invokes ``pkg-config`` command-line utility to retrieve the
  26. package information into specified variables. If ``pkg-config`` or the
  27. specified package ``<package>`` is NOT found, the result variables remain
  28. empty.
  29. The arguments are:
  30. ``<package>``
  31. Name of the package as defined in its PC metadata file (``<package>.pc``).
  32. ``<includedir>``
  33. Variable name to store the package's include directory.
  34. ``<libdir>``
  35. Variable name to store the directory containing the package library.
  36. ``<linkflags>``
  37. Variable name to store the linker flags for the package.
  38. ``<cflags>``
  39. Variable name to store the compiler flags for the package.
  40. Examples
  41. ^^^^^^^^
  42. Using this module fills the desired information into the four given variables:
  43. .. code-block:: cmake
  44. include(UsePkgConfig)
  45. pkgconfig(
  46. libart-2.0
  47. LIBART_INCLUDEDIR
  48. LIBART_LIBDIR
  49. LIBART_LDFLAGS
  50. LIBART_CFLAGS
  51. )
  52. Migrating to the :module:`FindPkgConfig` would look something like this:
  53. .. code-block:: cmake
  54. find_package(PkgConfig QUIET)
  55. if(PkgConfig_FOUND)
  56. pkg_check_modules(LIBART QUIET libart-2.0)
  57. endif()
  58. message(STATUS "LIBART_INCLUDEDIR=${LIBART_INCLUDEDIR}")
  59. message(STATUS "LIBART_LIBDIR=${LIBART_LIBDIR}")
  60. message(STATUS "LIBART_LDFLAGS=${LIBART_LDFLAGS}")
  61. message(STATUS "LIBART_CFLAGS=${LIBART_CFLAGS}")
  62. #]=======================================================================]
  63. find_program(PKGCONFIG_EXECUTABLE NAMES pkg-config )
  64. macro(PKGCONFIG _package _include_DIR _link_DIR _link_FLAGS _cflags)
  65. message(STATUS
  66. "WARNING: you are using the obsolete 'PKGCONFIG' macro, use FindPkgConfig")
  67. # reset the variables at the beginning
  68. set(${_include_DIR})
  69. set(${_link_DIR})
  70. set(${_link_FLAGS})
  71. set(${_cflags})
  72. # if pkg-config has been found
  73. if(PKGCONFIG_EXECUTABLE)
  74. execute_process(COMMAND ${PKGCONFIG_EXECUTABLE} ${_package} --exists RESULT_VARIABLE _return_VALUE OUTPUT_VARIABLE _pkgconfigDevNull )
  75. # and if the package of interest also exists for pkg-config, then get the information
  76. if(NOT _return_VALUE)
  77. execute_process(COMMAND ${PKGCONFIG_EXECUTABLE} ${_package} --variable=includedir
  78. OUTPUT_VARIABLE ${_include_DIR} OUTPUT_STRIP_TRAILING_WHITESPACE )
  79. string(REGEX REPLACE "[\r\n]" " " ${_include_DIR} "${${_include_DIR}}")
  80. execute_process(COMMAND ${PKGCONFIG_EXECUTABLE} ${_package} --variable=libdir
  81. OUTPUT_VARIABLE ${_link_DIR} OUTPUT_STRIP_TRAILING_WHITESPACE )
  82. string(REGEX REPLACE "[\r\n]" " " ${_link_DIR} "${${_link_DIR}}")
  83. execute_process(COMMAND ${PKGCONFIG_EXECUTABLE} ${_package} --libs
  84. OUTPUT_VARIABLE ${_link_FLAGS} OUTPUT_STRIP_TRAILING_WHITESPACE )
  85. string(REGEX REPLACE "[\r\n]" " " ${_link_FLAGS} "${${_link_FLAGS}}")
  86. execute_process(COMMAND ${PKGCONFIG_EXECUTABLE} ${_package} --cflags
  87. OUTPUT_VARIABLE ${_cflags} OUTPUT_STRIP_TRAILING_WHITESPACE )
  88. string(REGEX REPLACE "[\r\n]" " " ${_cflags} "${${_cflags}}")
  89. else()
  90. message(STATUS "PKGCONFIG() indicates that ${_package} is not installed (install the package which contains ${_package}.pc if you want to support this feature)")
  91. endif()
  92. # if pkg-config has NOT been found, INFORM the user
  93. else()
  94. message(STATUS "WARNING: PKGCONFIG() indicates that the tool pkg-config has not been found on your system. You should install it.")
  95. endif()
  96. endmacro()
  97. mark_as_advanced(PKGCONFIG_EXECUTABLE)