diff --git a/cmake/modules/Buildc-ares.cmake b/cmake/modules/Buildc-ares.cmake new file mode 100644 index 00000000000..83cac92f956 --- /dev/null +++ b/cmake/modules/Buildc-ares.cmake @@ -0,0 +1,22 @@ +function(build_c_ares) + include(ExternalProject) + set(C-ARES_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/c-ares") + set(C-ARES_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/c-ares") + ExternalProject_Add(c-ares_ext + SOURCE_DIR "${C-ARES_SOURCE_DIR}" + CMAKE_ARGS + -DCARES_STATIC=ON + -DCARES_SHARED=OFF + -DCARES_INSTALL=OFF + BINARY_DIR "${C-ARES_BINARY_DIR}" + BUILD_COMMAND ${CMAKE_COMMAND} --build + INSTALL_COMMAND "") + add_library(c-ares::c-ares STATIC IMPORTED) + add_dependencies(c-ares::c-ares c-ares_ext) + set_target_properties(c-ares::c-ares PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${C-ARES_SOURCE_DIR}" + IMPORTED_LINK_INTERFACE_LANGUAGES "C" + IMPORTED_LOCATION "${C-ARES_BINARY_DIR}/lib/libcares.a") + # to appease find_package() + add_custom_target(c-ares DEPENDS c-ares::c-ares) +endfunction() diff --git a/cmake/modules/Findc-ares.cmake b/cmake/modules/Findc-ares.cmake new file mode 100644 index 00000000000..766b6a3179e --- /dev/null +++ b/cmake/modules/Findc-ares.cmake @@ -0,0 +1,31 @@ +find_package(PkgConfig) + +pkg_search_module(PC_cares + libcares) + +find_path(c-ares_INCLUDE_DIR + NAMES ares_dns.h + PATHS ${PC_cares_INCLUDE_DIRS}) + +find_library(c-ares_LIBRARY + NAMES cares + PATHS ${PC_cares_LIBRARY_DIRS}) + +set(c-ares_VERSION ${PC_cares_VERSION}) + +include (FindPackageHandleStandardArgs) + +find_package_handle_standard_args(c-ares + FOUND_VAR c-ares_FOUND + REQUIRED_VARS + c-ares_INCLUDE_DIR + c-ares_LIBRARY + VERSION_VAR c-ares_VERSION) + +if(c-ares_FOUND AND NOT (TARGET c-ares::c-ares)) + add_library(c-ares::c-ares UNKNOWN IMPORTED) + set_target_properties(c-ares::c-ares PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${c-ares_INCLUDE_DIR}" + IMPORTED_LINK_INTERFACE_LANGUAGES "C" + IMPORTED_LOCATION "${c-ares_LIBRARY}") +endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b59f666a13d..c1f7f484c91 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -384,10 +384,15 @@ include_directories("${CMAKE_SOURCE_DIR}/src/dmclock/support/src") include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/src/googletest/googletest/include") if(WITH_SEASTAR) - find_package(fmt CONFIG QUIET) + find_package(fmt QUIET CONFIG) if(NOT fmt_FOUND) add_subdirectory(fmt) endif() + find_package(c-ares 1.13.0 QUIET MODULE) + if(NOT c-ares_FOUND) + include(Buildc-ares) + build_c_ares() + endif() macro(find_package name) if(NOT TARGET ${name}) _find_package(${ARGV})