cmake: improve dependency checks
authorMichael Wallner <mike@php.net>
Mon, 2 Nov 2020 09:38:08 +0000 (10:38 +0100)
committerMichael Wallner <mike@php.net>
Mon, 2 Nov 2020 09:38:08 +0000 (10:38 +0100)
14 files changed:
CMake/CheckDependency.cmake
CMake/CheckTbb.cmake [new file with mode: 0644]
CMake/FindMemcached.cmake
CMake/_Include.cmake
CMakeConfig.txt
CMakeLists.txt
src/bin/CMakeLists.txt
src/libhashkit/CMakeLists.txt
src/libmemcached/CMakeLists.txt
src/libmemcachedprotocol/CMakeLists.txt
src/libmemcachedutil/CMakeLists.txt
test/CMakeLists.txt
test/conf.h.in
test/lib/Cluster.cpp

index bc7ecaa267c2082f35f71a9634ff8a62fe5bdbae..f57d12aea1b6951f92961e0a369050cef4f82c6c 100644 (file)
@@ -25,12 +25,14 @@ function(check_dependency_basic NAME LIB HEADER)
        if(FOUND)
                message("--   Found library '${LIB${LIB}}'")
                message("--   Found include '${${HEADER_CONST}_ROOT}'")
+        add_library(${NAME} INTERFACE IMPORTED)
+        set_target_properties(${NAME} PROPERTIES
+                INTERFACE_INCLUDE_DIRECTORIES ${${HEADER_CONST}_ROOT}
+                INTERFACE_LINK_LIBRARIES ${LIB${LIB}})
        else()
                message("--   No library '${LIB}' found")
        endif()
-       set(${NAME} ${FOUND} PARENT_SCOPE)
-       set(${NAME}_LIBRARIES ${LIB${LIB}} PARENT_SCOPE)
-       set(${NAME}_INCLUDEDIR ${${HEADER_CONST}_ROOT} PARENT_SCOPE)
+       set(${NAME}_FOUND ${FOUND} PARENT_SCOPE)
 endfunction()
 
 function(check_dependency NAME LIB HEADER)
@@ -39,19 +41,17 @@ function(check_dependency NAME LIB HEADER)
         if(NOT ${NAME}_FOUND)
             pkg_check_modules(${NAME} ${LIB}${ARGN})
         endif()
-               if(NOT ${NAME}_FOUND)
-                       check_dependency_basic(${NAME} ${LIB} ${HEADER})
-               else()
-                       set(${NAME} ${${NAME}_FOUND} PARENT_SCOPE)
-                       set(${NAME}_LIBRARIES ${${NAME}_LDFLAGS} PARENT_SCOPE)
-                       set(${NAME}_INCLUDEDIR ${${NAME}_INCLUDEDIR} PARENT_SCOPE)
+        if(${NAME}_FOUND)
+            add_library(${NAME} ALIAS PkgConfig::${NAME})
+        endif()
+    endif()
+    if(NOT ${NAME}_FOUND)
+        check_dependency_basic(${NAME} ${LIB} ${HEADER})
+    endif()
 
-                       safe_string(${LIB} LIB_CONST)
-                       set(HAVE_${NAME} ${${NAME}_FOUND} PARENT_SCOPE)
-                       safe_string(${HEADER} HEADER_CONST)
-                       find_path(HAVE_${HEADER_CONST} ${HEADER} PATHS ${${NAME}_INCLUDEDIR})
-               endif()
-    else()
-               check_dependency_basic(${NAME} ${LIB} ${HEADER})
+    set(HAVE_${NAME} ${${NAME}_FOUND} PARENT_SCOPE)
+    if(${NAME}_FOUND)
+        safe_string(${HEADER} HEADER_CONST)
+        find_path(HAVE_${HEADER_CONST} ${HEADER} PATHS ${${NAME}_INCLUDEDIR})
     endif()
 endfunction()
