From a7196a5184ec74e79d1067b280a376d422fc5cf8 Mon Sep 17 00:00:00 2001 From: Mike Dev Date: Tue, 2 Oct 2018 22:21:34 +0200 Subject: [PATCH] [CMake] Don't depend on boost filesystem when std::filesystem is available --- CMakeLists.txt | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0bc5834..e561187 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,12 +4,45 @@ # See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt cmake_minimum_required( VERSION 3.1 ) +if(${CMAKE_VERSION} VERSION_GREATER "3.8.0") + # Honor CXX_STANDARD_REQUIRED in check_cxx_source_compiles + # (see https://cmake.org/cmake/help/v3.8/policy/CMP0067.html) + cmake_policy( SET CMP0067 NEW ) +endif() project( boostdep LANGUAGES CXX ) +## See if we can use std::filesystem from c++17 +# +# NOTE1: No CXX_STANDARD_REQUIRED - we use Boost::filesystem as a fallback +# NOTE2: Some compilers (e.g. g++-8) will require additional linker flags +# in order to use std::filesystem. +# We are NOT handling those special cases. +# NOTE3: Prior to 3.8.0 check_cxx_source_compiles will not honor the c++17 setting, +# so with older cmake versions this will always fallback on the boost version + +set( CMAKE_CXX_STANDARD 17 ) + +include( CheckCXXSourceCompiles ) +check_cxx_source_compiles( +" +#include +#ifdef __cpp_lib_filesystem + #include +#endif +int main() { + std::filesystem::is_directory( std::filesystem::current_path() ); +} +" +has_std_filesystem ) + add_executable( boostdep src/boostdep.cpp ) -find_package( Boost COMPONENTS filesystem REQUIRED ) -target_link_libraries( boostdep Boost::filesystem ) +if( NOT has_std_filesystem ) + if( NOT TARGET Boost::filesystem ) + find_package( Boost COMPONENTS filesystem REQUIRED ) + endif() + target_link_libraries( boostdep Boost::filesystem ) +endif() install( TARGETS boostdep RUNTIME DESTINATION bin )