Repository: conan-io/conan
Pull request: Bugfix: PkgConfigDeps generator creates bad `Requires` references
Changelog: Bugfix: Fixed bad `Requires` declaration in `*.pc` files.
Closes: #9631 
Docs: omit

- [x] Refer to the issue that supports this Pull Request.
- [x] If the issue has missing info, explain the purpose/use case/pain/need that covers this Pull Request.
- [x] I've read the [Contributing guide](https://github.com/conan-io/conan/blob/develop/.github/CONTRIBUTING.md).
- [x] I've followed the PEP8 style guides for Python code.
- [ ] I've opened another PR in the Conan docs repo to the ``develop`` branch, documenting this one. 

<sup>**Note:** By default this PR will skip the slower tests and will use a limited set of python versions. Check [here](https://github.com/conan-io/conan/blob/develop/.github/PR_INCREASE_TESTING.md) how to increase the testing level by writing some tags in the current PR body text.</sup>


Problem statement:
new PkgConfigDeps generator creates wrong gtest suite pc files
<!--
  Please don't forget to update the issue title.
  Include all applicable information to help us reproduce your problem.

  To help us debug your issue please explain:
-->

### Environment Details (include every applicable attribute)
  * Operating System+version: Ubuntu 21.04
  * Compiler+version: g++ 11
  * Conan version: 1.40.1
  * Python version: 3.9.5

### Steps to reproduce (Include if Applicable)

Use PkgConfigDeps to download gtest as a dependency and if I set it to install teh pc files in a directory other than the build directory. I get the pc files:

```
gtest.pc
gtest-libgtest.pc
```

Inside gtest.pc I see:

```
Name: gtest
Description: Conan package: gtest
Version: 1.10.0
Requires: libgtest gtest_main gmock gmock_main
```

The libraries are wrong, it should be, for example `gtest-libgtest`, and manually changing the files to have that prefix solves the issue.

This does not happen if I install the files in the build directory using the old pkg_config generator, in that case I do get a plain libgtest.pc file.

Hints from issue discussion:
Hi @Makogan 

Do you mean the line shot be something like?

```
Requires: gtest-libgtest gtest-gtest_main gtest-gmock gtest-gmock_main
```
So it includes the other .pc file as transitive dependency automatically?

Yes, this seems a bug, lets try to fix it for next release. Thanks for reporting!
Btw, it could be something missing in the recipe itself, needs to be checked. I see something like:
```python
 self.cpp_info.components["libgtest"].names["cmake_find_package"] = "gtest"
        self.cpp_info.components["libgtest"].names["cmake_find_package_multi"] = "gtest"
        self.cpp_info.components["libgtest"].libs = ["gtest{}".format(self._postfix)]
```

Which is the legacy way of setting names. It is possible that the new ``PkgConfigDeps`` uses only the new properties or something like that, and the recipe needs to be modernized too.
@memsharded Yes, that's what I mean, all the lib names are missing the prefix, I am not sure about the recipes, I have yet to write one myself so I am not in a good position to give feedback about it.

Commit message: Fixed bad pkg-config require names

Patch to review:
diff --git a/conan/tools/gnu/pkgconfigdeps.py b/conan/tools/gnu/pkgconfigdeps.py
index 13fe1179537..efa9764ffe1 100644
--- a/conan/tools/gnu/pkgconfigdeps.py
+++ b/conan/tools/gnu/pkgconfigdeps.py
@@ -48,6 +48,9 @@ class PkgConfigDeps(object):
     def __init__(self, conanfile):
         self._conanfile = conanfile
 
+    def _get_composed_require_name(self, pkg_name, comp_name):
+        return "%s-%s" % (pkg_name, comp_name)
+
     def _get_require_comp_name(self, dep, req):
         # FIXME: this str() is only needed for python2.7 (unicode values). Remove it for Conan 2.0
         pkg_name = str(dep.ref.name)
@@ -55,7 +58,7 @@ def _get_require_comp_name(self, dep, req):
         # FIXME: it could allow defining requires to not direct dependencies
         req = self._conanfile.dependencies.host[pkg]
         cmp_name = get_component_alias(req, comp_name)
-        return cmp_name
+        return self._get_composed_require_name(pkg, cmp_name)
 
     def _get_components(self, dep):
         ret = []
@@ -75,10 +78,10 @@ def _get_public_require_deps(self, dep):
                 pkg, cmp_name = require.split("::")
                 req = dep.dependencies.direct_host[pkg]
                 public_comp_deps.append(
-                    (get_target_namespace(req), get_component_alias(req, cmp_name)))
+                    self._get_composed_require_name(pkg, get_component_alias(req, cmp_name))
+                )
             else:  # Points to a component of same package
