Skip to content

Commit 9041abc

Browse files
committed
Let user choose which filesystem lib (std/boost) to use
1 parent 0fd7f80 commit 9041abc

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

Diff for: CMakeLists.txt

+21-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,28 @@ cmake_minimum_required( VERSION 3.1 )
77

88
project( boostdep LANGUAGES CXX )
99

10+
option( BOOSTDEP_USE_STD_FS "Use std::filesystem instead of boost::filesystem" OFF )
11+
1012
add_executable( boostdep src/boostdep.cpp )
1113

12-
find_package( Boost COMPONENTS filesystem REQUIRED )
13-
target_link_libraries( boostdep Boost::filesystem )
14+
if( BOOSTDEP_USE_STD_FS )
15+
16+
message(STATUS "Using std::filesystem as filesystem library")
17+
18+
target_compile_definitions( boostdep PUBLIC BOOSTDEP_USE_STD_FS )
19+
target_compile_features( boostdep PUBLIC cxx_std_17 )
20+
21+
# NOTE: Some compilers (e.g. g++-8) will require additional linker flags
22+
# in order to use std::filesystem (e.g. -lstdc++fs).
23+
# We are NOT handling those special cases
24+
25+
else()
26+
27+
message(STATUS "Using Boost::filesystem as filesystem library")
28+
29+
find_package( Boost COMPONENTS filesystem REQUIRED )
30+
target_link_libraries( boostdep PUBLIC Boost::filesystem )
31+
32+
endif()
1433

1534
install( TARGETS boostdep RUNTIME DESTINATION bin )

Diff for: src/boostdep.cpp

+18-7
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
#define _CRT_SECURE_NO_WARNINGS
1111

12-
#include <boost/filesystem.hpp>
13-
#include <boost/filesystem/fstream.hpp>
1412
#include <string>
1513
#include <iostream>
1614
#include <fstream>
@@ -23,7 +21,16 @@
2321
#include <streambuf>
2422
#include <sstream>
2523

26-
namespace fs = boost::filesystem;
24+
#ifdef BOOSTDEP_USE_STD_FS
25+
#include <filesystem>
26+
namespace fs = std::filesystem;
27+
#else
28+
#include <boost/filesystem.hpp>
29+
#include <boost/filesystem/fstream.hpp>
30+
namespace fs = boost::filesystem;
31+
#endif
32+
33+
2734

2835
// header -> module
2936
static std::map< std::string, std::string > s_header_map;
@@ -50,7 +57,7 @@ static void scan_module_headers( fs::path const & path )
5057

5158
for( ; it != last; ++it )
5259
{
53-
if( it->status().type() == fs::directory_file )
60+
if( fs::is_directory( it->status() ) )
5461
{
5562
continue;
5663
}
@@ -76,7 +83,7 @@ static void scan_submodules( fs::path const & path )
7683
{
7784
fs::directory_entry const & e = *it;
7885

79-
if( e.status().type() != fs::directory_file )
86+
if( !fs::is_directory( it->status() ) )
8087
{
8188
continue;
8289
}
@@ -211,7 +218,7 @@ static void scan_module_path( fs::path const & dir, bool remove_prefix, std::map
211218

212219
for( ; it != last; ++it )
213220
{
214-
if( it->status().type() == fs::directory_file )
221+
if( fs::is_directory( it->status() ) )
215222
{
216223
continue;
217224
}
@@ -223,7 +230,11 @@ static void scan_module_path( fs::path const & dir, bool remove_prefix, std::map
223230
header = header.substr( n+1 );
224231
}
225232

233+
#ifdef BOOSTDEP_USE_STD_FS
234+
std::ifstream is( it->path() );
235+
#else
226236
fs::ifstream is( it->path() );
237+
#endif
227238

228239
scan_header_dependencies( header, is, deps, from );
229240
}
@@ -1669,7 +1680,7 @@ static void add_module_headers( fs::path const & dir, std::set<std::string> & he
16691680

16701681
for( ; it != last; ++it )
16711682
{
1672-
if( it->status().type() == fs::directory_file )
1683+
if( fs::is_directory( it->status() ) )
16731684
{
16741685
continue;
16751686
}

0 commit comments

Comments
 (0)