Skip to content

Commit c6c8f72

Browse files
committed
Document pkg-config running from package
Signed-off-by: Uilian Ries <uilianries@gmail.com>
1 parent 2e94bde commit c6c8f72

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

reference/tools/gnu/pkgconfig.rst

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ Read a ``pc`` file and access the information:
2525
print(pkg_config.variables['prefix']) # something like'/usr/local'
2626
2727
28-
Use the ``pc`` file information to fill a ``cpp_info`` object:
28+
Using PkgConfig to fill a ``cpp_info`` object
29+
---------------------------------------------
2930

31+
The ``PkgConfig`` class can be used to fill a ``CppInfo`` object with the information that will be consumed by ``PkgConfigDeps`` generator later.
32+
This is a useful feature when packaging a system library that provides a ``.pc`` file, or when a proprietary package has a build system that only outputs ``.pc`` files.
3033

3134
.. code-block:: python
3235
@@ -35,6 +38,45 @@ Use the ``pc`` file information to fill a ``cpp_info`` object:
3538
pkg_config.fill_cpp_info(self.cpp_info, is_system=False, system_libs=["m", "rt"])
3639
3740
41+
However, ``PkgConfig`` will invoke the ``pkg-config`` executable to extract the information from the ``.pc`` file.
42+
The ``pkg-config`` executable must be available in the system path for this case, otherwise, it will fail when installing the consumed package.
43+
44+
45+
Using pkg-config from Conan package instead of system
46+
-----------------------------------------------------
47+
48+
.. include:: ../../../common/experimental_warning.inc
49+
50+
In case not having ``pkg-config`` available in the system, it is possible to use the ``pkg-config`` executable provided by a Conan package:
51+
52+
.. code-block:: python
53+
54+
import os
55+
from conan import ConanFile
56+
from conan.tools.gnu import PkgConfig
57+
from conan.tools import CppInfo
58+
59+
class Pkg(ConanFile):
60+
61+
def tool_requires(self):
62+
self.requires("pkgconf/[*]")
63+
64+
...
65+
66+
def package(self):
67+
pkg_config = PkgConfig(self, "libastral", pkg_config_path=".")
68+
cpp_info = CppInfo(self)
69+
pkg_config.fill_cpp_info(cpp_info, is_system=False, system_libs=["m", "rt"])
70+
cpp_info.save(os.path.join(self.package_folder, "cpp_info.json"))
71+
72+
def package_info(self):
73+
self.cpp_info = CppInfo(self).load(os.path.join(self.package_folder, "cpp_info.json"))
74+
75+
76+
The ``pkg-config`` executable provided by the Conan package ``pkgconf`` will be invoked only when creating the Conan binary package.
77+
The ``.pc`` information will be extracted from the ``cpp_info.json`` file located in the package folder, it will fill the ``self.cpp_info`` object.
78+
This way, the ``PkgConfig`` will not need to invoke the ``pkg-config`` executable again to extract the information from the ``.pc`` file,
79+
when consuming the package.
3880

3981
Reference
4082
---------

0 commit comments

Comments
 (0)