Repository: conan-io/conan-center-index

Issue:
[package] google-cloud-cpp/2.x: broken on conan v1 with `os=macOS`, `shared=True`
### Description

Running a build at HEAD produces the following errors: https://github.com/conan-io/conan-center-index/pull/23149#issuecomment-2004990402
https://c3i.jfrog.io/c3i/misc/summary.html?json=https://c3i.jfrog.io/c3i/misc/logs/pr/23149/1-macos-m1-clang/google-cloud-cpp/2.12.0//summary.json

The relevant settings are:
- conan v1
- macOS
- shared=True

It fails when trying to run the `grpc_cpp_plugin`. It cannot find the library `libprotoc`. I assume this has to do with the SIP issues clearing the `DYLD_LIBRARY_PATH` on macOS.

This is blocking the upgrade `google-cloud-cpp` to `2.22.0`.

### Package and Environment Details

google-cloud-cpp

### Conan profile

Configuration (profile_host):
[settings]
arch=armv8
build_type=Debug
compiler=apple-clang
compiler.libcxx=libc++
compiler.version=13.0
os=Macos
[options]
google-cloud-cpp:shared=True
[build_requires]
[env]
[conf]
tools.system.package_manager:mode=install
tools.system.package_manager:sudo=True
tools.apple:sdk_path=/Applications/conan/xcode/13.0/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk

Configuration (profile_build):
[settings]
arch=armv8
arch_build=armv8
build_type=Release
compiler=apple-clang
compiler.libcxx=libc++
compiler.version=13
os=Macos
os_build=Macos
[options]
[build_requires]
[env]

### Steps to reproduce

I cannot reproduce this locally, but modifying the `google-cloud-cpp` (e.g. by adding a comment) and opening a PR at HEAD will run the CI which reveals the problem.

### Logs

https://c3i.jfrog.io/c3i/misc/logs/pr/23149/1-macos-m1-clang/google-cloud-cpp/2.12.0//11eaac8f5ef0ed709285f13c35d5470ac3d26791-build.txt

```
dyld[12597]: Library not loaded: @rpath/libprotoc.32.dylib
  Referenced from: <AC7827B1-65E8-319B-AA17-CE18CCA918F8> /Users/jenkins/workspace/prod-v1/bsr/3770/abcfa/.conan/data/grpc/1.54.3/_/_/package/08b363a449a446ccba121b62ee45677c9adb5c1c/bin/grpc_cpp_plugin
  Reasodyld[12621]: dyld[12631]: Library not loaded: @rpath/libprotoc.32.dylib
  Referenced from: <AC7827B1-65E8-319B-AA17-CE18CCA918F8> /Users/jenkins/workspace/prod-v1/bsr/3770/abcfa/.conan/data/grpc/1.54.3/_/_/package/08b363a449a446ccba121b62ee45677c9adbLibrary not loaded: @rpath/libprdyld[12626]: n: tried: '/Users/jenkins/workspdyld[12622]: dyld[12633]: dyld[12638]: Library not loaded: @rpath/libpr5c1c/bin/grpc_cpp_plugin
  ReasoLibrary not loaded: @rpath/libprLibrary not loaded: @rpath/libprotoc.32.dylib
  Referenced from:Library not loaded: @rpath/libprdyld[12630]: otoc.32.dylib
  Referenced from: <AC7827B1-65E8-319B-AA17-CE18CCLibrary not loaded: @rpath/libprotoc.32.dylib
```


Discussion:

Resolving patch:
diff --git a/recipes/google-cloud-cpp/2.x/conanfile.py b/recipes/google-cloud-cpp/2.x/conanfile.py
--- a/recipes/google-cloud-cpp/2.x/conanfile.py
+++ b/recipes/google-cloud-cpp/2.x/conanfile.py
@@ -3,7 +3,7 @@
 from conan import ConanFile
 from conan.tools.build import check_min_cppstd, cross_building
 from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
-from conan.tools.env import VirtualRunEnv
+from conan.tools.env import VirtualBuildEnv, VirtualRunEnv
 from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, replace_in_file, rmdir
 from conan.tools.microsoft import check_min_vs, is_msvc
 from conan.tools.scm import Version
@@ -72,6 +72,10 @@ class GoogleCloudCppConan(ConanFile):
         "bigquery", "bigtable", "iam", "oauth2", "pubsub", "spanner", "storage",
     }
 
+    @property
+    def _is_legacy_one_profile(self):
+        return not hasattr(self, "settings_build")
+
     def export_sources(self):
         export_conandata_patches(self)
 
@@ -157,7 +161,8 @@ def requirements(self):
 
     def build_requirements(self):
         # For the `grpc-cpp-plugin` executable, and indirectly `protoc`
-        self.tool_requires("grpc/<host_version>")
+        if not self._is_legacy_one_profile:
+            self.tool_requires("grpc/<host_version>")
 
     def generate(self):
         tc = CMakeToolchain(self)
@@ -166,7 +171,9 @@ def generate(self):
         tc.variables["GOOGLE_CLOUD_CPP_ENABLE_WERROR"] = False
         tc.variables["GOOGLE_CLOUD_CPP_ENABLE"] = ",".join(self._components())
         tc.generate()
-        VirtualRunEnv(self).generate(scope="build")
+        VirtualBuildEnv(self).generate()
+        if self._is_legacy_one_profile:
+            VirtualRunEnv(self).generate(scope="build")
         deps = CMakeDeps(self)
         deps.generate()
 


Test patch:
diff --git a/recipes/google-cloud-cpp/2.x/test_package/conanfile.py b/recipes/google-cloud-cpp/2.x/test_package/conanfile.py
--- a/recipes/google-cloud-cpp/2.x/test_package/conanfile.py
+++ b/recipes/google-cloud-cpp/2.x/test_package/conanfile.py
@@ -3,7 +3,7 @@
 from conan import ConanFile
 from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
 from conan.tools.build import can_run
-from conan.tools.env import VirtualRunEnv
+from conan.tools.env import VirtualBuildEnv, VirtualRunEnv
 from conan.tools.scm import Version
 
 
@@ -14,6 +14,10 @@ class TestPackageConan(ConanFile):
     def requirements(self):
         self.requires(self.tested_reference_str)
 
+    @property
+    def _is_legacy_one_profile(self):
+        return not hasattr(self, "settings_build")
+
     def layout(self):
         cmake_layout(self)
     
@@ -30,9 +34,12 @@ def generate(self):
         tc = CMakeToolchain(self)
         tc.variables["WITH_COMPUTE"] = self._supports_compute()
         tc.generate()
+        if self._is_legacy_one_profile:
+            VirtualRunEnv(self).generate(scope="build")
+        else:
+            VirtualBuildEnv(self).generate()
         # Environment so that the compiled test executable can load shared libraries
-        runenv = VirtualRunEnv(self)
-        runenv.generate(scope="run")
+        VirtualRunEnv(self).generate(scope="run")
         deps = CMakeDeps(self)
         deps.generate()