From 933d75653d30b265769f0fd93c16e221231e2338 Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Fri, 25 Oct 2019 18:04:18 -0400 Subject: [PATCH] Add Findc-ares.cmake module This is a CMake find-module, which attempts to find c-ares without a CMake package file being available. If such a file is available, it uses that. Otherwise, it searches for the library and include directory manually. --- cmake/cares.cmake | 5 ++-- cmake/gRPCConfig.cmake.in | 3 ++ cmake/modules/Findc-ares.cmake | 48 +++++++++++++++++++++++++++++++ templates/CMakeLists.txt.template | 5 ++++ 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 cmake/modules/Findc-ares.cmake diff --git a/cmake/cares.cmake b/cmake/cares.cmake index cf7d74cc0fc..006a4a967a7 100644 --- a/cmake/cares.cmake +++ b/cmake/cares.cmake @@ -33,10 +33,9 @@ if(gRPC_CARES_PROVIDER STREQUAL "module") set(gRPC_INSTALL FALSE) endif() elseif(gRPC_CARES_PROVIDER STREQUAL "package") - # Use "CONFIG" as there is no built-in cmake module for c-ares. - find_package(c-ares REQUIRED CONFIG) + find_package(c-ares 1.13.0 REQUIRED) 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()") + set(_gRPC_FIND_CARES "if(NOT c-ares_FOUND)\n find_package(c-ares)\nendif()") endif() diff --git a/cmake/gRPCConfig.cmake.in b/cmake/gRPCConfig.cmake.in index 504d5a7f959..b03b2ba4084 100644 --- a/cmake/gRPCConfig.cmake.in +++ b/cmake/gRPCConfig.cmake.in @@ -1,3 +1,6 @@ +# Module path +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/modules) + # Depend packages @_gRPC_FIND_ZLIB@ @_gRPC_FIND_PROTOBUF@ diff --git a/cmake/modules/Findc-ares.cmake b/cmake/modules/Findc-ares.cmake new file mode 100644 index 00000000000..c614170d187 --- /dev/null +++ b/cmake/modules/Findc-ares.cmake @@ -0,0 +1,48 @@ +include(FindPackageHandleStandardArgs) + +function(__cares_get_version) + if(c-ares_INCLUDE_DIR AND EXISTS "${c-ares_INCLUDE_DIR}/ares_version.h") + file(STRINGS "${c-ares_INCLUDE_DIR}/ares_version.h" _cares_version_str REGEX "^#define ARES_VERSION_STR \"([^\n]*)\"$") + if(_cares_version_str MATCHES "#define ARES_VERSION_STR \"([^\n]*)\"") + set(c-ares_VERSION "${CMAKE_MATCH_1}" PARENT_SCOPE) + endif() + endif() +endfunction() + +# We need to disable version checking, since c-ares does not provide it. +set(_cares_version_var_suffixes "" _MAJOR _MINOR _PATCH _TWEAK _COUNT) +foreach(_suffix IN LISTS _cares_version_var_suffixes) + set(_cares_save_FIND_VERSION${_suffix} ${c-ares_FIND_VERSION${_suffix}}) + unset(c-ares_FIND_VERSION${_suffix}) +endforeach() +find_package(c-ares CONFIG) +foreach(_suffix IN LISTS _cares_version_var_suffixes) + set(c-ares_FIND_VERSION${_suffix} ${_cares_save_FIND_VERSION${_suffix}}) +endforeach() + +if(c-ares_FOUND) + if(NOT DEFINED c-ares_VERSION) + __cares_get_version() + endif() + + find_package_handle_standard_args(c-ares CONFIG_MODE) + return() +endif() + +find_path(c-ares_INCLUDE_DIR NAMES ares.h) +__cares_get_version() + +find_library(c-ares_LIBRARY cares) + +find_package_handle_standard_args(c-ares + REQUIRED_VARS c-ares_INCLUDE_DIR c-ares_LIBRARY + VERSION_VAR c-ares_VERSION + ) + +if(c-ares_FOUND) + add_library(c-ares::cares UNKNOWN IMPORTED) + set_target_properties(c-ares::cares PROPERTIES + IMPORTED_LOCATION "${c-ares_LIBRARY}" + INTERFACE_INCLUDE_DIRECTORIES "${c-ares_INCLUDE_DIR}" + ) +endif() diff --git a/templates/CMakeLists.txt.template b/templates/CMakeLists.txt.template index 4ef88f1dfba..34c036c27ae 100644 --- a/templates/CMakeLists.txt.template +++ b/templates/CMakeLists.txt.template @@ -202,6 +202,7 @@ ## Some libraries are shared even with BUILD_SHARED_LIBRARIES=OFF set(CMAKE_POSITION_INDEPENDENT_CODE TRUE) + set(CMAKE_MODULE_PATH "<%text>${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") if(MSVC) include(cmake/msvc_static_runtime.cmake) @@ -638,6 +639,10 @@ <%text>${CMAKE_CURRENT_BINARY_DIR}/gRPCConfigVersion.cmake DESTINATION <%text>${gRPC_INSTALL_CMAKEDIR} ) + install(FILES + <%text>${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/Findc-ares.cmake + DESTINATION <%text>${gRPC_INSTALL_CMAKEDIR}/modules + ) install(FILES <%text>${CMAKE_CURRENT_SOURCE_DIR}/etc/roots.pem DESTINATION <%text>${gRPC_INSTALL_SHAREDIR})