protobuf.cmake 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. if(gRPC_PROTOBUF_PROVIDER STREQUAL "module")
  15. # Building the protobuf tests require gmock what is not part of a standard protobuf checkout.
  16. # Disable them unless they are explicitly requested from the cmake command line (when we assume
  17. # gmock is downloaded to the right location inside protobuf).
  18. if(NOT protobuf_BUILD_TESTS)
  19. set(protobuf_BUILD_TESTS OFF CACHE BOOL "Build protobuf tests")
  20. endif()
  21. # Disable building protobuf with zlib. Building protobuf with zlib breaks
  22. # the build if zlib is not installed on the system.
  23. if(NOT protobuf_WITH_ZLIB)
  24. set(protobuf_WITH_ZLIB OFF CACHE BOOL "Build protobuf with zlib.")
  25. endif()
  26. if(NOT PROTOBUF_ROOT_DIR)
  27. set(PROTOBUF_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/protobuf)
  28. endif()
  29. if(EXISTS "${PROTOBUF_ROOT_DIR}/cmake/CMakeLists.txt")
  30. set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "Link static runtime libraries")
  31. add_subdirectory(${PROTOBUF_ROOT_DIR}/cmake third_party/protobuf)
  32. if(TARGET ${_gRPC_PROTOBUF_LIBRARY_NAME})
  33. set(_gRPC_PROTOBUF_LIBRARIES ${_gRPC_PROTOBUF_LIBRARY_NAME})
  34. endif()
  35. if(TARGET libprotoc)
  36. set(_gRPC_PROTOBUF_PROTOC_LIBRARIES libprotoc)
  37. endif()
  38. if(TARGET protoc)
  39. set(_gRPC_PROTOBUF_PROTOC protoc)
  40. if(CMAKE_CROSSCOMPILING)
  41. find_program(_gRPC_PROTOBUF_PROTOC_EXECUTABLE protoc)
  42. else()
  43. set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE $<TARGET_FILE:protoc>)
  44. endif()
  45. endif()
  46. # For well-known .proto files distributed with protobuf
  47. set(_gRPC_PROTOBUF_WELLKNOWN_INCLUDE_DIR "${PROTOBUF_ROOT_DIR}/src")
  48. else()
  49. message(WARNING "gRPC_PROTOBUF_PROVIDER is \"module\" but PROTOBUF_ROOT_DIR is wrong")
  50. endif()
  51. if(gRPC_INSTALL AND NOT _gRPC_INSTALL_SUPPORTED_FROM_MODULE)
  52. message(WARNING "gRPC_INSTALL will be forced to FALSE because gRPC_PROTOBUF_PROVIDER is \"module\" and CMake version (${CMAKE_VERSION}) is less than 3.13.")
  53. set(gRPC_INSTALL FALSE)
  54. endif()
  55. elseif(gRPC_PROTOBUF_PROVIDER STREQUAL "package")
  56. find_package(Protobuf REQUIRED ${gRPC_PROTOBUF_PACKAGE_TYPE})
  57. # {Protobuf,PROTOBUF}_FOUND is defined based on find_package type ("MODULE" vs "CONFIG").
  58. # For "MODULE", the case has also changed between cmake 3.5 and 3.6.
  59. # We use the legacy uppercase version for *_LIBRARIES AND *_INCLUDE_DIRS variables
  60. # as newer cmake versions provide them too for backward compatibility.
  61. if(Protobuf_FOUND OR PROTOBUF_FOUND)
  62. if(TARGET protobuf::${_gRPC_PROTOBUF_LIBRARY_NAME})
  63. set(_gRPC_PROTOBUF_LIBRARIES protobuf::${_gRPC_PROTOBUF_LIBRARY_NAME})
  64. else()
  65. set(_gRPC_PROTOBUF_LIBRARIES ${PROTOBUF_LIBRARIES})
  66. endif()
  67. if(TARGET protobuf::libprotoc)
  68. set(_gRPC_PROTOBUF_PROTOC_LIBRARIES protobuf::libprotoc)
  69. # extract the include dir from target's properties
  70. get_target_property(_gRPC_PROTOBUF_WELLKNOWN_INCLUDE_DIR protobuf::libprotoc INTERFACE_INCLUDE_DIRECTORIES)
  71. else()
  72. set(_gRPC_PROTOBUF_PROTOC_LIBRARIES ${PROTOBUF_PROTOC_LIBRARIES})
  73. set(_gRPC_PROTOBUF_WELLKNOWN_INCLUDE_DIR ${PROTOBUF_INCLUDE_DIRS})
  74. endif()
  75. if(TARGET protobuf::protoc)
  76. set(_gRPC_PROTOBUF_PROTOC protobuf::protoc)
  77. if(CMAKE_CROSSCOMPILING)
  78. find_program(_gRPC_PROTOBUF_PROTOC_EXECUTABLE protoc)
  79. else()
  80. set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE $<TARGET_FILE:protobuf::protoc>)
  81. endif()
  82. else()
  83. set(_gRPC_PROTOBUF_PROTOC ${PROTOBUF_PROTOC_EXECUTABLE})
  84. if(CMAKE_CROSSCOMPILING)
  85. find_program(_gRPC_PROTOBUF_PROTOC_EXECUTABLE protoc)
  86. else()
  87. set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE ${PROTOBUF_PROTOC_EXECUTABLE})
  88. endif()
  89. endif()
  90. set(_gRPC_FIND_PROTOBUF "if(NOT Protobuf_FOUND AND NOT PROTOBUF_FOUND)\n find_package(Protobuf ${gRPC_PROTOBUF_PACKAGE_TYPE})\nendif()")
  91. endif()
  92. endif()