Skip to content

Commit

Permalink
Remove alternative filesystem support.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinH committed Jul 15, 2023
1 parent 8933730 commit 788f223
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 216 deletions.
68 changes: 0 additions & 68 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,74 +49,6 @@ target_include_directories(pegtl INTERFACE
# require C++17
target_compile_features(pegtl INTERFACE cxx_std_17)

option(PEGTL_USE_BOOST_FILESYSTEM "Override the auto-detection of std::filesystem and use Boost.Filesystem" OFF)
option(PEGTL_USE_FILESYSTEM "Use available implementation of std::filesystem (std::, std::experiemntal, or Boost)" ON)

# Try compiling a test program with std::filesystem or one of its alternatives
function(check_filesystem_impl FILESYSTEM_HEADER FILESYSTEM_NAMESPACE OPTIONAL_LIBS OUT_RESULT)
set(TEST_FILE "test_${OUT_RESULT}.cpp")
configure_file(.cmake/test_filesystem.cpp.in ${TEST_FILE} @ONLY)

try_compile(TEST_RESULT
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_BINARY_DIR}/${TEST_FILE}
CXX_STANDARD 17)

if(NOT TEST_RESULT)
# Retry with each of the optional libraries
foreach(OPTIONAL_LIB IN LISTS OPTIONAL_LIBS)
try_compile(TEST_RESULT
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_BINARY_DIR}/${TEST_FILE}
LINK_LIBRARIES ${OPTIONAL_LIB}
CXX_STANDARD 17)

if(TEST_RESULT)
# Looks like the optional library was required, go ahead and add it to the link options.
message(STATUS "Adding ${OPTIONAL_LIB} to the PEGTL to build with ${FILESYSTEM_NAMESPACE}.")
target_link_libraries(${PROJECT_NAME} INTERFACE ${OPTIONAL_LIB})
break()
endif()
endforeach()
endif()

set(${OUT_RESULT} ${TEST_RESULT} PARENT_SCOPE)
endfunction()

if (NOT PEGTL_USE_FILESYSTEM)
target_compile_definitions(${PROJECT_NAME} INTERFACE TAO_PEGTL_NO_FILESYSTEM)
message(STATUS "Skipping std::filesystem (or variant) in PEGTL; File operations will not work or compile.")
elseif (PEGTL_USE_BOOST_FILESYSTEM)
# Force the use of Boost.Filesystem: #include <boost/filesystem.hpp> // boost::filesystem
find_package(Boost REQUIRED COMPONENTS filesystem)
target_link_libraries(${PROJECT_NAME} INTERFACE Boost::filesystem)
target_compile_definitions(${PROJECT_NAME} INTERFACE TAO_PEGTL_BOOST_FILESYSTEM)
else()
# Try compiling a minimal program with each header/namespace, in order of preference:
# C++17: #include <filesystem> // std::filesystem
# Experimental C++17: #include <experimental/filesystem> // std::experimental::filesystem
# Boost.Filesystem: #include <boost/filesystem.hpp> // boost::filesystem
check_filesystem_impl("filesystem" "std::filesystem" "stdc++fs;c++fs" STD_FILESYSTEM)
if(STD_FILESYSTEM)
message(STATUS "Using std::filesystem")
else()
check_filesystem_impl("experimental/filesystem" "std::experimental::filesystem" "stdc++fs;c++fs" STD_EXPERIMENTAL_FILESYSTEM)
if(STD_EXPERIMENTAL_FILESYSTEM)
target_compile_definitions(${PROJECT_NAME} INTERFACE TAO_PEGTL_STD_EXPERIMENTAL_FILESYSTEM)
message(WARNING "Using std::experimental::filesystem as a fallback")
else()
find_package(Boost COMPONENTS filesystem)
check_filesystem_impl("boost/filesystem.hpp" "boost::filesystem" Boost::filesystem BOOST_FILESYSTEM)
if(BOOST_FILESYSTEM)
target_compile_definitions(${PROJECT_NAME} INTERFACE TAO_PEGTL_BOOST_FILESYSTEM)
message(WARNING "Using Boost.Filesystem as a fallback")
else()
message(FATAL_ERROR "PEGTL requires C++17, including an implementation of std::filesystem, which your compiler toolchain does not seem to support. You can try installing Boost.Filesystem as a temporary workaround, but there's no guarantee the PEGTL will keep working with your project. Consider upgrading your compiler toolchain or downgrading the PEGTL to the previous version.")
endif()
endif()
endif()
endif()

