Skip to content

Commit

Permalink
Let user choose which filesystem lib (std/boost) to use
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-Devel committed Oct 30, 2018
1 parent 6cdbc5d commit 3f23585
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
52 changes: 50 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,60 @@
# 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 )

# Compile with c++17 if available (so we can use std::filesystem),
# but boostdep is c++98 compatible, so no CMAKE_CXX_STANDARD_REQUIRED is necessary
set( CMAKE_CXX_STANDARD 17 )

add_executable( boostdep src/boostdep.cpp )

find_package( Boost COMPONENTS filesystem REQUIRED )
target_link_libraries( boostdep Boost::filesystem )
####### options #######

# Decide if boostdep should use Boost.Filesystem or std::filesystem

# We default to using the standard library when available
# and Boost.Filesystem otherwise
# NOTE: Prior to 3.8.0 check_cxx_source_compiles will not honor the c++17 setting,
# so with older cmake versions this will never detect std::filesystem correctly
include( CheckCXXSourceCompiles )
check_cxx_source_compiles(
"
#include <filesystem>
int main() {
std::filesystem::is_directory( std::filesystem::current_path() );
}
"
has_std_filesystem )

option( BOOSTDEP_USE_STD_FS "Use std::filesystem instead of boost::filesystem" ${has_std_filesystem} )

#~~~~~~ options ~~~~~~

if( BOOSTDEP_USE_STD_FS )

message(STATUS "Using std::filesystem as filesystem library")
target_compile_definitions( boostdep PUBLIC BOOSTDEP_USE_STD_FS )
target_link_libraries( boostdep PUBLIC stdc++fs )
# 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")
if( NOT TARGET Boost::filesystem )
find_package( Boost COMPONENTS filesystem REQUIRED )
endif()
target_link_libraries( boostdep PUBLIC Boost::filesystem )

endif()


install( TARGETS boostdep RUNTIME DESTINATION bin )
25 changes: 18 additions & 7 deletions src/boostdep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#define _CRT_SECURE_NO_WARNINGS

#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <string>
#include <iostream>
#include <fstream>
Expand All @@ -21,7 +19,16 @@
#include <climits>
#include <cstdlib>

namespace fs = boost::filesystem;
#ifdef BOOSTDEP_USE_STD_FS
#include <filesystem>
namespace fs = std::filesystem;
#else
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
namespace fs = boost::filesystem;
#endif



// header -> module
static std::map< std::string, std::string > s_header_map;
Expand All @@ -48,7 +55,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;
}
Expand All @@ -74,7 +81,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;
}
Expand Down Expand Up @@ -209,7 +216,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;
}
Expand All @@ -221,7 +228,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 );
}
Expand Down Expand Up @@ -1667,7 +1678,7 @@ static void add_module_headers( fs::path const & dir, std::set<std::string> & he

for( ; it != last; ++it )
{
if( it->status().type() == fs::directory_file )
if( fs::is_directory( it->status() ) )
{
continue;
}
Expand Down

0 comments on commit 3f23585

Please sign in to comment.