cmake_pkg_config.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. cmake_pkg_config
  2. ----------------
  3. .. versionadded:: 3.31
  4. .. only:: html
  5. .. contents::
  6. Process pkg-config format package files.
  7. Synopsis
  8. ^^^^^^^^
  9. .. parsed-literal::
  10. cmake_pkg_config(EXTRACT <package> [<version>] [...])
  11. Introduction
  12. ^^^^^^^^^^^^
  13. This command generates CMake variables and targets from pkg-config format
  14. package files natively, without needing to invoke or even require the presence
  15. of a pkg-config implementation. A ``<package>`` is either an absolute path to a
  16. package file, or a package name to be searched for using the typical pkg-config
  17. search patterns. The optional ``<version>`` string has the same format and
  18. semantics as a pkg-config style version specifier, with the exception that if
  19. no comparison operator is specified ``=`` is assumed.
  20. .. _`common options`:
  21. There are multiple signatures for this command, and some of the options are
  22. common between them. They are:
  23. ``EXACT`` / ``QUIET`` / ``REQUIRED``
  24. The ``EXACT`` option requests that the version string be matched exactly
  25. (including empty string, if no version is provided), overriding the typical
  26. pkg-config version comparison algorithm. This will ignore any comparison
  27. operator attached to the version string.
  28. The ``QUIET`` option disables informational messages, including those
  29. indicating that the package cannot be found if it is not ``REQUIRED``. The
  30. ``REQUIRED`` option stops processing with an error message if the package
  31. cannot be found.
  32. ``STRICTNESS <mode>``
  33. Specify how strictly the contents of the package files will be verified during
  34. parsing and resolution. An invalid file, under the provided strictness mode,
  35. will cause the command to fail. Possible modes are:
  36. * ``STRICT``: Closely mirrors the behavior of the original FDO pkg-config.
  37. Variables and keywords must be unique. Variables must be defined before
  38. they are used. The Name, Description, and Version keywords must be present.
  39. The overall structure of the file must be valid and parsable.
  40. * ``PERMISSIVE``: Closely mirrors the behavior of the pkgconf implementation.
  41. Duplicate variables are overridden. Duplicate keywords are appended.
  42. Undefined variables resolve to empty strings. The Name, Description, and
  43. Version keywords must be present. The overall structure of the file must be
  44. valid and parsable.
  45. * ``BEST_EFFORT``: Same behavior as ``PERMISSIVE`` with regards to duplicate
  46. or uninitialized variables and keywords, but will not fail under any
  47. conditions. Package files which require BEST_EFFORT will fail validation
  48. under all other major implementations and should be fixed.
  49. The default strictness is ``PERMISSIVE``.
  50. ``ENV_MODE``
  51. Specifies which environment variables will be queried when running a given
  52. command. Possible modes are:
  53. * ``FDO``: Queries only the original set of ``PKG_CONFIG_*`` environment
  54. variables used by the freedesktop.org ``pkg-config`` implementation.
  55. * ``PKGCONF``: Queries the more extensive set of environment variables used
  56. by the ``pkgconf`` implementation.
  57. * ``IGNORE``: Ignores the presence, absence, and value of environment
  58. variables entirely. In all cases an environment variable would be queried
  59. its treated as defined, but with a value of empty string for the purpose
  60. of the operation. This does not modify the current environment. For boolean
  61. environment variables, such as ``PKG_CONFIG_ALLOW_*``, this means they are
  62. evaluated as truthy.
  63. ``PKG_CONFIG_SYSROOT_PATH`` is a minor exception. When ``ENV_MODE IGNORE``
  64. is used, no root path prepending will occur by default and ``pc_sysrootdir``
  65. remains defaulted to ``/``.
  66. Target-generating subcommands always ignore flag-filtering environment
  67. variables. The default environment mode is ``PKGCONF``.
  68. ``PC_LIBDIR <path>...``
  69. Overrides the default search location for package files; also used to derive
  70. the ``pc_path`` package variable.
  71. When this option is not provided, the default library directory is the first
  72. available of the following values:
  73. #. ``CMAKE_PKG_CONFIG_PC_LIB_DIRS``
  74. #. The ``PKG_CONFIG_LIBDIR`` environment variable
  75. #. The output of ``pkg-config --variable pc_path pkg-config``
  76. #. A platform-dependent default value
  77. ``PC_PATH <path>...``
  78. Overrides the supplemental package file directories which will be prepended
  79. to the search path; also used to derive the ``pc_path`` package variable.
  80. When this option is not provided, the default paths are the first available of
  81. the following values:
  82. #. ``CMAKE_PKG_CONFIG_PC_PATH``
  83. #. The ``PKG_CONFIG_PATH`` environment variable
  84. #. Empty list
  85. ``DISABLE_UNINSTALLED <bool>``
  86. Overrides the search behavior for "uninstalled" package files. These are
  87. package files with an "-uninstalled" suffix which describe packages integrated
  88. directly from a build tree.
  89. Normally such package files have higher priority than "installed" packages.
  90. When ``DISABLE_UNINSTALLED`` is true, searching for "uninstalled" packages
  91. is disabled.
  92. When this option is not provided, the default search behavior is determined
  93. by the first available of the following values:
  94. #. ``CMAKE_PKG_CONFIG_DISABLE_UNINSTALLED``
  95. #. If the ``PKG_CONFIG_DISABLE_UNINSTALLED`` environment variable is defined
  96. the search is disabled, otherwise it is enabled.
  97. ``PC_SYSROOT_DIR <path>``
  98. Overrides the root path which will be prepended to paths specified by ``-I``
  99. compile flags and ``-L`` library search locations; also used to derive the
  100. ``pc_sysrootdir`` package variable.
  101. When this option is not provided, the default root path is provided by the
  102. first available of the following values:
  103. #. ``CMAKE_PKG_CONFIG_SYSROOT_DIR``
  104. #. The ``PKG_CONFIG_SYSROOT_DIR`` environment variable
  105. #. If no root path is available, nothing will be prepended to include or
  106. library directory paths and ``pc_sysrootdir`` will be set to ``/``
  107. ``TOP_BUILD_DIR <path>``
  108. Overrides the top build directory path used to derived the ``pc_top_builddir``
  109. package variable.
  110. When this option is not provided, the default top build directory path is
  111. the first available of the following values:
  112. #. ``CMAKE_PKG_CONFIG_TOP_BUILD_DIR``
  113. #. The ``PKG_CONFIG_TOP_BUILD_DIR`` environment variable
  114. #. If no top build directory path is available, the ``pc_top_builddir``
  115. package variable is not set
  116. Signatures
  117. ^^^^^^^^^^
  118. .. signature::
  119. cmake_pkg_config(EXTRACT <package> [<version>] [...])
  120. Extract the contents of the package into variables.
  121. .. code-block:: cmake
  122. cmake_pkg_config(EXTRACT <package> [<version>]
  123. [REQUIRED] [EXACT] [QUIET]
  124. [STRICTNESS <mode>]
  125. [ENV_MODE <mode>]
  126. [PC_LIBDIR <path>...]
  127. [PC_PATH <path>...]
  128. [DISABLE_UNINSTALLED <bool>]
  129. [PC_SYSROOT_DIR <path>]
  130. [TOP_BUILD_DIR <path>]
  131. [SYSTEM_INCLUDE_DIRS <path>...]
  132. [SYSTEM_LIBRARY_DIRS <path>...]
  133. [ALLOW_SYSTEM_INCLUDES <bool>]
  134. [ALLOW_SYSTEM_LIBS <bool>])
  135. The following variables will be populated from the contents of package file:
  136. ==================================== ====== ========================================================================================
  137. Variable Type Definition
  138. ==================================== ====== ========================================================================================
  139. ``CMAKE_PKG_CONFIG_NAME`` String Value of the ``Name`` keyword
  140. ``CMAKE_PKG_CONFIG_DESCRIPTION`` String Value of the ``Description`` keyword
  141. ``CMAKE_PKG_CONFIG_VERSION`` String Value of the ``Version`` keyword
  142. ``CMAKE_PKG_CONFIG_PROVIDES`` List Value of the ``Provides`` keyword
  143. ``CMAKE_PKG_CONFIG_REQUIRES`` List Value of the ``Requires`` keyword
  144. ``CMAKE_PKG_CONFIG_CONFLICTS`` List Value of the ``Conflicts`` keyword
  145. ``CMAKE_PKG_CONFIG_CFLAGS`` String Value of the ``CFlags`` / ``Cflags`` keyword
  146. ``CMAKE_PKG_CONFIG_INCLUDES`` List All ``-I`` prefixed flags from ``CMAKE_PKG_CONFIG_CFLAGS``
  147. ``CMAKE_PKG_CONFIG_COMPILE_OPTIONS`` List All flags not prefixed with ``-I`` from ``CMAKE_PKG_CONFIG_CFLAGS``
  148. ``CMAKE_PKG_CONFIG_LIBS`` String Value of the ``Libs`` keyword
  149. ``CMAKE_PKG_CONFIG_LIBDIRS`` List All ``-L`` prefixed flags from ``CMAKE_PKG_CONFIG_LIBS``
  150. ``CMAKE_PKG_CONFIG_LIBNAMES`` List All ``-l`` prefixed flags from ``CMAKE_PKG_CONFIG_LIBS``
  151. ``CMAKE_PKG_CONFIG_LINK_OPTIONS`` List All flags not prefixed with ``-L`` or ``-l`` from ``CMAKE_PKG_CONFIG_LIBS``
  152. ``CMAKE_PKG_CONFIG_*_PRIVATE`` \* ``CFLAGS`` / ``LIBS`` / ``REQUIRES`` and derived, but in their ``.private`` suffix forms
  153. ==================================== ====== ========================================================================================
  154. ``SYSTEM_INCLUDE_DIRS``
  155. Overrides the "system" directories for the purpose of flag mangling include
  156. directories in ``CMAKE_PKG_CONFIG_CFLAGS`` and derived variables.
  157. When this option is not provided, the default directories are provided by the
  158. first available of the following values:
  159. #. ``CMAKE_PKG_CONFIG_SYS_INCLUDE_DIRS``
  160. #. The ``PKG_CONFIG_SYSTEM_INCLUDE_PATH`` environment variable
  161. #. The output of ``pkgconf --variable pc_system_includedirs pkg-config``
  162. #. A platform-dependent default value
  163. Additionally, when the ``ENV_MODE`` is ``PKGCONF`` the
  164. ``CMAKE_PKG_CONFIG_PKGCONF_INCLUDES`` variable will be concatenated to the
  165. list if available. If it is not available, the following environment variables
  166. will be queried and concatenated:
  167. * ``CPATH``
  168. * ``C_INCLUDE_PATH``
  169. * ``CPLUS_INCLUDE_PATH``
  170. * ``OBJC_INCLUDE_PATH``
  171. * ``INCLUDE`` (Windows Only)
  172. ``SYSTEM_LIBRARY_DIRS``
  173. Overrides the "system" directories for the purpose of flag mangling library
  174. directories in ``CMAKE_PKG_CONFIG_LIBS`` and derived variables.
  175. When this option is not provided, the default directories are provided by the
  176. first available of the following values:
  177. #. ``CMAKE_PKG_CONFIG_SYS_LIB_DIRS``
  178. #. The ``PKG_CONFIG_SYSTEM_LIBRARY_PATH`` environment variable
  179. #. The output of ``pkgconf --variable pc_system_libdirs pkg-config``
  180. #. A platform-dependent default value
  181. Additionally, when the ``ENV_MODE`` is ``PKGCONF`` the
  182. ``CMAKE_PKG_CONFIG_PKGCONF_LIB_DIRS`` variable will be concatenated to the
  183. list if available. If it is not available, the ``LIBRARY_PATH`` environment
  184. variable will be queried and concatenated.
  185. ``ALLOW_SYSTEM_INCLUDES``
  186. Preserves "system" directories during flag mangling of include directories
  187. in ``CMAKE_PKG_CONFIG_CFLAGS`` and derived variables.
  188. When this option is not provided, the default value is determined by the first
  189. available of the following values:
  190. #. ``CMAKE_PKG_CONFIG_ALLOW_SYS_INCLUDES``
  191. #. If the ``PKG_CONFIG_ALLOW_SYSTEM_CFLAGS`` environment variable is defined
  192. the flags are preserved, otherwise they are filtered during flag mangling.
  193. ``ALLOW_SYSTEM_LIBS``
  194. Preserves "system" directories during flag mangling of library directories
  195. in ``CMAKE_PKG_CONFIG_LIBS`` and derived variables.
  196. When this option is not provided, the default value is determined by the first
  197. available of the following values:
  198. #. ``CMAKE_PKG_CONFIG_ALLOW_SYS_LIBS``
  199. #. If the ``PKG_CONFIG_ALLOW_SYSTEM_LIBS`` environment variable is defined
  200. the flags are preserved, otherwise they are filtered during flag mangling.