# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
#
# Configuration options for building the atlas-stats externals. Collected
# into a single place.
#

find_package( Python 3.6 COMPONENTS Interpreter Development QUIET )
find_package( Xrootd 4.1 QUIET )
find_package( dcap QUIET )
find_package( Davix QUIET )
find_package( OpenSSL QUIET )
find_package( UUID QUIET )
find_package( Eigen QUIET )
# find_package( TBB 2018 QUIET ) # - commented out because trouble with 2021 versions so just build in the project instead

# Get the OS name.
atlas_os_id( _os _osIsValid )

# Decide whether to build Python.
set( _flag FALSE )
if( ( NOT Python_Interpreter_FOUND ) OR ( NOT Python_Development_FOUND ) OR APPLE )
    set( _flag TRUE )
else()
    # If Python *is* found, then check whether it comes from the system,
    # or from a custom/user build. As in general we can't accept Python
    # from the system.
    get_filename_component( _pythonDir ${Python_EXECUTABLE} DIRECTORY )
    if( "${_pythonDir}" MATCHES "^(/usr)?/bin(32|64)?$" )
        # Since that's not appropriate for us... :-(
        set( _flag TRUE )
    endif()
    unset( _pythonDir )
endif()
option( ATLAS_BUILD_PYTHON
        "Build Python as part of the release" ${_flag} )

# Decide whether to build XRootD.
set( _flag FALSE )
if( NOT XROOTD_FOUND OR ATLAS_BUILD_PYTHON )
    set( _flag TRUE )
else()
    # If XRootD *was* found, but it was found on the system, then don't
    # accept it. As for our build we must use C++14/17, which the system
    # version will not be compiled with.
    if( "${XROOTD_INCLUDE_DIR}" STREQUAL "/usr/include" )
        set( _flag TRUE )
    endif()
endif()
option( ATLAS_BUILD_XROOTD
        "Build XRootD as part of the release" FALSE ) # update for alma9 ... will let ROOT do the build instead

if (NOT EIGEN_FOUND OR ATLAS_BUILD_EIGEN )
    set( _flag TRUE )
else()
    set( _flag FALSE )
endif()

option( ATLAS_BUILD_EIGEN "Build Eigen as part of the release" ${_flag} )

# Decide whether to build DCAP.
set( _flag FALSE )
#if( NOT DCAP_FOUND AND _osIsValid AND
#( "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64" ) AND
#( ( "${_os}" STREQUAL "slc6" ) OR ( "${_os}" STREQUAL "centos7" ) ) )
#    set( _flag TRUE )
#endif()
option( ATLAS_BUILD_DCAP
        "Build DCAP as part of the release" ${_flag} )

# Decide whether to build LibXml2.
set( _flag TRUE )
#if( APPLE )
#    set( _flag FALSE )
#endif()
option( ATLAS_BUILD_LIBXML2
        "Build LibXml2 as part of the release" ${_flag} )

# Decide whether to build Davix.
set( _flag FALSE )
#if( NOT DAVIX_FOUND AND NOT APPLE AND UUID_FOUND AND OPENSSL_FOUND )
#    set( _flag TRUE )
#endif()
option( ATLAS_BUILD_DAVIX
        "Build Davix as part of the release" ${_flag} )

# Decide whether to build TBB.
set( _flag FALSE )
#if( NOT TBB_FOUND )
#    set( _flag TRUE )
#endif()
option( ATLAS_BUILD_TBB
        "Build TBB as part of the release" ${_flag} )

option( ATLAS_BUILD_BOOST "Build Boost" TRUE )


