Skip to content

Commit

Permalink
Copy functionality to check for fs libraries from dd4hep
Browse files Browse the repository at this point in the history
Make podio look for a compatible filesystem library itself during the
cmake stage, eventually falling back to boost in case the compiler
doesn't support it natively.
  • Loading branch information
tmadlener committed Sep 22, 2020
1 parent 2313ed9 commit 74a9486
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 28 deletions.
11 changes: 5 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ option(CREATE_DOC "Whether or not to create doxygen doc target." OFF)
option(ENABLE_SIO "Build the SIO block handlers and readers and writers." OFF)


option(USE_BOOST_FS "Use the boost filesystem library (in case your system does not yet have it)" OFF)

#--- Declare ROOT dependency ---------------------------------------------------
list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
Expand All @@ -84,18 +82,19 @@ if(ROOT_VERSION VERSION_LESS 6.14)
endif()
list(APPEND PODIO_IO_HANDLERS ROOT)

#--- optionally build with SIO -------------------------------------------------
#--- enable podio macros--------------------------------------------------------
include(cmake/podioMacros.cmake)

# optionally build with SIO -------------------------------------------------
if(ENABLE_SIO)
find_package( SIO REQUIRED)
PODIO_CHECK_CPP_FS(PODIO_FS_LIBS)
if (SIO_FOUND)
MESSAGE( STATUS "Found SIO library - will build SIO I/O support" )
list(APPEND PODIO_IO_HANDLERS SIO)
endif()
endif()

#--- enable podio macros--------------------------------------------------------
include(cmake/podioMacros.cmake)

#--- enable unit testing capabilities ------------------------------------------
include(CTest)

Expand Down
37 changes: 37 additions & 0 deletions cmake/podioMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,40 @@ function(PODIO_GENERATE_DATAMODEL datamodel YAML_FILE RETURN_HEADERS RETURN_SOUR
)

endfunction()

#---------------------------------------------------------------------------------------------------
function(PODIO_CHECK_CPP_FS FS_LIBS)
SET(${have_filesystem} FALSE)
MESSAGE(VERBOSE "Checking for filesystem support of compiler")
# GNU implementation prior to 9.1 requires linking with -lstdc++fs and LLVM
# implementation prior to LLVM 9.0 requires linking with -lc++fs
# After that it should be built-in
FOREACH(FS_LIB_NAME "" stdc++fs c++fs)
MESSAGE(VERBOSE "Linking against ${FS_LIB_NAME}")
try_compile(have_filesystem ${CMAKE_BINARY_DIR}/try ${PROJECT_SOURCE_DIR}/cmake/try_filesystem.cpp
CXX_STANDARD ${CMAKE_CXX_STANDARD}
CXX_EXTENSIONS False
OUTPUT_VARIABLE HAVE_FS_OUTPUT
LINK_LIBRARIES ${FS_LIB_NAME}
)
MESSAGE(DEBUG "-----> " ${HAVE_FS_OUTPUT})
IF(have_filesystem)
MESSAGE(VERBOSE "Compiler supports filesystem when linking against ${FS_LIB_NAME}")
SET(${FS_LIBS} ${FS_LIB_NAME} PARENT_SCOPE)
BREAK()
ENDIF()
MESSAGE("Compiler not compatible when linking against ${FS_LIB_NAME}")
ENDFOREACH()

IF(NOT have_filesystem)
MESSAGE(STATUS "Compiler does not have filesystem support, falling back to boost::filesystem")
find_package(Boost REQUIRED COMPONENTS filesystem system)
SET(${FS_LIBS} Boost::filesystem Boost::system PARENT_SCOPE)
SET_TARGET_PROPERTIES(Boost::filesystem
PROPERTIES
INTERFACE_COMPILE_DEFINITIONS USE_BOOST_FILESYSTEM
)
GET_TARGET_PROPERTY(BOOST_FILESYSTEM_LOC Boost::filesystem IMPORTED_LOCATION)
GET_FILENAME_COMPONENT(BOOST_DIR ${BOOST_FILESYSTEM_LOC} DIRECTORY)
ENDIF()
endfunction()
12 changes: 12 additions & 0 deletions cmake/try_filesystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <iostream>
#include <filesystem>

int main(int, char**) {
namespace fs = std::filesystem;

for (const auto& file : fs::directory_iterator("./")) {
std::cout << file.path() << ": " << file.file_size() << '\n';
}

return 0;
}
25 changes: 5 additions & 20 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,14 @@ target_sources(podioDict PRIVATE podioDict.cxx)

# SIO I/O library
if(ENABLE_SIO)

if( USE_BOOST_FS )
find_package(boost)
add_definitions(-D USE_BOOST_FS)
endif()

add_library(podioSioIO SHARED ${sio_sources})
add_library(podio::podioSioIO ALIAS podioSioIO)

if(USE_BOOST_FS)
target_include_directories(podioSioIO PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
${SIO_INCLUDE_DIRS}
${BOOST_INCLUDE_DIR})
target_link_libraries(podioSioIO PUBLIC podio::podio ${SIO_LIBRARIES} ${CMAKE_DL_LIBS} boost_filesystem)
else()
target_include_directories(podioSioIO PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
${SIO_INCLUDE_DIRS})
target_link_libraries(podioSioIO PUBLIC podio::podio ${SIO_LIBRARIES} ${CMAKE_DL_LIBS})
endif()
target_include_directories(podioSioIO PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
${SIO_INCLUDE_DIRS})
target_link_libraries(podioSioIO PUBLIC podio::podio ${SIO_LIBRARIES} ${CMAKE_DL_LIBS} ${PODIO_FS_LIBS})

LIST(APPEND INSTALL_LIBRARIES podioSioIO)
endif()
Expand Down
4 changes: 2 additions & 2 deletions src/SIOBlock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <dlfcn.h>
#include <cstdlib>
#include <sstream>
#ifdef USE_BOOST_FS
#ifdef USE_BOOST_FILESYSTEM
#include <boost/filesystem.hpp>
#else
#include <filesystem>
Expand Down Expand Up @@ -152,7 +152,7 @@ namespace podio {
}

std::vector<std::string> SIOBlockLibraryLoader::getLibNames() {
#ifdef USE_BOOST_FS
#ifdef USE_BOOST_FILESYSTEM
namespace fs = boost::filesystem;
#else
namespace fs = std::filesystem;
Expand Down

0 comments on commit 74a9486

Please sign in to comment.