Repository: conan-io/conan-center-index

Issue:
[package] googleapis/cci.*: missing dependencies between googleapis libraries
### Description

The `googleapis` package includes multiple libraries, but does not have the dependencies between these libraries.  AFAICT, the generated rules to compile these libraries include these dependencies, but the generated `Findgoogleapis*` files do not preserve these dependencies.

I can try to prepare a PR to solve this problem.  It seems that it would require defining a component for each library, with the right dependencies for each.

### Package and Environment Details

* Package Name/Version: **googleapis/cci.20220711**
* Operating System+version: **Linux**
* Compiler+version: **GCC 12**
* Conan version: **conan 1.55.0**
* Python version: **Python 3.10.9**


### Conan profile

Configuration for profile default:

[settings]
os=Linux
os_build=Linux
arch=x86_64
arch_build=x86_64
compiler=gcc
compiler.version=12
compiler.libcxx=libstdc++
build_type=Release
[options]
[conf]
[build_requires]
[env]


### Steps to reproduce

Apply the following patch and then test with `conan test test_package googleapis/cci.20220711@`

```patch
diff --git a/recipes/googleapis/all/test_package/test_package.cpp b/recipes/googleapis/all/test_package/test_package.cpp
index 32f0b086e..3b9a08aef 100644
--- a/recipes/googleapis/all/test_package/test_package.cpp
+++ b/recipes/googleapis/all/test_package/test_package.cpp
@@ -1,14 +1,13 @@
 #include <iostream>
-#include "google/type/color.pb.h"
+#include <google/bigtable/v2/bigtable.pb.h>
 
 int main() {
     std::cout << "Conan - test package for googleapis\n";
 
-    google::type::Color c;
-    c.set_red(255);
-    c.set_blue(255);
+    google::bigtable::v2::CheckAndMutateRowRequest request;
+    request.set_table_name("projects/my-project/instances/my-instance/tables/my-table");
 
-    std::cout << c.DebugString();
+    std::cout << request.DebugString();
 
     return 0;
 }
```


### Logs

<details><summary>Click to expand log</summary>

