Repository: conan-io/conan-center-index

Issue:
[package] grpc/all: can't compile by g++ 11
### Package and Environment Details (include every applicable attribute)
  * Package Name/Version: **grpc/all**
  * Operating System+version: **Linux Fedora 34**
  * Compiler+version: **GCC 11**
  * Conan version: **conan 1.41.0**
  * Python version: **Python 3.9.7**


### Conan profile (output of `conan profile show default` or `conan profile show <profile>` if custom profile is in use)
```
[settings]
os=Linux
os_build=Linux
arch=x86_64
arch_build=x86_64
compiler=gcc
compiler.version=11.2
compiler.libcxx=libstdc++11
build_type=Release
[options]
[conf]
[build_requires]
[env]
```

### Steps to reproduce (Include if Applicable)

Compile Error:
```
conan install grpc/1.39.1@ --build=missing
```

Work Fine:
```
 conan install -s compiler.cppstd=14 grpc/1.39.1@ --build=missing
 conan install -s compiler.cppstd=17 grpc/1.39.1@ --build=missing
```

### Logs (Include/Attach if Applicable)
<details><summary>Click to expand log</summary>

```
[  6%] Building CXX object source_subfolder/CMakeFiles/gpr.dir/src/core/lib/gpr/time.cc.o
In file included from /home/toge/.conan/data/abseil/20210324.2/_/_/package/2a63f338fd63cfdf27f3f42261415c769e45e632/include/absl/strings/internal/str_format/output.h:29,
                 from /home/toge/.conan/data/abseil/20210324.2/_/_/package/2a63f338fd63cfdf27f3f42261415c769e45e632/include/absl/strings/internal/str_format/extension.h:28,
                 from /home/toge/.conan/data/abseil/20210324.2/_/_/package/2a63f338fd63cfdf27f3f42261415c769e45e632/include/absl/strings/internal/str_format/arg.h:32,
                 from /home/toge/.conan/data/abseil/20210324.2/_/_/package/2a63f338fd63cfdf27f3f42261415c769e45e632/include/absl/strings/str_format.h:76,
                 from /home/toge/.conan/data/grpc/1.39.1/_/_/build/b759697e35ac036a61c817ce022e43105a1f9e4e/source_subfolder/src/core/lib/gpr/log_linux.cc:44:
/home/toge/.conan/data/abseil/20210324.2/_/_/package/2a63f338fd63cfdf27f3f42261415c769e45e632/include/absl/strings/string_view.h:51:26: error: ‘string_view’ in namespace ‘std’ does not name a type
   51 | using string_view = std::string_view;
      |                          ^~~~~~~~~~~
/home/toge/.conan/data/abseil/20210324.2/_/_/package/2a63f338fd63cfdf27f3f42261415c769e45e632/include/absl/strings/string_view.h:51:21: note: ‘std::string_view’ is only available from C++17 onwards
   51 | using string_view = std::string_view;
      |                     ^~~
/home/toge/.conan/data/abseil/20210324.2/_/_/package/2a63f338fd63cfdf27f3f42261415c769e45e632/include/absl/strings/string_view.h:611:8: error: ‘string_view’ does not name a type
  611 | inline string_view ClippedSubstr(string_view s, size_t pos,
      |        ^~~~~~~~~~~
/home/toge/.conan/data/abseil/20210324.2/_/_/package/2a63f338fd63cfdf27f3f42261415c769e45e632/include/absl/strings/string_view.h:622:11: error: ‘string_view’ does not name a type
  622 | constexpr string_view NullSafeStringView(const char* p) {
      |           ^~~~~~~~~~~
In file included from /home/toge/.conan/data/abseil/20210324.2/_/_/package/2a63f338fd63cfdf27f3f42261415c769e45e632/include/absl/strings/internal/str_format/extension.h:28,
                 from /home/toge/.conan/data/abseil/20210324.2/_/_/package/2a63f338fd63cfdf27f3f42261415c769e45e632/include/absl/strings/internal/str_format/arg.h:32,
                 from /home/toge/.conan/data/abseil/20210324.2/_/_/package/2a63f338fd63cfdf27f3f42261415c769e45e632/include/absl/strings/str_format.h:76,
                 from /home/toge/.conan/data/grpc/1.39.1/_/_/build/b759697e35ac036a61c817ce022e43105a1f9e4e/source_subfolder/src/core/lib/gpr/log_linux.cc:44:
/home/toge/.conan/data/abseil/20210324.2/_/_/package/2a63f338fd63cfdf27f3f42261415c769e45e632/include/absl/strings/internal/str_format/output.h:43:14: error: ‘string_view’ has not been declared
   43 |   void Write(string_view v);
      |              ^~~~~~~~~~~
/home/toge/.conan/data/abseil/20210324.2/_/_/package/2a63f338fd63cfdf27f3f42261415c769e45e632/include/absl/strings/internal/str_format/output.h:58:14: error: ‘string_view’ has not been declared
   58 |   void Write(string_view v);
      |              ^~~~~~~~~~~
```
</details>