diff --git a/CMake/CheckTbb.cmake b/CMake/CheckTbb.cmake
new file mode 100644 (file)
index 0000000..1245e03
--- /dev/null
@@ -0,0 +1,20 @@
+function(CHECK_TBB)
+    check_header(execution)
+    # TBBConfig only sets TBB_FOUND to FALSE
+    if(HAVE_EXECUTION)
+        check_dependency(LIBTBB tbb tbb/task.h)
+        if(HAVE_LIBTBB)
+            cmake_push_check_state(RESET)
+            get_property(LIBTBB_INCLUDEDIR TARGET LIBTBB PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
+            get_property(LIBTBB_LIBRARIES TARGET LIBTBB PROPERTY INTERFACE_LINK_LIBRARIES)
+            set(CMAKE_REQUIRED_INCLUDES "${LIBTBB_INCLUDEDIR}")
+            set(CMAKE_REQUIRED_LIBRARIES "${LIBTBB_LIBRARIES}")
+            set(CMAKE_REQUIRED_FLAGS -std=c++17)
+            check_compiles(HAVE_TBB "std::vector<char> a={1,2,3}; std::all_of(std::execution::par,a.begin(),a.end(),[](char i){return i>0;});" vector algorithm execution)
+            cmake_pop_check_state(RESET)
+            if(HAVE_TBB)
+                set(HAVE_LIBTBB 1 PARENT_SCOPE)
+            endif()
+        endif()
+    endif()
+endfunction()
index d153d20b6e4928c627fa00bb12dc7b21edf2d76d..a1f8cf0d26b32d87a3d511a572809596127bc5bd 100644 (file)
@@ -1,7 +1,7 @@
 find_program(MEMCACHED_EXECUTABLE NAMES memcached
         HINTS $ENV{MEMCACHED_DIR}
         PATH_SUFFIXES bin
-        DOC "memcached(), Memcached daemon"
+        DOC "memcached(1), Memcached daemon"
         )
 
 include(FindPackageHandleStandardArgs)
index c9af10bab93777562c631ece7730d6d01be89e50..93f4fb4ed3dc23257fb78d2f170127b0c1de1cf5 100644 (file)
@@ -39,7 +39,7 @@ if(ENABLE_MEMASLAP)
     check_stdatomic()
     check_dependency(LIBEVENT event event.h)
     check_decl(getline stdio.h)
-    if(LIBEVENT AND HAVE_C_STDATOMIC)
+    if(HAVE_LIBEVENT AND HAVE_C_STDATOMIC)
         set(HAVE_MEMASLAP 1)
     endif()
 endif()
@@ -66,7 +66,7 @@ endif()
 ## sasl
 if(ENABLE_SASL)
     check_dependency(LIBSASL sasl2 sasl/sasl.h)
-    if(LIBSASL)
+    if(HAVE_LIBSASL)
         set(LIBMEMCACHED_WITH_SASL_SUPPORT 1)
     endif()
 endif()
@@ -128,6 +128,10 @@ check_decl(abi::__cxa_demangle cxxabi.h)
 find_package(Backtrace)
 if(Backtrace_FOUND)
     set(HAVE_BACKTRACE 1)
+    add_library(BACKTRACE INTERFACE IMPORTED)
+    set_target_properties(BACKTRACE PROPERTIES
+            INTERFACE_LINK_LIBRARIES "${Backtrace_LIBRARIES}"
+            INTERFACE_INCLUDE_DIRECTORIES "${Backtrace_INCLUDE_DIR}")
 endif()
 
 check_type(in_port_t netinet/in.h)
index 710f4cd0bf3ba4fd98f2671cb7c252d1bd2063b9..1f23c2a97dc0404468c536ad94c843cf8502e700 100644 (file)
@@ -78,7 +78,7 @@ set(HAVE_GCC_BUILTIN_ATOMIC 1)
 
 # modules
 
-set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/CMake)
+list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMake")
 
 if(NOT BUILD_DOCSONLY)
     include(CMake/_Include.cmake)
index 83bdaa017418e2e1da7ca279dd60a7c82eb4e812..18278f63f4824a6d47a07f2078d87ec4bf2ba935 100644 (file)
@@ -11,8 +11,7 @@ project(libmemcached
         DESCRIPTION "libmemcached https://github.com/m6w6/libmemcached"
         )
 
