cmake_pkg_config.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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::[<prefix>_]<package>`` and are exposed
  27. via the :prop_tgt:`INTERFACE_LINK_LIBRARIES` target property of an
  28. ``IMPORT``-generated 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. ``PREFIX <name>``
  138. Specifying a prefix creates an independent collection of pkg-config targets
  139. separate from previously populated targets. This enables multiple version of
  140. a given package to co-exist, for example packages from different sysroots.
  141. The default prefix is an empty string.
  142. ``BIND_PC_REQUIRES <<name>=<target>>...``
  143. A list of ``<name>=<target>`` pairs, the ``name`` is a package name as it
  144. appears in the ``Requires`` list of a pkg-config file and the ``target`` is a
  145. CMake-native target name (not a pkg-config target).
  146. When a given package name appears in the ``Requires`` list of a package, it
  147. will be fulfilled with the associated CMake target. This behavior applies to
  148. all dependencies in the pkg-config graph that have not been previously
  149. populated.
  150. Signatures
  151. ^^^^^^^^^^
  152. .. signature::
  153. cmake_pkg_config(EXTRACT <package> [<version>] [...])
  154. .. versionadded:: 3.31
  155. Extract the contents of the package into variables.
  156. .. code-block:: cmake
  157. cmake_pkg_config(EXTRACT <package> [<version>]
  158. [REQUIRED] [EXACT] [QUIET]
  159. [SYSTEM_INCLUDE_DIRS <path>...]
  160. [SYSTEM_LIBRARY_DIRS <path>...]
  161. [ALLOW_SYSTEM_INCLUDES <bool>]
  162. [ALLOW_SYSTEM_LIBS <bool>]
  163. [STRICTNESS <mode>]
  164. [ENV_MODE <mode>]
  165. [PC_LIBDIR <path>...]
  166. [PC_PATH <path>...]
  167. [DISABLE_UNINSTALLED <bool>]
  168. [PC_SYSROOT_DIR <path>]
  169. [TOP_BUILD_DIR <path>])
  170. The following variables will be populated from the contents of package file:
  171. ==================================== ====== ========================================================================================
  172. Variable Type Definition
  173. ==================================== ====== ========================================================================================
  174. ``CMAKE_PKG_CONFIG_NAME`` String Value of the ``Name`` keyword
  175. ``CMAKE_PKG_CONFIG_DESCRIPTION`` String Value of the ``Description`` keyword
  176. ``CMAKE_PKG_CONFIG_VERSION`` String Value of the ``Version`` keyword
  177. ``CMAKE_PKG_CONFIG_PROVIDES`` List Value of the ``Provides`` keyword
  178. ``CMAKE_PKG_CONFIG_REQUIRES`` List Value of the ``Requires`` keyword
  179. ``CMAKE_PKG_CONFIG_CONFLICTS`` List Value of the ``Conflicts`` keyword
  180. ``CMAKE_PKG_CONFIG_CFLAGS`` String Value of the ``CFlags`` / ``Cflags`` keyword
  181. ``CMAKE_PKG_CONFIG_INCLUDES`` List All ``-I`` prefixed flags from ``CMAKE_PKG_CONFIG_CFLAGS``
  182. ``CMAKE_PKG_CONFIG_COMPILE_OPTIONS`` List All flags not prefixed with ``-I`` from ``CMAKE_PKG_CONFIG_CFLAGS``
  183. ``CMAKE_PKG_CONFIG_LIBS`` String Value of the ``Libs`` keyword
  184. ``CMAKE_PKG_CONFIG_LIBDIRS`` List All ``-L`` prefixed flags from ``CMAKE_PKG_CONFIG_LIBS``
  185. ``CMAKE_PKG_CONFIG_LIBNAMES`` List All ``-l`` prefixed flags from ``CMAKE_PKG_CONFIG_LIBS``
  186. ``CMAKE_PKG_CONFIG_LINK_OPTIONS`` List All flags not prefixed with ``-L`` or ``-l`` from ``CMAKE_PKG_CONFIG_LIBS``
  187. ``CMAKE_PKG_CONFIG_*_PRIVATE`` \* ``CFLAGS`` / ``LIBS`` / ``REQUIRES`` and derived, but in their ``.private`` suffix forms
  188. ==================================== ====== ========================================================================================
  189. ``SYSTEM_INCLUDE_DIRS``
  190. Overrides the "system" directories for the purpose of flag mangling include
  191. directories in ``CMAKE_PKG_CONFIG_CFLAGS`` and derived variables.
  192. When this option is not provided, the default directories are provided by the
  193. first available of the following values:
  194. #. ``CMAKE_PKG_CONFIG_SYS_INCLUDE_DIRS``
  195. #. The ``PKG_CONFIG_SYSTEM_INCLUDE_PATH`` environment variable
  196. #. The output of ``pkgconf --variable pc_system_includedirs pkg-config``
  197. #. A platform-dependent default value
  198. Additionally, when the ``ENV_MODE`` is ``PKGCONF`` the
  199. ``CMAKE_PKG_CONFIG_PKGCONF_INCLUDES`` variable will be concatenated to the
  200. list if available. If it is not available, the following environment variables
  201. will be queried and concatenated:
  202. * ``CPATH``
  203. * ``C_INCLUDE_PATH``
  204. * ``CPLUS_INCLUDE_PATH``
  205. * ``OBJC_INCLUDE_PATH``
  206. * ``INCLUDE`` (Windows Only)
  207. ``SYSTEM_LIBRARY_DIRS``
  208. Overrides the "system" directories for the purpose of flag mangling library
  209. directories in ``CMAKE_PKG_CONFIG_LIBS`` and derived variables.
  210. When this option is not provided, the default directories are provided by the
  211. first available of the following values:
  212. #. ``CMAKE_PKG_CONFIG_SYS_LIB_DIRS``
  213. #. The ``PKG_CONFIG_SYSTEM_LIBRARY_PATH`` environment variable
  214. #. The output of ``pkgconf --variable pc_system_libdirs pkg-config``
  215. #. A platform-dependent default value
  216. Additionally, when the ``ENV_MODE`` is ``PKGCONF`` the
  217. ``CMAKE_PKG_CONFIG_PKGCONF_LIB_DIRS`` variable will be concatenated to the
  218. list if available. If it is not available, the ``LIBRARY_PATH`` environment
  219. variable will be queried and concatenated.
  220. ``ALLOW_SYSTEM_INCLUDES``
  221. Preserves "system" directories during flag mangling of include directories
  222. in ``CMAKE_PKG_CONFIG_CFLAGS`` and derived variables.
  223. When this option is not provided, the default value is determined by the first
  224. available of the following values:
  225. #. ``CMAKE_PKG_CONFIG_ALLOW_SYS_INCLUDES``
  226. #. If the ``PKG_CONFIG_ALLOW_SYSTEM_CFLAGS`` environment variable is defined
  227. the flags are preserved, otherwise they are filtered during flag mangling.
  228. ``ALLOW_SYSTEM_LIBS``
  229. Preserves "system" directories during flag mangling of library directories
  230. in ``CMAKE_PKG_CONFIG_LIBS`` and derived variables.
  231. When this option is not provided, the default value is determined by the first
  232. available of the following values:
  233. #. ``CMAKE_PKG_CONFIG_ALLOW_SYS_LIBS``
  234. #. If the ``PKG_CONFIG_ALLOW_SYSTEM_LIBS`` environment variable is defined
  235. the flags are preserved, otherwise they are filtered during flag mangling.
  236. .. signature::
  237. cmake_pkg_config(POPULATE <package> [<version>] [...])
  238. .. versionadded:: 4.1
  239. Populate a package in the pkg-config target namespace
  240. .. code-block:: cmake
  241. cmake_pkg_config(POPULATE <package> [<version>]
  242. [REQUIRED] [EXACT] [QUIET]
  243. [PREFIX <prefix>]
  244. [BIND_PC_REQUIRES <<name>=<target>>...]
  245. [STRICTNESS <mode>]
  246. [ENV_MODE <mode>]
  247. [PC_LIBDIR <path>...]
  248. [PC_PATH <path>...]
  249. [DISABLE_UNINSTALLED <bool>]
  250. [PC_SYSROOT_DIR <path>]
  251. [TOP_BUILD_DIR <path>])
  252. ``POPULATE`` enables manual control of resolution of a given package's
  253. ``Requires`` list without importing onto a native CMake target. Once populated,
  254. a package and its dependencies will be used for resolution of all future
  255. ``POPULATE`` and ``IMPORT`` commands.
  256. A ``PKGCONFIG_<package>_FOUND`` variable will be set to indicate whether the
  257. package was found.
  258. .. signature::
  259. cmake_pkg_config(IMPORT <package> [<version>] [...])
  260. .. versionadded:: 4.1
  261. Import a pkg-config target as a CMake :prop_tgt:`IMPORTED` target
  262. .. code-block:: cmake
  263. cmake_pkg_config(IMPORT <package> [<version>]
  264. [REQUIRED] [EXACT] [QUIET]
  265. [NAME <name>]
  266. [PREFIX <prefix>]
  267. [BIND_PC_REQUIRES <<name>=<target>>...]
  268. [STRICTNESS <mode>]
  269. [ENV_MODE <mode>]
  270. [PC_LIBDIR <path>...]
  271. [PC_PATH <path>...]
  272. [DISABLE_UNINSTALLED <bool>]
  273. [PC_SYSROOT_DIR <path>]
  274. [TOP_BUILD_DIR <path>])
  275. Creates a native CMake ``IMPORTED`` target that can be linked to via
  276. :command:`target_link_libraries`. This new target is named
  277. ``PkgConfig::<package>`` by default.
  278. A ``PKGCONFIG_<package>_FOUND`` variable will be set to indicate whether the
  279. package was found.
  280. ``NAME``
  281. Overrides the name of the created CMake target to ``PkgConfig::<name>``. This
  282. *does not* affect the ``PKGCONFIG_<package>_FOUND`` variable.