Skip to content

Commit

Permalink
[CMake] Don't depend on boost filesystem when std::filesystem is avai…
Browse files Browse the repository at this point in the history
…lable
  • Loading branch information
Mike-Devel committed Oct 16, 2018
1 parent 851f8e4 commit f5a455b
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cstdint>
#ifdef __cpp_lib_filesystem
#include <filesystem>
#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 )

0 comments on commit f5a455b

Please sign in to comment.