diff --git a/CMakeLists.txt b/CMakeLists.txt index 4dd91c1..8b9150b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,8 @@ include(FetchContent) #Set default build type if none was specified include(cmake/helpers.cmake) default_build_type("Release") +link_std_fs() +message(STATUS fs_lib:${STD_FS_LIB}) set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) @@ -310,7 +312,15 @@ target_include_directories(aare_core PUBLIC "$" ) -target_link_libraries(aare_core PUBLIC fmt::fmt nlohmann_json::nlohmann_json PRIVATE aare_compiler_flags ) +target_link_libraries( + aare_core + PUBLIC + fmt::fmt + nlohmann_json::nlohmann_json + ${STD_FS_LIB} # from helpers.cmake + PRIVATE + aare_compiler_flags +) set_target_properties(aare_core PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} diff --git a/cmake/helpers.cmake b/cmake/helpers.cmake index 2a6dfb7..9e27234 100644 --- a/cmake/helpers.cmake +++ b/cmake/helpers.cmake @@ -4,3 +4,43 @@ if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) set(CMAKE_BUILD_TYPE ${val} CACHE STRING "Build type (default ${val})" FORCE) endif() endfunction() + +function(link_std_fs) +# from pybind11 +# Check if we need to add -lstdc++fs or -lc++fs or nothing +if(DEFINED CMAKE_CXX_STANDARD AND CMAKE_CXX_STANDARD LESS 17) + set(STD_FS_NO_LIB_NEEDED TRUE) +elseif(MSVC) + set(STD_FS_NO_LIB_NEEDED TRUE) +else() + file( + WRITE ${CMAKE_CURRENT_BINARY_DIR}/main.cpp + "#include \nint main(int argc, char ** argv) {\n std::filesystem::path p(argv[0]);\n return p.string().length();\n}" + ) + try_compile( + STD_FS_NO_LIB_NEEDED ${CMAKE_CURRENT_BINARY_DIR} + SOURCES ${CMAKE_CURRENT_BINARY_DIR}/main.cpp + COMPILE_DEFINITIONS -std=c++17) + try_compile( + STD_FS_NEEDS_STDCXXFS ${CMAKE_CURRENT_BINARY_DIR} + SOURCES ${CMAKE_CURRENT_BINARY_DIR}/main.cpp + COMPILE_DEFINITIONS -std=c++17 + LINK_LIBRARIES stdc++fs) + try_compile( + STD_FS_NEEDS_CXXFS ${CMAKE_CURRENT_BINARY_DIR} + SOURCES ${CMAKE_CURRENT_BINARY_DIR}/main.cpp + COMPILE_DEFINITIONS -std=c++17 + LINK_LIBRARIES c++fs) +endif() + +if(${STD_FS_NEEDS_STDCXXFS}) + set(STD_FS_LIB stdc++fs) +elseif(${STD_FS_NEEDS_CXXFS}) + set(STD_FS_LIB c++fs) +elseif(${STD_FS_NO_LIB_NEEDED}) + set(STD_FS_LIB "") +else() + message(WARNING "Unknown C++17 compiler - not passing -lstdc++fs") + set(STD_FS_LIB "") +endif() +endfunction() \ No newline at end of file