Repository: conan-io/conan-center-index

Issue:
[package] grpc/1.37.1: TLS doesn't work because it links against grpc++_unsecure instead of grpc++
### Package and Environment Details
  * Package Name/Version: **grpc/1.37.1**
  * Operating System+version: **Windows 10**
  * Compiler+version: **Visual Studio 16**
  * Conan version: **conan 1.35.2**

### Details
When this package is used, it makes the application to link against unsecure and secure versions of Grpc libraries simultaneously, which breaks TLS functionality. When you try to use TLS you get the following error in the console:

```
D0518 10:47:09.802000000 30164 init_unsecure.cc:30] Using insecure gRPC build. Security handshakers will not be invoked even if secure credentials are used.
```

### Steps to reproduce
Add this conan package as a dependency and look at the generated `conanbuildinfo.cmake` file's `CONAN_LIBS_GRPC` variable, it will contain secure and unsecure versions of gRPC libs.

### Cmake logs
[cmake generate cache output.txt](https://github.com/conan-io/conan-center-index/files/6499795/cmake.generate.cache.output.txt)

### Workaround
Look at an [answer from @drmacdon](https://github.com/conan-io/conan-center-index/issues/5551#issuecomment-981263965)


Discussion:
I am facing the same issue with grpc 1.37.0 and 1.38.0.
I think it would make sense to introduce some option so conan package can be configured accordingly.
same here. Main problem in CONAN_PKG:: package, another and IMHO simpler workaround is to use "cmake_find_package[_multi]" generator and standard package names like:
```
find_package(gRPC CONFIG REQUIRED)
target_link_libraries(... gRPC::grpc++ gRPC::grpc++_reflection gRPC::gpr gRPC::grpc)
```

ps. there are special integration for such generators... so better to check conan docs
A work-around is to filter any grpc unsecure target references from the INTERFACE_LINK_LIBRARIES.

```cmake
  find_package(gRPC CONFIG REQUIRED) # grpc::grpc

  # gRPC produces an unsecure library which is useful when not leveraging TLS/SSL but
  # since that library provides the "void grpc_security_init(void)" symbol if it appears
  # first on the link line it will link the init_unsecure.cc definition rather then the
  # init_secure.cc definition resulting the error warning message from init_unsecure.cc
  # "Using insecure gRPC build. Security handshakers will not be invoked even if secure 
  # credentials are used." which is visible when launching with GRPC_VERBOSITY=DEBUG
  # and GRPC_TRACE=all flags. Without the logging flags, it was reported as gRPC error 
  # UNAVAILABLE (14): failed to connect to all addresses.
  # The below strips _unsecure libs from link line to ensure linking with secure implementation.
  get_target_property(_libs grpc::grpc INTERFACE_LINK_LIBRARIES)
  list(FILTER _libs EXCLUDE REGEX ".*grpc.*_unsecure.*")
  set_target_properties(grpc::grpc PROPERTIES INTERFACE_LINK_LIBRARIES "${_libs}")
```

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
@@ -26,6 +26,7 @@ class grpcConan(ConanFile):
         "php_plugin": [True, False],
         "python_plugin": [True, False],
         "ruby_plugin": [True, False],
+        "secure": [True, False]
     }
     default_options = {
         "fPIC": True,
@@ -38,6 +39,7 @@ class grpcConan(ConanFile):
         "php_plugin": True,
         "python_plugin": True,
         "ruby_plugin": True,
+        "secure": False,
     }
 
     short_paths = True
@@ -211,13 +213,14 @@ def package_info(self):
         self.cpp_info.components["_grpc"].libs = ["grpc"]
 
         # gRPC::grpc_unsecure
-        self.cpp_info.components["grpc_unsecure"].names["cmake_find_package"] = "grpc_unsecure"
-        self.cpp_info.components["grpc_unsecure"].names["cmake_find_package_multi"] = "grpc_unsecure"
-        self.cpp_info.components["grpc_unsecure"].requires = ["zlib::zlib", "c-ares::cares", "address_sorting", "re2::re2", "upb", "abseil::absl_flat_hash_map", "abseil::absl_inlined_vector", "abseil::absl_statusor", "gpr", "address_sorting", "upb"]
-        if tools.is_apple_os(self.settings.os):
-            self.cpp_info.components["grpc_unsecure"].frameworks = ["CoreFoundation"]
-        self.cpp_info.components["grpc_unsecure"].system_libs = _system_libs
-        self.cpp_info.components["grpc_unsecure"].libs = ["grpc_unsecure"]
+        if not self.options.secure:
+            self.cpp_info.components["grpc_unsecure"].names["cmake_find_package"] = "grpc_unsecure"
+            self.cpp_info.components["grpc_unsecure"].names["cmake_find_package_multi"] = "grpc_unsecure"
+            self.cpp_info.components["grpc_unsecure"].requires = ["zlib::zlib", "c-ares::cares", "address_sorting", "re2::re2", "upb", "abseil::absl_flat_hash_map", "abseil::absl_inlined_vector", "abseil::absl_statusor", "gpr", "address_sorting", "upb"]
+            if tools.is_apple_os(self.settings.os):
+                self.cpp_info.components["grpc_unsecure"].frameworks = ["CoreFoundation"]
+            self.cpp_info.components["grpc_unsecure"].system_libs = _system_libs
+            self.cpp_info.components["grpc_unsecure"].libs = ["grpc_unsecure"]
 
         # gRPC::grpc++
         self.cpp_info.components["grpc++"].names["cmake_find_package"] = "grpc++"
@@ -250,11 +253,12 @@ def package_info(self):
         self.cpp_info.components["grpc++_reflection"].libs = ["grpc++_reflection"]
 
         # gRPC::grpc++_unsecure
-        self.cpp_info.components["grpc++_unsecure"].names["cmake_find_package"] = "grpc++_unsecure"
-        self.cpp_info.components["grpc++_unsecure"].names["cmake_find_package_multi"] = "grpc++_unsecure"
-        self.cpp_info.components["grpc++_unsecure"].requires = ["protobuf::libprotobuf", "grpc_unsecure"]
-        self.cpp_info.components["grpc++_unsecure"].system_libs = _system_libs
-        self.cpp_info.components["grpc++_unsecure"].libs = ["grpc++_unsecure"]
+        if not self.options.secure:
+            self.cpp_info.components["grpc++_unsecure"].names["cmake_find_package"] = "grpc++_unsecure"
+            self.cpp_info.components["grpc++_unsecure"].names["cmake_find_package_multi"] = "grpc++_unsecure"
+            self.cpp_info.components["grpc++_unsecure"].requires = ["protobuf::libprotobuf", "grpc_unsecure"]
+            self.cpp_info.components["grpc++_unsecure"].system_libs = _system_libs
+            self.cpp_info.components["grpc++_unsecure"].libs = ["grpc++_unsecure"]
 
         # gRPC::grpcpp_channelz
         self.cpp_info.components["grpcpp_channelz"].names["cmake_find_package"] = "grpcpp_channelz"


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
@@ -51,5 +51,9 @@ include_directories("${CMAKE_CURRENT_BINARY_DIR}")
 foreach(_target greeter_client_server)
 # greeter_async_client greeter_async_server)
   add_executable(${_target} "${_target}.cc")
-  target_link_libraries(${_target} gRPC::grpc++_unsecure hw_proto_obj)
+  if(NOT SECURE)
+    target_link_libraries(${_target} gRPC::grpc++_unsecure hw_proto_obj)
+  else()
+    target_link_libraries(${_target} gRPC::grpc++ hw_proto_obj)
+  endif()
 endforeach()
diff --git a/recipes/grpc/all/test_package/conanfile.py b/recipes/grpc/all/test_package/conanfile.py
--- a/recipes/grpc/all/test_package/conanfile.py
+++ b/recipes/grpc/all/test_package/conanfile.py
@@ -12,6 +12,7 @@ def build_requirements(self):
 
     def build(self):
         cmake = CMake(self)
+        cmake.definitions["SECURE"] = self.options["grpc"].secure
         cmake.configure()
         cmake.build()