Skip to content

Commit ffc89d1

Browse files
committed
build: add support for std::filesystem
Add a macro to check if linking with -lstdc++fs or -lc++fs is required.
1 parent c194293 commit ffc89d1

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

build-aux/m4/l_filesystem.m4

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
dnl Copyright (c) 2022 The Bitcoin Core developers
2+
dnl Distributed under the MIT software license, see the accompanying
3+
dnl file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
# GCC 8.1 and earlier requires -lstdc++fs
6+
# Clang 8.0.0 (libc++) and earlier requires -lc++fs
7+
8+
m4_define([_CHECK_FILESYSTEM_testbody], [[
9+
#include <filesystem>
10+
11+
namespace fs = std::filesystem;
12+
13+
int main() {
14+
(void)fs::current_path().root_name();
15+
return 0;
16+
}
17+
]])
18+
19+
AC_DEFUN([CHECK_FILESYSTEM], [
20+
21+
AC_LANG_PUSH(C++)
22+
23+
AC_MSG_CHECKING([whether std::filesystem can be used without link library])
24+
25+
AC_LINK_IFELSE([AC_LANG_SOURCE([_CHECK_FILESYSTEM_testbody])],[
26+
AC_MSG_RESULT([yes])
27+
],[
28+
AC_MSG_RESULT([no])
29+
SAVED_LIBS="$LIBS"
30+
LIBS="$SAVED_LIBS -lstdc++fs"
31+
AC_MSG_CHECKING([whether std::filesystem needs -lstdc++fs])
32+
AC_LINK_IFELSE([AC_LANG_SOURCE([_CHECK_FILESYSTEM_testbody])],[
33+
AC_MSG_RESULT([yes])
34+
],[
35+
AC_MSG_RESULT([no])
36+
AC_MSG_CHECKING([whether std::filesystem needs -lc++fs])
37+
LIBS="$SAVED_LIBS -lc++fs"
38+
AC_LINK_IFELSE([AC_LANG_SOURCE([_CHECK_FILESYSTEM_testbody])],[
39+
AC_MSG_RESULT([yes])
40+
],[
41+
AC_MSG_FAILURE([cannot figure out how to use std::filesystem])
42+
])
43+
])
44+
])
45+
46+
AC_LANG_POP
47+
])

configure.ac

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ AX_CXX_COMPILE_STDCXX([17], [noext], [mandatory])
8383
dnl Check if -latomic is required for <std::atomic>
8484
CHECK_ATOMIC
8585

86+
dnl check if additional link flags are required for std::filesystem
87+
CHECK_FILESYSTEM
88+
8689
dnl Unless the user specified OBJCXX, force it to be the same as CXX. This ensures
8790
dnl that we get the same -std flags for both.
8891
m4_ifdef([AC_PROG_OBJCXX],[

0 commit comments

Comments
 (0)