gRPCBuild.cmake 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # This script clones gRPC project and builds it with its dependencies
  2. # In order to use it
  3. # - copy DownloadProject.cmake, DownloadProject.CMakeLists.cmake.in and current file
  4. # into a separate folder outside the project.
  5. # - create there a simple CMakeLists.txt with content:
  6. # -------
  7. # cmake_minimum_required(VERSION 3.1)
  8. # project(gRPCTargetsBuilder)
  9. # include(gRPCBuild.cmake)
  10. # -------
  11. #
  12. # - from a build directory run "cmake <folder with created CMakeLists.txt>"
  13. #
  14. # In order to use the built gRPC project with qtprotobuf, please, specify CMAKE_PREFIX_PATH and other vars. E.g.
  15. # PATH="<build-grpc>/grpc/bin:$PATH" cmake -DCMAKE_PREFIX_PATH="<build-grpc>/protobuf/lib/cmake;<build-grpc>/grpc/lib/cmake" -Dprotobuf_MODULE_COMPATIBLE=ON <qtprotobuf_CMakeLists_dir>
  16. include(cmake/DownloadProject.cmake)
  17. download_project(PROJ gRPCDownload
  18. GIT_REPOSITORY https://github.com/grpc/grpc.git
  19. GIT_TAG v1.20.x
  20. )
  21. set(gRPCDownload_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR})
  22. include(ExternalProject)
  23. # Builds c-ares project from the git submodule.
  24. # Note: For all external projects, instead of using checked-out code, one could
  25. # specify GIT_REPOSITORY and GIT_TAG to have cmake download the dependency directly,
  26. # without needing to add a submodule to your project.
  27. ExternalProject_Add(c-ares
  28. PREFIX c-ares
  29. SOURCE_DIR "${gRPCDownload_SOURCE_DIR}/third_party/cares/cares"
  30. CMAKE_CACHE_ARGS
  31. -DCARES_SHARED:BOOL=OFF
  32. -DCARES_STATIC:BOOL=ON
  33. -DCARES_STATIC_PIC:BOOL=ON
  34. -DCMAKE_INSTALL_PREFIX:PATH=${gRPCDownload_INSTALL_DIR}/c-ares
  35. )
  36. # Builds protobuf project from the git submodule.
  37. ExternalProject_Add(protobuf
  38. PREFIX protobuf
  39. SOURCE_DIR "${gRPCDownload_SOURCE_DIR}/third_party/protobuf/cmake"
  40. CMAKE_CACHE_ARGS
  41. -Dprotobuf_BUILD_TESTS:BOOL=OFF
  42. -Dprotobuf_WITH_ZLIB:BOOL=OFF
  43. -Dprotobuf_MSVC_STATIC_RUNTIME:BOOL=OFF
  44. -DCMAKE_INSTALL_PREFIX:PATH=${gRPCDownload_INSTALL_DIR}/protobuf
  45. )
  46. # Setup libprotobuf as a target
  47. set(LIBPROTOC_PREFIX_PATH "${gRPCDownload_INSTALL_DIR}/protobuf")
  48. set(LIBPROTOC_BINARY_PATH "${LIBPROTOC_PREFIX_PATH}/lib")
  49. set(LIBPROTOC_LIB_INCLUDE "${LIBPROTOC_PREFIX_PATH}/include")
  50. add_library(libprotoc IMPORTED STATIC GLOBAL)
  51. add_library(libprotobuf IMPORTED STATIC GLOBAL)
  52. add_executable(protoc IMPORTED GLOBAL)
  53. add_dependencies(libprotoc protobuf)
  54. add_dependencies(libprotobuf protobuf)
  55. add_dependencies(protoc protobuf)
  56. set_target_properties(protoc PROPERTIES
  57. IMPORTED_LOCATION "${LIBPROTOC_PREFIX_PATH}/bin/protoc"
  58. )
  59. set_target_properties(libprotoc PROPERTIES
  60. IMPORTED_LOCATION "${LIBPROTOC_BINARY_PATH}/libprotoc.a"
  61. INTERFACE_INCLUDE_DIRECTORIES $<BUILD_INTERFACE:${LIBPROTOC_LIB_INCLUDE}>
  62. )
  63. set_target_properties(libprotobuf PROPERTIES
  64. IMPORTED_LOCATION "${LIBPROTOC_BINARY_PATH}/libprotobuf.a"
  65. INTERFACE_INCLUDE_DIRECTORIES $<BUILD_INTERFACE:${LIBPROTOC_LIB_INCLUDE}>
  66. )
  67. # Builds zlib project from the git submodule.
  68. ExternalProject_Add(zlib
  69. PREFIX zlib
  70. SOURCE_DIR "${gRPCDownload_SOURCE_DIR}/third_party/zlib"
  71. CMAKE_CACHE_ARGS
  72. -DCMAKE_INSTALL_PREFIX:PATH=${gRPCDownload_INSTALL_DIR}/zlib
  73. )
  74. # the location where protobuf-config.cmake will be installed varies by platform
  75. if (WIN32)
  76. set(_FINDPACKAGE_PROTOBUF_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf/cmake")
  77. else()
  78. set(_FINDPACKAGE_PROTOBUF_CONFIG_DIR "${gRPCDownload_INSTALL_DIR}/protobuf/lib/cmake/protobuf")
  79. endif()
  80. # if OPENSSL_ROOT_DIR is set, propagate that hint path to the external projects with OpenSSL dependency.
  81. set(_CMAKE_ARGS_OPENSSL_ROOT_DIR "")
  82. if (OPENSSL_ROOT_DIR)
  83. set(_CMAKE_ARGS_OPENSSL_ROOT_DIR "-DOPENSSL_ROOT_DIR:PATH=${OPENSSL_ROOT_DIR}")
  84. endif()
  85. # Builds gRPC based on locally checked-out sources and set arguments so that all the dependencies
  86. # are correctly located.
  87. ExternalProject_Add(gRPC
  88. PREFIX grpc
  89. SOURCE_DIR "${gRPCDownload_SOURCE_DIR}"
  90. CMAKE_CACHE_ARGS
  91. -DgRPC_INSTALL:BOOL=ON
  92. -DgRPC_BUILD_TESTS:BOOL=OFF
  93. -DgRPC_PROTOBUF_PROVIDER:STRING=package
  94. -DgRPC_PROTOBUF_PACKAGE_TYPE:STRING=CONFIG
  95. -DProtobuf_DIR:PATH=${_FINDPACKAGE_PROTOBUF_CONFIG_DIR}
  96. -DgRPC_ZLIB_PROVIDER:STRING=package
  97. -DZLIB_ROOT:STRING=${gRPCDownload_INSTALL_DIR}/zlib
  98. -DgRPC_CARES_PROVIDER:STRING=package
  99. -Dc-ares_DIR:PATH=${gRPCDownload_INSTALL_DIR}/c-ares/lib/cmake/c-ares
  100. -DgRPC_SSL_PROVIDER:STRING=package
  101. ${_CMAKE_ARGS_OPENSSL_ROOT_DIR}
  102. -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/grpc
  103. DEPENDS c-ares protobuf zlib
  104. )
  105. set(LIBGRPC_PREFIX_PATH "${gRPCDownload_INSTALL_DIR}/grpc")
  106. add_executable(grpc_cpp_plugin IMPORTED GLOBAL)
  107. set_target_properties(grpc_cpp_plugin PROPERTIES
  108. "IMPORTED_LOCATION" "${LIBGRPC_PREFIX_PATH}/bin/grpc_cpp_plugin"
  109. )
  110. add_library(grpc++ IMPORTED STATIC GLOBAL)
  111. set_target_properties(grpc++ PROPERTIES
  112. IMPORTED_LOCATION "${LIBGRPC_PREFIX_PATH}/lib/libgrpc++.a"
  113. INTERFACE_INCLUDE_DIRECTORIES $<BUILD_INTERFACE:${LIBGRPC_PREFIX_PATH}/include>
  114. IMPORTED_LINK_INTERFACE_LIBRARIES "${LIBGRPC_PREFIX_PATH}/lib/libgrpc.a;${LIBGRPC_PREFIX_PATH}/lib/libgpr.a;${LIBGRPC_PREFIX_PATH}/lib/libaddress_sorting.a;${gRPCDownload_INSTALL_DIR}/c-ares/lib/libcares.a;${gRPCDownload_INSTALL_DIR}/zlib/lib/libz.a;ssl;crypto;pthread"
  115. )
  116. add_library(grpc IMPORTED STATIC GLOBAL)
  117. set_target_properties(grpc PROPERTIES
  118. IMPORTED_LOCATION "${LIBGRPC_PREFIX_PATH}/lib/libgrpc.a"
  119. )
  120. add_dependencies(grpc_cpp_plugin gRPC)
  121. add_dependencies(grpc++ gRPC)
  122. add_dependencies(grpc gRPC)