# Helper function for adding two targets. A shared library and a corresponding
# ROOT dictionary that is necessary for e.g. python bindings.
# Arguments:
#   libname         The base name for the library (and target)
#   headers         The header files that should be passed to dictionary generation
#   sources         The source files for the shared libraries
#   selection       The selection xml passed to the dictionary generation
#
# The function creates the following targets
#   <libname>       The shared library. Also available under the podio::<libname> alias
#                   This is not linked against anything and has include directories set
#                   to podio only. So some target_link_libraries are most likely to be
#                   done outside
#   <libname>Dict   The dictionary shared library. This is linked against podio::podio
#                   and the necessary ROOT libraries
#
# Additionally the following files are generated by root
#   - <libname>DictDict.rootmap
#   - <libname>Dict_rdict.pcm
# these files have to be installed to the same directory as the dictionary shared
# library
FUNCTION(PODIO_ADD_LIB_AND_DICT libname headers sources selection )
  # shared library
  add_library(${libname} SHARED ${sources})
  add_library(podio::${libname} ALIAS ${libname})
  target_include_directories(${libname} PUBLIC
    $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  )
  target_include_directories(${libname} SYSTEM PUBLIC
    ${Python3_INCLUDE_DIRS}
  )

  # dictionary
  set(dictname ${libname}Dict)
  add_library(${dictname} SHARED)
  target_include_directories(${dictname} PUBLIC
    $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
  target_link_libraries(${dictname} PUBLIC podio::${libname} podio::podio ROOT::Core ROOT::Tree)
  if(ENABLE_RNTUPLE)
    target_link_libraries(${dictname} PUBLIC ROOT::ROOTNTuple)
  endif()
  PODIO_GENERATE_DICTIONARY(${dictname} ${headers} SELECTION ${selection}
    OPTIONS --library ${CMAKE_SHARED_LIBRARY_PREFIX}${dictname}${CMAKE_SHARED_LIBRARY_SUFFIX}
    )
  # prevent generating dictionary twice
  set_target_properties(${dictname}-dictgen PROPERTIES EXCLUDE_FROM_ALL TRUE)
  target_sources(${dictname} PRIVATE ${dictname}.cxx)
ENDFUNCTION()


# --- Core podio library and dictionary without I/O
SET(core_sources
  CollectionIDTable.cc
  GenericParameters.cc
  DatamodelRegistry.cc
  DatamodelRegistryIOHelpers.cc
  UserDataCollection.cc
  CollectionBufferFactory.cc
  MurmurHash3.cpp
  SchemaEvolution.cc
  Glob.cc
  )

SET(core_headers
  ${PROJECT_SOURCE_DIR}/include/podio/CollectionBase.h
  ${PROJECT_SOURCE_DIR}/include/podio/CollectionIDTable.h
  ${PROJECT_SOURCE_DIR}/include/podio/ICollectionProvider.h
  ${PROJECT_SOURCE_DIR}/include/podio/ObjectID.h
  ${PROJECT_SOURCE_DIR}/include/podio/UserDataCollection.h
  ${PROJECT_SOURCE_DIR}/include/podio/podioVersion.h
  ${PROJECT_SOURCE_DIR}/include/podio/DatamodelRegistry.h
  ${PROJECT_SOURCE_DIR}/include/podio/utilities/DatamodelRegistryIOHelpers.h
  ${PROJECT_SOURCE_DIR}/include/podio/GenericParameters.h
  ${PROJECT_SOURCE_DIR}/include/podio/LinkCollection.h
  ${PROJECT_SOURCE_DIR}/include/podio/utilities/Glob.h
  )

PODIO_ADD_LIB_AND_DICT(podio "${core_headers}" "${core_sources}" selection.xml)
target_compile_options(podio PRIVATE -pthread)


# --- Root I/O functionality and corresponding dictionary
SET(root_sources
  rootUtils.h
  ROOTWriter.cc
  ROOTReader.cc
  ROOTLegacyReader.cc
  ROOTFrameData.cc
  RootHelpers.cc
)
if(ENABLE_RNTUPLE)
  list(APPEND root_sources
      RNTupleReader.cc
      RNTupleWriter.cc
     )
endif()

SET(root_headers
  ${PROJECT_SOURCE_DIR}/include/podio/ROOTReader.h
  ${PROJECT_SOURCE_DIR}/include/podio/ROOTLegacyReader.h
  ${PROJECT_SOURCE_DIR}/include/podio/ROOTWriter.h
  ${PROJECT_SOURCE_DIR}/include/podio/ROOTFrameData.h
  ${PROJECT_SOURCE_DIR}/include/podio/utilities/RootHelpers.h
  )
if(ENABLE_RNTUPLE)
  list(APPEND root_headers
      ${PROJECT_SOURCE_DIR}/include/podio/RNTupleReader.h
      ${PROJECT_SOURCE_DIR}/include/podio/RNTupleWriter.h
     )
endif()

PODIO_ADD_LIB_AND_DICT(podioRootIO "${root_headers}" "${root_sources}" root_selection.xml)
target_link_libraries(podioRootIO PUBLIC podio::podio ROOT::Core ROOT::RIO ROOT::Tree ROOT::ROOTVecOps)
if(ENABLE_RNTUPLE)
  target_link_libraries(podioRootIO PUBLIC ROOT::ROOTNTuple)
  target_compile_definitions(podioRootIO PUBLIC PODIO_ENABLE_RNTUPLE=1)
else()
  target_compile_definitions(podioRootIO PUBLIC PODIO_ENABLE_RNTUPLE=0)
endif()


# --- SIO I/O functionality and corresponding dictionary
if(ENABLE_SIO)
  SET(sio_sources
    SIOBlockUserData.cc
    SIOBlock.cc
    SIOWriter.cc
    SIOReader.cc
    SIOFrameData.cc
    sioUtils.h
    SIOLegacyReader.cc
    )

  SET(sio_headers
    ${PROJECT_SOURCE_DIR}/include/podio/SIOReader.h
    ${PROJECT_SOURCE_DIR}/include/podio/SIOLegacyReader.h
    ${PROJECT_SOURCE_DIR}/include/podio/SIOWriter.h
    )

  PODIO_ADD_LIB_AND_DICT(podioSioIO "${sio_headers}" "${sio_sources}" sio_selection.xml)
  target_link_libraries(podioSioIO PUBLIC podio::podio SIO::sio ${CMAKE_DL_LIBS})
  target_compile_definitions(podioSioIO PUBLIC PODIO_ENABLE_SIO=1)

  LIST(APPEND INSTALL_LIBRARIES podioSioIO podioSioIODict)
endif()


# --- IO
set(io_sources
  Writer.cc
  Reader.cc
  )

set(io_headers
  ${PROJECT_SOURCE_DIR}/include/podio/Writer.h
  ${PROJECT_SOURCE_DIR}/include/podio/Reader.h
  )

add_library(podioIO SHARED ${io_sources})
add_library(podio::podioIO ALIAS podioIO)
target_include_directories(podioIO PUBLIC
    $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_link_libraries(podioIO PUBLIC podio::podio podio::podioRootIO)
if(ENABLE_SIO)
  target_link_libraries(podioIO PUBLIC podio::podioSioIO)
endif()


# --- DataSource
if(ENABLE_DATASOURCE)
  set(rds_sources
      DataSource.cc
  )

  set(rds_headers
      ${PROJECT_SOURCE_DIR}/include/podio/DataSource.h
  )

  podio_add_lib_and_dict(podioDataSource "${rds_headers}" "${rds_sources}" rds_selection.xml)
  target_link_libraries(podioDataSource PUBLIC podio::podio
                                               podio::podioIO
                                               podio::podioRootIO
                                               ROOT::Core
                                               ROOT::RIO
                                               ROOT::Tree
                                               ROOT::ROOTVecOps
                                               ROOT::ROOTDataFrame
  )
  target_compile_definitions(podioDataSource PUBLIC PODIO_ENABLE_DATASOURCE=1)
endif()


# --- Install everything
if (NOT ENABLE_DATASOURCE)
  install(TARGETS podio podioDict podioRootIO podioRootIODict podioIO ${INSTALL_LIBRARIES}
    EXPORT podioTargets
    DESTINATION "${CMAKE_INSTALL_LIBDIR}")
else()
  install(TARGETS podio podioDict podioRootIO podioRootIODict podioIO podioDataSource podioDataSourceDict ${INSTALL_LIBRARIES}
    EXPORT podioTargets
    DESTINATION "${CMAKE_INSTALL_LIBDIR}")
endif()

# Only install the necessary headers
file(GLOB headers_necessary
     "${PROJECT_SOURCE_DIR}/include/podio/*.h")

if (NOT ENABLE_SIO)
  list(FILTER headers_necessary EXCLUDE REGEX SIO.*\\.h$)
endif()
if (NOT ENABLE_RNTUPLE)
  list(FILTER headers_necessary EXCLUDE REGEX RNTuple.*\\.h$)
endif()
if (NOT ENABLE_DATASOURCE)
  list(FILTER headers_necessary EXCLUDE REGEX DataSource.h)
endif()

install(FILES ${headers_necessary}
        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/podio
)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/podio/utilities
                  ${PROJECT_SOURCE_DIR}/include/podio/detail
        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/podio
)

install(FILES
  ${CMAKE_CURRENT_BINARY_DIR}/podioDictDict.rootmap
  ${CMAKE_CURRENT_BINARY_DIR}/libpodioDict_rdict.pcm
  ${CMAKE_CURRENT_BINARY_DIR}/podioRootIODictDict.rootmap
  ${CMAKE_CURRENT_BINARY_DIR}/libpodioRootIODict_rdict.pcm
  DESTINATION "${CMAKE_INSTALL_LIBDIR}")

if (ENABLE_SIO)
  install(FILES
    ${CMAKE_CURRENT_BINARY_DIR}/podioSioIODictDict.rootmap
    ${CMAKE_CURRENT_BINARY_DIR}/libpodioSioIODict_rdict.pcm
    DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    )
endif()

if (ENABLE_DATASOURCE)
  install(FILES
    ${CMAKE_CURRENT_BINARY_DIR}/podioDataSourceDictDict.rootmap
    ${CMAKE_CURRENT_BINARY_DIR}/libpodioDataSourceDict_rdict.pcm
    DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    )
endif()

add_executable(podio_test_hashes test_hashes.cpp)
target_link_libraries(podio_test_hashes PRIVATE podio::podio)
install(TARGETS podio_test_hashes
  EXPORT podioTargets
  DESTINATION "${CMAKE_INSTALL_BINDIR}"
)
