|
| 1 | +# Verbatim copy of https://github.com/lethal-guitar/RigelEngine/blob/011976b3feb35a7f038e9fe10c622e818b417e45/cmake/Modules/FindFilesystem.cmake |
| 2 | + |
| 3 | +# START OF FILE |
| 4 | + |
| 5 | +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 6 | +# file Copyright.txt or https://cmake.org/licensing for details. |
| 7 | + |
| 8 | +# This is copied from: |
| 9 | +# https://github.com/vector-of-bool/CMakeCM/blob/master/modules/FindFilesystem.cmake |
| 10 | + |
| 11 | +#[=======================================================================[.rst: |
| 12 | +
|
| 13 | +FindFilesystem |
| 14 | +############## |
| 15 | +
|
| 16 | +This module supports the C++17 standard library's filesystem utilities. Use the |
| 17 | +:imp-target:`std::filesystem` imported target to |
| 18 | +
|
| 19 | +Options |
| 20 | +******* |
| 21 | +
|
| 22 | +The ``COMPONENTS`` argument to this module supports the following values: |
| 23 | +
|
| 24 | +.. find-component:: Experimental |
| 25 | + :name: fs.Experimental |
| 26 | +
|
| 27 | + Allows the module to find the "experimental" Filesystem TS version of the |
| 28 | + Filesystem library. This is the library that should be used with the |
| 29 | + ``std::experimental::filesystem`` namespace. |
| 30 | +
|
| 31 | +.. find-component:: Final |
| 32 | + :name: fs.Final |
| 33 | +
|
| 34 | + Finds the final C++17 standard version of the filesystem library. |
| 35 | +
|
| 36 | +If no components are provided, behaves as if the |
| 37 | +:find-component:`fs.Final` component was specified. |
| 38 | +
|
| 39 | +If both :find-component:`fs.Experimental` and :find-component:`fs.Final` are |
| 40 | +provided, first looks for ``Final``, and falls back to ``Experimental`` in case |
| 41 | +of failure. If ``Final`` is found, :imp-target:`std::filesystem` and all |
| 42 | +:ref:`variables <fs.variables>` will refer to the ``Final`` version. |
| 43 | +
|
| 44 | +
|
| 45 | +Imported Targets |
| 46 | +**************** |
| 47 | +
|
| 48 | +.. imp-target:: std::filesystem |
| 49 | +
|
| 50 | + The ``std::filesystem`` imported target is defined when any requested |
| 51 | + version of the C++ filesystem library has been found, whether it is |
| 52 | + *Experimental* or *Final*. |
| 53 | +
|
| 54 | + If no version of the filesystem library is available, this target will not |
| 55 | + be defined. |
| 56 | +
|
| 57 | + .. note:: |
| 58 | + This target has ``cxx_std_17`` as an ``INTERFACE`` |
| 59 | + :ref:`compile language standard feature <req-lang-standards>`. Linking |
| 60 | + to this target will automatically enable C++17 if no later standard |
| 61 | + version is already required on the linking target. |
| 62 | +
|
| 63 | +
|
| 64 | +.. _fs.variables: |
| 65 | +
|
| 66 | +Variables |
| 67 | +********* |
| 68 | +
|
| 69 | +.. variable:: CXX_FILESYSTEM_IS_EXPERIMENTAL |
| 70 | +
|
| 71 | + Set to ``TRUE`` when the :find-component:`fs.Experimental` version of C++ |
| 72 | + filesystem library was found, otherwise ``FALSE``. |
| 73 | +
|
| 74 | +.. variable:: CXX_FILESYSTEM_HAVE_FS |
| 75 | +
|
| 76 | + Set to ``TRUE`` when a filesystem header was found. |
| 77 | +
|
| 78 | +.. variable:: CXX_FILESYSTEM_HEADER |
| 79 | +
|
| 80 | + Set to either ``filesystem`` or ``experimental/filesystem`` depending on |
| 81 | + whether :find-component:`fs.Final` or :find-component:`fs.Experimental` was |
| 82 | + found. |
| 83 | +
|
| 84 | +.. variable:: CXX_FILESYSTEM_NAMESPACE |
| 85 | +
|
| 86 | + Set to either ``std::filesystem`` or ``std::experimental::filesystem`` |
| 87 | + depending on whether :find-component:`fs.Final` or |
| 88 | + :find-component:`fs.Experimental` was found. |
| 89 | +
|
| 90 | +
|
| 91 | +Examples |
| 92 | +******** |
| 93 | +
|
| 94 | +Using `find_package(Filesystem)` with no component arguments: |
| 95 | +
|
| 96 | +.. code-block:: cmake |
| 97 | +
|
| 98 | + find_package(Filesystem REQUIRED) |
| 99 | +
|
| 100 | + add_executable(my-program main.cpp) |
| 101 | + target_link_libraries(my-program PRIVATE std::filesystem) |
| 102 | +
|
| 103 | +
|
| 104 | +#]=======================================================================] |
| 105 | + |
| 106 | + |
| 107 | +if(TARGET std::filesystem) |
| 108 | + # This module has already been processed. Don't do it again. |
| 109 | + return() |
| 110 | +endif() |
| 111 | + |
| 112 | +cmake_policy(PUSH) |
| 113 | +if(POLICY CMP0067) |
| 114 | + # pass CMAKE_CXX_STANDARD to check_cxx_source_compiles() |
| 115 | + # has to appear before including CheckCXXSourceCompiles module |
| 116 | + cmake_policy(SET CMP0067 NEW) |
| 117 | +endif() |
| 118 | + |
| 119 | +include(CMakePushCheckState) |
| 120 | +include(CheckIncludeFileCXX) |
| 121 | +include(CheckCXXSourceCompiles) |
| 122 | + |
| 123 | +cmake_push_check_state() |
| 124 | + |
| 125 | +set(CMAKE_REQUIRED_QUIET ${Filesystem_FIND_QUIETLY}) |
| 126 | + |
| 127 | +# All of our tests required C++17 or later |
| 128 | +set(CMAKE_CXX_STANDARD 17) |
| 129 | + |
| 130 | +# Normalize and check the component list we were given |
| 131 | +set(want_components ${Filesystem_FIND_COMPONENTS}) |
| 132 | +if(Filesystem_FIND_COMPONENTS STREQUAL "") |
| 133 | + set(want_components Final) |
| 134 | +endif() |
| 135 | + |
| 136 | +# Warn on any unrecognized components |
| 137 | +set(extra_components ${want_components}) |
| 138 | +list(REMOVE_ITEM extra_components Final Experimental) |
| 139 | +foreach(component IN LISTS extra_components) |
| 140 | + message(WARNING "Extraneous find_package component for Filesystem: ${component}") |
| 141 | +endforeach() |
| 142 | + |
| 143 | +# Detect which of Experimental and Final we should look for |
| 144 | +set(find_experimental TRUE) |
| 145 | +set(find_final TRUE) |
| 146 | +if(NOT "Final" IN_LIST want_components) |
| 147 | + set(find_final FALSE) |
| 148 | +endif() |
| 149 | +if(NOT "Experimental" IN_LIST want_components) |
| 150 | + set(find_experimental FALSE) |
| 151 | +endif() |
| 152 | + |
| 153 | +if(find_final) |
| 154 | + check_include_file_cxx("filesystem" _CXX_FILESYSTEM_HAVE_HEADER) |
| 155 | + mark_as_advanced(_CXX_FILESYSTEM_HAVE_HEADER) |
| 156 | + if(_CXX_FILESYSTEM_HAVE_HEADER) |
| 157 | + # We found the non-experimental header. Don't bother looking for the |
| 158 | + # experimental one. |
| 159 | + set(find_experimental FALSE) |
| 160 | + endif() |
| 161 | +else() |
| 162 | + set(_CXX_FILESYSTEM_HAVE_HEADER FALSE) |
| 163 | +endif() |
| 164 | + |
| 165 | +if(find_experimental) |
| 166 | + check_include_file_cxx("experimental/filesystem" _CXX_FILESYSTEM_HAVE_EXPERIMENTAL_HEADER) |
| 167 | + mark_as_advanced(_CXX_FILESYSTEM_HAVE_EXPERIMENTAL_HEADER) |
| 168 | +else() |
| 169 | + set(_CXX_FILESYSTEM_HAVE_EXPERIMENTAL_HEADER FALSE) |
| 170 | +endif() |
| 171 | + |
| 172 | +if(_CXX_FILESYSTEM_HAVE_HEADER) |
| 173 | + set(_have_fs TRUE) |
| 174 | + set(_fs_header filesystem) |
| 175 | + set(_fs_namespace std::filesystem) |
| 176 | +elseif(_CXX_FILESYSTEM_HAVE_EXPERIMENTAL_HEADER) |
| 177 | + set(_have_fs TRUE) |
| 178 | + set(_fs_header experimental/filesystem) |
| 179 | + set(_fs_namespace std::experimental::filesystem) |
| 180 | +else() |
| 181 | + set(_have_fs FALSE) |
| 182 | +endif() |
| 183 | + |
| 184 | +set(CXX_FILESYSTEM_HAVE_FS ${_have_fs} CACHE BOOL "TRUE if we have the C++ filesystem headers") |
| 185 | +set(CXX_FILESYSTEM_HEADER ${_fs_header} CACHE STRING "The header that should be included to obtain the filesystem APIs") |
| 186 | +set(CXX_FILESYSTEM_NAMESPACE ${_fs_namespace} CACHE STRING "The C++ namespace that contains the filesystem APIs") |
| 187 | + |
| 188 | +set(_found FALSE) |
| 189 | + |
| 190 | +if(CXX_FILESYSTEM_HAVE_FS) |
| 191 | + # We have some filesystem library available. Do link checks |
| 192 | + string(CONFIGURE [[ |
| 193 | + #include <@CXX_FILESYSTEM_HEADER@> |
| 194 | + |
| 195 | + int main() { |
| 196 | + auto cwd = @CXX_FILESYSTEM_NAMESPACE@::current_path(); |
| 197 | + return static_cast<int>(cwd.string().size()); |
| 198 | + } |
| 199 | + ]] code @ONLY) |
| 200 | + |
| 201 | + # Try to compile a simple filesystem program without any linker flags |
| 202 | + check_cxx_source_compiles("${code}" CXX_FILESYSTEM_NO_LINK_NEEDED) |
| 203 | + |
| 204 | + set(can_link ${CXX_FILESYSTEM_NO_LINK_NEEDED}) |
| 205 | + |
| 206 | + if(NOT CXX_FILESYSTEM_NO_LINK_NEEDED) |
| 207 | + set(prev_libraries ${CMAKE_REQUIRED_LIBRARIES}) |
| 208 | + # Add the libstdc++ flag |
| 209 | + set(CMAKE_REQUIRED_LIBRARIES ${prev_libraries} -lstdc++fs) |
| 210 | + check_cxx_source_compiles("${code}" CXX_FILESYSTEM_STDCPPFS_NEEDED) |
| 211 | + set(can_link ${CXX_FILESYSTEM_STDCPPFS_NEEDED}) |
| 212 | + if(NOT CXX_FILESYSTEM_STDCPPFS_NEEDED) |
| 213 | + # Try the libc++ flag |
| 214 | + set(CMAKE_REQUIRED_LIBRARIES ${prev_libraries} -lc++fs) |
| 215 | + check_cxx_source_compiles("${code}" CXX_FILESYSTEM_CPPFS_NEEDED) |
| 216 | + set(can_link ${CXX_FILESYSTEM_CPPFS_NEEDED}) |
| 217 | + endif() |
| 218 | + endif() |
| 219 | + |
| 220 | + if(can_link) |
| 221 | + add_library(std::filesystem INTERFACE IMPORTED) |
| 222 | + target_compile_features(std::filesystem INTERFACE cxx_std_17) |
| 223 | + set(_found TRUE) |
| 224 | + if(CXX_FILESYSTEM_NO_LINK_NEEDED) |
| 225 | + # on certain linux distros we have a version of libstdc++ which has the final code for c++17 fs in the |
| 226 | + # libstdc++.so.*. BUT when compiling with g++ < 9, we MUST still link with libstdc++fs.a |
| 227 | + # libc++ should not suffer from this issue, so, in theory we should be fine with only checking for |
| 228 | + # GCC's libstdc++ |
| 229 | + if((CMAKE_CXX_COMPILER_ID MATCHES "GNU") AND (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0.0")) |
| 230 | + target_link_libraries(std::filesystem INTERFACE -lstdc++fs) |
| 231 | + endif() |
| 232 | + elseif(CXX_FILESYSTEM_STDCPPFS_NEEDED) |
| 233 | + target_link_libraries(std::filesystem INTERFACE -lstdc++fs) |
| 234 | + elseif(CXX_FILESYSTEM_CPPFS_NEEDED) |
| 235 | + target_link_libraries(std::filesystem INTERFACE -lc++fs) |
| 236 | + endif() |
| 237 | + endif() |
| 238 | +endif() |
| 239 | + |
| 240 | +cmake_pop_check_state() |
| 241 | + |
| 242 | +set(Filesystem_FOUND ${_found} CACHE BOOL "TRUE if we can compile and link a program using std::filesystem" FORCE) |
| 243 | + |
| 244 | +if(Filesystem_FIND_REQUIRED AND NOT Filesystem_FOUND) |
| 245 | + message(FATAL_ERROR "Cannot Compile simple program using std::filesystem") |
| 246 | +endif() |
| 247 | + |
| 248 | +cmake_policy(POP) |
0 commit comments