Skip to content

Commit

Permalink
Merge pull request #224 from sameeul/drop_boost_req
Browse files Browse the repository at this point in the history
Drop boost requirement
  • Loading branch information
constantinpape authored Oct 31, 2023
2 parents de0900d + c8d76ad commit c0c3259
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 251 deletions.
1 change: 0 additions & 1 deletion .github/workflows/environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ dependencies:
- lz4-c
- xz
- zlib
- boost-cpp>=1.63
- xtensor >=0.24,<0.25
- xtensor-python >=0.26,<0.27
- xsimd >=8,<9
Expand Down
84 changes: 4 additions & 80 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,7 @@ message(STATUS "Building z5 v${${PROJECT_NAME}_VERSION}")
# C++ Standard
##############################

# check whether we have c++ 17

message(STATUS "CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
set(CPP17 "${CMAKE_CXX_FLAGS}" MATCHES "-std=c\\+\\+17" CACHE INTERNAL "")
if (CPP17)
message(STATUS "Using c++ 17")
set(CMAKE_CXX_STANDARD 17)
else()
message(STATUS "Using c++ 14")
set(CMAKE_CXX_STANDARD 14)
endif()
set(CMAKE_CXX_STANDARD 17)

# make sure the compiler supports the requested c++ standard
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand All @@ -53,14 +43,6 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
endif()
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)

# NOTE whether we need to enable the old ABI depends
# on which ABI boost is compiled with.
# If you get boost linker errors try to enable / disable the old ABI
option(USE_OLD_ABI "Use the old GCC ABI to be compatible with old GLIBC versions" OFF)
if(USE_OLD_ABI)
message(STATUS "Using old GCC ABI")
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
endif()

if(MSVC)
add_definitions(/DNOMINMAX)
Expand Down Expand Up @@ -155,53 +137,11 @@ endif()
# find libraries - pthread
find_package(Threads)


###############################
# Boost
###############################

# If WITH_BOOST_FS is set, we always use the boost filesystem library
# otherwise, we check if we have c++17 support. If so, we use std::filesystem,
# otherwise we fall back to boost filesystem.
if(CPP17)
SET(WITH_BOOST_FS FALSE CACHE BOOL "")
else()
SET(WITH_BOOST_FS TRUE CACHE BOOL "")
# add C++ filesystem
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
SET(FILESYSTEM_LIBRARIES "stdc++fs")
endif()

if (MSVC)
SET(BOOST_ROOT "${CMAKE_PREFIX_PATH}/Library")
SET(BOOST_LIBRARYDIR "${CMAKE_PREFIX_PATH}/Library/lib")
else()
SET(BOOST_ROOT "${CMAKE_PREFIX_PATH}")
SET(BOOST_LIBRARYDIR "${CMAKE_PREFIX_PATH}/lib")
endif()
SET(Boost_NO_SYSTEM_PATHS ON)

if(WITH_BOOST_FS)
message(STATUS "With boost filesystem")
find_package(Boost 1.63.0 COMPONENTS system filesystem REQUIRED)
add_definitions(-DWITH_BOOST_FS)
SET(FILESYSTEM_LIBRARIES "${Boost_FILESYSTEM_LIBRARY};${Boost_SYSTEM_LIBRARY}")
else()
message(STATUS "With std filesystem")
find_package(Boost 1.63.0 REQUIRED)

# see this issue for discussions about the filesystem lib in CMake
# https://gitlab.kitware.com/cmake/cmake/issues/17834
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
SET(FILESYSTEM_LIBRARIES "stdc++fs")
endif()

# on clang, we need to enable libc++experimental, see
# https://stackoverflow.com/a/45332844
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
SET(FILESYSTEM_LIBRARIES "c++experimental")
endif()
endif()
include_directories(${Boost_INCLUDE_DIR})


