zlib.cmake 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright 2017 gRPC authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # The CMakeLists.txt for zlib doesn't propagate include directories
  15. # transitively so `_gRPC_ZLIB_INCLUDE_DIR` should be set for gRPC
  16. # to find header files.
  17. if(gRPC_ZLIB_PROVIDER STREQUAL "module")
  18. if(NOT ZLIB_ROOT_DIR)
  19. set(ZLIB_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/zlib)
  20. endif()
  21. if(EXISTS "${ZLIB_ROOT_DIR}/CMakeLists.txt")
  22. # TODO(jtattermusch): workaround for https://github.com/madler/zlib/issues/218
  23. include_directories("${ZLIB_ROOT_DIR}")
  24. add_subdirectory(${ZLIB_ROOT_DIR} third_party/zlib)
  25. if(TARGET zlibstatic)
  26. set(_gRPC_ZLIB_LIBRARIES zlibstatic)
  27. set(_gRPC_ZLIB_INCLUDE_DIR "${ZLIB_ROOT_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/third_party/zlib")
  28. if(gRPC_INSTALL AND _gRPC_INSTALL_SUPPORTED_FROM_MODULE)
  29. install(TARGETS zlibstatic EXPORT gRPCTargets
  30. RUNTIME DESTINATION ${gRPC_INSTALL_BINDIR}
  31. LIBRARY DESTINATION ${gRPC_INSTALL_LIBDIR}
  32. ARCHIVE DESTINATION ${gRPC_INSTALL_LIBDIR})
  33. endif()
  34. endif()
  35. else()
  36. message(WARNING "gRPC_ZLIB_PROVIDER is \"module\" but ZLIB_ROOT_DIR is wrong")
  37. endif()
  38. if(gRPC_INSTALL AND NOT _gRPC_INSTALL_SUPPORTED_FROM_MODULE)
  39. message(WARNING "gRPC_INSTALL will be forced to FALSE because gRPC_ZLIB_PROVIDER is \"module\" and CMake version (${CMAKE_VERSION}) is less than 3.13.")
  40. set(gRPC_INSTALL FALSE)
  41. endif()
  42. elseif(gRPC_ZLIB_PROVIDER STREQUAL "package")
  43. # zlib installation directory can be configured by setting ZLIB_ROOT
  44. # We allow locating zlib using both "CONFIG" and "MODULE" as the expectation
  45. # is that many Linux systems will have zlib installed via a distribution
  46. # package ("MODULE"), while on Windows the user is likely to have installed
  47. # zlib using cmake ("CONFIG").
  48. # See https://cmake.org/cmake/help/v3.6/module/FindZLIB.html
  49. find_package(ZLIB REQUIRED)
  50. if(TARGET ZLIB::ZLIB)
  51. set(_gRPC_ZLIB_LIBRARIES ZLIB::ZLIB)
  52. else()
  53. set(_gRPC_ZLIB_LIBRARIES ${ZLIB_LIBRARIES})
  54. endif()
  55. set(_gRPC_ZLIB_INCLUDE_DIR ${ZLIB_INCLUDE_DIRS})
  56. set(_gRPC_FIND_ZLIB "if(NOT ZLIB_FOUND)\n find_package(ZLIB)\nendif()")
  57. endif()