cmake_pkg_config.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. cmake_pkg_config(`POPULATE`_ <package> [<version>] [...])
  12. cmake_pkg_config(`IMPORT`_ <package> [<version>] [...])
  13. Introduction
  14. ^^^^^^^^^^^^
  15. This command generates CMake variables and targets from pkg-config format
  16. package files natively, without needing to invoke or even require the presence
  17. of a pkg-config implementation. A ``<package>`` is either an absolute path to a
  18. package file, or a package name to be searched for using the typical pkg-config
  19. search patterns. The optional ``<version>`` string has the same format and
  20. semantics as a pkg-config style version specifier, with the exception that if
  21. no comparison operator is specified ``=`` is assumed.
  22. PkgConfig Targets
  23. ^^^^^^^^^^^^^^^^^
  24. ``cmake_pkg_config`` may recursively generate target-like names in the global
  25. scope in order to resolve a package ``IMPORT`` or ``POPULATE`` command. These
  26. names take the form of ``@foreign_pkgcfg::<package>`` and are exposed via the
  27. :prop_tgt:`INTERFACE_LINK_LIBRARIES` target property of an ``IMPORT``-generated
  28. target.
  29. It is not possible to modify or address these pkg-config native targets via
  30. normal target-based commands. Limited control over their generation is possible
  31. via the ``POPULATE`` command, but modification should generally be performed
  32. inside the corresponding package file, not downstream in CMake.
  33. Pkg-config targets are reused across commands. Once a given package name has
  34. been resolved via ``POPULATE`` or ``IMPORT`` (but not ``EXTRACT``), all future
  35. requests for the corresponding package name by those commands will resolve to
  36. the previously generated pkg-config target.
  37. ``EXTRACT`` always performs the complete package name lookup in order to allow
  38. searches for multiple installations of the same package in custom dependency
  39. management schemes.
  40. Common Options
  41. ^^^^^^^^^^^^^^
  42. There are multiple signatures for this command, and some of the options are
  43. common between them. They are:
  44. ``EXACT`` / ``QUIET`` / ``REQUIRED``
  45. The ``EXACT`` option requests that the version string be matched exactly
  46. (including empty string, if no version is provided), overriding the typical
  47. pkg-config version comparison algorithm. This will ignore any comparison
  48. operator attached to the version string.
  49. The ``QUIET`` option disables informational messages, including those
  50. indicating that the package cannot be found if it is not ``REQUIRED``. The
  51. ``REQUIRED`` option stops processing with an error message if the package
  52. cannot be found.
  53. ``STRICTNESS <mode>``
  54. Specify how strictly the contents of the package files will be verified during
  55. parsing and resolution. An invalid file, under the provided strictness mode,
  56. will cause the command to fail. Possible modes are:
  57. * ``STRICT``: Closely mirrors the behavior of the original FDO pkg-config.
  58. Variables and keywords must be unique. Variables must be defined before
  59. they are used. The Name, Description, and Version keywords must be present.
  60. The overall structure of the file must be valid and parsable.
  61. * ``PERMISSIVE``: Closely mirrors the behavior of the pkgconf implementation.
  62. Duplicate variables are overridden. Duplicate keywords are appended.
  63. Undefined variables resolve to empty strings. The Name, Description, and
  64. Version keywords must be present. The overall structure of the file must be
  65. valid and parsable.
  66. * ``BEST_EFFORT``: Same behavior as ``PERMISSIVE`` with regards to duplicate
  67. or uninitialized variables and keywords, but will not fail under any
  68. conditions. Package files which require BEST_EFFORT will fail validation
  69. under all other major implementations and should be fixed.
  70. The default strictness is ``PERMISSIVE``.
  71. ``ENV_MODE``
  72. Specifies which environment variables will be queried when running a given
  73. command. Possible modes are:
  74. * ``FDO``: Queries only the original set of ``PKG_CONFIG_*`` environment
  75. variables used by the freedesktop.org ``pkg-config`` implementation.
  76. * ``PKGCONF``: Queries the more extensive set of environment variables used
  77. by the ``pkgconf`` implementation.
  78. * ``IGNORE``: Ignores the presence, absence, and value of environment
  79. variables entirely. In all cases an environment variable would be queried
  80. its treated as defined, but with a value of empty string for the purpose
  81. of the operation. This does not modify the current environment. For boolean
  82. environment variables, such as ``PKG_CONFIG_ALLOW_*``, this means they are
  83. evaluated as truthy.
  84. ``PKG_CONFIG_SYSROOT_PATH`` is a minor exception. When ``ENV_MODE IGNORE``
  85. is used, no root path prepending will occur by default and ``pc_sysrootdir``
  86. remains defaulted to ``/``.
  87. Target-generating subcommands always ignore flag-filtering environment
  88. variables. The default environment mode is ``PKGCONF``.
  89. ``PC_LIBDIR <path>...``
  90. Overrides the default search location for package files; also used to derive
  91. the ``pc_path`` package variable.
  92. When this option is not provided, the default library directory is the first
  93. available of the following values:
  94. #. ``CMAKE_PKG_CONFIG_PC_LIB_DIRS``
  95. #. The ``PKG_CONFIG_LIBDIR`` environment variable
  96. #. The output of ``pkg-config --variable pc_path pkg-config``
  97. #. A platform-dependent default value
  98. ``PC_PATH <path>...``
  99. Overrides the supplemental package file directories which will be prepended
  100. to the search path; also used to derive the ``pc_path`` package variable.
  101. When this option is not provided, the default paths are the first available of
  102. the following values:
  103. #. ``CMAKE_PKG_CONFIG_PC_PATH``
  104. #. The ``PKG_CONFIG_PATH`` environment variable
  105. #. Empty list
  106. ``DISABLE_UNINSTALLED <bool>``
  107. Overrides the search behavior for "uninstalled" package files. These are
  108. package files with an "-uninstalled" suffix which describe packages integrated
  109. directly from a build tree.
  110. Normally such package files have higher priority than "installed" packages.
  111. When ``DISABLE_UNINSTALLED`` is true, searching for "uninstalled" packages
  112. is disabled.
  113. When this option is not provided, the default search behavior is determined
  114. by the first available of the following values:
  115. #. ``CMAKE_PKG_CONFIG_DISABLE_UNINSTALLED``
  116. #. If the ``PKG_CONFIG_DISABLE_UNINSTALLED`` environment variable is defined
  117. the search is disabled, otherwise it is enabled.
  118. ``PC_SYSROOT_DIR <path>``
  119. Overrides the root path which will be prepended to paths specified by ``-I``
  120. compile flags and ``-L`` library search locations; also used to derive the
  121. ``pc_sysrootdir`` package variable.
  122. When this option is not provided, the default root path is provided by the
  123. first available of the following values:
  124. #. ``CMAKE_PKG_CONFIG_SYSROOT_DIR``
  125. #. The ``PKG_CONFIG_SYSROOT_DIR`` environment variable
  126. #. If no root path is available, nothing will be prepended to include or
  127. library directory paths and ``pc_sysrootdir`` will be set to ``/``
  128. ``TOP_BUILD_DIR <path>``
  129. Overrides the top build directory path used to derive the ``pc_top_builddir``
  130. package variable.
  131. When this option is not provided, the default top build directory path is
  132. the first available of the following values:
  133. #. ``CMAKE_PKG_CONFIG_TOP_BUILD_DIR``
  134. #. The ``PKG_CONFIG_TOP_BUILD_DIR`` environment variable
  135. #. If no top build directory path is available, the ``pc_top_builddir``
  136. package variable is not set
  137. ``BIND_PC_REQUIRES``
  138. A list of ``<Name>=<Target>`` pairs, the ``Name`` is a package name as it
  139. appears in the ``Requires`` list of a pkg-config file and the ``Target`` is a
  140. CMake-native target name (not a pkg-config target).
  141. When a given package name appears in the ``Requires`` list of a package, it
  142. will be fulfilled with the associated CMake target. This behavior applies to
  143. all dependencies in the pkg-config graph that have not been previously
  144. populated.
  145. Signatures
  146. ^^^^^^^^^^
  147. .. signature::
  148. cmake_pkg_config(EXTRACT <package> [<version>] [...])
  149. .. versionadded:: 3.31
  150. Extract the contents of the package into variables.
  151. .. code-block:: cmake
  152. cmake_pkg_config(EXTRACT <package> [<version>]
  153. [REQUIRED] [EXACT] [QUIET]
  154. [SYSTEM_INCLUDE_DIRS <path>...]
  155. [SYSTEM_LIBRARY_DIRS <path>...]
  156. [ALLOW_SYSTEM_INCLUDES <bool>]
  157. [ALLOW_SYSTEM_LIBS <bool>]
  158. [STRICTNESS <mode>]
  159. [ENV_MODE <mode>]
  160. [PC_LIBDIR <path>...]
  161. [PC_PATH <path>...]
  162. [DISABLE_UNINSTALLED <bool>]
  163. [PC_SYSROOT_DIR <path>]
  164. [TOP_BUILD_DIR <path>])
  165. The following variables will be populated from the contents of package file:
  166. ==================================== ====== ========================================================================================
  167. Variable Type Definition
  168. ==================================== ====== ========================================================================================
  169. ``CMAKE_PKG_CONFIG_NAME`` String Value of the ``Name`` keyword
  170. ``CMAKE_PKG_CONFIG_DESCRIPTION`` String Value of the ``Description`` keyword
  171. ``CMAKE_PKG_CONFIG_VERSION`` String Value of the ``Version`` keyword
  172. ``CMAKE_PKG_CONFIG_PROVIDES`` List Value of the ``Provides`` keyword
  173. ``CMAKE_PKG_CONFIG_REQUIRES`` List Value of the ``Requires`` keyword
  174. ``CMAKE_PKG_CONFIG_CONFLICTS`` List Value of the ``Conflicts`` keyword
  175. ``CMAKE_PKG_CONFIG_CFLAGS`` String Value of the ``CFlags`` / ``Cflags`` keyword
  176. ``CMAKE_PKG_CONFIG_INCLUDES`` List All ``-I`` prefixed flags from ``CMAKE_PKG_CONFIG_CFLAGS``
  177. ``CMAKE_PKG_CONFIG_COMPILE_OPTIONS`` List All flags not prefixed with ``-I`` from ``CMAKE_PKG_CONFIG_CFLAGS``
  178. ``CMAKE_PKG_CONFIG_LIBS`` String Value of the ``Libs`` keyword
  179. ``CMAKE_PKG_CONFIG_LIBDIRS`` List All ``-L`` prefixed flags from ``CMAKE_PKG_CONFIG_LIBS``
  180. ``CMAKE_PKG_CONFIG_LIBNAMES`` List All ``-l`` prefixed flags from ``CMAKE_PKG_CONFIG_LIBS``
  181. ``CMAKE_PKG_CONFIG_LINK_OPTIONS`` List All flags not prefixed with ``-L`` or ``-l`` from ``CMAKE_PKG_CONFIG_LIBS``
  182. ``CMAKE_PKG_CONFIG_*_PRIVATE`` \* ``CFLAGS`` / ``LIBS`` / ``REQUIRES`` and derived, but in their ``.private`` suffix forms
  183. ==================================== ====== ========================================================================================
  184. ``SYSTEM_INCLUDE_DIRS``
  185. Overrides the "system" directories for the purpose of flag mangling include
  186. directories in ``CMAKE_PKG_CONFIG_CFLAGS`` and derived variables.
  187. When this option is not provided, the default directories are provided by the
  188. first available of the following values:
  189. #. ``CMAKE_PKG_CONFIG_SYS_INCLUDE_DIRS``
  190. #. The ``PKG_CONFIG_SYSTEM_INCLUDE_PATH`` environment variable
  191. #. The output of ``pkgconf --variable pc_system_includedirs pkg-config``
  192. #. A platform-dependent default value
  193. Additionally, when the ``ENV_MODE`` is ``PKGCONF`` the
  194. ``CMAKE_PKG_CONFIG_PKGCONF_INCLUDES`` variable will be concatenated to the
  195. list if available. If it is not available, the following environment variables
  196. will be queried and concatenated:
  197. * ``CPATH``
  198. * ``C_INCLUDE_PATH``
  199. * ``CPLUS_INCLUDE_PATH``
  200. * ``OBJC_INCLUDE_PATH``
  201. * ``INCLUDE`` (Windows Only)
  202. ``SYSTEM_LIBRARY_DIRS``
  203. Overrides the "system" directories for the purpose of flag mangling library
  204. directories in ``CMAKE_PKG_CONFIG_LIBS`` and derived variables.
  205. When this option is not provided, the default directories are provided by the
  206. first available of the following values:
  207. #. ``CMAKE_PKG_CONFIG_SYS_LIB_DIRS``
  208. #. The ``PKG_CONFIG_SYSTEM_LIBRARY_PATH`` environment variable
  209. #. The output of ``pkgconf --variable pc_system_libdirs pkg-config``
  210. #. A platform-dependent default value
  211. Additionally, when the ``ENV_MODE`` is ``PKGCONF`` the
  212. ``CMAKE_PKG_CONFIG_PKGCONF_LIB_DIRS`` variable will be concatenated to the
  213. list if available. If it is not available, the ``LIBRARY_PATH`` environment
  214. variable will be queried and concatenated.
  215. ``ALLOW_SYSTEM_INCLUDES``
  216. Preserves "system" directories during flag mangling of include directories
  217. in ``CMAKE_PKG_CONFIG_CFLAGS`` and derived variables.
  218. When this option is not provided, the default value is determined by the first
  219. available of the following values:
  220. #. ``CMAKE_PKG_CONFIG_ALLOW_SYS_INCLUDES``
  221. #. If the ``PKG_CONFIG_ALLOW_SYSTEM_CFLAGS`` environment variable is defined
  222. the flags are preserved, otherwise they are filtered during flag mangling.
  223. ``ALLOW_SYSTEM_LIBS``
  224. Preserves "system" directories during flag mangling of library directories
  225. in ``CMAKE_PKG_CONFIG_LIBS`` and derived variables.
  226. When this option is not provided, the default value is determined by the first
  227. available of the following values:
  228. #. ``CMAKE_PKG_CONFIG_ALLOW_SYS_LIBS``
  229. #. If the ``PKG_CONFIG_ALLOW_SYSTEM_LIBS`` environment variable is defined
  230. the flags are preserved, otherwise they are filtered during flag mangling.
  231. .. signature::
  232. cmake_pkg_config(POPULATE <package> [<version>] [...])
  233. .. versionadded:: 4.1
  234. Populate a package in the pkg-config target namespace
  235. .. code-block:: cmake
  236. cmake_pkg_config(POPULATE <package> [<version>]
  237. [REQUIRED] [EXACT] [QUIET]
  238. [BIND_PC_REQUIRES <<name>=<target>>...]
  239. [STRICTNESS <mode>]
  240. [ENV_MODE <mode>]
  241. [PC_LIBDIR <path>...]
  242. [PC_PATH <path>...]
  243. [DISABLE_UNINSTALLED <bool>]
  244. [PC_SYSROOT_DIR <path>]
  245. [TOP_BUILD_DIR <path>])
  246. ``POPULATE`` enables manual control of resolution of a given package's
  247. ``Requires`` list without importing onto a native CMake target. Once populated,
  248. a package and its dependencies will be used for resolution of all future
  249. ``POPULATE`` and ``IMPORT`` commands.
  250. A ``PKGCONFIG_<package>_FOUND`` variable will be set to indicate whether the
  251. package was found.
  252. .. signature::
  253. cmake_pkg_config(IMPORT <package> [<version>] [...])
  254. .. versionadded:: 4.1
  255. Import a pkg-config target as a CMake :prop_tgt:`IMPORTED` target
  256. .. code-block:: cmake
  257. cmake_pkg_config(IMPORT <package> [<version>]
  258. [REQUIRED] [EXACT] [QUIET]
  259. [BIND_PC_REQUIRES <<name>=<target>>...]
  260. [STRICTNESS <mode>]
  261. [ENV_MODE <mode>]
  262. [PC_LIBDIR <path>...]
  263. [PC_PATH <path>...]
  264. [DISABLE_UNINSTALLED <bool>]
  265. [PC_SYSROOT_DIR <path>]
  266. [TOP_BUILD_DIR <path>])
  267. Creates a native CMake ``IMPORTED`` target that can be linked to via
  268. :command:`target_link_libraries`. This new target is named
  269. ``PkgConfig::<package>``.
  270. A ``PKGCONFIG_<package>_FOUND`` variable will be set to indicate whether the
  271. package was found.