Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std filesystem instead of Boost.Filesystem if available. #7

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ matrix:
compiler: clang++
env: TOOLSET=clang CXXSTD=03,11,14,1z

- os: linux
compiler: g++
env: CMAKE=1
addons:
apt:
packages:
- libboost-filesystem-dev
- g++-8
sources:
- ubuntu-toolchain-r-test
install: true
script:
- export CC=gcc-8 && export CXX=g++-8
- mkdir __build__ && cd __build__
- cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~ ..
- cmake --build . --target install

- os: linux
compiler: g++
env: CMAKE=1
Expand Down
23 changes: 21 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
25 changes: 17 additions & 8 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 @@ -23,7 +21,18 @@
#include <streambuf>
#include <sstream>

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



// header -> module
static std::map< std::string, std::string > s_header_map;
Expand All @@ -50,7 +59,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 @@ -76,7 +85,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 @@ -211,7 +220,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 @@ -223,7 +232,7 @@ static void scan_module_path( fs::path const & dir, bool remove_prefix, std::map
header = header.substr( n+1 );
}

fs::ifstream is( it->path() );
ifstream_t is( it->path() );

scan_header_dependencies( header, is, deps, from );
}
Expand Down Expand Up @@ -1669,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