|
| 1 | +# Copyright 2020, Collabora, Ltd. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: BSL-1.0 |
| 4 | + |
| 5 | +if(MSVC AND MSVC_VERSION GREATER 1890) |
| 6 | + set(HAVE_FILESYSTEM_WITHOUT_LIB |
| 7 | + ON |
| 8 | + CACHE INTERNAL "" FORCE) |
| 9 | + if(MSVC_VERSION GREATER 1910) |
| 10 | + # Visual Studio 2017 Update 3 added new filesystem impl, |
| 11 | + # which only works in C++17 mode. |
| 12 | + set(HAVE_FILESYSTEM_NEEDS_17 |
| 13 | + ON |
| 14 | + CACHE INTERNAL "" FORCE) |
| 15 | + endif() |
| 16 | +else() |
| 17 | + include(CheckCXXSourceCompiles) |
| 18 | + |
| 19 | + # This is just dummy code that is known to not compile if std::filesystem isn't working right |
| 20 | + set(_stdfs_test_source |
| 21 | + "int main() { |
| 22 | + (void)is_regular_file(\"/\"); |
| 23 | + return 0; |
| 24 | + } |
| 25 | + " |
| 26 | + ) |
| 27 | + set(_stdfs_conditions |
| 28 | + "// If the C++ macro is set to the version containing C++17, it must support |
| 29 | + // the final C++17 package |
| 30 | + #if __cplusplus >= 201703L |
| 31 | + #define USE_FINAL_FS 1 |
| 32 | +
|
| 33 | + #elif defined(_MSC_VER) && _MSC_VER >= 1900 |
| 34 | +
|
| 35 | + #if defined(_HAS_CXX17) && _HAS_CXX17 |
| 36 | + // When MSC supports c++17 use <filesystem> package. |
| 37 | + #define USE_FINAL_FS 1 |
| 38 | + #endif // !_HAS_CXX17 |
| 39 | +
|
| 40 | + // Right now, GCC still only supports the experimental filesystem items starting in GCC 6 |
| 41 | + #elif (__GNUC__ >= 6) |
| 42 | + #define USE_EXPERIMENTAL_FS 1 |
| 43 | +
|
| 44 | + // If Clang, check for feature support |
| 45 | + #elif defined(__clang__) && (__cpp_lib_filesystem || __cpp_lib_experimental_filesystem) |
| 46 | + #if __cpp_lib_filesystem |
| 47 | + #define USE_FINAL_FS 1 |
| 48 | + #else |
| 49 | + #define USE_EXPERIMENTAL_FS 1 |
| 50 | + #endif |
| 51 | +
|
| 52 | + #endif |
| 53 | + ") |
| 54 | + set(_stdfs_source |
| 55 | + "${_stdfs_conditions} |
| 56 | + #ifdef USE_FINAL_FS |
| 57 | + #include <filesystem> |
| 58 | + using namespace std::filesystem; |
| 59 | + #endif |
| 60 | + ${_stdfs_test_source} |
| 61 | + " |
| 62 | + ) |
| 63 | + set(_stdfs_experimental_source |
| 64 | + "${_stdfs_conditions} |
| 65 | + #ifdef USE_EXPERIMENTAL_FS |
| 66 | + #include <experimental/filesystem> |
| 67 | + using namespace std::experimental::filesystem; |
| 68 | + #endif |
| 69 | + ${_stdfs_test_source} |
| 70 | + " |
| 71 | + ) |
| 72 | + set(_stdfs_needlib_source |
| 73 | + "${_stdfs_conditions} |
| 74 | + #ifdef USE_FINAL_FS |
| 75 | + #include <filesystem> |
| 76 | + using namespace std::filesystem; |
| 77 | + #endif |
| 78 | + #ifdef USE_EXPERIMENTAL_FS |
| 79 | + #include <experimental/filesystem> |
| 80 | + using namespace std::experimental::filesystem; |
| 81 | + #endif |
| 82 | + ${_stdfs_test_source} |
| 83 | + " |
| 84 | + ) |
| 85 | + |
| 86 | + # First, just look for the include. |
| 87 | + # We're checking if it compiles, not if the include exists, |
| 88 | + # because the source code uses similar conditionals to decide. |
| 89 | + set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) |
| 90 | + unset(CMAKE_REQUIRED_LIBRARIES) |
| 91 | + unset(CMAKE_REQUIRED_FLAGS) |
| 92 | + check_cxx_source_compiles("${_stdfs_source}" HAVE_FILESYSTEM_IN_STD) |
| 93 | + check_cxx_source_compiles("${_stdfs_experimental_source}" HAVE_FILESYSTEM_IN_STDEXPERIMENTAL) |
| 94 | + |
| 95 | + set(CMAKE_REQUIRED_FLAGS "-DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD_REQUIRED=TRUE") |
| 96 | + check_cxx_source_compiles("${_stdfs_source}" HAVE_FILESYSTEM_IN_STD_17) |
| 97 | + unset(CMAKE_REQUIRED_FLAGS) |
| 98 | + |
| 99 | + if(HAVE_FILESYSTEM_IN_STD_17 AND NOT HAVE_FILESYSTEM_IN_STD) |
| 100 | + set(HAVE_FILESYSTEM_NEEDS_17 |
| 101 | + ON |
| 102 | + CACHE INTERNAL "" |
| 103 | + ) |
| 104 | + set(CMAKE_REQUIRED_FLAGS "-DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD_REQUIRED=TRUE") |
| 105 | + else() |
| 106 | + set(HAVE_FILESYSTEM_NEEDS_17 |
| 107 | + OFF |
| 108 | + CACHE INTERNAL "" |
| 109 | + ) |
| 110 | + endif() |
| 111 | + |
| 112 | + # Now, see if we need libstdc++fs |
| 113 | + set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE) |
| 114 | + check_cxx_source_compiles("${_stdfs_needlib_source}" HAVE_FILESYSTEM_WITHOUT_LIB) |
| 115 | + set(CMAKE_REQUIRED_LIBRARIES stdc++fs) |
| 116 | + check_cxx_source_compiles("${_stdfs_needlib_source}" HAVE_FILESYSTEM_NEEDING_LIB) |
| 117 | + unset(CMAKE_REQUIRED_LIBRARIES) |
| 118 | + unset(CMAKE_TRY_COMPILE_TARGET_TYPE) |
| 119 | + |
| 120 | +endif() |
| 121 | + |
| 122 | +function(openxr_add_filesystem_utils TARGET_NAME) |
| 123 | + target_sources(${TARGET_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/src/common/filesystem_utils.cpp) |
| 124 | + target_include_directories(${TARGET_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/src/common) |
| 125 | + if(NOT BUILD_WITH_STD_FILESYSTEM) |
| 126 | + target_compile_definitions(${TARGET_NAME} PRIVATE DISABLE_STD_FILESYSTEM) |
| 127 | + else() |
| 128 | + if(HAVE_FILESYSTEM_NEEDS_17) |
| 129 | + set_property(TARGET ${TARGET_NAME} PROPERTY CXX_STANDARD 17) |
| 130 | + set_property(TARGET ${TARGET_NAME} PROPERTY CXX_STANDARD_REQUIRED TRUE) |
| 131 | + endif() |
| 132 | + if(HAVE_FILESYSTEM_NEEDING_LIB AND NOT HAVE_FILESYSTEM_WITHOUT_LIB) |
| 133 | + target_link_libraries(${TARGET_NAME} PRIVATE stdc++fs) |
| 134 | + endif() |
| 135 | + endif() |
| 136 | +endfunction() |
0 commit comments