From 5baf725a9de91b651d10a9917669b24d0f0cfe2f Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Wed, 27 Jul 2016 16:25:37 +0300 Subject: CMake: Choice third party provider Now we can use submodules or external packages --- templates/CMakeLists.txt.template | 126 ++++++++++++++++++++++++++++++-------- 1 file changed, 99 insertions(+), 27 deletions(-) (limited to 'templates') diff --git a/templates/CMakeLists.txt.template b/templates/CMakeLists.txt.template index 8624dc4b25..4c8d155b29 100644 --- a/templates/CMakeLists.txt.template +++ b/templates/CMakeLists.txt.template @@ -42,14 +42,16 @@ <%! def get_deps(target_dict): deps = [] + if target_dict.get('baselib', False): + deps.append("${_gRPC_BASELIB_LIBRARIES}") if target_dict.get('build', None) in ['protoc']: - deps.append("libprotoc") + deps.append("${_gRPC_PROTOBUF_PROTOC_LIBRARIES}") if target_dict.get('secure', False): - deps = ["ssl"] + deps.append("${_gRPC_SSL_LIBRARIES}") if target_dict['name'] in ['grpc++', 'grpc++_unsecure', 'grpc++_codegen_lib']: - deps.append("${PROTOBUF_LIBRARY_NAME}") + deps.append("${_gRPC_PROTOBUF_LIBRARIES}") elif target_dict['name'] in ['grpc']: - deps.append("zlibstatic") + deps.append("${_gRPC_ZLIB_LIBRARIES}") for d in target_dict.get('deps', []): deps.append(d) return deps @@ -64,35 +66,105 @@ set(PACKAGE_BUGREPORT "https://github.com/grpc/grpc/issues/") project(<%text>${PACKAGE_NAME} C CXX) - if(NOT BORINGSSL_ROOT_DIR) - set(BORINGSSL_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}/third_party/boringssl) - endif() - if(NOT PROTOBUF_ROOT_DIR) - set(PROTOBUF_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}/third_party/protobuf) + set(gRPC_ZLIB_PROVIDER "module" CACHE STRING "Provider of zlib library") + set_property(CACHE gRPC_ZLIB_PROVIDER PROPERTY STRINGS "module" "package") + + set(gRPC_SSL_PROVIDER "module" CACHE STRING "Provider of ssl library") + set_property(CACHE gRPC_SSL_PROVIDER PROPERTY STRINGS "module" "package") + + set(gRPC_PROTOBUF_PROVIDER "module" CACHE STRING "Provider of protobuf library") + set_property(CACHE gRPC_PROTOBUF_PROVIDER PROPERTY STRINGS "module" "package") + + set(gRPC_USE_PROTO_LITE OFF CACHE BOOL "Use the protobuf-lite library") + + if (gRPC_USE_PROTO_LITE) + set(_gRPC_PROTOBUF_LIBRARY_NAME "libprotobuf-lite") + add_definitions("-DGRPC_USE_PROTO_LITE") + else() + set(_gRPC_PROTOBUF_LIBRARY_NAME "libprotobuf") endif() - if(NOT ZLIB_ROOT_DIR) - set(ZLIB_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}/third_party/zlib) + + 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") + 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() + elseif("<%text>${gRPC_ZLIB_PROVIDER}" STREQUAL "package") + find_package(ZLIB) + if(TARGET ZLIB::ZLIB) + set(_gRPC_ZLIB_LIBRARIES ZLIB::ZLIB) + endif() endif() - # 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") + 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() + if(NOT PROTOBUF_ROOT_DIR) + set(PROTOBUF_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}/third_party/protobuf) + endif() + if(EXISTS "<%text>${PROTOBUF_ROOT_DIR}/cmake/CMakeLists.txt") + 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() + else() + message(WARNING "gRPC_PROTOBUF_PROVIDER is \"module\" but PROTOBUF_ROOT_DIR is wrong") + endif() + elseif("<%text>${gRPC_PROTOBUF_PROVIDER}" STREQUAL "package") + find_package(protobuf CONFIG) + if(protobuf_FOUND) + if(TARGET protobuf::<%text>${_gRPC_PROTOBUF_LIBRARY_NAME}) + set(_gRPC_PROTOBUF_LIBRARIES protobuf::<%text>${_gRPC_PROTOBUF_LIBRARY_NAME}) + endif() + if(TARGET protobuf::libprotoc) + set(_gRPC_PROTOBUF_PROTOC_LIBRARIES protobuf::libprotoc) + endif() + else() + find_package(Protobuf MODULE) + endif() endif() - add_subdirectory(<%text>${BORINGSSL_ROOT_DIR} third_party/boringssl) - add_subdirectory(<%text>${PROTOBUF_ROOT_DIR}/cmake third_party/protobuf) - add_subdirectory(<%text>${ZLIB_ROOT_DIR} third_party/zlib) + 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") + add_subdirectory(<%text>${BORINGSSL_ROOT_DIR} third_party/boringssl) + if(TARGET ssl) + set(_gRPC_SSL_LIBRARIES ssl) + endif() + else() + message(WARNING "gRPC_SSL_PROVIDER is \"module\" but BORINGSSL_ROOT_DIR is wrong") + endif() + elseif("<%text>${gRPC_SSL_PROVIDER}" STREQUAL "package") + find_package(OpenSSL) + if(TARGET OpenSSL::SSL) + set(_gRPC_SSL_LIBRARIES OpenSSL::SSL) + endif() + endif() - set(CMAKE_C_FLAGS "<%text>${CMAKE_C_FLAGS} -std=c11") - set(CMAKE_CXX_FLAGS "<%text>${CMAKE_CXX_FLAGS} -std=c++11") + if(NOT MSVC) + set(CMAKE_C_FLAGS "<%text>${CMAKE_C_FLAGS} -std=c11") + set(CMAKE_CXX_FLAGS "<%text>${CMAKE_CXX_FLAGS} -std=c++11") + endif() - if (GRPC_USE_PROTO_LITE) - set(PROTOBUF_LIBRARY_NAME "libprotobuf-lite") - add_definitions("-DGRPC_USE_PROTO_LITE") - else() - set(PROTOBUF_LIBRARY_NAME "libprotobuf") + if(WIN32 AND MSVC) + set(_gRPC_BASELIB_LIBRARIES wsock32 ws2_32) endif() % for lib in libs: @@ -119,7 +191,7 @@ PRIVATE <%text>${CMAKE_CURRENT_SOURCE_DIR}/include PRIVATE <%text>${BORINGSSL_ROOT_DIR}/include PRIVATE <%text>${PROTOBUF_ROOT_DIR}/src - PRIVATE <%text>${ZLIB_ROOT_DIR} + PRIVATE <%text>${ZLIB_INCLUDE_DIR} PRIVATE <%text>${CMAKE_CURRENT_BINARY_DIR}/third_party/zlib ) -- cgit v1.2.3