# - h5nexus
cmake_minimum_required(VERSION 3.26)

project(h5nexus VERSION 0.1.0 LANGUAGES CXX)

#--- set C++ standard ---------------------------------------------------------
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

#--- set a default build type if none was specified ---------------------------
set(default_build_type "Release")

if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  message(STATUS "Setting build type to '${default_build_type}' as none was specified.")
  set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE
      STRING "Choose the type of build." FORCE)
  # Set the possible values of build type for cmake-gui
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
    "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif ()

#--- check for pkg-config -----------------------------------------------------
find_package(PkgConfig REQUIRED)

#--- check for git ------------------------------------------------------------
find_package(Git REQUIRED)

#--- check for HDF5 -----------------------------------------------------------
find_package(HDF5 REQUIRED COMPONENTS CXX)
if(NOT HDF5_FOUND)
    message(FATAL_ERROR "HDF5 C++ library not found")
endif()
include_directories(${HDF5_INCLUDE_DIRS})

#--- check for ROOT -----------------------------------------------------------
find_package(ROOT 6.36 REQUIRED COMPONENTS Minuit2)
if (ROOT_miniut2_FOUND)
  #---Define useful ROOT functions and macros (e.g. ROOT_GENERATE_DICTIONARY)
  include(${ROOT_USE_FILE})
endif (ROOT_miniut2_FOUND)

#--- all checks done -> feed config.h -----------------------------------------
set(HAVE_CONFIG_H 1 CACHE INTERNAL "config.h is available")
configure_file(${CMAKE_SOURCE_DIR}/cmake/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)

#--- check if project source is a git repo ------------------------------------
if (EXISTS "${CMAKE_SOURCE_DIR}/.git/HEAD")
  message(STATUS "is a git repo")
  set(IS_GIT_REPO 1)
else ()
  message(STATUS "is NOT a git repo")
  set(IS_GIT_REPO 0)
endif ()

#--- start create git-revision.h ----------------------------------------------
if (IS_GIT_REPO)
  execute_process(COMMAND sh ${CMAKE_SOURCE_DIR}/src/git_revision.sh)

  set(HAVE_GIT_REV_H "-DHAVE_GIT_REV_H")  
  set(GIT_REV_H "git-revision.h")
else (IS_GIT_REPO)
  set(HAVE_GIT_REV_H "")  
  set(GIT_REV_H "")
endif (IS_GIT_REPO)

#--- end create git-revision.h ------------------------------------------------

#--- write summary of the installation
cmake_host_system_information(RESULT PROCESSOR QUERY PROCESSOR_DESCRIPTION)

message("")
message("|-----------------------------------------------------------------------|")
message("|                                                                       |")
message("|                            Summary                                    |")
message("|                                                                       |")
message("|-----------------------------------------------------------------------|")
message("")
message(" System:    ${CMAKE_HOST_SYSTEM_NAME} ${CMAKE_SYSTEM_PROCESSOR} - ${CMAKE_HOST_SYSTEM_VERSION}")
message(" Processor: ${PROCESSOR} (${CMAKE_SYSTEM_PROCESSOR})")
message(" ----------")
message("")
message(" h5nexus Version: ${musrfit_VERSION}")
message(" ----------------")
message("")
message(" Build Type: ${CMAKE_BUILD_TYPE}")
message(" -----------")
message("")
message(" Requirements:")
message(" -------------")
message("")
message(" HDF5    found in ${HDF5_INCLUDE_DIRS}, Version: ${HDF5_VERSION}")
message(" ROOT    found in ${ROOT_INCLUDE_DIRS}, Version: ${ROOT_VERSION}")
message("")
message(" Installation directories:")
message(" -------------------------")
message("")
message(" Programs                : ${CMAKE_INSTALL_PREFIX}/bin")
message("")
message("-------------------------------------------------------------------------")
message("")

#--- add executable -----------------------------------------------------------
add_executable(h5nexus 
               ../../PNeXus.cpp
               main.cpp)
target_compile_options(h5nexus BEFORE PRIVATE "-DHAVE_CONFIG_H" "${HAVE_GIT_REV_H}")
target_include_directories(h5nexus 
  BEFORE PRIVATE
    $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/build>
    $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/../..>
    $<BUILD_INTERFACE:${ROOT_INCLUDE_DIRS}>
)
target_link_libraries(h5nexus ${HDF5_CXX_LIBRARIES} ${ROOT_LIBRARIES})