```
conan test test_package googleapis/cci.20220711@
Configuration:
[settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=gcc
compiler.libcxx=libstdc++
compiler.version=12
os=Linux
os_build=Linux
[options]
[build_requires]
[env]

googleapis/cci.20220711 (test package): Installing package
Requirements
    googleapis/cci.20220711 from 'conancenter' - Cache
    protobuf/3.21.4 from 'conancenter' - Cache
    zlib/1.2.13 from 'conancenter' - Cache
Packages
    googleapis/cci.20220711:1f2fc6cba1a905f299dfce1a8bdf819ef1ecb710 - Cache
    protobuf/3.21.4:d403f0cadc6326871b16f4359bea9431daa4a81a - Cache
    zlib/1.2.13:2a19826344ff00be1c04403f2f8e7008ed3a7cc6 - Cache

Installing (downloading, building) binaries...
zlib/1.2.13: Already installed!
protobuf/3.21.4: Already installed!
protobuf/3.21.4: Appending PATH environment variable: /usr/local/google/home/coryan/.conan/data/protobuf/3.21.4/_/_/package/d403f0cadc6326871b16f4359bea9431daa4a81a/bin
googleapis/cci.20220711: Already installed!
googleapis/cci.20220711 (test package): Generator 'VirtualRunEnv' calling 'generate()'
googleapis/cci.20220711 (test package): Generator txt created conanbuildinfo.txt
googleapis/cci.20220711 (test package): WARN: Using the new toolchains and generators without specifying a build profile (e.g: -pr:b=default) is discouraged and might cause failures and unexpected behavior
googleapis/cci.20220711 (test package): Generator 'CMakeDeps' calling 'generate()'
googleapis/cci.20220711 (test package): Calling generate()
googleapis/cci.20220711 (test package): WARN: Using the new toolchains and generators without specifying a build profile (e.g: -pr:b=default) is discouraged and might cause failures and unexpected behavior
googleapis/cci.20220711 (test package): Preset 'release' added to CMakePresets.json. Invoke it manually using 'cmake --preset release'
googleapis/cci.20220711 (test package): If your CMake version is not compatible with CMakePresets (<3.19) call cmake like: 'cmake <path> -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=/usr/local/google/home/coryan/cci-develop/recipes/googleapis/all/test_package/build/generators/conan_toolchain.cmake -DCMAKE_POLICY_DEFAULT_CMP0091=NEW -DCMAKE_BUILD_TYPE=Release'
googleapis/cci.20220711 (test package): Aggregating env generators
googleapis/cci.20220711 (test package): Generated conaninfo.txt
googleapis/cci.20220711 (test package): Generated graphinfo
Using lockfile: '/usr/local/google/home/coryan/cci-develop/recipes/googleapis/all/test_package/build/generators/conan.lock'
Using cached profile from lockfile
googleapis/cci.20220711 (test package): Calling build()
googleapis/cci.20220711 (test package): CMake command: cmake -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE="/usr/local/google/home/coryan/cci-develop/recipes/googleapis/all/test_package/build/generators/conan_toolchain.cmake" -DCMAKE_POLICY_DEFAULT_CMP0091="NEW" -DCMAKE_BUILD_TYPE="Release" "/usr/local/google/home/coryan/cci-develop/recipes/googleapis/all/test_package/."
-- Using Conan toolchain: /usr/local/google/home/coryan/cci-develop/recipes/googleapis/all/test_package/build/generators/conan_toolchain.cmake
-- Conan: Target declared 'googleapis::googleapis'
-- Conan: Component target declared 'protobuf::libprotobuf'
-- Conan: Component target declared 'protobuf::libprotoc'
-- Conan: Target declared 'protobuf::protobuf'
-- Conan: Target declared 'ZLIB::ZLIB'
-- Conan: Including build module from '/usr/local/google/home/coryan/.conan/data/protobuf/3.21.4/_/_/package/d403f0cadc6326871b16f4359bea9431daa4a81a/lib/cmake/protobuf/protobuf-generate.cmake'
-- Conan: Including build module from '/usr/local/google/home/coryan/.conan/data/protobuf/3.21.4/_/_/package/d403f0cadc6326871b16f4359bea9431daa4a81a/lib/cmake/protobuf/protobuf-module.cmake'
-- Conan: Including build module from '/usr/local/google/home/coryan/.conan/data/protobuf/3.21.4/_/_/package/d403f0cadc6326871b16f4359bea9431daa4a81a/lib/cmake/protobuf/protobuf-options.cmake'
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/google/home/coryan/cci-develop/recipes/googleapis/all/test_package/build/Release
googleapis/cci.20220711 (test package): CMake command: cmake --build "/usr/local/google/home/coryan/cci-develop/recipes/googleapis/all/test_package/build/Release" '--' '-j96'
Consolidate compiler generated dependencies of target test_package
[ 50%] Building CXX object CMakeFiles/test_package.dir/test_package.cpp.o
[100%] Linking CXX executable test_package
/usr/bin/ld: /usr/local/google/home/coryan/.conan/data/googleapis/cci.20220711/_/_/package/1f2fc6cba1a905f299dfce1a8bdf819ef1ecb710/lib/libgoogle_bigtable_v2_bigtable_proto.a(bigtable.pb.cc.o):(.data.rel.ro+0x120): undefined reference to `descriptor_table_google_2fapi_2fannotations_2eproto'
/usr/bin/ld: /usr/local/google/home/coryan/.conan/data/googleapis/cci.20220711/_/_/package/1f2fc6cba1a905f299dfce1a8bdf819ef1ecb710/lib/libgoogle_bigtable_v2_bigtable_proto.a(bigtable.pb.cc.o):(.data.rel.ro+0x128): undefined reference to `descriptor_table_google_2fapi_2fclient_2eproto'
/usr/bin/ld: /usr/local/google/home/coryan/.conan/data/googleapis/cci.20220711/_/_/package/1f2fc6cba1a905f299dfce1a8bdf819ef1ecb710/lib/libgoogle_bigtable_v2_bigtable_proto.a(bigtable.pb.cc.o):(.data.rel.ro+0x130): undefined reference to `descriptor_table_google_2fapi_2ffield_5fbehavior_2eproto'
/usr/bin/ld: /usr/local/google/home/coryan/.conan/data/googleapis/cci.20220711/_/_/package/1f2fc6cba1a905f299dfce1a8bdf819ef1ecb710/lib/libgoogle_bigtable_v2_bigtable_proto.a(bigtable.pb.cc.o):(.data.rel.ro+0x138): undefined reference to `descriptor_table_google_2fapi_2fresource_2eproto'
/usr/bin/ld: /usr/local/google/home/coryan/.conan/data/googleapis/cci.20220711/_/_/package/1f2fc6cba1a905f299dfce1a8bdf819ef1ecb710/lib/libgoogle_bigtable_v2_bigtable_proto.a(bigtable.pb.cc.o):(.data.rel.ro+0x140): undefined reference to `descriptor_table_google_2fapi_2frouting_2eproto'
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/test_package.dir/build.make:395: test_package] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/test_package.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2```

</details>



Discussion:

Resolving patch:
diff --git a/recipes/googleapis/all/conanfile.py b/recipes/googleapis/all/conanfile.py
--- a/recipes/googleapis/all/conanfile.py
+++ b/recipes/googleapis/all/conanfile.py
@@ -16,6 +16,7 @@
 
 required_conan_version = ">=1.45.0"
 
+
 class GoogleAPIS(ConanFile):
     name = "googleapis"
     description = "Public interface definitions of Google APIs"
@@ -38,9 +39,12 @@ class GoogleAPIS(ConanFile):
 
     def export_sources(self):
         self.copy("CMakeLists.txt")
+        for patch in self.conan_data.get("patches", {}).get(self.version, []):
+            self.copy(patch["patch_file"])
 
     def source(self):
         get(self, **self.conan_data["sources"][str(self.version)], destination=self.source_folder, strip_root=True)
+        self._patch_sources()
 
     def config_options(self):
         if self.settings.os == "Windows":
@@ -62,9 +66,6 @@ def validate(self):
         if self.options.shared and not self.options["protobuf"].shared:
             raise ConanInvalidConfiguration("If built as shared, protobuf must be shared as well. Please, use `protobuf:shared=True`")
 
-    def requirements(self):
-        self.requires('protobuf/3.21.4')
-
     @property
     def _cmake_new_enough(self):
         try:
@@ -79,6 +80,9 @@ def _cmake_new_enough(self):
         else:
             return True
 
+    def requirements(self):
+        self.requires('protobuf/3.21.4')
+
     def build_requirements(self):
         self.build_requires('protobuf/3.21.4')
         # CMake >= 3.20 is required. There is a proto with dots in the name 'k8s.min.proto' and CMake fails to generate project files
@@ -130,6 +134,7 @@ def deactivate_library(key):
         if Version(self.deps_cpp_info["protobuf"].version) <= "3.21.5" and self.settings.os == "Macos" or \
             self.settings.os == "Android":
             deactivate_library("//google/storagetransfer/v1:storagetransfer_proto")
+            deactivate_library("//google/storagetransfer/v1:storagetransfer_cc_proto")
         #  - Inconvenient macro names from /usr/include/math.h : DOMAIN
         if (self.settings.os == "Linux" and self.settings.compiler == "clang" and self.settings.compiler.libcxx == "libc++") or \
             self.settings.compiler in ["Visual Studio", "msvc"]:
@@ -156,12 +161,24 @@ def deactivate_library(key):
 
     def build(self):
         proto_libraries = self._parse_proto_libraries()
-        with open(os.path.join(self.source_folder, "CMakeLists.txt"), "a", encoding="utf-8") as f:
+        # Use a separate file to host the generated code, which is generated in full each time.
+        # This is safe to call multiple times, for example, if you need to invoke `conan build` more than
+        # once.
+        with open(os.path.join(self.source_folder, "generated_targets.cmake"), "w", encoding="utf-8") as f:
+            f.write("# Generated C++ library targets for googleapis\n")
+            f.write("# DO NOT EDIT - change the generation code in conanfile.py instead\n")
             for it in filter(lambda u: u.is_used, proto_libraries):
                 f.write(it.cmake_content)
         cmake = self._configure_cmake()
         cmake.build()
 
+    def _patch_sources(self):
+        for patch in self.conan_data.get("patches", {}).get(self.version, []):
+            # Using **patch here results in an error with the required `patch_description` field.
+            tools.patch(patch_file=patch['patch_file'])
+
+    _DEPS_FILE = "res/generated_targets.deps"
+
     def package(self):
         copy(self, pattern="LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
         copy(self, pattern="*.proto", src=self.source_folder, dst=os.path.join(self.package_folder, "res"))
@@ -173,11 +190,21 @@ def package(self):
         copy(self, pattern="*.dylib", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
         copy(self, pattern="*.a", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
 
+        with open(os.path.join(self.package_folder, self._DEPS_FILE), "w", encoding="utf-8") as f:
+            for lib in filter(lambda u: u.is_used, self._parse_proto_libraries()):
+                interface = 'LIB' if lib.srcs else 'INTERFACE'
+                f.write(f"{lib.cmake_target} {interface} {','.join(lib.cmake_deps)}\n")
+
     def package_id(self):
         self.info.requires["protobuf"].full_package_mode()
 
     def package_info(self):
-        # We are not creating components, we can just collect the libraries
-        self.cpp_info.libs = tools.collect_libs(self)
-        if self.settings.os == "Linux":
-            self.cpp_info.system_libs.extend(["m"])
+        with open(os.path.join(self.package_folder, self._DEPS_FILE), "r", encoding="utf-8") as f:
+            for line in f.read().splitlines():
+                (name, libtype, deps) = line.rstrip('\n').split(' ')
+                self.cpp_info.components[name].requires = deps.split(',')
+                if libtype == 'LIB':
+                    self.cpp_info.components[name].libs = [name]
+                self.cpp_info.components[name].names["pkg_config"] = name
+                if self.settings.os == "Linux":
+                    self.cpp_info.components[name].system_libs.extend(["m"])


Test patch:
diff --git a/recipes/googleapis/all/test_package/conanfile.py b/recipes/googleapis/all/test_package/conanfile.py
--- a/recipes/googleapis/all/test_package/conanfile.py
+++ b/recipes/googleapis/all/test_package/conanfile.py
@@ -7,7 +7,7 @@
 
 class TestPackageConan(ConanFile):
     settings = "os", "arch", "compiler", "build_type"
-    generators = "CMakeDeps", "VirtualRunEnv"
+    generators = "cmake_find_package", "CMakeDeps", "VirtualRunEnv"
 
     def requirements(self):
         self.requires(self.tested_reference_str)
diff --git a/recipes/googleapis/all/test_package/test_package.cpp b/recipes/googleapis/all/test_package/test_package.cpp
--- a/recipes/googleapis/all/test_package/test_package.cpp
+++ b/recipes/googleapis/all/test_package/test_package.cpp
@@ -1,14 +1,13 @@
 #include <iostream>
-#include "google/type/color.pb.h"
+#include <google/bigtable/v2/bigtable.pb.h>
 
 int main() {
     std::cout << "Conan - test package for googleapis\n";
 
-    google::type::Color c;
-    c.set_red(255);
-    c.set_blue(255);
+    google::bigtable::v2::CheckAndMutateRowRequest request;
+    request.set_table_name("projects/my-project/instances/my-instance/tables/my-table");
 
-    std::cout << c.DebugString();
+    std::cout << request.DebugString();
 
     return 0;
 }