# Introduce STATANA_XXX_VERSION options to control software versions
# Also STATANA_XXX_SOURCE for controlling source where relevant (may not create these for now)
set( STATANA_ROOT_VERSION "master" CACHE STRING "Version of ROOT")
set( STATANA_PYHF_VERSION "0.7.6" CACHE STRING "Version of pyhf" ) # this currently does nothing!
set( STATANA_ROOFITEXTENSIONS_VERSION "v1.0.4" CACHE STRING "Version of RooFitExtensions"  )
set( STATANA_XROOFIT_VERSION "master" CACHE STRING "Version of xRooFit"  )
set( STATANA_COMMONSTATTOOLS_VERSION "" CACHE STRING "Version of CommonStatTools" )
set( STATANA_BOOTSTRAPGENERATOR_VERSION "71e6ebe2" CACHE STRING "Version of BootstrapGenerator" )
set( STATANA_HISTFITTER_VERSION "v1.3.1" CACHE STRING "Version of HistFitter" )
set( STATANA_TREXFITTER_VERSION "TRExFitter-00-04-26" CACHE STRING "Version of TRExFitter" )
set( STATANA_SIGNIFICANCE_VERSION "d59b9a2" CACHE STRING "Version of Significance" )
set( STATANA_ROOUNFOLD_VERSION "" CACHE STRING "Version of RooUnfold" )
set( STATANA_QUICKFIT_VERSION "a0e81c25" CACHE STRING "Version of quickFit" )
set( STATANA_WORKSPACECOMBINER_VERSION "" CACHE STRING "Version of workspaceCombiner" )
set( STATANA_XMLANAWSBUILDER_VERSION "ba8fda1a" CACHE STRING "Version of xmlAnaWSBuilder" )
set( STATANA_CMSCOMBINE_VERSION "dev_jlangfor_combinev10" CACHE STRING "Version of CMSCombine" )


option( TRACK_CHANGES "forces cmake to detect changes in directories of submodules and recompile if changes detected - slower to build, but required for development" ON)

option( TRACK_CHANGES_ROOT "forces cmake to detect changes in ROOT and recompile if changes detected - slower to build, but required for development" OFF)

set( CERN_GITLAB_PREFIX "https://:@gitlab.cern.ch:8443" CACHE STRING "Prefix to use for accessing gitlab.cern.ch, defaults to krb5" )

option( CCACHE "build with ccache for speeding up repeated compilation" OFF)

if((CMAKE_CXX_COMPILER_LAUNCHER STREQUAL "ccache") AND (NOT ${CCACHE}))
  message(WARNING "WARNING: Found CMAKE_CXX_COMPILER_LAUNCHER set to ccache, but CCACHE option is set to OFF. \
This will disable ccache for external projects. To enable ccache, set CCACHE project option to ON.")
elseif(${CCACHE})
  set(CMAKE_CXX_COMPILER_LAUNCHER "ccache" CACHE STRING "Compiler launcher for CXX." FORCE)
endif()

# ROOT and PyAnalysis are built by default for this project.

list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})
find_package(ROOT QUIET)
set( _flag TRUE )
if (ROOT_FOUND)
    set(ROOT_VERSION "v${ROOT_VERSION_MAJOR}-${ROOT_VERSION_MINOR}-${ROOT_VERSION_PATCH}")
    message(STATUS "INFO: Found existing ROOT ${ROOT_VERSION}: ${ROOTSYS}")
endif()
if (ROOT_FOUND AND ROOT_VERSION STREQUAL ${STATANA_ROOT_VERSION})
    message(STATUS "INFO: Can Build on top of Existing ROOT: ${ROOT_VERSION}")
    set( _flag FALSE )
endif()
option( ATLAS_BUILD_ROOT
        "Build ROOT as part of the release" ${_flag} )
option( ATLAS_BUILD_PYANALYSIS
        "Build the PyAnalysis package as part of this project" ${ATLAS_BUILD_PYTHON} )

option( STATANA_VERBOSE "verbose build progress" OFF )

# Make CMake forget that it found any of these packages. (In case it did.)
# Since they could interfere with the environment setup of the project.
# Whichever package ends up needing those externals, will anyway ask for
# them explicitly.
get_property( _packages GLOBAL PROPERTY PACKAGES_FOUND )
list( REMOVE_ITEM _packages Python Xrootd dcap Davix OpenSSL UUID TBB ROOT )
set_property( GLOBAL PROPERTY PACKAGES_FOUND ${_packages} )
unset( _packages )

# Clean up.
unset( _flag )
unset( _os )
unset( _osIsValid )