-set(CMAKE_CXX_STANDARD 11)
-set(CMAKE_CXX_STANDARD_REQUIRED ON)
+set(CXX_STANDARD 11)
 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
 
 include(CMakeConfig.txt)
index c6e11b4d166a4f82705de47589ce9b9d880edf82..988f4809a21806109e6916948aa15511aa5fce99 100644 (file)
@@ -2,6 +2,7 @@ add_subdirectory(common)
 
 foreach(CLIENT IN LISTS CLIENTS)
     add_executable(${CLIENT} ${CLIENT}.cc)
+    set_target_properties(${CLIENT} PROPERTIES CXX_STANDARD ${CXX_STANDARD})
     target_include_directories(${CLIENT} PRIVATE ..)
     target_link_libraries(${CLIENT} PRIVATE libclient_common)
     if(CMAKE_INSTALL_RPATH)
@@ -33,13 +34,7 @@ if(HAVE_MEMASLAP)
             memaslap/ms_task.c
             memaslap/ms_thread.c)
     target_include_directories(memaslap PRIVATE memaslap)
-    target_link_libraries(memaslap PRIVATE libclient_common Threads::Threads)
-    if(PKG_CONFIG_FOUND)
-        target_link_libraries(memaslap PRIVATE PkgConfig::LIBEVENT)
-    else()
-        target_include_directories(memaslap PRIVATE ${LIBEVENT_INCLUDEDIR})
-        target_link_libraries(memaslap PRIVATE ${LIBEVENT_LIBRARIES})
-    endif()
+    target_link_libraries(memaslap PRIVATE libclient_common Threads::Threads LIBEVENT)
     if(CMAKE_INSTALL_RPATH)
         set_target_properties(${CLIENT} PROPERTIES
                 INSTALL_RPATH ${CMAKE_INSTALL_RPATH}/../${CMAKE_INSTALL_LIBDIR})
