From 9041abce7a2d190017de0898c96d30ad965b5f57 Mon Sep 17 00:00:00 2001 From: Mike Dev Date: Mon, 15 Oct 2018 18:28:28 +0200 Subject: [PATCH] Let user choose which filesystem lib (std/boost) to use --- CMakeLists.txt | 23 +++++++++++++++++++++-- src/boostdep.cpp | 25 ++++++++++++++++++------- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0bc5834..c134974 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,9 +7,28 @@ cmake_minimum_required( VERSION 3.1 ) project( boostdep LANGUAGES CXX ) +option( BOOSTDEP_USE_STD_FS "Use std::filesystem instead of boost::filesystem" OFF ) + add_executable( boostdep src/boostdep.cpp ) -find_package( Boost COMPONENTS filesystem REQUIRED ) -target_link_libraries( boostdep Boost::filesystem ) +if( BOOSTDEP_USE_STD_FS ) + + message(STATUS "Using std::filesystem as filesystem library") + + target_compile_definitions( boostdep PUBLIC BOOSTDEP_USE_STD_FS ) + target_compile_features( boostdep PUBLIC cxx_std_17 ) + + # NOTE: Some compilers (e.g. g++-8) will require additional linker flags + # in order to use std::filesystem (e.g. -lstdc++fs). + # We are NOT handling those special cases + +else() + + message(STATUS "Using Boost::filesystem as filesystem library") + + find_package( Boost COMPONENTS filesystem REQUIRED ) + target_link_libraries( boostdep PUBLIC Boost::filesystem ) + +endif() install( TARGETS boostdep RUNTIME DESTINATION bin ) diff --git a/src/boostdep.cpp b/src/boostdep.cpp index f517d2d..27488c0 100644 --- a/src/boostdep.cpp +++ b/src/boostdep.cpp @@ -9,8 +9,6 @@ #define _CRT_SECURE_NO_WARNINGS -#include -#include #include #include #include @@ -23,7 +21,16 @@ #include #include -namespace fs = boost::filesystem; +#ifdef BOOSTDEP_USE_STD_FS + #include + namespace fs = std::filesystem; +#else + #include + #include + namespace fs = boost::filesystem; +#endif + + // header -> module static std::map< std::string, std::string > s_header_map; @@ -50,7 +57,7 @@ static void scan_module_headers( fs::path const & path ) for( ; it != last; ++it ) { - if( it->status().type() == fs::directory_file ) + if( fs::is_directory( it->status() ) ) { continue; } @@ -76,7 +83,7 @@ static void scan_submodules( fs::path const & path ) { fs::directory_entry const & e = *it; - if( e.status().type() != fs::directory_file ) + if( !fs::is_directory( it->status() ) ) { continue; } @@ -211,7 +218,7 @@ static void scan_module_path( fs::path const & dir, bool remove_prefix, std::map for( ; it != last; ++it ) { - if( it->status().type() == fs::directory_file ) + if( fs::is_directory( it->status() ) ) { continue; } @@ -223,7 +230,11 @@ static void scan_module_path( fs::path const & dir, bool remove_prefix, std::map header = header.substr( n+1 ); } +#ifdef BOOSTDEP_USE_STD_FS + std::ifstream is( it->path() ); +#else fs::ifstream is( it->path() ); +#endif scan_header_dependencies( header, is, deps, from ); } @@ -1669,7 +1680,7 @@ static void add_module_headers( fs::path const & dir, std::set & he for( ; it != last; ++it ) { - if( it->status().type() == fs::directory_file ) + if( fs::is_directory( it->status() ) ) { continue; }