]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cmake: fix StdFilesystem detection
authorKefu Chai <kchai@redhat.com>
Thu, 2 Aug 2018 05:45:51 +0000 (13:45 +0800)
committerKefu Chai <kchai@redhat.com>
Thu, 2 Aug 2018 06:37:49 +0000 (14:37 +0800)
before this change, we fails to detect std::filesystem with clang++,
because  the cmake project created by try_compile() only expands
following options passed from its caller:
- COMPILE_DEFINITIONS
- INCLUDE_DIRECTORIES
- LINK_DIRECTORIES
- LINK_LIBRARIES

which do not include CMAKE_CXX_FLAGS, so either we need to (ab)use
COMPILE_DEFINITIONS for passing -std=c++17, or we can change the
CMAKE_CXX_FLAGS in the parent env, as it turns out the created cmake
project does inherit this flag from current project. in this change,
we use the COMPILE_DEFINITIONS approach: simpler this way. and we
can drop it once cmake 3.8 is required.

Signed-off-by: Kefu Chai <kchai@redhat.com>
cmake/modules/FindStdFilesystem.cmake

index 83bb9862aeea4d05a5b6c8caaa90b4612c965c34..6a6cd029da23d2726f06d7b5a0fa3e1b302382f7 100644 (file)
@@ -2,14 +2,26 @@ set(_std_filesystem_test_src
   ${CMAKE_CURRENT_LIST_DIR}/FindStdFilesystem_test.cc)
 
 macro(try_std_filesystem_library _library _result)
+  if(CMAKE_VERSION VERSION_LESS "3.8")
+    # abuse the definition flags, because they are quite
+    # the same as CMAKE_C_FLAGS: they are passed to the
+    # compiler.
+    set(_std_filesystem_try_compile_arg
+      COMPILE_DEFINITIONS "-std=c++17")
+  else()
+    set(_std_filesystem_try_compile_arg
+      CXX_STANDARD 17)
+  endif()
   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})
+    LINK_LIBRARIES ${_library}
+    ${_std_filesystem_try_compile_arg})
+  unset(_std_filesystem_try_compile_arg)
   if(_std_filesystem_compiles)
     set(${_result} ${_library})
   endif()
+  unset(_std_filesystem_compiles)
 endmacro()
 
 
@@ -23,6 +35,8 @@ if(NOT StdFilesystem_LIBRARY)
   try_std_filesystem_library("c++fs" StdFilesystem_LIBRARY)
 endif()
 
+unset(_std_filesystem_test_src)
+
 include(FindPackageHandleStandardArgs)
 find_package_handle_standard_args(StdFilesystem
   FOUND_VAR StdFilesystem_FOUND