summaryrefslogtreecommitdiffstats
path: root/cmake/modules/FindStdFilesystem.cmake
blob: 6a6cd029da23d2726f06d7b5a0fa3e1b302382f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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}
    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()


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()

unset(_std_filesystem_test_src)

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()