-                public_comp_deps.append((get_target_namespace(dep),
-                                         get_component_alias(dep, require)))
+                public_comp_deps.append(get_component_alias(dep, require))
         return public_comp_deps
 
     @property
@@ -90,22 +93,24 @@ def content(self):
 
             if dep.new_cpp_info.has_components:
                 components = self._get_components(dep)
+                # Adding one *.pc file per component, e.g., pkg-comp1.pc
                 for comp_genname, comp_cpp_info, comp_requires_gennames in components:
-                    pkg_comp_genname = "%s-%s" % (pkg_genname, comp_genname)
+                    pkg_comp_genname = self._get_composed_require_name(pkg_genname, comp_genname)
                     ret["%s.pc" % pkg_comp_genname] = self._pc_file_content(
                         pkg_comp_genname, comp_cpp_info,
-                        comp_requires_gennames, dep.package_folder,
-                        dep.ref.version)
+                        comp_requires_gennames,
+                        dep.package_folder, dep.ref.version)
+                # Adding the pkg *.pc file (including its components as requires if any)
                 comp_gennames = [comp_genname for comp_genname, _, _ in components]
                 if pkg_genname not in comp_gennames:
+                    pkg_requires = (self._get_composed_require_name(pkg_genname, i)
+                                    for i in comp_gennames)
                     ret["%s.pc" % pkg_genname] = self._global_pc_file_contents(pkg_genname,
                                                                                dep,
-                                                                               comp_gennames)
+                                                                               pkg_requires)
             else:
-                require_public_deps = [_d for _, _d in
-                                       self._get_public_require_deps(dep)]
                 ret["%s.pc" % pkg_genname] = self._pc_file_content(pkg_genname, dep.new_cpp_info,
-                                                                   require_public_deps,
+                                                                   self._get_public_require_deps(dep),
                                                                    dep.package_folder,
                                                                    dep.ref.version)
         return ret
diff --git a/conans/test/functional/toolchains/gnu/test_pkgconfigdeps.py b/conans/test/functional/toolchains/gnu/test_pkgconfigdeps.py
index 0ddca780fb1..a8df46eaaf9 100644
--- a/conans/test/functional/toolchains/gnu/test_pkgconfigdeps.py
+++ b/conans/test/functional/toolchains/gnu/test_pkgconfigdeps.py
@@ -285,8 +285,12 @@ def package_info(self):
         """)
     client2.save({"conanfile.txt": conanfile})
     client2.run("install .")
+    pc_content = client2.load("pkg.pc")
+    assert "Requires: pkg-mycomponent" in pc_content
     pc_content = client2.load("pkg-mycomponent.pc")
     assert "Requires: other" in pc_content
+    pc_content = client2.load("pkg-myothercomp.pc")
+    assert "Requires: pkg-mycomponent" in pc_content
 
 
 def test_pkg_getting_public_requires():
@@ -328,8 +332,10 @@ def package_info(self):
     client2.save({"conanfile.txt": conanfile})
     client2.run("install .")
     pc_content = client2.load("pkg.pc")
-    assert "Requires: cmp1" in pc_content
+    assert "Requires: other-cmp1" in pc_content
+    pc_content = client2.load("other.pc")
+    assert "Requires: other-cmp1 other-cmp2 other-cmp3" in pc_content
     assert client2.load("other-cmp1.pc")
     assert client2.load("other-cmp2.pc")
     pc_content = client2.load("other-cmp3.pc")
-    assert "Requires: cmp1" in pc_content
+    assert "Requires: other-cmp1" in pc_content


Code review comments:
diff_hunk: @@ -48,14 +48,17 @@ class PkgConfigDeps(object):
     def __init__(self, conanfile):
         self._conanfile = conanfile
 
+    def _get_composed_require_name(self, pkg_name, comp_name):
path: conan/tools/gnu/pkgconfigdeps.py
text: @user1:
Mark it ``staticmethod``