Skip to content

Commit

Permalink
cmake: Add CheckStdFilesystem module
Browse files Browse the repository at this point in the history
  • Loading branch information
hebasto committed Feb 28, 2023
1 parent 6317164 commit a06290e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ include(cmake/introspection.cmake)
include(cmake/crc32c.cmake)
include(cmake/leveldb.cmake)

include(CheckStdFilesystem)
check_std_filesystem()

add_subdirectory(src)

message("\n")
Expand Down
36 changes: 36 additions & 0 deletions cmake/module/CheckStdFilesystem.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (c) 2023 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

# GCC 8.x (libstdc++) requires -lstdc++fs
# Clang 8.x (libc++) requires -lc++fs

function(check_std_filesystem)
set(source "
#include <filesystem>
int main()
{
(void)std::filesystem::current_path().root_name();
}
")

include(CheckCXXSourceCompiles)
check_cxx_source_links("${source}" STD_FILESYSTEM_NO_EXTRA_LIBS_NEEDED)
if(STD_FILESYSTEM_NO_EXTRA_LIBS_NEEDED)
return()
endif()

add_library(std_filesystem INTERFACE)
check_cxx_source_links_with_libs(stdc++fs "${source}" STD_FILESYSTEM_NEEDS_LINK_TO_LIBSTDCXXFS)
if(STD_FILESYSTEM_NEEDS_LINK_TO_LIBSTDCXXFS)
target_link_libraries(std_filesystem INTERFACE stdc++fs)
return()
endif()
check_cxx_source_links_with_libs(c++fs "${source}" STD_FILESYSTEM_NEEDS_LINK_TO_LIBCXXFS)
if(STD_FILESYSTEM_NEEDS_LINK_TO_LIBCXXFS)
target_link_libraries(std_filesystem INTERFACE c++fs)
return()
endif()
message(FATAL_ERROR "Cannot figure out how to use std::filesystem.")
endfunction()

0 comments on commit a06290e

Please sign in to comment.