Discussion:

I think it might be related to [this](https://github.com/grpc/grpc/blob/a1db97be9094f20bc2d414621dc6b4487427faba/CMakeLists.txt#L193-L199) and [this](https://github.com/conan-io/conan-center-index/blob/9a33af9891e2321d7bf1db43998e5ea2c95aef0f/recipes/abseil/all/conanfile.py#L68-L80).
Looks like gRPC defaults to C++11, which of course doesn't has `std::string_view`, and since now GCC 11 now defaults to C++17 abseil was built to use that instead of the it's polyfill.

Resolving patch:
diff --git a/recipes/grpc/all/conanfile.py b/recipes/grpc/all/conanfile.py
--- a/recipes/grpc/all/conanfile.py
+++ b/recipes/grpc/all/conanfile.py
@@ -11,7 +11,7 @@ class grpcConan(ConanFile):
     url = "https://github.com/conan-io/conan-center-index"
     homepage = "https://github.com/grpc/grpc"
     license = "Apache-2.0"
-    exports_sources = ["CMakeLists.txt", "cmake/*"]
+    exports_sources = ["CMakeLists.txt", "cmake/*", "patches/**"]
     generators = "cmake", "cmake_find_package", "cmake_find_package_multi"
     short_paths = True
 
@@ -77,6 +77,9 @@ def validate(self):
             if compiler_version < 14:
                 raise ConanInvalidConfiguration("gRPC can only be built with Visual Studio 2015 or higher.")
 
+        if self.settings.compiler.get_safe("cppstd"):
+            tools.check_min_cppstd(self, 11)
+
     def source(self):
         tools.get(**self.conan_data["sources"][self.version], destination=self._source_subfolder, strip_root=True)
         # Fix the protoc search path for cross compiling
@@ -126,10 +129,21 @@ def _configure_cmake(self):
         self._cmake.definitions["gRPC_BUILD_GRPC_PYTHON_PLUGIN"] = self.options.python_plugin
         self._cmake.definitions["gRPC_BUILD_GRPC_RUBY_PLUGIN"] = self.options.ruby_plugin
 
+        # Some compilers will start defaulting to C++17, so abseil will be built using C++17
+        # gRPC will force C++11 if CMAKE_CXX_STANDARD is not defined
+        # So, if settings.compiler.cppstd is not defined there will be a mismatch
+        if not tools.valid_min_cppstd(self, 11):
+            self._cmake.definitions["CMAKE_CXX_STANDARD"] = 11
+
         self._cmake.configure(build_folder=self._build_subfolder)
         return self._cmake
 
+    def _patch_sources(self):
+        for patch in self.conan_data.get("patches", {}).get(self.version, []):
+            tools.patch(**patch)
+
     def build(self):
+        self._patch_sources()
         cmake = self._configure_cmake()
         cmake.build()
 


Test patch:
diff --git a/recipes/grpc/all/test_package/CMakeLists.txt b/recipes/grpc/all/test_package/CMakeLists.txt
--- a/recipes/grpc/all/test_package/CMakeLists.txt
+++ b/recipes/grpc/all/test_package/CMakeLists.txt
@@ -1,14 +1,9 @@
-cmake_minimum_required(VERSION 3.1)
+cmake_minimum_required(VERSION 3.8)
 project(test_package)
 
 include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
 conan_basic_setup()
 
-if(WIN32)
-  add_definitions(-D_WIN32_WINNT=0x600)
-endif()
-set(CMAKE_CXX_STANDARD 11)
-
 # Find Protobuf installation
 
 set(protobuf_MODULE_COMPATIBLE TRUE)
@@ -47,6 +42,7 @@ add_custom_command(
 
 # Build generated sources as an object library
 add_library(hw_proto_obj OBJECT ${hw_proto_srcs} ${hw_grpc_srcs})
+target_compile_features(hw_proto_obj PUBLIC cxx_std_11)
 
 # Include generated *.pb.h files
 include_directories("${CMAKE_CURRENT_BINARY_DIR}")
@@ -54,6 +50,6 @@ include_directories("${CMAKE_CURRENT_BINARY_DIR}")
 # Targets greeter_[async_](client)
 foreach(_target greeter_client_server)
 # greeter_async_client greeter_async_server)
-  add_executable(${_target} "${_target}.cc" $<TARGET_OBJECTS:hw_proto_obj>)
-  target_link_libraries(${_target} gRPC::grpc++_unsecure)
+  add_executable(${_target} "${_target}.cc")
+  target_link_libraries(${_target} gRPC::grpc++_unsecure hw_proto_obj)
 endforeach()