# testing
option(PEGTL_BUILD_TESTS "Build test programs" ${PEGTL_IS_MAIN_PROJECT})
if(PEGTL_BUILD_TESTS)
Expand Down
1 change: 1 addition & 0 deletions doc/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* Moved depth counter to adapter class in contrib.
* Changed default top-level `rewind_mode` to `dontcare`.
* Replaced `rewind_mode` values `dontcare` and `active` with new value `optional`.
* Removed support for `boost::filesystem` and `std::experimental::filesystem`.
* Removed support for building an amalgamated header.
* Removed support for Visual Studio 2017.
* Removed support for GCC 7.
Expand Down
18 changes: 0 additions & 18 deletions doc/Installing-and-Using.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
## Contents

* [Requirements](#requirements)
* [Filesystem](#filesystem)
* [Disabling Exceptions](#disabling-exceptions)
* [Disabling RTTI](#disabling-rtti)
* [Installation Packages](#installation-packages)
Expand Down Expand Up @@ -46,23 +45,6 @@ It should also work with other C++17 compilers on other Unix systems (or any suf
The PEGTL is written with an emphasis on clean code and is compatible with
the `-pedantic`, `-Wall`, `-Wextra` and `-Werror` compiler switches.

## Filesystem

By default the PEGTL uses `std::filesystem` facilities for filenames, however when using older compilers, or preferring the Boost filesystem library, manual intervention might be required.

GCC supports for `std::experimental::filesystem` in `libstdc++fs` since version 7, support for `std::filesystem` in `libstdc++fs` since version 8, and support for `std::filesystem` in regular `libstdc++` since version 9.

Clang [supports](https://libcxx.llvm.org/docs/UsingLibcxx.html) `std::experimental::filesystem` in `libc++experimental` since version 5, `std::filesystem` in `libc++fs` since version 7, and `std::filesystem` in regular `libc++` since version 9.

Some Linux distributions have Clang packages without `libc++` and instead configure Clang to use the `libstdc++` from the default GCC.
In such cases it is necessary to use e.g. `libstdc++fs` and include `<experimental/filesystem>` when a Clang 8 it used on a system with GCC 7 default compiler.

When building with CMake, the appropriate `std::filesystem` or `std::experimental::filesystem` and matching available C++ standard library are chosen automatically.
Alternatively `-DPEGTL_BOOST_FILESYSTEM=ON` can be passed to CMake to use `boost::filesystem` and matching Boost library instead.

When building with Make, appropriate flags for `include/tao/pegtl/internal/filesystem.hpp` and the linker need to be set manually or by changing the included `Makefile`.
Using `-DTAO_PEGTL_BOOST_FILESYSTEM=1` and setting up the compiler and linker to find the Boost headers and libraries and link against `boost_filesystem` can also be set up manually with Make.

## Disabling Exceptions

The PEGTL is compatible with `-fno-exceptions`, however, when disabling exceptions:
Expand Down
1 change: 0 additions & 1 deletion doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* [Getting Started](Getting-Started.md)
* [Installing and Using](Installing-and-Using.md)
* [Requirements](Installing-and-Using.md#requirements)
* [Filesystem](Installing-and-Using.md#filesystem)
* [Disabling Exceptions](Installing-and-Using.md#disabling-exceptions)
* [Disabling RTTI](Installing-and-Using.md#disabling-rtti)
* [Installation Packages](Installing-and-Using.md#installation-packages)
Expand Down
65 changes: 0 additions & 65 deletions include/tao/pegtl/internal/filesystem.hpp

This file was deleted.

4 changes: 2 additions & 2 deletions include/tao/pegtl/internal/mmap_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#else
#endif

#include "filesystem.hpp"
#include <filesystem>

#include "../config.hpp"

Expand All @@ -26,7 +26,7 @@ namespace TAO_PEGTL_NAMESPACE::internal
{
const mmap_file_impl data;

explicit mmap_file( const internal::filesystem::path& path )
explicit mmap_file( const std::filesystem::path& path )
: data( path )
{}

Expand Down
24 changes: 11 additions & 13 deletions include/tao/pegtl/internal/mmap_file_posix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,25 @@
#define TAO_PEGTL_INTERNAL_MMAP_FILE_POSIX_HPP

#include <fcntl.h>
#include <filesystem>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <utility>

#if !defined( __cpp_exceptions )
#include <cstdio>
#include <exception>
#endif

#include <utility>

#include "filesystem.hpp"

#include "../config.hpp"

namespace TAO_PEGTL_NAMESPACE::internal
{
struct mmap_file_open
{
explicit mmap_file_open( const internal::filesystem::path& path ) // NOLINT(modernize-pass-by-value)
explicit mmap_file_open( const std::filesystem::path& path ) // NOLINT(modernize-pass-by-value)
: m_path( path ),
m_fd( open() )
{}
Expand All @@ -49,8 +47,8 @@ namespace TAO_PEGTL_NAMESPACE::internal
if( ::fstat( m_fd, &st ) < 0 ) {
// LCOV_EXCL_START
#if defined( __cpp_exceptions )
const internal::error_code ec( errno, internal::system_category() );
throw internal::filesystem::filesystem_error( "fstat() failed", m_path, ec );
const std::error_code ec( errno, std::system_category() );
throw std::filesystem::filesystem_error( "fstat() failed", m_path, ec );
#else
std::perror( "fstat() failed" );
std::terminate();
Expand All @@ -60,7 +58,7 @@ namespace TAO_PEGTL_NAMESPACE::internal
return static_cast< std::size_t >( st.st_size );
}

const internal::filesystem::path m_path;
const std::filesystem::path m_path;
const int m_fd;

private:
Expand All @@ -77,8 +75,8 @@ namespace TAO_PEGTL_NAMESPACE::internal
return fd;
}
#if defined( __cpp_exceptions )
const internal::error_code ec( errno, internal::system_category() );
throw internal::filesystem::filesystem_error( "open() failed", m_path, ec );
const std::error_code ec( errno, std::system_category() );
throw std::filesystem::filesystem_error( "open() failed", m_path, ec );
#else
std::perror( "open() failed" );
std::terminate();
Expand All @@ -89,7 +87,7 @@ namespace TAO_PEGTL_NAMESPACE::internal
class mmap_file_posix
{
public:
explicit mmap_file_posix( const internal::filesystem::path& path )
explicit mmap_file_posix( const std::filesystem::path& path )
: mmap_file_posix( mmap_file_open( path ) )
{}

Expand All @@ -100,8 +98,8 @@ namespace TAO_PEGTL_NAMESPACE::internal
if( ( m_size != 0 ) && ( reinterpret_cast< intptr_t >( m_data ) == -1 ) ) {
// LCOV_EXCL_START
#if defined( __cpp_exceptions )
const internal::error_code ec( errno, internal::system_category() );
throw internal::filesystem::filesystem_error( "mmap() failed", reader.m_path, ec );
const std::error_code ec( errno, std::system_category() );
throw std::filesystem::filesystem_error( "mmap() failed", reader.m_path, ec );
#else
std::perror( "mmap() failed" );
std::terminate();
Expand Down
30 changes: 15 additions & 15 deletions include/tao/pegtl/internal/mmap_file_win32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
#include <exception>
#endif

#include "filesystem.hpp"
#include <filesystem>

namespace TAO_PEGTL_NAMESPACE::internal
{
struct mmap_file_open
{
explicit mmap_file_open( const internal::filesystem::path& path )
explicit mmap_file_open( const std::filesystem::path& path )
: m_path( path ),
m_handle( open() )
{}
Expand All @@ -61,8 +61,8 @@ namespace TAO_PEGTL_NAMESPACE::internal
LARGE_INTEGER size;
if( !::GetFileSizeEx( m_handle, &size ) ) {
#if defined( __cpp_exceptions )
internal::error_code ec( ::GetLastError(), internal::system_category() );
throw internal::filesystem::filesystem_error( "GetFileSizeEx() failed", m_path, ec );
std::error_code ec( ::GetLastError(), std::system_category() );
throw std::filesystem::filesystem_error( "GetFileSizeEx() failed", m_path, ec );
#else
std::perror( "GetFileSizeEx() failed" );
std::terminate();
Expand All @@ -71,7 +71,7 @@ namespace TAO_PEGTL_NAMESPACE::internal
return std::size_t( size.QuadPart );
}

const internal::filesystem::path m_path;
const std::filesystem::path m_path;
const HANDLE m_handle;

private:
Expand All @@ -88,8 +88,8 @@ namespace TAO_PEGTL_NAMESPACE::internal
return handle;
}
#if defined( __cpp_exceptions )
internal::error_code ec( ::GetLastError(), internal::system_category() );
throw internal::filesystem::filesystem_error( "CreateFile2() failed", m_path, ec );
std::error_code ec( ::GetLastError(), std::system_category() );
throw std::filesystem::filesystem_error( "CreateFile2() failed", m_path, ec );
#else
std::perror( "CreateFile2() failed" );
std::terminate();
Expand All @@ -106,8 +106,8 @@ namespace TAO_PEGTL_NAMESPACE::internal
return handle;
}
#if defined( __cpp_exceptions )
internal::error_code ec( ::GetLastError(), internal::system_category() );
throw internal::filesystem::filesystem_error( "CreateFileW()", m_path, ec );
std::error_code ec( ::GetLastError(), std::system_category() );
throw std::filesystem::filesystem_error( "CreateFileW()", m_path, ec );
#else
std::perror( "CreateFileW() failed" );
std::terminate();
Expand All @@ -118,7 +118,7 @@ namespace TAO_PEGTL_NAMESPACE::internal

struct mmap_file_mmap
{
explicit mmap_file_mmap( const internal::filesystem::path& path )
explicit mmap_file_mmap( const std::filesystem::path& path )
: mmap_file_mmap( mmap_file_open( path ) )
{}

Expand Down Expand Up @@ -160,8 +160,8 @@ namespace TAO_PEGTL_NAMESPACE::internal
return handle;
}
#if defined( __cpp_exceptions )
internal::error_code ec( ::GetLastError(), internal::system_category() );
throw internal::filesystem::filesystem_error( "CreateFileMappingW() failed", reader.m_path, ec );
std::error_code ec( ::GetLastError(), std::system_category() );
throw std::filesystem::filesystem_error( "CreateFileMappingW() failed", reader.m_path, ec );
#else
std::perror( "CreateFileMappingW() failed" );
std::terminate();
Expand All @@ -172,7 +172,7 @@ namespace TAO_PEGTL_NAMESPACE::internal
class mmap_file_win32
{
public:
explicit mmap_file_win32( const internal::filesystem::path& path )
explicit mmap_file_win32( const std::filesystem::path& path )
: mmap_file_win32( mmap_file_mmap( path ) )
{}

Expand All @@ -186,8 +186,8 @@ namespace TAO_PEGTL_NAMESPACE::internal
{
if( ( m_size != 0 ) && ( intptr_t( m_data ) == 0 ) ) {
#if defined( __cpp_exceptions )
internal::error_code ec( ::GetLastError(), internal::system_category() );
throw internal::filesystem::filesystem_error( "MapViewOfFile() failed", ec );
std::error_code ec( ::GetLastError(), std::system_category() );
throw std::filesystem::filesystem_error( "MapViewOfFile() failed", ec );
#else
std::perror( "MapViewOfFile() failed" );
std::terminate();
Expand Down
Loading

0 comments on commit 788f223

Please sign in to comment.