From 6a043d2faca18650c8d1c9d627403bf22fc55276 Mon Sep 17 00:00:00 2001 From: Jim Hague Date: Tue, 29 Oct 2019 09:15:04 +0000 Subject: [PATCH] Add libuv extension. --- CMakeLists.txt | 46 ++++++++++++++++++++++ cmake/modules/FindLibuv.cmake | 73 +++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 cmake/modules/FindLibuv.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index e7248d49..d0e61e87 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -113,6 +113,7 @@ option(BUILD_STUBBY "Compile and install stubby, the (stub) resolver daemon." OF option(USE_LIBEV "Use libev if available." ON) option(USE_LIBEVENT2 "Use libevent2 if available." ON) +option(USE_LIBUV "Use libuv if available." ON) option(USE_LIBIDN "Use libidn if available." ON) option(USE_LIBIDN2 "Use libidn2 if available." ON) @@ -687,6 +688,45 @@ if (USE_LIBEVENT2) endif () endif () +# libuv extension. +if (USE_LIBUV) + find_package(Libuv) + if (Libuv_FOUND) + # Copy module header to getdns include dir. + file(COPY src/getdns/getdns_ext_libuv.h DESTINATION getdns) + + add_library(uv_objects OBJECT src/extension/libuv.c) + target_include_directories(uv_objects + PRIVATE + src + ${CMAKE_CURRENT_BINARY_DIR} + ) + set_property(TARGET uv_objects PROPERTY POSITION_INDEPENDENT_CODE 1) + set_property(TARGET uv_objects PROPERTY C_STANDARD 11) + if (ENABLE_STATIC) + add_library(getdns_ex_uv STATIC $) + target_include_directories(getdns_ex_uv PRIVATE Libuv::Libuv) + target_link_libraries(getdns_ex_uv PUBLIC getdns Libuv::Libuv) + set_target_properties(getdns_ex_uv PROPERTIES OUTPUT_NAME getdns_ex_uv${static_lib_suffix}) + endif () + if (ENABLE_SHARED) + add_library(getdns_ex_uv_shared SHARED $) + target_include_directories(getdns_ex_uv_shared PRIVATE Libuv::Libuv) + target_link_libraries(getdns_ex_uv_shared PUBLIC getdns_shared Libuv::Libuv) + set_target_properties(getdns_ex_uv_shared PROPERTIES OUTPUT_NAME getdns_ex_uv) + target_shared_library_version(getdns_ex_uv_shared ${GETDNS_VERSION_CURRENT} ${GETDNS_VERSION_REVISION} ${GETDNS_VERSION_AGE}) + file(STRINGS src/extension/libuv.symbols symbols) + target_shared_library_exports(getdns_ex_uv_shared getdns_ex_uv "${symbols}") + if (NOT ENABLE_STATIC) + add_library(getdns_ex_uv ALIAS getdns_ex_uv_shared) + endif () + endif () + else () + message(WARNING "Libuv not found, libuv extension not built.") + set(USE_LIBUV OFF) + endif () +endif () + # The tools. if (BUILD_GETDNS_QUERY) add_executable(getdns_query src/tools/getdns_query.c) @@ -812,6 +852,9 @@ if (ENABLE_STATIC) if (USE_LIBEVENT2) install(TARGETS getdns_ex_event LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) endif () + if (USE_LIBUV) + install(TARGETS getdns_ex_uv LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) + endif () endif () if (ENABLE_SHARED) install(TARGETS getdns_shared LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) @@ -821,6 +864,9 @@ if (ENABLE_SHARED) if (USE_LIBEVENT2) install(TARGETS getdns_ex_event_shared LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) endif () + if (USE_LIBUV) + install(TARGETS getdns_ex_uv_shared LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) + endif () endif () if (BUILD_GETDNS_QUERY) install(TARGETS getdns_query RUNTIME DESTINATION bin) diff --git a/cmake/modules/FindLibuv.cmake b/cmake/modules/FindLibuv.cmake new file mode 100644 index 00000000..4779adb4 --- /dev/null +++ b/cmake/modules/FindLibuv.cmake @@ -0,0 +1,73 @@ +#[=======================================================================[.rst: +FindLibuv +--------- + +Find the Libuv library. + +Imported targets +^^^^^^^^^^^^^^^^ + +This module defines the following :prop_tgt:`IMPORTED` targets: + +``Libuv::Libuv`` + The Libuv library, if found. + +Result variables +^^^^^^^^^^^^^^^^ + +This module will set the following variables in your project: + +``Libuv_FOUND`` + If false, do not try to use Libuv. +``LIBUV_INCLUDE_DIR`` + where to find check.h, etc. +``LIBUV_LIBRARIES`` + the libraries needed to use Libuv. +``LIBUV_VERSION`` + the version of the Libuv library found + +#]=======================================================================] + +find_path(LIBUV_INCLUDE_DIR uv.h + HINTS + "${LIBUV_DIR}" + "${LIBUV_DIR}/include" +) + +find_library(LIBUV_LIBRARY NAMES uv libuv + HINTS + "${LIBUV_DIR}" + "${LIBUV_DIR}/lib" +) + +set(LIBUV_LIBRARIES "") + +if (LIBUV_INCLUDE_DIR AND LIBUV_LIBRARY) + if (NOT TARGET Libuv::Libuv) + add_library(Libuv::Libuv UNKNOWN IMPORTED) + set_target_properties(Libuv::Libuv PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${LIBUV_INCLUDE_DIR}" + IMPORTED_LINK_INTERFACE_LANGUAGES "C" + IMPORTED_LOCATION "${LIBUV_LIBRARY}" + ) + endif () + + if (NOT LIBUV_VERSION AND LIBUV_INCLUDE_DIR) + if (EXISTS "${LIBUV_INCLUDE_DIR}/uv-version.h") + file(STRINGS "${LIBUV_INCLUDE_DIR}/uv-version.h" LIBUV_VER_H REGEX "^#define UV_VERSION_(MAJOR|MINOR|PATCH) ") + elseif (EXISTS "${LIBUV_INCLUDE_DIR}/uv/version.h") + file(STRINGS "${LIBUV_INCLUDE_DIR}/uv/version.h" LIBUV_VER_H REGEX "^#define UV_VERSION_(MAJOR|MINOR|PATCH) ") + endif () + string(REGEX REPLACE "^.*_MAJOR ([0-9]+).*_MINOR ([0-9]+).*_PATCH ([0-9]+).*$" "\\1.\\2.\\3" LIBUV_VERSION "${LIBUV_VER_H}") + endif () +endif() + +list(APPEND LIBUV_LIBRARIES "${LIBUV_LIBRARY}") + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(Libuv + REQUIRED_VARS LIBUV_LIBRARIES LIBUV_INCLUDE_DIR + VERSION_VAR LIBUV_VERSION + ) + +mark_as_advanced(LIBUV_INCLUDE_DIR LIBUV_LIBRARIES LIBUV_LIBRARY)