Repository: conan-io/conan

Issue:
[bug] pkg_config generator has wrong casing in the requires section
coming from https://github.com/conan-io/conan-center-index/pull/471#issuecomment-565057342

Seems that there is a missing conditional to make the default name `.lower()` here:

https://github.com/conan-io/conan/blob/5336a31744bb114f419ad91310bc3e50dc2eb1cd/conans/client/generators/pkg_config.py#L90

As it was done previously for the file name here:
https://github.com/conan-io/conan/blob/5336a31744bb114f419ad91310bc3e50dc2eb1cd/conans/client/generators/pkg_config.py#L43


Discussion:

Resolving patch:
diff --git a/conans/client/generators/pkg_config.py b/conans/client/generators/pkg_config.py
--- a/conans/client/generators/pkg_config.py
+++ b/conans/client/generators/pkg_config.py
@@ -35,12 +35,7 @@ def compiler(self):
     def content(self):
         ret = {}
         for depname, cpp_info in self.deps_build_info.dependencies:
-            # the name for the pc will be converted to lowercase when cpp_info.name is specified
-            # but with cpp_info.names["pkg_config"] will be literal
-            if cpp_info.name and cpp_info.get_name("pkg_config") != cpp_info.name:
-                name = cpp_info.get_name("pkg_config")
-            else:
-                name = cpp_info.name.lower() if cpp_info.name != depname else depname
+            name = _get_name(depname, cpp_info)
             ret["%s.pc" % name] = self.single_pc_file_contents(name, cpp_info)
         return ret
 
@@ -88,13 +83,24 @@ def single_pc_file_contents(self, name, cpp_info):
              ["-D%s" % d for d in cpp_info.defines]]))
 
         if cpp_info.public_deps:
-            pkg_config_names = [self.deps_build_info[public_dep].get_name("pkg_config") for
-                                public_dep in cpp_info.public_deps]
+            pkg_config_names = []
+            for public_dep in cpp_info.public_deps:
+                pkg_config_names.append(_get_name(public_dep, self.deps_build_info[public_dep]))
             public_deps = " ".join(pkg_config_names)
             lines.append("Requires: %s" % public_deps)
         return "\n".join(lines) + "\n"
 
 
+def _get_name(depname, cpp_info):
+    # the name for the pc will be converted to lowercase when cpp_info.name is specified
+    # but with cpp_info.names["pkg_config"] will be literal
+    if "pkg_config" in cpp_info.names:
+        name = cpp_info.names["pkg_config"]
+    else:
+        name = cpp_info.name.lower() if cpp_info.name != depname else depname
+    return name
+
+
 def _concat_if_not_empty(groups):
     return " ".join([param for group in groups for param in group if param and param.strip()])
 


Test patch:
diff --git a/conans/test/unittests/client/generators/pkg_config_test.py b/conans/test/unittests/client/generators/pkg_config_test.py
--- a/conans/test/unittests/client/generators/pkg_config_test.py
+++ b/conans/test/unittests/client/generators/pkg_config_test.py
@@ -85,6 +85,7 @@ def variables_setup_test(self):
     def pkg_config_custom_names_test(self):
         conanfile = ConanFile(TestBufferConanOutput(), None)
         conanfile.initialize(Settings({}), EnvValues())
+
         ref = ConanFileReference.loads("MyPkg/0.1@lasote/stables")
         cpp_info = CppInfo("dummy_root_folder1")
         cpp_info.name = "my_pkg"
@@ -116,6 +117,25 @@ def pkg_config_custom_names_test(self):
         cpp_info.cxxflags = ["-cxxflag"]
         cpp_info.public_deps = ["MyPkg", "MyPkg1"]
         conanfile.deps_cpp_info.update(cpp_info, ref.name)
+
+        ref = ConanFileReference.loads("zlib/1.2.11@lasote/stable")
+        cpp_info = CppInfo("dummy_root_folder_zlib")
+        cpp_info.name = "ZLIB"
+        cpp_info.defines = ["MYZLIBDEFINE2"]
+        cpp_info.version = "1.2.11"
+        conanfile.deps_cpp_info.update(cpp_info, ref.name)
+
+        ref = ConanFileReference.loads("bzip2/0.1@lasote/stables")
+        cpp_info = CppInfo("dummy_root_folder2")
+        cpp_info.name = "BZip2"
+        cpp_info.names["pkg_config"] = "BZip2"
+        cpp_info.defines = ["MYDEFINE2"]
+        cpp_info.version = "2.3"
+        cpp_info.exelinkflags = ["-exelinkflag"]
+        cpp_info.sharedlinkflags = ["-sharedlinkflag"]
+        cpp_info.cxxflags = ["-cxxflag"]
+        cpp_info.public_deps = ["MyPkg", "MyPkg1", "zlib"]
+        conanfile.deps_cpp_info.update(cpp_info, ref.name)
         generator = PkgConfigGenerator(conanfile)
         files = generator.content
 
@@ -130,7 +150,6 @@ def pkg_config_custom_names_test(self):
 Cflags: -I${includedir} -cxxflag -DMYDEFINE2
 Requires: my_pkg_custom_name my_pkg1_custom_name
 """)
-
         self.assertEqual(files["my_pkg1_custom_name.pc"], """prefix=dummy_root_folder1
 libdir=${prefix}/lib
 includedir=${prefix}/include
@@ -141,7 +160,6 @@ def pkg_config_custom_names_test(self):
 Libs: -L${libdir}
 Cflags: -I${includedir} -Flag1=21 -DMYDEFINE11
 """)
-
         self.assertEqual(files["my_pkg_custom_name.pc"], """prefix=dummy_root_folder1
 libdir=${prefix}/lib
 includedir=${prefix}/include
@@ -151,6 +169,17 @@ def pkg_config_custom_names_test(self):
 Version: 1.3
 Libs: -L${libdir}
 Cflags: -I${includedir} -Flag1=23 -DMYDEFINE1
+""")
+        self.assertEqual(files["BZip2.pc"], """prefix=dummy_root_folder2
+libdir=${prefix}/lib
+includedir=${prefix}/include
+
+Name: BZip2
+Description: Conan package: BZip2
+Version: 2.3
+Libs: -L${libdir} -sharedlinkflag -exelinkflag
+Cflags: -I${includedir} -cxxflag -DMYDEFINE2
+Requires: my_pkg_custom_name my_pkg1_custom_name zlib
 """)
 
     def apple_frameworks_test(self):