###################
# xtensor libraries
###################
Expand Down Expand Up @@ -318,22 +258,6 @@ if(BUILD_Z5PY)
# find pybind11, which is required to build the python bindings
# the find package command will find python and set the python variables
find_package(pybind11 REQUIRED)
if (CPP17)
message(STATUS "Using c++ 17 for pybind")
if(MSVC)
set(PYBIND11_CPP_STANDARD /std:c++17)
else()
set(PYBIND11_CPP_STANDARD -std=c++17)
endif()
else()
message(STATUS "Using c++ 14 for pybind")
if(MSVC)
set(PYBIND11_CPP_STANDARD /std:c++14)
else()
set(PYBIND11_CPP_STANDARD -std=c++14)
endif()
endif()

# pybind11 does not include numpy, so we must search for it in addition here
find_package(NumPy REQUIRED)
include_directories(${NUMPY_INCLUDE_DIRS})
Expand Down
92 changes: 6 additions & 86 deletions include/z5/common.hxx
Original file line number Diff line number Diff line change
@@ -1,93 +1,13 @@
#pragma once

#include <fstream>
#include <filesystem>
namespace fs = std::filesystem;

inline fs::path relativeImpl(const fs::path & from, const fs::path & to){
return fs::relative(to, from);
}

// helpful summary of compiler versions
// https://blog.kowalczyk.info/article/j/guide-to-predefined-macros-in-c-compilers-gcc-clang-msvc-etc..html

// include boost::filesystem or std::filesystem header
// and define the namespace fs
#ifdef WITH_BOOST_FS
#ifndef BOOST_FILESYSTEM_NO_DEPRECATED
#define BOOST_FILESYSTEM_NO_DEPRECATED
#endif
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
namespace fs = boost::filesystem;

// relative path in boost filesystem behaves unexpectedly, so we use the
// same implementation as below
inline fs::path relativeImpl(const fs::path & from, const fs::path & to) {
fs::path::const_iterator fromIter = from.begin();
fs::path::const_iterator toIter = to.begin();

while (fromIter != from.end() && toIter != to.end() && (*toIter) == (*fromIter)){
++toIter;
++fromIter;
}

fs::path finalPath;
while (fromIter != from.end()){
finalPath /= "..";
++fromIter;
}

while (toIter != to.end()){
finalPath /= *toIter;
++toIter;
}

return finalPath;
}
#else
// macos behaves very weird here, I can't get it to build on
// osx < 10.15 right now. For now the workaround is to use boost filesystem ...
// TODO MSVC check ?
#if (defined(__GNUC__) && (__GNUC__ > 7)) || defined(__clang__) || (defined(_MSC_VER) && _MSC_VER > 1900)
#include <filesystem>
namespace fs = std::filesystem;

// need to be consistent with the other implementations
inline fs::path relativeImpl(const fs::path & from, const fs::path & to){
return fs::relative(to, from);
}

#else
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;

// experimental::filesystem does not have relative yet, so
// we re-implement it following:
// https://stackoverflow.com/questions/10167382/boostfilesystem-get-relative-path/37715252#37715252
inline fs::path relativeImpl(const fs::path & from, const fs::path & to){
// Start at the root path and while they are the same then do nothing then when they first
// diverge take the entire from path, swap it with '..' segments, and then append the remainder of the to path.
fs::path::const_iterator fromIter = from.begin();
fs::path::const_iterator toIter = to.begin();

// Loop through both while they are the same to find nearest common directory
while (fromIter != from.end() && toIter != to.end() && (*toIter) == (*fromIter)){
++toIter;
++fromIter;
}

// Replace from path segments with '..' (from => nearest common directory)
fs::path finalPath;
while (fromIter != from.end()){
finalPath /= "..";
++fromIter;
}

// Append the remainder of the to path (nearest common directory => to)
while (toIter != to.end()){
finalPath /= *toIter;
++toIter;
}

return finalPath;
}
#endif
#endif

