From f38b1526f8981698cdd512694cd65d75073db60f Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 19 Dec 2017 14:05:41 -0800 Subject: simplify top level CMakeLists.txt --- CMakeLists.txt | 183 ++------------------------------------ cmake/benchmark.cmake | 33 +++++++ cmake/cares.cmake | 36 ++++++++ cmake/gflags.cmake | 33 +++++++ cmake/msvc_static_runtime.cmake | 14 +++ cmake/protobuf.cmake | 77 ++++++++++++++++ cmake/ssl.cmake | 38 ++++++++ cmake/zlib.cmake | 39 ++++++++ templates/CMakeLists.txt.template | 183 ++------------------------------------ 9 files changed, 282 insertions(+), 354 deletions(-) create mode 100644 cmake/benchmark.cmake create mode 100644 cmake/cares.cmake create mode 100644 cmake/gflags.cmake create mode 100644 cmake/protobuf.cmake create mode 100644 cmake/ssl.cmake create mode 100644 cmake/zlib.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 49eb38f331..bd5b676619 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,183 +102,12 @@ else() set(_gRPC_PROTOBUF_LIBRARY_NAME "libprotobuf") endif() -if("${gRPC_ZLIB_PROVIDER}" STREQUAL "module") - 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}) - - add_subdirectory(${ZLIB_ROOT_DIR} third_party/zlib) - if(TARGET zlibstatic) - set(_gRPC_ZLIB_LIBRARIES zlibstatic) - endif() - else() - message(WARNING "gRPC_ZLIB_PROVIDER is \"module\" but ZLIB_ROOT_DIR is wrong") - endif() - if(gRPC_INSTALL) - message(WARNING "gRPC_INSTALL will be forced to FALSE because gRPC_ZLIB_PROVIDER is \"module\"") - set(gRPC_INSTALL FALSE) - endif() -elseif("${gRPC_ZLIB_PROVIDER}" STREQUAL "package") - find_package(ZLIB REQUIRED) - set(_gRPC_ZLIB_LIBRARIES ${ZLIB_LIBRARIES}) - set(_gRPC_FIND_ZLIB "if(NOT ZLIB_FOUND)\n find_package(ZLIB)\nendif()") -endif() - -if("${gRPC_CARES_PROVIDER}" STREQUAL "module") - if(NOT CARES_ROOT_DIR) - set(CARES_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/cares/cares) - 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) - endif() - if(gRPC_INSTALL) - message(WARNING "gRPC_INSTALL will be forced to FALSE because gRPC_CARES_PROVIDER is \"module\"") - set(gRPC_INSTALL FALSE) - endif() -elseif("${gRPC_CARES_PROVIDER}" STREQUAL "package") - find_package(c-ares REQUIRED CONFIG) - if(TARGET c-ares::cares) - set(_gRPC_CARES_LIBRARIES c-ares::cares) - endif() - set(_gRPC_FIND_CARES "if(NOT c-ares_FOUND)\n find_package(c-ares CONFIG)\nendif()") -endif() - -if("${gRPC_PROTOBUF_PROVIDER}" STREQUAL "module") - # Building the protobuf tests require gmock what is not part of a standard protobuf checkout. - # Disable them unless they are explicitly requested from the cmake command line (when we assume - # gmock is downloaded to the right location inside protobuf). - if(NOT protobuf_BUILD_TESTS) - set(protobuf_BUILD_TESTS OFF CACHE BOOL "Build protobuf tests") - endif() - # Disable building protobuf with zlib. Building protobuf with zlib breaks - # the build if zlib is not installed on the system. - if(NOT protobuf_WITH_ZLIB) - set(protobuf_WITH_ZLIB OFF CACHE BOOL "Build protobuf with zlib.") - endif() - 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) - if(TARGET ${_gRPC_PROTOBUF_LIBRARY_NAME}) - set(_gRPC_PROTOBUF_LIBRARIES ${_gRPC_PROTOBUF_LIBRARY_NAME}) - endif() - if(TARGET libprotoc) - set(_gRPC_PROTOBUF_PROTOC_LIBRARIES libprotoc) - endif() - if(TARGET protoc) - set(_gRPC_PROTOBUF_PROTOC protoc) - set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE $) - endif() - else() - message(WARNING "gRPC_PROTOBUF_PROVIDER is \"module\" but PROTOBUF_ROOT_DIR is wrong") - endif() - if(gRPC_INSTALL) - message(WARNING "gRPC_INSTALL will be forced to FALSE because gRPC_PROTOBUF_PROVIDER is \"module\"") - set(gRPC_INSTALL FALSE) - endif() -elseif("${gRPC_PROTOBUF_PROVIDER}" STREQUAL "package") - find_package(Protobuf REQUIRED ${gRPC_PROTOBUF_PACKAGE_TYPE}) - if(Protobuf_FOUND OR PROTOBUF_FOUND) - if(TARGET protobuf::${_gRPC_PROTOBUF_LIBRARY_NAME}) - set(_gRPC_PROTOBUF_LIBRARIES protobuf::${_gRPC_PROTOBUF_LIBRARY_NAME}) - else() - set(_gRPC_PROTOBUF_LIBRARIES ${PROTOBUF_LIBRARIES}) - endif() - if(TARGET protobuf::libprotoc) - set(_gRPC_PROTOBUF_PROTOC_LIBRARIES protobuf::libprotoc) - else() - set(_gRPC_PROTOBUF_PROTOC_LIBRARIES ${PROTOBUF_PROTOC_LIBRARIES}) - endif() - if(TARGET protobuf::protoc) - set(_gRPC_PROTOBUF_PROTOC protobuf::protoc) - set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE $) - else() - set(_gRPC_PROTOBUF_PROTOC ${PROTOBUF_PROTOC_EXECUTABLE}) - set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE ${PROTOBUF_PROTOC_EXECUTABLE}) - endif() - 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() - -if("${gRPC_SSL_PROVIDER}" STREQUAL "module") - if(NOT BORINGSSL_ROOT_DIR) - set(BORINGSSL_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/boringssl) - endif() - if(EXISTS "${BORINGSSL_ROOT_DIR}/CMakeLists.txt") - set(OPENSSL_NO_ASM ON) # make boringssl buildable with Visual Studio - add_subdirectory(${BORINGSSL_ROOT_DIR} third_party/boringssl) - if(TARGET ssl) - set(_gRPC_SSL_LIBRARIES ssl) - set(_gRPC_SSL_INCLUDE_DIR ${BORINGSSL_ROOT_DIR}/include) - endif() - else() - message(WARNING "gRPC_SSL_PROVIDER is \"module\" but BORINGSSL_ROOT_DIR is wrong") - endif() - if(gRPC_INSTALL) - message(WARNING "gRPC_INSTALL will be forced to FALSE because gRPC_SSL_PROVIDER is \"module\"") - set(gRPC_INSTALL FALSE) - endif() -elseif("${gRPC_SSL_PROVIDER}" STREQUAL "package") - find_package(OpenSSL REQUIRED) - set(_gRPC_SSL_LIBRARIES ${OPENSSL_LIBRARIES}) - set(_gRPC_SSL_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR}) - set(_gRPC_FIND_SSL "if(NOT OPENSSL_FOUND)\n find_package(OpenSSL)\nendif()") -endif() - -if("${gRPC_GFLAGS_PROVIDER}" STREQUAL "module") - if(NOT GFLAGS_ROOT_DIR) - 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() - else() - message(WARNING "gRPC_GFLAGS_PROVIDER is \"module\" but GFLAGS_ROOT_DIR is wrong") - endif() -elseif("${gRPC_GFLAGS_PROVIDER}" STREQUAL "package") - find_package(gflags) - if(TARGET gflags::gflags) - set(_gRPC_GFLAGS_LIBRARIES gflags::gflags) - endif() - set(_gRPC_FIND_GFLAGS "if(NOT gflags_FOUND)\n find_package(gflags)\nendif()") -endif() - -if("${gRPC_BENCHMARK_PROVIDER}" STREQUAL "module") - if(NOT BENCHMARK_ROOT_DIR) - set(BENCHMARK_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/benchmark) - endif() - if(EXISTS "${BENCHMARK_ROOT_DIR}/CMakeLists.txt") - add_subdirectory(${BENCHMARK_ROOT_DIR} third_party/benchmark) - if(TARGET benchmark) - set(_gRPC_BENCHMARK_LIBRARIES benchmark) - 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) - if(TARGET benchmark::benchmark) - set(_gRPC_BENCHMARK_LIBRARIES benchmark::benchmark) - endif() - set(_gRPC_FIND_BENCHMARK "if(NOT benchmark_FOUND)\n find_package(benchmark)\nendif()") -endif() +include(cmake/zlib.cmake) +include(cmake/cares.cmake) +include(cmake/protobuf.cmake) +include(cmake/ssl.cmake) +include(cmake/gflags.cmake) +include(cmake/benchmark.cmake) if(NOT MSVC) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99") diff --git a/cmake/benchmark.cmake b/cmake/benchmark.cmake new file mode 100644 index 0000000000..c6284225a3 --- /dev/null +++ b/cmake/benchmark.cmake @@ -0,0 +1,33 @@ +# Copyright 2017 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +if("${gRPC_BENCHMARK_PROVIDER}" STREQUAL "module") + if(NOT BENCHMARK_ROOT_DIR) + set(BENCHMARK_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/benchmark) + endif() + if(EXISTS "${BENCHMARK_ROOT_DIR}/CMakeLists.txt") + add_subdirectory(${BENCHMARK_ROOT_DIR} third_party/benchmark) + if(TARGET benchmark) + set(_gRPC_BENCHMARK_LIBRARIES benchmark) + 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) + if(TARGET benchmark::benchmark) + set(_gRPC_BENCHMARK_LIBRARIES benchmark::benchmark) + 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 new file mode 100644 index 0000000000..521cf52e77 --- /dev/null +++ b/cmake/cares.cmake @@ -0,0 +1,36 @@ +# Copyright 2017 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +if("${gRPC_CARES_PROVIDER}" STREQUAL "module") + if(NOT CARES_ROOT_DIR) + set(CARES_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/cares/cares) + 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) + endif() + if(gRPC_INSTALL) + message(WARNING "gRPC_INSTALL will be forced to FALSE because gRPC_CARES_PROVIDER is \"module\"") + set(gRPC_INSTALL FALSE) + endif() +elseif("${gRPC_CARES_PROVIDER}" STREQUAL "package") + find_package(c-ares REQUIRED CONFIG) + if(TARGET c-ares::cares) + set(_gRPC_CARES_LIBRARIES c-ares::cares) + 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 new file mode 100644 index 0000000000..1864bda357 --- /dev/null +++ b/cmake/gflags.cmake @@ -0,0 +1,33 @@ +# Copyright 2017 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +if("${gRPC_GFLAGS_PROVIDER}" STREQUAL "module") + if(NOT GFLAGS_ROOT_DIR) + 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() + else() + message(WARNING "gRPC_GFLAGS_PROVIDER is \"module\" but GFLAGS_ROOT_DIR is wrong") + endif() +elseif("${gRPC_GFLAGS_PROVIDER}" STREQUAL "package") + find_package(gflags) + if(TARGET gflags::gflags) + set(_gRPC_GFLAGS_LIBRARIES gflags::gflags) + endif() + set(_gRPC_FIND_GFLAGS "if(NOT gflags_FOUND)\n find_package(gflags)\nendif()") +endif() diff --git a/cmake/msvc_static_runtime.cmake b/cmake/msvc_static_runtime.cmake index fc6d1d62d3..844bd02653 100644 --- a/cmake/msvc_static_runtime.cmake +++ b/cmake/msvc_static_runtime.cmake @@ -1,3 +1,17 @@ +# Copyright 2017 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + option(gRPC_MSVC_STATIC_RUNTIME "Link with static msvc runtime libraries" OFF) if(gRPC_MSVC_STATIC_RUNTIME) diff --git a/cmake/protobuf.cmake b/cmake/protobuf.cmake new file mode 100644 index 0000000000..e2206a2319 --- /dev/null +++ b/cmake/protobuf.cmake @@ -0,0 +1,77 @@ +# Copyright 2017 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +if("${gRPC_PROTOBUF_PROVIDER}" STREQUAL "module") + # Building the protobuf tests require gmock what is not part of a standard protobuf checkout. + # Disable them unless they are explicitly requested from the cmake command line (when we assume + # gmock is downloaded to the right location inside protobuf). + if(NOT protobuf_BUILD_TESTS) + set(protobuf_BUILD_TESTS OFF CACHE BOOL "Build protobuf tests") + endif() + # Disable building protobuf with zlib. Building protobuf with zlib breaks + # the build if zlib is not installed on the system. + if(NOT protobuf_WITH_ZLIB) + set(protobuf_WITH_ZLIB OFF CACHE BOOL "Build protobuf with zlib.") + endif() + 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) + if(TARGET ${_gRPC_PROTOBUF_LIBRARY_NAME}) + set(_gRPC_PROTOBUF_LIBRARIES ${_gRPC_PROTOBUF_LIBRARY_NAME}) + endif() + if(TARGET libprotoc) + set(_gRPC_PROTOBUF_PROTOC_LIBRARIES libprotoc) + endif() + if(TARGET protoc) + set(_gRPC_PROTOBUF_PROTOC protoc) + set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE $) + endif() + else() + message(WARNING "gRPC_PROTOBUF_PROVIDER is \"module\" but PROTOBUF_ROOT_DIR is wrong") + endif() + if(gRPC_INSTALL) + message(WARNING "gRPC_INSTALL will be forced to FALSE because gRPC_PROTOBUF_PROVIDER is \"module\"") + set(gRPC_INSTALL FALSE) + endif() +elseif("${gRPC_PROTOBUF_PROVIDER}" STREQUAL "package") + find_package(Protobuf REQUIRED ${gRPC_PROTOBUF_PACKAGE_TYPE}) + if(Protobuf_FOUND OR PROTOBUF_FOUND) + if(TARGET protobuf::${_gRPC_PROTOBUF_LIBRARY_NAME}) + set(_gRPC_PROTOBUF_LIBRARIES protobuf::${_gRPC_PROTOBUF_LIBRARY_NAME}) + else() + set(_gRPC_PROTOBUF_LIBRARIES ${PROTOBUF_LIBRARIES}) + endif() + if(TARGET protobuf::libprotoc) + set(_gRPC_PROTOBUF_PROTOC_LIBRARIES protobuf::libprotoc) + else() + set(_gRPC_PROTOBUF_PROTOC_LIBRARIES ${PROTOBUF_PROTOC_LIBRARIES}) + endif() + if(TARGET protobuf::protoc) + set(_gRPC_PROTOBUF_PROTOC protobuf::protoc) + set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE $) + else() + set(_gRPC_PROTOBUF_PROTOC ${PROTOBUF_PROTOC_EXECUTABLE}) + set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE ${PROTOBUF_PROTOC_EXECUTABLE}) + endif() + 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/ssl.cmake b/cmake/ssl.cmake new file mode 100644 index 0000000000..75ce069fe6 --- /dev/null +++ b/cmake/ssl.cmake @@ -0,0 +1,38 @@ +# Copyright 2017 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +if("${gRPC_SSL_PROVIDER}" STREQUAL "module") + if(NOT BORINGSSL_ROOT_DIR) + set(BORINGSSL_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/boringssl) + endif() + if(EXISTS "${BORINGSSL_ROOT_DIR}/CMakeLists.txt") + set(OPENSSL_NO_ASM ON) # make boringssl buildable with Visual Studio + add_subdirectory(${BORINGSSL_ROOT_DIR} third_party/boringssl) + if(TARGET ssl) + set(_gRPC_SSL_LIBRARIES ssl) + set(_gRPC_SSL_INCLUDE_DIR ${BORINGSSL_ROOT_DIR}/include) + endif() + else() + message(WARNING "gRPC_SSL_PROVIDER is \"module\" but BORINGSSL_ROOT_DIR is wrong") + endif() + if(gRPC_INSTALL) + message(WARNING "gRPC_INSTALL will be forced to FALSE because gRPC_SSL_PROVIDER is \"module\"") + set(gRPC_INSTALL FALSE) + endif() +elseif("${gRPC_SSL_PROVIDER}" STREQUAL "package") + find_package(OpenSSL REQUIRED) + set(_gRPC_SSL_LIBRARIES ${OPENSSL_LIBRARIES}) + set(_gRPC_SSL_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR}) + set(_gRPC_FIND_SSL "if(NOT OPENSSL_FOUND)\n find_package(OpenSSL)\nendif()") +endif() diff --git a/cmake/zlib.cmake b/cmake/zlib.cmake new file mode 100644 index 0000000000..16cd9e66d5 --- /dev/null +++ b/cmake/zlib.cmake @@ -0,0 +1,39 @@ +# Copyright 2017 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +if("${gRPC_ZLIB_PROVIDER}" STREQUAL "module") + 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}) + + add_subdirectory(${ZLIB_ROOT_DIR} third_party/zlib) + if(TARGET zlibstatic) + set(_gRPC_ZLIB_LIBRARIES zlibstatic) + endif() + else() + message(WARNING "gRPC_ZLIB_PROVIDER is \"module\" but ZLIB_ROOT_DIR is wrong") + endif() + if(gRPC_INSTALL) + message(WARNING "gRPC_INSTALL will be forced to FALSE because gRPC_ZLIB_PROVIDER is \"module\"") + set(gRPC_INSTALL FALSE) + endif() +elseif("${gRPC_ZLIB_PROVIDER}" STREQUAL "package") + find_package(ZLIB REQUIRED) + set(_gRPC_ZLIB_LIBRARIES ${ZLIB_LIBRARIES}) + set(_gRPC_FIND_ZLIB "if(NOT ZLIB_FOUND)\n find_package(ZLIB)\nendif()") +endif() diff --git a/templates/CMakeLists.txt.template b/templates/CMakeLists.txt.template index 5ef34e47ad..8de0ccde82 100644 --- a/templates/CMakeLists.txt.template +++ b/templates/CMakeLists.txt.template @@ -147,183 +147,12 @@ set(_gRPC_PROTOBUF_LIBRARY_NAME "libprotobuf") endif() - if("<%text>${gRPC_ZLIB_PROVIDER}" STREQUAL "module") - if(NOT ZLIB_ROOT_DIR) - set(ZLIB_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}/third_party/zlib) - endif() - set(ZLIB_INCLUDE_DIR "<%text>${ZLIB_ROOT_DIR}") - if(EXISTS "<%text>${ZLIB_ROOT_DIR}/CMakeLists.txt") - # TODO(jtattermusch): workaround for https://github.com/madler/zlib/issues/218 - include_directories(<%text>${ZLIB_INCLUDE_DIR}) - - add_subdirectory(<%text>${ZLIB_ROOT_DIR} third_party/zlib) - if(TARGET zlibstatic) - set(_gRPC_ZLIB_LIBRARIES zlibstatic) - endif() - else() - message(WARNING "gRPC_ZLIB_PROVIDER is \"module\" but ZLIB_ROOT_DIR is wrong") - endif() - if(gRPC_INSTALL) - message(WARNING "gRPC_INSTALL will be forced to FALSE because gRPC_ZLIB_PROVIDER is \"module\"") - set(gRPC_INSTALL FALSE) - endif() - elseif("<%text>${gRPC_ZLIB_PROVIDER}" STREQUAL "package") - find_package(ZLIB REQUIRED) - set(_gRPC_ZLIB_LIBRARIES <%text>${ZLIB_LIBRARIES}) - set(_gRPC_FIND_ZLIB "if(NOT ZLIB_FOUND)\n find_package(ZLIB)\nendif()") - endif() - - if("<%text>${gRPC_CARES_PROVIDER}" STREQUAL "module") - if(NOT CARES_ROOT_DIR) - set(CARES_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}/third_party/cares/cares) - endif() - set(CARES_SHARED OFF CACHE BOOL "disable shared library") - set(CARES_STATIC ON CACHE BOOL "link cares statically") - set(CARES_INCLUDE_DIR "<%text>${CMAKE_CURRENT_SOURCE_DIR}/third_party/cares/cares") - add_subdirectory(third_party/cares/cares) - if(TARGET c-ares) - set(_gRPC_CARES_LIBRARIES c-ares) - endif() - if(gRPC_INSTALL) - message(WARNING "gRPC_INSTALL will be forced to FALSE because gRPC_CARES_PROVIDER is \"module\"") - set(gRPC_INSTALL FALSE) - endif() - elseif("<%text>${gRPC_CARES_PROVIDER}" STREQUAL "package") - find_package(c-ares REQUIRED CONFIG) - if(TARGET c-ares::cares) - set(_gRPC_CARES_LIBRARIES c-ares::cares) - endif() - set(_gRPC_FIND_CARES "if(NOT c-ares_FOUND)\n find_package(c-ares CONFIG)\nendif()") - endif() - - if("<%text>${gRPC_PROTOBUF_PROVIDER}" STREQUAL "module") - # Building the protobuf tests require gmock what is not part of a standard protobuf checkout. - # Disable them unless they are explicitly requested from the cmake command line (when we assume - # gmock is downloaded to the right location inside protobuf). - if(NOT protobuf_BUILD_TESTS) - set(protobuf_BUILD_TESTS OFF CACHE BOOL "Build protobuf tests") - endif() - # Disable building protobuf with zlib. Building protobuf with zlib breaks - # the build if zlib is not installed on the system. - if(NOT protobuf_WITH_ZLIB) - set(protobuf_WITH_ZLIB OFF CACHE BOOL "Build protobuf with zlib.") - endif() - if(NOT PROTOBUF_ROOT_DIR) - set(PROTOBUF_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}/third_party/protobuf) - endif() - set(PROTOBUF_WELLKNOWN_IMPORT_DIR <%text>${PROTOBUF_ROOT_DIR}/src) - if(EXISTS "<%text>${PROTOBUF_ROOT_DIR}/cmake/CMakeLists.txt") - set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "Link static runtime libraries") - add_subdirectory(<%text>${PROTOBUF_ROOT_DIR}/cmake third_party/protobuf) - if(TARGET <%text>${_gRPC_PROTOBUF_LIBRARY_NAME}) - set(_gRPC_PROTOBUF_LIBRARIES <%text>${_gRPC_PROTOBUF_LIBRARY_NAME}) - endif() - if(TARGET libprotoc) - set(_gRPC_PROTOBUF_PROTOC_LIBRARIES libprotoc) - endif() - if(TARGET protoc) - set(_gRPC_PROTOBUF_PROTOC protoc) - set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE $) - endif() - else() - message(WARNING "gRPC_PROTOBUF_PROVIDER is \"module\" but PROTOBUF_ROOT_DIR is wrong") - endif() - if(gRPC_INSTALL) - message(WARNING "gRPC_INSTALL will be forced to FALSE because gRPC_PROTOBUF_PROVIDER is \"module\"") - set(gRPC_INSTALL FALSE) - endif() - elseif("<%text>${gRPC_PROTOBUF_PROVIDER}" STREQUAL "package") - find_package(Protobuf REQUIRED <%text>${gRPC_PROTOBUF_PACKAGE_TYPE}) - if(Protobuf_FOUND OR PROTOBUF_FOUND) - if(TARGET protobuf::<%text>${_gRPC_PROTOBUF_LIBRARY_NAME}) - set(_gRPC_PROTOBUF_LIBRARIES protobuf::<%text>${_gRPC_PROTOBUF_LIBRARY_NAME}) - else() - set(_gRPC_PROTOBUF_LIBRARIES <%text>${PROTOBUF_LIBRARIES}) - endif() - if(TARGET protobuf::libprotoc) - set(_gRPC_PROTOBUF_PROTOC_LIBRARIES protobuf::libprotoc) - else() - set(_gRPC_PROTOBUF_PROTOC_LIBRARIES <%text>${PROTOBUF_PROTOC_LIBRARIES}) - endif() - if(TARGET protobuf::protoc) - set(_gRPC_PROTOBUF_PROTOC protobuf::protoc) - set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE $) - else() - set(_gRPC_PROTOBUF_PROTOC <%text>${PROTOBUF_PROTOC_EXECUTABLE}) - set(_gRPC_PROTOBUF_PROTOC_EXECUTABLE <%text>${PROTOBUF_PROTOC_EXECUTABLE}) - endif() - set(_gRPC_FIND_PROTOBUF "if(NOT Protobuf_FOUND AND NOT PROTOBUF_FOUND)\n find_package(Protobuf <%text>${gRPC_PROTOBUF_PACKAGE_TYPE})\nendif()") - endif() - if(PROTOBUF_FOUND) - include_directories(<%text>${PROTOBUF_INCLUDE_DIRS}) - endif() - set(PROTOBUF_WELLKNOWN_IMPORT_DIR /usr/local/include) - endif() - - if("<%text>${gRPC_SSL_PROVIDER}" STREQUAL "module") - if(NOT BORINGSSL_ROOT_DIR) - set(BORINGSSL_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}/third_party/boringssl) - endif() - if(EXISTS "<%text>${BORINGSSL_ROOT_DIR}/CMakeLists.txt") - set(OPENSSL_NO_ASM ON) # make boringssl buildable with Visual Studio - add_subdirectory(<%text>${BORINGSSL_ROOT_DIR} third_party/boringssl) - if(TARGET ssl) - set(_gRPC_SSL_LIBRARIES ssl) - set(_gRPC_SSL_INCLUDE_DIR <%text>${BORINGSSL_ROOT_DIR}/include) - endif() - else() - message(WARNING "gRPC_SSL_PROVIDER is \"module\" but BORINGSSL_ROOT_DIR is wrong") - endif() - if(gRPC_INSTALL) - message(WARNING "gRPC_INSTALL will be forced to FALSE because gRPC_SSL_PROVIDER is \"module\"") - set(gRPC_INSTALL FALSE) - endif() - elseif("<%text>${gRPC_SSL_PROVIDER}" STREQUAL "package") - find_package(OpenSSL REQUIRED) - set(_gRPC_SSL_LIBRARIES <%text>${OPENSSL_LIBRARIES}) - set(_gRPC_SSL_INCLUDE_DIR <%text>${OPENSSL_INCLUDE_DIR}) - set(_gRPC_FIND_SSL "if(NOT OPENSSL_FOUND)\n find_package(OpenSSL)\nendif()") - endif() - - if("<%text>${gRPC_GFLAGS_PROVIDER}" STREQUAL "module") - if(NOT GFLAGS_ROOT_DIR) - set(GFLAGS_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}/third_party/gflags) - endif() - if(EXISTS "<%text>${GFLAGS_ROOT_DIR}/CMakeLists.txt") - add_subdirectory(<%text>${GFLAGS_ROOT_DIR} third_party/gflags) - if(TARGET gflags_static) - set(_gRPC_GFLAGS_LIBRARIES gflags_static) - endif() - else() - message(WARNING "gRPC_GFLAGS_PROVIDER is \"module\" but GFLAGS_ROOT_DIR is wrong") - endif() - elseif("<%text>${gRPC_GFLAGS_PROVIDER}" STREQUAL "package") - find_package(gflags) - if(TARGET gflags::gflags) - set(_gRPC_GFLAGS_LIBRARIES gflags::gflags) - endif() - set(_gRPC_FIND_GFLAGS "if(NOT gflags_FOUND)\n find_package(gflags)\nendif()") - endif() - - if("<%text>${gRPC_BENCHMARK_PROVIDER}" STREQUAL "module") - if(NOT BENCHMARK_ROOT_DIR) - set(BENCHMARK_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}/third_party/benchmark) - endif() - if(EXISTS "<%text>${BENCHMARK_ROOT_DIR}/CMakeLists.txt") - add_subdirectory(<%text>${BENCHMARK_ROOT_DIR} third_party/benchmark) - if(TARGET benchmark) - set(_gRPC_BENCHMARK_LIBRARIES benchmark) - endif() - else() - message(WARNING "gRPC_BENCHMARK_PROVIDER is \"module\" but BENCHMARK_ROOT_DIR is wrong") - endif() - elseif("<%text>${gRPC_BENCHMARK_PROVIDER}" STREQUAL "package") - find_package(benchmark) - if(TARGET benchmark::benchmark) - set(_gRPC_BENCHMARK_LIBRARIES benchmark::benchmark) - endif() - set(_gRPC_FIND_BENCHMARK "if(NOT benchmark_FOUND)\n find_package(benchmark)\nendif()") - endif() + include(cmake/zlib.cmake) + include(cmake/cares.cmake) + include(cmake/protobuf.cmake) + include(cmake/ssl.cmake) + include(cmake/gflags.cmake) + include(cmake/benchmark.cmake) if(NOT MSVC) set(CMAKE_C_FLAGS "<%text>${CMAKE_C_FLAGS} -std=c99") -- cgit v1.2.3