Merge branch 'master' into cmake-export-fix
diff --git a/cmake/benchmark.cmake b/cmake/benchmark.cmake
index c628422..753dc06 100644
--- a/cmake/benchmark.cmake
+++ b/cmake/benchmark.cmake
@@ -20,14 +20,17 @@
       add_subdirectory(${BENCHMARK_ROOT_DIR} third_party/benchmark)
       if(TARGET benchmark)
           set(_gRPC_BENCHMARK_LIBRARIES benchmark)
+          set(_gRPC_BENCHMARK_INCLUDE_DIR "${BENCHMARK_ROOT_DIR}/include")
       endif()
   else()
       message(WARNING "gRPC_BENCHMARK_PROVIDER is \"module\" but BENCHMARK_ROOT_DIR is wrong")
   endif()
 elseif("${gRPC_BENCHMARK_PROVIDER}" STREQUAL "package")
-  find_package(benchmark)
+  find_package(benchmark REQUIRED)
   if(TARGET benchmark::benchmark)
     set(_gRPC_BENCHMARK_LIBRARIES benchmark::benchmark)
+    # extract the include dir from target's properties
+    get_target_property(_gRPC_BENCHMARK_INCLUDE_DIR benchmark::benchmark INTERFACE_INCLUDE_DIRECTORIES)
   endif()
   set(_gRPC_FIND_BENCHMARK "if(NOT benchmark_FOUND)\n  find_package(benchmark)\nendif()")
 endif()
diff --git a/cmake/cares.cmake b/cmake/cares.cmake
index 521cf52..53d7582 100644
--- a/cmake/cares.cmake
+++ b/cmake/cares.cmake
@@ -18,11 +18,13 @@
   endif()
   set(CARES_SHARED OFF CACHE BOOL "disable shared library")
   set(CARES_STATIC ON CACHE BOOL "link cares statically")
-  set(CARES_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/cares/cares")
   add_subdirectory(third_party/cares/cares)
+
   if(TARGET c-ares)
     set(_gRPC_CARES_LIBRARIES c-ares)
+    set(_gRPC_CARES_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/cares/cares" "${CMAKE_CURRENT_BINARY_DIR}/third_party/cares/cares")
   endif()
+
   if(gRPC_INSTALL)
     message(WARNING "gRPC_INSTALL will be forced to FALSE because gRPC_CARES_PROVIDER is \"module\"")
     set(gRPC_INSTALL FALSE)
@@ -31,6 +33,7 @@
   find_package(c-ares REQUIRED CONFIG)
   if(TARGET c-ares::cares)
     set(_gRPC_CARES_LIBRARIES c-ares::cares)
+    set(_gRPC_CARES_INCLUDE_DIR ${c-ares_INCLUDE_DIR})
   endif()
   set(_gRPC_FIND_CARES "if(NOT c-ares_FOUND)\n  find_package(c-ares CONFIG)\nendif()")
 endif()
diff --git a/cmake/gflags.cmake b/cmake/gflags.cmake
index 1864bda..f86a141 100644
--- a/cmake/gflags.cmake
+++ b/cmake/gflags.cmake
@@ -17,17 +17,19 @@
     set(GFLAGS_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gflags)
   endif()
   if(EXISTS "${GFLAGS_ROOT_DIR}/CMakeLists.txt")
-      add_subdirectory(${GFLAGS_ROOT_DIR} third_party/gflags)
-      if(TARGET gflags_static)
-          set(_gRPC_GFLAGS_LIBRARIES gflags_static)
-      endif()
+    add_subdirectory(${GFLAGS_ROOT_DIR} third_party/gflags)
+    if(TARGET gflags_static)
+      set(_gRPC_GFLAGS_LIBRARIES gflags_static)
+      set(_gRPC_GFLAGS_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/third_party/gflags/include")
+    endif()
   else()
-      message(WARNING "gRPC_GFLAGS_PROVIDER is \"module\" but GFLAGS_ROOT_DIR is wrong")
+    message(WARNING "gRPC_GFLAGS_PROVIDER is \"module\" but GFLAGS_ROOT_DIR is wrong")
   endif()
 elseif("${gRPC_GFLAGS_PROVIDER}" STREQUAL "package")
-  find_package(gflags)
+  find_package(gflags REQUIRED)
   if(TARGET gflags::gflags)
     set(_gRPC_GFLAGS_LIBRARIES gflags::gflags)
+    set(_gRPC_GFLAGS_INCLUDE_DIR ${GFLAGS_INCLUDE_DIR})
   endif()
   set(_gRPC_FIND_GFLAGS "if(NOT gflags_FOUND)\n  find_package(gflags)\nendif()")
 endif()
diff --git a/cmake/protobuf.cmake b/cmake/protobuf.cmake
index e2206a2..cb799b5 100644
--- a/cmake/protobuf.cmake
+++ b/cmake/protobuf.cmake
@@ -27,7 +27,7 @@
   if(NOT PROTOBUF_ROOT_DIR)
     set(PROTOBUF_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/protobuf)
   endif()
-  set(PROTOBUF_WELLKNOWN_IMPORT_DIR ${PROTOBUF_ROOT_DIR}/src)
+
   if(EXISTS "${PROTOBUF_ROOT_DIR}/cmake/CMakeLists.txt")
     set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "Link static runtime libraries")
     add_subdirectory(${PROTOBUF_ROOT_DIR}/cmake third_party/protobuf)
@@ -41,6 +41,9 @@
       set(_gRPC_PROTOBUF_PROTOC protoc)
       set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE $<TARGET_FILE:protoc>)
     endif()
+    set(_gRPC_PROTOBUF_INCLUDE_DIR "${PROTOBUF_ROOT_DIR}")
+    # For well-known .proto files distributed with protobuf
+    set(_gRPC_PROTOBUF_WELLKNOWN_INCLUDE_DIR "${PROTOBUF_ROOT_DIR}/src")
   else()
       message(WARNING "gRPC_PROTOBUF_PROVIDER is \"module\" but PROTOBUF_ROOT_DIR is wrong")
   endif()
@@ -50,6 +53,11 @@
   endif()
 elseif("${gRPC_PROTOBUF_PROVIDER}" STREQUAL "package")
   find_package(Protobuf REQUIRED ${gRPC_PROTOBUF_PACKAGE_TYPE})
+
+  # {Protobuf,PROTOBUF}_FOUND is defined based on find_package type ("MODULE" vs "CONFIG").
+  # For "MODULE", the case has also changed between cmake 3.5 and 3.6.
+  # We use the legacy uppercase version for *_LIBRARIES AND *_INCLUDE_DIRS variables
+  # as newer cmake versions provide them too for backward compatibility.
   if(Protobuf_FOUND OR PROTOBUF_FOUND)
     if(TARGET protobuf::${_gRPC_PROTOBUF_LIBRARY_NAME})
       set(_gRPC_PROTOBUF_LIBRARIES protobuf::${_gRPC_PROTOBUF_LIBRARY_NAME})
@@ -58,8 +66,11 @@
     endif()
     if(TARGET protobuf::libprotoc)
       set(_gRPC_PROTOBUF_PROTOC_LIBRARIES protobuf::libprotoc)
+      # extract the include dir from target's properties
+      get_target_property(_gRPC_PROTOBUF_WELLKNOWN_INCLUDE_DIR protobuf::libprotoc INTERFACE_INCLUDE_DIRECTORIES)
     else()
       set(_gRPC_PROTOBUF_PROTOC_LIBRARIES ${PROTOBUF_PROTOC_LIBRARIES})
+      set(_gRPC_PROTOBUF_WELLKNOWN_INCLUDE_DIR ${PROTOBUF_INCLUDE_DIRS})
     endif()
     if(TARGET protobuf::protoc)
       set(_gRPC_PROTOBUF_PROTOC protobuf::protoc)
@@ -68,10 +79,7 @@
       set(_gRPC_PROTOBUF_PROTOC ${PROTOBUF_PROTOC_EXECUTABLE})
       set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE ${PROTOBUF_PROTOC_EXECUTABLE})
     endif()
