From 09b40d9007ee169a18be67dd95e6114b53cbb88d Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Mon, 2 Nov 2020 10:38:08 +0100 Subject: [PATCH] cmake: improve dependency checks --- CMake/CheckDependency.cmake | 32 ++++++++++++------------- CMake/CheckTbb.cmake | 20 ++++++++++++++++ CMake/FindMemcached.cmake | 2 +- CMake/_Include.cmake | 8 +++++-- CMakeConfig.txt | 2 +- CMakeLists.txt | 3 +-- src/bin/CMakeLists.txt | 9 ++----- src/libhashkit/CMakeLists.txt | 1 + src/libmemcached/CMakeLists.txt | 24 +++++++++---------- src/libmemcachedprotocol/CMakeLists.txt | 1 + src/libmemcachedutil/CMakeLists.txt | 5 +++- test/CMakeLists.txt | 21 +++++++--------- test/conf.h.in | 1 + test/lib/Cluster.cpp | 8 +++---- 14 files changed, 79 insertions(+), 58 deletions(-) create mode 100644 CMake/CheckTbb.cmake diff --git a/CMake/CheckDependency.cmake b/CMake/CheckDependency.cmake index bc7ecaa2..f57d12ae 100644 --- a/CMake/CheckDependency.cmake +++ b/CMake/CheckDependency.cmake @@ -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 index 00000000..1245e03b --- /dev/null +++ b/CMake/CheckTbb.cmake @@ -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 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() diff --git a/CMake/FindMemcached.cmake b/CMake/FindMemcached.cmake index d153d20b..a1f8cf0d 100644 --- a/CMake/FindMemcached.cmake +++ b/CMake/FindMemcached.cmake @@ -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) diff --git a/CMake/_Include.cmake b/CMake/_Include.cmake index c9af10ba..93f4fb4e 100644 --- a/CMake/_Include.cmake +++ b/CMake/_Include.cmake @@ -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) diff --git a/CMakeConfig.txt b/CMakeConfig.txt index 710f4cd0..1f23c2a9 100644 --- a/CMakeConfig.txt +++ b/CMakeConfig.txt @@ -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) diff --git a/CMakeLists.txt b/CMakeLists.txt index 83bdaa01..18278f63 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/src/bin/CMakeLists.txt b/src/bin/CMakeLists.txt index c6e11b4d..988f4809 100644 --- a/src/bin/CMakeLists.txt +++ b/src/bin/CMakeLists.txt @@ -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}) diff --git a/src/libhashkit/CMakeLists.txt b/src/libhashkit/CMakeLists.txt index daeec57b..20377d8b 100644 --- a/src/libhashkit/CMakeLists.txt +++ b/src/libhashkit/CMakeLists.txt @@ -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} diff --git a/src/libmemcached/CMakeLists.txt b/src/libmemcached/CMakeLists.txt index 6f0c6a6a..4f483011 100644 --- a/src/libmemcached/CMakeLists.txt +++ b/src/libmemcached/CMakeLists.txt @@ -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 $ $ diff --git a/src/libmemcachedprotocol/CMakeLists.txt b/src/libmemcachedprotocol/CMakeLists.txt index e239d21c..bf7c8ca9 100644 --- a/src/libmemcachedprotocol/CMakeLists.txt +++ b/src/libmemcachedprotocol/CMakeLists.txt @@ -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} diff --git a/src/libmemcachedutil/CMakeLists.txt b/src/libmemcachedutil/CMakeLists.txt index 7e2a6e67..32808800 100644 --- a/src/libmemcachedutil/CMakeLists.txt +++ b/src/libmemcachedutil/CMakeLists.txt @@ -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 $ $ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4538512f..040a8c61 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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) diff --git a/test/conf.h.in b/test/conf.h.in index b34f03a9..284d9653 100644 --- a/test/conf.h.in +++ b/test/conf.h.in @@ -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@") diff --git a/test/lib/Cluster.cpp b/test/lib/Cluster.cpp index 1589e68c..37ec184e 100644 --- a/test/lib/Cluster.cpp +++ b/test/lib/Cluster.cpp @@ -2,7 +2,7 @@ #include "Retry.hpp" #include -#if HAVE_EXECUTION +#if HAVE_EXECUTION && HAVE_TBB # include #endif #include @@ -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) { -- 2.30.2