]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
cmake: extract std::filesystem linkage checking into module
authorKefu Chai <kchai@redhat.com>
Sat, 28 Jul 2018 02:36:20 +0000 (10:36 +0800)
committerKefu Chai <kchai@redhat.com>
Sun, 29 Jul 2018 12:25:32 +0000 (20:25 +0800)
see
https://libcxx.llvm.org/docs/UsingLibcxx.html#using-libc-experimental-and-experimental,

> Note that as of libc++ 7.0 using the <experimental/filesystem>
> 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 <kchai@redhat.com>
CMakeLists.txt
cmake/modules/FindStdFilesystem.cmake [new file with mode: 0644]
cmake/modules/FindStdFilesystem_test.cc [new file with mode: 0644]
librarytest.sh [deleted file]
src/test/CMakeLists.txt

index f80020cd787516159f8b71a91099a5c70491676e..fecf25b4d9e058c9cf18b2d490b9f30c953eba21 100644 (file)
@@ -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 (file)
index 0000000..83bb986
--- /dev/null
@@ -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 (file)
index 0000000..82f2a8b
--- /dev/null
@@ -0,0 +1,13 @@
+#if __has_include(<filesystem>)
+#include <filesystem>
+namespace fs = std::filesystem;
+#elif __has_include(<experimental/filesystem>)
+#include <experimental/filesystem>
+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 (executable)
index 12bc8c3..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-#!/bin/sh
-
-CXX=$1
-
-shift
-
-echo "#include <utility>
-#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'
index 6ad9840ac059b6381e60f521284025c0de24b239..28c5dc213cc620415e03526c308eb60bcc55b416 100644 (file)
@@ -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