From e6c7e0f521f38040c51b1115f5a9968a35fca87d Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sat, 28 Jul 2018 10:36:20 +0800 Subject: [PATCH] cmake: extract std::filesystem linkage checking into module see https://libcxx.llvm.org/docs/UsingLibcxx.html#using-libc-experimental-and-experimental, > Note that as of libc++ 7.0 using the > requires linking libc++fs instead of libc++experimental. do not build ceph_test_admin_socket_output if we are not able to find the library for std::experimental::filesystem . it is a workaround of https://reviews.freebsd.org/D10840 where FreeBSD 11.2 does not ship libc++experimental.a . Signed-off-by: Kefu Chai --- CMakeLists.txt | 12 -------- cmake/modules/FindStdFilesystem.cmake | 37 +++++++++++++++++++++++++ cmake/modules/FindStdFilesystem_test.cc | 13 +++++++++ librarytest.sh | 17 ------------ src/test/CMakeLists.txt | 25 +++++++---------- 5 files changed, 60 insertions(+), 44 deletions(-) create mode 100644 cmake/modules/FindStdFilesystem.cmake create mode 100644 cmake/modules/FindStdFilesystem_test.cc delete mode 100755 librarytest.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index f80020cd787..fecf25b4d9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -635,18 +635,6 @@ include_directories(SYSTEM ${PROJECT_BINARY_DIR}/include) find_package(Threads REQUIRED) -execute_process( - COMMAND ./librarytest.sh ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS} - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} - OUTPUT_VARIABLE CXX_STDLIB - ) - -if(CXX_STDLIB STREQUAL "libstdc++" OR CXX_STDLIB STREQUAL "libc++") - message(STATUS "We are using ${CXX_STDLIB}.") -else() - message(FATAL_ERROR - "Unable to determine C++ standard library, got ${CXX_STDLIB}.") -endif() option(WITH_SELINUX "build SELinux policy" OFF) if(WITH_SELINUX) diff --git a/cmake/modules/FindStdFilesystem.cmake b/cmake/modules/FindStdFilesystem.cmake new file mode 100644 index 00000000000..83bb9862aee --- /dev/null +++ b/cmake/modules/FindStdFilesystem.cmake @@ -0,0 +1,37 @@ +set(_std_filesystem_test_src + ${CMAKE_CURRENT_LIST_DIR}/FindStdFilesystem_test.cc) + +macro(try_std_filesystem_library _library _result) + try_compile(_std_filesystem_compiles + ${CMAKE_CURRENT_BINARY_DIR} + SOURCES ${_std_filesystem_test_src} + CMAKE_FLAGS -DCMAKE_CXX_FLAGS="-std=c++17" + LINK_LIBRARIES ${_library}) + if(_std_filesystem_compiles) + set(${_result} ${_library}) + endif() +endmacro() + + +if(NOT StdFilesystem_LIBRARY) + try_std_filesystem_library("stdc++fs" StdFilesystem_LIBRARY) +endif() +if(NOT StdFilesystem_LIBRARY) + try_std_filesystem_library("c++experimental" StdFilesystem_LIBRARY) +endif() +if(NOT StdFilesystem_LIBRARY) + try_std_filesystem_library("c++fs" StdFilesystem_LIBRARY) +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(StdFilesystem + FOUND_VAR StdFilesystem_FOUND + REQUIRED_VARS StdFilesystem_LIBRARY) + +mark_as_advanced(StdFilesystem_LIBRARY) + +if(StdFilesystem_FOUND AND NOT (TARGET StdFilesystem::filesystem)) + add_library(StdFilesystem::filesystem INTERFACE IMPORTED) + set_target_properties(StdFilesystem::filesystem PROPERTIES + INTERFACE_LINK_LIBRARIES ${StdFilesystem_LIBRARY}) +endif() diff --git a/cmake/modules/FindStdFilesystem_test.cc b/cmake/modules/FindStdFilesystem_test.cc new file mode 100644 index 00000000000..82f2a8b576b --- /dev/null +++ b/cmake/modules/FindStdFilesystem_test.cc @@ -0,0 +1,13 @@ +#if __has_include() +#include +namespace fs = std::filesystem; +#elif __has_include() +#include +namespace fs = std::experimental::filesystem; +#else +#error std::filesystem not available! +#endif + +int main() { + [[maybe_unused]] fs::path path("/"); +} diff --git a/librarytest.sh b/librarytest.sh deleted file mode 100755 index 12bc8c3cca9..00000000000 --- a/librarytest.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/sh - -CXX=$1 - -shift - -echo "#include -#if defined(_LIBCPP_VERSION) -#define MYRESULT libc++ -#elif defined(__GLIBCXX__) -#define MYRESULT libstdc++ -#else -#define MYRESULT unknown -#endif - -HelloFriendsTheAnsWerIs MYRESULT" | ${CXX} -E -xc++ $* - | \ - grep "HelloFriendsTheAnsWerIs" | cut -f 2 -d ' ' | tr -d '\n' diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 6ad9840ac05..28c5dc213cc 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -517,23 +517,18 @@ endif(HAVE_BLKID) # ceph_test_admin_socket_output -add_executable(ceph_test_admin_socket_output - test_admin_socket_output.cc - admin_socket_output.cc - admin_socket_output_tests.cc - ) -target_link_libraries(ceph_test_admin_socket_output - ceph-common) -if(CXX_STDLIB STREQUAL "libstdc++") +find_package(StdFilesystem) +if(StdFilesystem_FOUND) + add_executable(ceph_test_admin_socket_output + test_admin_socket_output.cc + admin_socket_output.cc + admin_socket_output_tests.cc) target_link_libraries(ceph_test_admin_socket_output - -lstdc++fs) -elseif(CXX_STDLIB STREQUAL "libc++") - target_link_libraries(ceph_test_admin_socket_output - -lc++experimental) + ceph-common StdFilesystem::filesystem) + install(TARGETS + ceph_test_admin_socket_output + DESTINATION ${CMAKE_INSTALL_BINDIR}) endif() -install(TARGETS - ceph_test_admin_socket_output - DESTINATION ${CMAKE_INSTALL_BINDIR}) #make check starts here -- 2.39.5