cmake_minimum_required(VERSION 3.15)

project(
  jungfraucalibration
  DESCRIPTION "helper functions for Jungfrau calibration software"
  HOMEPAGE_URL "https://gitea.psi.ch/detectors/JFCalibration2"
  LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

include(FetchContent)

option(JF_FETCH_AARE "Fetch aare library from github" ON)

if(JF_FETCH_AARE) 
    FetchContent_Declare(aare GIT_REPOSITORY https://github.com/slsdetectorgroup/aare
    GIT_TAG dev/jungfraucalibration) #use newest version - change to main once merged 
    FetchContent_MakeAvailable(aare)
    install(
        TARGETS aare_core
        EXPORT ${TARGETS_EXPORT_NAME}
    )
    install(TARGETS aare_compiler_flags
    EXPORT ${TARGETS_EXPORT_NAME}
    ) #mmh is this the way to go I think I will define the same compiler options twice now? can directly use aare_compiler_flags instead of compiler_flags
    message(STATUS "target: aare")
else()
    #set(AARE_INSTALL_PATH "/usr/local/aare" CACHE PATH "Installation directory for AARE")
    list(APPEND CMAKE_PREFIX_PATH ${AARE_INSTALL_PATH}) 
    message(STATUS "looking for aare in: ${CMAKE_PREFIX_PATH}")
    find_package(aare REQUIRED) 

    if(TARGET aare_core)
        message(STATUS "found aare_core target")
    else()
        message(FATAL_ERROR "aare_core target was not found!")
    endif()
    #message(STATUS "found aare: ${AARE_INCLUDE_DIRS}")
endif()

set(SourceFiles
    ${CMAKE_CURRENT_SOURCE_DIR}/src/BadChannels.cpp)

add_library(jungfraucalibration STATIC ${SourceFiles} ${PUBLICHEADERS})
target_include_directories(
  jungfraucalibration PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>")

target_link_libraries(jungfraucalibration PUBLIC aare_core) # TODO: should it be public? 

# TODO: add other compile options? 
target_compile_features(jungfraucalibration PRIVATE cxx_std_17)

#add_subdirectory(examples)

