Skip to content

Commit f8ed530

Browse files
committedDec 4, 2020
ENH: filesystem detection
1 parent 2ec3375 commit f8ed530

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed
 

‎CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake/Modules)
77
check_insource()
88
setup_compiler()
99

10+
enable_filesystem()
1011
enable_MKL()
1112
enable_MPI()
1213
enable_OpenMP()

‎cmake/has_stdfs.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Check if std::filesystem is available
2+
// Source: https://stackoverflow.com/a/54290906
3+
// with modifications
4+
5+
#include <filesystem>
6+
7+
int main( int /*argc*/, char ** /*argv[]*/ ) {
8+
std::filesystem::path somepath{ "dir1/dir2/filename.txt" };
9+
auto fname = somepath.filename( );
10+
return 0;
11+
}

‎cmake/macros.cmake

+29-5
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@ macro(setup_compiler)
1515

1616
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
1717
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic-errors")
18-
19-
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0)
20-
link_libraries(stdc++fs)
21-
endif()
18+
2219
elseif (CMAKE_CXX_COMPILER_ID MATCHES Intel)
23-
# link_libraries(stdc++fs)
2420
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.0.1)
2521
message(FATAL_ERROR "Intel compiler is too old, besthea can be"
2622
" compiled only with icpc 19.0.1 or higher")
@@ -60,6 +56,34 @@ macro(setup_compiler)
6056
add_compile_definitions(DATA_WIDTH=${DATA_WIDTH})
6157
endmacro()
6258

59+
macro(enable_filesystem)
60+
# try without -lstd++fs
61+
try_run(RUNS_WITH_STDFS COMPILES_WITH_STDFS
62+
"${CMAKE_BINARY_DIR}/try"
63+
"${CMAKE_SOURCE_DIR}/cmake/has_stdfs.cpp"
64+
CMAKE_FLAGS -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD_REQUIRED=ON
65+
RUN_OUTPUT_VARIABLE TRY_OUTPUT)
66+
67+
if (NOT COMPILES_WITH_STDFS OR RUNS_WITH_STDFS STREQUAL "FAILED_TO_RUN")
68+
# try with -lstd++fs
69+
try_run(RUNS_WITH_STDFS COMPILES_WITH_STDFS
70+
"${CMAKE_BINARY_DIR}/try"
71+
"${CMAKE_SOURCE_DIR}/cmake/has_stdfs.cpp"
72+
CMAKE_FLAGS -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD_REQUIRED=ON
73+
LINK_LIBRARIES stdc++fs
74+
RUN_OUTPUT_VARIABLE TRY_OUTPUT)
75+
76+
if (NOT COMPILES_WITH_STDFS OR RUNS_WITH_STDFS STREQUAL "FAILED_TO_RUN")
77+
message(FATAL_ERROR "No std::filesystem support")
78+
else()
79+
message(STATUS "Found std::filesystem: libstdc++fs")
80+
link_libraries(stdc++fs)
81+
endif()
82+
else()
83+
message(STATUS "Found std::filesystem: libstdc++")
84+
endif()
85+
endmacro()
86+
6387
# also adds flag to linker, but we want to link against iomp5
6488
#macro(enable_OpenMP)
6589
# find_package(OpenMP REQUIRED)

‎test/test_ensight_export/test_ensight_export.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ int main( int argc, char * argv[] ) {
7272
}
7373

7474
for ( lo i = 0; i < mesh.get_n_elements( ); ++i ) {
75-
something_data[ i ] = sin( (double)i ) * cos( (double)ts );
75+
something_data[ i ] = sin( (double) i ) * cos( (double) ts );
7676
}
7777
}
7878

0 commit comments

Comments
 (0)
Please sign in to comment.