From ceecb0ca27350a0f4e53d199a6aac0e44d586ce2 Mon Sep 17 00:00:00 2001 From: froejdh_e Date: Thu, 22 May 2025 10:20:16 +0200 Subject: [PATCH] added extra fs link and fixed execute_program warning --- CMakeLists.txt | 9 ++++---- cmake/FindClangFormat.cmake | 4 +++- cmake/helpers.cmake | 46 +++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 cmake/helpers.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 212281f68..3b290aa5a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,7 @@ if (${CMAKE_VERSION} VERSION_GREATER "3.24") endif() include(cmake/project_version.cmake) include(cmake/SlsAddFlag.cmake) +include(cmake/helpers.cmake) @@ -193,11 +194,9 @@ find_package(ClangFormat) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) - message(STATUS "No build type selected, default to Release") - set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type (default Release)" FORCE) -endif() - +default_build_type("Release") +set_std_fs_lib() +message(STATUS "Extra linking to fs lib:${STD_FS_LIB}") #Enable LTO if available include(CheckIPOSupported) diff --git a/cmake/FindClangFormat.cmake b/cmake/FindClangFormat.cmake index 8e697896f..7b7fdd27f 100644 --- a/cmake/FindClangFormat.cmake +++ b/cmake/FindClangFormat.cmake @@ -25,7 +25,9 @@ mark_as_advanced( ClangFormat_BIN) if(ClangFormat_FOUND) - exec_program(${ClangFormat_BIN} ${CMAKE_CURRENT_SOURCE_DIR} ARGS --version OUTPUT_VARIABLE CLANG_VERSION_TEXT) + execute_process(COMMAND ${ClangFormat_BIN} --version + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE CLANG_VERSION_TEXT) string(REGEX MATCH "([0-9]+)\\.[0-9]+\\.[0-9]+" CLANG_VERSION ${CLANG_VERSION_TEXT}) if((${CLANG_VERSION} GREATER "9") OR (${CLANG_VERSION} EQUAL "9")) # A CMake script to find all source files and setup clang-format targets for them diff --git a/cmake/helpers.cmake b/cmake/helpers.cmake new file mode 100644 index 000000000..2ddb9d057 --- /dev/null +++ b/cmake/helpers.cmake @@ -0,0 +1,46 @@ +function(default_build_type val) +if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message(STATUS "No build type selected, default to Release") + set(CMAKE_BUILD_TYPE ${val} CACHE STRING "Build type (default ${val})" FORCE) +endif() +endfunction() + +function(set_std_fs_lib) +# 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 PARENT_SCOPE) +elseif(${STD_FS_NEEDS_CXXFS}) + set(STD_FS_LIB c++fs PARENT_SCOPE) +elseif(${STD_FS_NO_LIB_NEEDED}) + set(STD_FS_LIB "" PARENT_SCOPE) +else() + message(WARNING "Unknown C++17 compiler - not passing -lstdc++fs") + set(STD_FS_LIB "") +endif() +endfunction() \ No newline at end of file