namespace z5 {

Expand Down
5 changes: 0 additions & 5 deletions include/z5/factory.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ namespace z5 {
return;
}
nlohmann::json j;

#ifdef WITH_BOOST_FS
fs::ifstream file(path);
#else
std::ifstream file(path);
#endif
file >> j;
file.close();

Expand Down
20 changes: 0 additions & 20 deletions include/z5/filesystem/attributes.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ namespace attrs_detail {
if(!fs::exists(path)) {
return;
}
#ifdef WITH_BOOST_FS
fs::ifstream file(path);
#else
std::ifstream file(path);
#endif
file >> j;
file.close();
}
Expand All @@ -27,22 +23,14 @@ namespace attrs_detail {
nlohmann::json jOut;
// if we already have attributes, read them
if(fs::exists(path)) {
#ifdef WITH_BOOST_FS
fs::ifstream file(path);
#else
std::ifstream file(path);
#endif
file >> jOut;
file.close();
}
for(auto jIt = j.begin(); jIt != j.end(); ++jIt) {
jOut[jIt.key()] = jIt.value();
}
#ifdef WITH_BOOST_FS
fs::ofstream file(path);
#else
std::ofstream file(path);
#endif
file << jOut;
file.close();
}
Expand All @@ -51,23 +39,15 @@ namespace attrs_detail {
nlohmann::json jOut;
// if we already have attributes, read them
if(fs::exists(path)) {
#ifdef WITH_BOOST_FS
fs::ifstream file(path);
#else
std::ifstream file(path);
#endif
file >> jOut;
file.close();
}
else {
return;
}
jOut.erase(key);
#ifdef WITH_BOOST_FS
fs::ofstream file(path);
#else
std::ofstream file(path);
#endif
file << jOut;
file.close();
}
Expand Down
16 changes: 0 additions & 16 deletions include/z5/filesystem/dataset.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -209,22 +209,14 @@ namespace filesystem {
private:

inline void write(const fs::path & path, const std::vector<char> & buffer) const {
#ifdef WITH_BOOST_FS
fs::ofstream file(path, std::ios::binary);
#else
std::ofstream file(path, std::ios::binary);
#endif
file.write(&buffer[0], buffer.size());
file.close();
}

inline void read(const fs::path & path, std::vector<char> & buffer) const {
// open input stream and read the filesize
#ifdef WITH_BOOST_FS
fs::ifstream file(path, std::ios::binary);
#else
std::ifstream file(path, std::ios::binary);
#endif

file.seekg(0, std::ios::end);
const std::size_t file_size = file.tellg();
Expand Down Expand Up @@ -256,11 +248,7 @@ namespace filesystem {

inline bool read_varlen_from_n5_header(const fs::path & path,
std::size_t & chunkSize) const {
#ifdef WITH_BOOST_FS
fs::ifstream file(path, std::ios::binary);
#else
std::ifstream file(path, std::ios::binary);
#endif

// read the mode
uint16_t mode;
Expand Down Expand Up @@ -291,11 +279,7 @@ namespace filesystem {

inline void read_shape_from_n5_header(const fs::path & path,
types::ShapeType & chunkShape) const {
#ifdef WITH_BOOST_FS
fs::ifstream file(path, std::ios::binary);
#else
std::ifstream file(path, std::ios::binary);
#endif

// advance the file by 2 to skip the mode
file.seekg(2);
Expand Down
2 changes: 1 addition & 1 deletion include/z5/filesystem/handle.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ namespace handle {
// get z5::filesystem::handle::File from char pointer corresponding
// to the file on filesystem
inline File getFileHandle(const char * path) {
fs::path path_(std::string(path));
fs::path path_{std::string(path)};
File ret(path);
return ret;
}
Expand Down
8 changes: 0 additions & 8 deletions include/z5/filesystem/metadata.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,13 @@ namespace filesystem {
namespace metadata_detail {

inline void writeMetadata(const fs::path & path, const nlohmann::json & j) {
#ifdef WITH_BOOST_FS
fs::ofstream file(path);
#else
std::ofstream file(path);
#endif
file << std::setw(4) << j << std::endl;
file.close();
}

inline void readMetadata(const fs::path & path, nlohmann::json & j) {
#ifdef WITH_BOOST_FS
fs::ifstream file(path);
#else
std::ifstream file(path);
#endif
file >> j;
file.close();
}
Expand Down
Loading

0 comments on commit c0c3259

Please sign in to comment.