CMakeLists.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright 2018 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. #
  15. # cmake "superbuild" file for C++ helloworld example.
  16. # This build file demonstrates how to build the helloworld project
  17. # and all its dependencies in a single cmake build (hence "superbuild")
  18. # that is easy to build and maintain.
  19. # cmake's ExternalProject_Add() is used to import all the sub-projects,
  20. # including the "helloworld" project itself.
  21. # See https://blog.kitware.com/cmake-superbuilds-git-submodules/
  22. cmake_minimum_required(VERSION 3.5.1)
  23. # Project
  24. project(HelloWorld-SuperBuild C CXX)
  25. include(ExternalProject)
  26. # Note: For all external projects, instead of using checked-out code, one could
  27. # specify GIT_REPOSITORY and GIT_TAG to have cmake download the dependency directly,
  28. # without needing to add a submodule to your project.
  29. # Builds absl project from the git submodule.
  30. ExternalProject_Add(absl
  31. PREFIX absl
  32. SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/abseil-cpp"
  33. CMAKE_CACHE_ARGS
  34. -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=TRUE
  35. -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/absl
  36. )
  37. # Builds c-ares project from the git submodule.
  38. ExternalProject_Add(c-ares
  39. PREFIX c-ares
  40. SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/cares/cares"
  41. CMAKE_CACHE_ARGS
  42. -DCARES_SHARED:BOOL=OFF
  43. -DCARES_STATIC:BOOL=ON
  44. -DCARES_STATIC_PIC:BOOL=ON
  45. -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares
  46. )
  47. # Builds protobuf project from the git submodule.
  48. ExternalProject_Add(protobuf
  49. PREFIX protobuf
  50. SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/protobuf/cmake"
  51. CMAKE_CACHE_ARGS
  52. -Dprotobuf_BUILD_TESTS:BOOL=OFF
  53. -Dprotobuf_WITH_ZLIB:BOOL=OFF
  54. -Dprotobuf_MSVC_STATIC_RUNTIME:BOOL=OFF
  55. -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/protobuf
  56. )
  57. # Builds re2 project from the git submodule.
  58. ExternalProject_Add(re2
  59. PREFIX re2
  60. SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/re2"
  61. CMAKE_CACHE_ARGS
  62. -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=TRUE
  63. -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/re2
  64. )
  65. # Builds zlib project from the git submodule.
  66. ExternalProject_Add(zlib
  67. PREFIX zlib
  68. SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/zlib"
  69. CMAKE_CACHE_ARGS
  70. -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/zlib
  71. )
  72. # the location where protobuf-config.cmake will be installed varies by platform
  73. if (WIN32)
  74. set(_FINDPACKAGE_PROTOBUF_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf/cmake")
  75. else()
  76. set(_FINDPACKAGE_PROTOBUF_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf/lib/cmake/protobuf")
  77. endif()
  78. # if OPENSSL_ROOT_DIR is set, propagate that hint path to the external projects with OpenSSL dependency.
  79. set(_CMAKE_ARGS_OPENSSL_ROOT_DIR "")
  80. if (OPENSSL_ROOT_DIR)
  81. set(_CMAKE_ARGS_OPENSSL_ROOT_DIR "-DOPENSSL_ROOT_DIR:PATH=${OPENSSL_ROOT_DIR}")
  82. endif()
  83. # Builds gRPC based on locally checked-out sources and set arguments so that all the dependencies
  84. # are correctly located.
  85. ExternalProject_Add(grpc
  86. PREFIX grpc
  87. SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../.."
  88. CMAKE_CACHE_ARGS
  89. -DgRPC_INSTALL:BOOL=ON
  90. -DgRPC_BUILD_TESTS:BOOL=OFF
  91. -DgRPC_BUILD_MSVC_MP_COUNT:STRING=-1
  92. -DgRPC_PROTOBUF_PROVIDER:STRING=package
  93. -DgRPC_PROTOBUF_PACKAGE_TYPE:STRING=CONFIG
  94. -DProtobuf_DIR:PATH=${_FINDPACKAGE_PROTOBUF_CONFIG_DIR}
  95. -DgRPC_RE2_PROVIDER:STRING=package
  96. -Dre2_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/re2/lib/cmake/re2
  97. -DgRPC_ZLIB_PROVIDER:STRING=package
  98. -DZLIB_ROOT:STRING=${CMAKE_CURRENT_BINARY_DIR}/zlib
  99. -DgRPC_ABSL_PROVIDER:STRING=package
  100. -Dabsl_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/absl/lib/cmake/absl
  101. -DgRPC_CARES_PROVIDER:STRING=package
  102. -Dc-ares_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares/lib/cmake/c-ares
  103. -DgRPC_SSL_PROVIDER:STRING=package
  104. ${_CMAKE_ARGS_OPENSSL_ROOT_DIR}
  105. -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/grpc
  106. DEPENDS c-ares protobuf re2 zlib absl
  107. )
  108. # Build the helloworld projects itself using a CMakeLists.txt that assumes all the dependencies
  109. # have already been installed.
  110. # Even though helloworld is not really an "external project" from perspective of this build,
  111. # we are still importing it using ExternalProject_Add because that allows us to use find_package()
  112. # to locate all the dependencies (if we were building helloworld directly in this build we,
  113. # we would have needed to manually import the libraries as opposed to reusing targets exported by
  114. # gRPC and protobuf).
  115. ExternalProject_Add(helloworld
  116. PREFIX helloworld
  117. SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.."
  118. BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/helloworld"
  119. INSTALL_COMMAND ""
  120. CMAKE_CACHE_ARGS
  121. -DProtobuf_DIR:PATH=${_FINDPACKAGE_PROTOBUF_CONFIG_DIR}
  122. -Dc-ares_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares/lib/cmake/c-ares
  123. -Dre2_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/re2/lib/cmake/re2
  124. -DZLIB_ROOT:STRING=${CMAKE_CURRENT_BINARY_DIR}/zlib
  125. -Dabsl_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/absl/lib/cmake/absl
  126. ${_CMAKE_ARGS_OPENSSL_ROOT_DIR}
  127. -DgRPC_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/grpc/lib/cmake/grpc
  128. DEPENDS protobuf grpc
  129. )