Skip to content

Commit

Permalink
Add prefix to pkgconfig root (#14051)
Browse files Browse the repository at this point in the history
* Add prefix var to pkg-config alias and root pkgs

This is useful for meson, to get the path of the installed package

* Add pkg_config_custom_content to root pc file

* Added extra check. Minor changes

---------

Co-authored-by: Francisco Ramirez de Anton <franchuti688@gmail.com>
  • Loading branch information
bruchar1 and franramirez688 authored Jun 15, 2023
1 parent 26f6f19 commit 302d079
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
27 changes: 19 additions & 8 deletions conan/tools/gnu/pkgconfigdeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ class _PCContentGenerator:
""")

shortened_template = textwrap.dedent("""\
prefix={{ prefix_path }}
{% if pkg_config_custom_content %}
# Custom PC content
{{ pkg_config_custom_content }}
{% endif %}
Name: {{ name }}
Description: {{ description }}
Version: {{ version }}
Expand All @@ -163,15 +169,17 @@ def __init__(self, conanfile, dep):
self._conanfile = conanfile
self._dep = dep

def content(self, info):
assert isinstance(info, _PCInfo) and info.cpp_info is not None

def _get_prefix_path(self):
# If editable, package_folder can be None
root_folder = self._dep.recipe_folder if self._dep.package_folder is None \
else self._dep.package_folder
version = info.cpp_info.get_property("component_version") or self._dep.ref.version
return root_folder.replace("\\", "/")

def content(self, info):
assert isinstance(info, _PCInfo) and info.cpp_info is not None

prefix_path = root_folder.replace("\\", "/")
prefix_path = self._get_prefix_path()
version = info.cpp_info.get_property("component_version") or self._dep.ref.version
libdirs = _get_formatted_dirs(info.cpp_info.libdirs, prefix_path)
includedirs = _get_formatted_dirs(info.cpp_info.includedirs, prefix_path)
bindirs = _get_formatted_dirs(info.cpp_info.bindirs, prefix_path)
Expand Down Expand Up @@ -199,8 +207,11 @@ def content(self, info):

def shortened_content(self, info):
assert isinstance(info, _PCInfo)

custom_content = info.cpp_info.get_property("pkg_config_custom_content") if info.cpp_info \
else None
context = {
"prefix_path": self._get_prefix_path(),
"pkg_config_custom_content": custom_content,
"name": info.name,
"description": info.description,
"version": self._dep.ref.version,
Expand Down Expand Up @@ -351,8 +362,8 @@ def _update_pc_files(info):
# Issue related: https://github.com/conan-io/conan/issues/10341
pkg_name = _get_package_name(self._dep, self._build_context_suffix)
if f"{pkg_name}.pc" not in pc_files:
package_info = _PCInfo(pkg_name, pkg_requires, f"Conan package: {pkg_name}", None,
_get_package_aliases(self._dep))
package_info = _PCInfo(pkg_name, pkg_requires, f"Conan package: {pkg_name}",
self._dep.cpp_info, _get_package_aliases(self._dep))
# It'll be enough creating a shortened PC file. This file will be like an alias
pc_files[f"{package_info.name}.pc"] = self._content_generator.shortened_content(package_info)
for alias in package_info.aliases:
Expand Down
36 changes: 31 additions & 5 deletions conans/test/integration/toolchains/gnu/test_pkgconfigdeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,20 @@ def test_pkg_config_name_full_aliases():
"""
client = TestClient()
conanfile = textwrap.dedent("""
import textwrap
from conan import ConanFile
class Recipe(ConanFile):
def package_info(self):
custom_content = textwrap.dedent(\"""
datadir=${prefix}/share
schemasdir=${datadir}/mylib/schemas
\""")
self.cpp_info.set_property("pkg_config_name", "pkg_other_name")
self.cpp_info.set_property("pkg_config_aliases", ["pkg_alias1", "pkg_alias2"])
# Custom content only added to root pc file -> pkg_other_name.pc
self.cpp_info.set_property("pkg_config_custom_content", custom_content)
self.cpp_info.components["cmp1"].libs = ["libcmp1"]
self.cpp_info.components["cmp1"].set_property("pkg_config_name", "compo1")
self.cpp_info.components["cmp1"].set_property("pkg_config_aliases", ["compo1_alias"])
Expand Down Expand Up @@ -421,11 +428,14 @@ def package_info(self):
client.run("install .")

pc_content = client.load("compo1.pc")
prefix = pc_content.splitlines()[0]
assert "Description: Conan component: pkg_other_name-compo1" in pc_content
assert "Requires" not in pc_content

pc_content = client.load("compo1_alias.pc")
content = textwrap.dedent("""\
content = textwrap.dedent(f"""\
{prefix}
Name: compo1_alias
Description: Alias compo1_alias for compo1
Version: 0.3
Expand All @@ -434,11 +444,25 @@ def package_info(self):
assert content == pc_content

pc_content = client.load("pkg_other_name.pc")
assert "Description: Conan package: pkg_other_name" in pc_content
assert "Requires: compo1" in pc_content
content = textwrap.dedent(f"""\
{prefix}
# Custom PC content
datadir=${{prefix}}/share
schemasdir=${{datadir}}/mylib/schemas
Name: pkg_other_name
Description: Conan package: pkg_other_name
Version: 0.3
Requires: compo1
""")
assert content == pc_content

pc_content = client.load("pkg_alias1.pc")
content = textwrap.dedent("""\
content = textwrap.dedent(f"""\
{prefix}
Name: pkg_alias1
Description: Alias pkg_alias1 for pkg_other_name
Version: 0.3
Expand All @@ -447,7 +471,9 @@ def package_info(self):
assert content == pc_content

pc_content = client.load("pkg_alias2.pc")
content = textwrap.dedent("""\
content = textwrap.dedent(f"""\
{prefix}
Name: pkg_alias2
Description: Alias pkg_alias2 for pkg_other_name
Version: 0.3
Expand Down

0 comments on commit 302d079

Please sign in to comment.