+    set(_gRPC_PROTOBUF_INCLUDE_DIR ${PROTOBUF_INCLUDE_DIRS})
     set(_gRPC_FIND_PROTOBUF "if(NOT Protobuf_FOUND AND NOT PROTOBUF_FOUND)\n  find_package(Protobuf ${gRPC_PROTOBUF_PACKAGE_TYPE})\nendif()")
   endif()
-  if(PROTOBUF_FOUND)
-    include_directories(${PROTOBUF_INCLUDE_DIRS})
-  endif()
-  set(PROTOBUF_WELLKNOWN_IMPORT_DIR /usr/local/include)
 endif()
diff --git a/cmake/zlib.cmake b/cmake/zlib.cmake
index e324802..e4c94f5 100644
--- a/cmake/zlib.cmake
+++ b/cmake/zlib.cmake
@@ -16,15 +16,15 @@
   if(NOT ZLIB_ROOT_DIR)
     set(ZLIB_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/zlib)
   endif()
-  set(ZLIB_INCLUDE_DIR "${ZLIB_ROOT_DIR}")
   if(EXISTS "${ZLIB_ROOT_DIR}/CMakeLists.txt")
-      # TODO(jtattermusch): workaround for https://github.com/madler/zlib/issues/218
-      include_directories(${ZLIB_INCLUDE_DIR})
+    # TODO(jtattermusch): workaround for https://github.com/madler/zlib/issues/218
+    include_directories("${ZLIB_ROOT_DIR}")
+    add_subdirectory(${ZLIB_ROOT_DIR} third_party/zlib)
 
-      add_subdirectory(${ZLIB_ROOT_DIR} third_party/zlib)
-      if(TARGET zlibstatic)
-          set(_gRPC_ZLIB_LIBRARIES zlibstatic)
-      endif()
+    if(TARGET zlibstatic)
+      set(_gRPC_ZLIB_LIBRARIES zlibstatic)
+      set(_gRPC_ZLIB_INCLUDE_DIR "${ZLIB_ROOT_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/third_party/zlib")
+    endif()
   else()
       message(WARNING "gRPC_ZLIB_PROVIDER is \"module\" but ZLIB_ROOT_DIR is wrong")
   endif()
@@ -34,12 +34,12 @@
   endif()
 elseif("${gRPC_ZLIB_PROVIDER}" STREQUAL "package")
   find_package(ZLIB REQUIRED)
-  
+
   if(TARGET ZLIB::ZLIB)
     set(_gRPC_ZLIB_LIBRARIES ZLIB::ZLIB)
   else()
     set(_gRPC_ZLIB_LIBRARIES ${ZLIB_LIBRARIES})
   endif()
-  
+  set(_gRPC_ZLIB_INCLUDE_DIR ${ZLIB_INCLUDE_DIRS})
   set(_gRPC_FIND_ZLIB "if(NOT ZLIB_FOUND)\n  find_package(ZLIB)\nendif()")
 endif()