index daeec57b9123aa7087b4e7c98368dca0792a88de..20377d8ba34477a1197c4ced95136893e0678064 100644 (file)
@@ -30,6 +30,7 @@ add_library(libhashkit SHARED
         )
 add_library(hashkit ALIAS libhashkit)
 set_target_properties(libhashkit PROPERTIES
+        CXX_STANDARD ${CXX_STANDARD}
         LIBRARY_OUTPUT_NAME hashkit
         LIBRARY_OUTPUT_NAME_DEBUG hashkit-dbg
         SOVERSION ${LIBHASHKIT_SO_VERSION}
index 6f0c6a6abfc9c1fc89880386a490860ea18b8890..4f48301153f36b1f77ee333958ddb07b01815dc0 100644 (file)
@@ -85,21 +85,19 @@ add_library(libmemcached SHARED
         ${LIBMEMCACHED_SOURCES})
 add_library(memcached ALIAS libmemcached)
 set_target_properties(libmemcached PROPERTIES
+        CXX_STANDARD ${CXX_STANDARD}
         LIBRARY_OUTPUT_NAME memcached
         LIBRARY_OUTPUT_NAME_DEBUG memcached-dbg
         SOVERSION ${LIBMEMCACHED_SO_VERSION}
         VERSION v${LIBMEMCACHED_VERSION})
 target_compile_definitions(libmemcached PRIVATE -DBUILDING_LIBMEMCACHED)
-target_link_libraries(libmemcached PUBLIC libhashkit Threads::Threads ${CMAKE_DL_LIBS} ${Backtrace_LIBRARIES})
-if(LIBSASL_FOUND)
-    if(PKG_CONFIG_FOUND)
-        target_link_libraries(libmemcached PUBLIC PkgConfig::LIBSASL)
-    else()
-        target_link_libraries(libmemcached PUBLIC ${LIBSASL_LIBRARIES})
-        target_include_directories(libmemcached PUBLIC ${LIBSASL_INCLUDEDIR})
-    endif()
+target_link_libraries(libmemcached PUBLIC libhashkit Threads::Threads ${CMAKE_DL_LIBS})
+if(HAVE_BACKTRACE)
+    target_link_libraries(libmemcached PRIVATE BACKTRACE)
+endif()
+if(HAVE_LIBSASL)
+    target_link_libraries(libmemcached PUBLIC LIBSASL)
 endif()
-target_include_directories(libmemcached PRIVATE ${Backtrace_INCLUDE_DIR})
 target_include_directories(libmemcached PRIVATE
         ${CMAKE_SOURCE_DIR}/src
         ${CMAKE_BINARY_DIR}/src
@@ -127,15 +125,17 @@ install_public_headers(
 add_library(libmemcachedinternal STATIC
         ${LIBMEMCACHED_SOURCES})
 add_library(memcachedinternal ALIAS libmemcachedinternal)
-set_target_properties(libmemcachedinternal PROPERTIES LIBRARY_OUTPUT_NAME memcachedinternal)
+set_target_properties(libmemcachedinternal PROPERTIES CXX_STANDARD ${CXX_STANDARD} LIBRARY_OUTPUT_NAME memcachedinternal)
 target_compile_definitions(libmemcachedinternal PRIVATE -DBUILDING_LIBMEMCACHEDINTERNAL)
-target_link_libraries(libmemcachedinternal PUBLIC libhashkit Threads::Threads ${LIBSASL_LIBRARIES} ${CMAKE_DL_LIBS} ${Backtrace_LIBRARIES})
+target_link_libraries(libmemcachedinternal PUBLIC libhashkit Threads::Threads ${CMAKE_DL_LIBS} ${Backtrace_LIBRARIES})
+if(HAVE_LIBSASL)
+    target_link_libraries(libmemcachedinternal PUBLIC LIBSASL)
+endif()
 target_include_directories(libmemcachedinternal PRIVATE ${Backtrace_INCLUDE_DIR})
 target_include_directories(libmemcachedinternal PRIVATE
         ${CMAKE_SOURCE_DIR}/src
         ${CMAKE_BINARY_DIR}/src
         ${CMAKE_BINARY_DIR})
-target_include_directories(libmemcachedinternal PUBLIC ${LIBSASL_INCLUDEDIR})
 target_include_directories(libmemcachedinternal PUBLIC
         $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
         $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>
index e239d21c95d821e385149fabe80c97d288eaf066..bf7c8ca976f4c6546154913cebfcf16495e6d893 100644 (file)
@@ -9,6 +9,7 @@ add_library(libmemcachedprotocol SHARED
         )
 add_library(memcachedprotocol ALIAS libmemcachedprotocol)
 set_target_properties(libmemcachedprotocol PROPERTIES
+        CXX_STANDARD ${CXX_STANDARD}
         LIBRARY_OUTPUT_NAME memcachedprotocol
         LIBRARY_OUTPUT_NAME_DEBUG memcachedprotocol-dbg
         SOVERSION ${LIBMEMCACHEDPROTOCOL_SO_VERSION}
index 7e2a6e67513068e99f2ed7e03793bc2a92ce9c79..328088008b5d5466bde7f7957d524e28f3e2877c 100644 (file)
@@ -10,6 +10,7 @@ add_library(libmemcachedutil SHARED
         )
 add_library(memcachedutil ALIAS libmemcachedutil)
 set_target_properties(libmemcachedutil PROPERTIES
+        CXX_STANDARD ${CXX_STANDARD}
         LIBRARY_OUTPUT_NAME memcachedutil
         LIBRARY_OUTPUT_NAME_DEBUG memcachedutil-dbg
         SOVERSION ${LIBMEMCACHEDUTIL_SO_VERSION}
@@ -21,11 +22,13 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL AppleClang)
                )
 endif()
 target_link_libraries(libmemcachedutil PUBLIC Threads::Threads)
+if(HAVE_LIBSASL)
+    target_link_libraries(libmemcachedutil PUBLIC LIBSASL)
+endif()
 target_include_directories(libmemcachedutil PRIVATE
         ${CMAKE_SOURCE_DIR}/src
         ${CMAKE_BINARY_DIR}/src
         ${CMAKE_BINARY_DIR})
-target_include_directories(libmemcachedutil PUBLIC ${LIBSASL_INCLUDEDIR})
 target_include_directories(libmemcachedutil PUBLIC
         $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
         $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>
index 4538512f6950aa9a62a473ba3d34b06e4915fd06..040a8c61550b31113f62c47fb9a815952b1d4812 100644 (file)
@@ -2,7 +2,9 @@ if(NOT BUILD_TESTING OR NOT CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
     return()
 endif()
 
-include(CMake/Catch.cmake)
+list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake")
+include(Catch)
+include(CheckTbb)
 
 if(NOT MEMCACHED_BINARY)
     message(SEND_ERROR "Could not find memcached(1) binary")
@@ -13,13 +15,6 @@ check_decl(pipe2 unistd.h)
 check_decl(SOCK_NONBLOCK sys/socket.h)
 check_decl(SOCK_CLOEXEC sys/socket.h)
 
-check_header(execution)
-
-# TBBConfig only sets TBB_FOUND to FALSE
-set(TBB_FOUND TRUE)
-set(TBB_FIND_RELEASE_ONLY TRUE)
-find_package(TBB)
-
 file(GLOB_RECURSE TESTING_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
 set(TESTING_ROOT ${CMAKE_CURRENT_BINARY_DIR})
 set_source_files_properties(main.cpp PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON)
@@ -32,11 +27,13 @@ target_include_directories(runtests PRIVATE
         ${CMAKE_SOURCE_DIR}/src
         ${CMAKE_BINARY_DIR}/src)
 target_link_libraries(runtests PRIVATE libhashkit libmemcachedinternal libmemcachedutil)
-if(TBB_FOUND AND HAVE_EXECUTION)
-    target_link_libraries(runtests PRIVATE TBB::tbb)
-else()
-    set(HAVE_EXECUTION 0)
+
+check_tbb()
+if(HAVE_TBB)
+    message("HAVE TBB")
+    target_link_libraries(runtests PRIVATE LIBTBB)
 endif()
+
 add_dependencies(runtests ${CLIENTS})
 if(HAVE_MEMASLAP)
     add_dependencies(runtests memaslap)
index b34f03a9adab3f6174cd5d585cf81e5969a5d8e7..284d965310d073043efc3f1e9920899c3e99cac7 100644 (file)
@@ -20,6 +20,7 @@
 #cmakedefine HAVE_SOCK_NONBLOCK 1
 #cmakedefine HAVE_SOCK_CLOEXEC 1
 #cmakedefine HAVE_EXECUTION 1
+#cmakedefine HAVE_TBB 1
 
 #cmakedefine TESTING_ROOT "@TESTING_ROOT@"
 #cmakedefine MEMCACHED_BINARY getenv_else("MEMCACHED_BINARY", "@MEMCACHED_BINARY@")
index 1589e68ccf6906f9f699b4924ba5e00289a0a937..37ec184e54d5f141ecd0c71b30b99e359d72f20d 100644 (file)
@@ -2,7 +2,7 @@
 #include "Retry.hpp"
 
 #include <algorithm>
-#if HAVE_EXECUTION
+#if HAVE_EXECUTION && HAVE_TBB
 #  include <execution>
 #endif
 #include <sys/wait.h>
@@ -56,7 +56,7 @@ void Cluster::stop(bool graceful) {
 
 bool Cluster::isStopped() {
   return none_of(
-#if HAVE_EXECUTION
+#if HAVE_EXECUTION && HAVE_TBB
     execution::par,
 #endif
       cluster.begin(), cluster.end(), [](Server &s) {
@@ -66,7 +66,7 @@ bool Cluster::isStopped() {
 
 bool Cluster::isListening() const {
   return all_of(
-#if HAVE_EXECUTION
+#if HAVE_EXECUTION && HAVE_TBB
     execution::par,
 #endif
       cluster.begin(), cluster.end(), [](const Server &s) {
@@ -79,7 +79,7 @@ bool Cluster::ensureListening() {
     return false;
   }
   auto listening = all_of(
-#if HAVE_EXECUTION
+#if HAVE_EXECUTION && HAVE_TBB
     execution::par,
 #endif
   cluster.begin(), cluster.end(), [](Server &s) {