#------------------------------------------------------------------------
# -*- mode: cmake -*-
#------------------------------------------------------------------------
#
# LDASTools Frame Utility Library  - Frame Utility library
#
# Copyright (C) 2018 California Institute of Technology
#
# LDASTools Frame Utility Library  is free software; you may redistribute
# it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 (GPLv2)
#  of the License or at your discretion, any later version.
#
# LDASTools Frame Utility Library is distributed in the hope that it will
# be useful, but without any warranty or even the implied warranty of
# merchantability or fitness for a particular purpose. See the GNU
# General Public License (GPLv2) for more details.
#
# Neither the names of the California Institute of Technology (Caltech),
# The Massachusetts Institute of Technology (M.I.T), The Laser
# Interferometer Gravitational-Wave Observatory (LIGO), nor the names
# of its contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# You should have received a copy of the licensing terms for this
# software included in the file LICENSE located in the top-level
# directory of this package. If you did not, you can view a copy at
# http://dcc.ligo.org/M1500244/LICENSE
#
#------------------------------------------------------------------------
set( BOOST_MINIMUM_VERSION 1.67 )

cmake_minimum_required(VERSION 3.12...4.0)

if (NOT "${CMAKE_CXX_STANDARD}")
  set(CMAKE_CXX_STANDARD 17)
endif( )
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS NO)
if ( NOT CMAKE_OSX_ARCHITECTURES )
  set( CMAKE_OSX_ARCHITECTURES x86_64 )
endif( )

#------------------------------------------------------------------------
# Configuration options
#------------------------------------------------------------------------

set( LDAS_TOOLS_DISKCACHEAPI_PACKAGE_NAME ldas-tools-diskcacheAPI )
set( LDAS_TOOLS_DISKCACHEAPI_VERSION      2.7.10 )

set( LDAS_TOOLS_CMAKE_VERSION   1.4.0 )
set( LDAS_TOOLS_AL_VERSION      2.8.0 )
set( LDAS_TOOLS_LDASGEN_VERSION 2.7.5 )

set(
  PROJECT_DESCRIPTION
  "LDAS Tools disk cache library adds additional functionality for frame file caching"
  )
set(
  PROJECT_DESCRIPTION_LONG
  "This provides an extended set of functions to manipulate and manage caches of frame files."
  )

set( LDAS_TOOLS_CMAKE_VERSION   1.4.0 )
set( LDAS_TOOLS_AL_VERSION      2.8.0 )
set( LDAS_TOOLS_LDASGEN_VERSION 2.7.5 )

set( BOOST_MINIMUM_VERSION 1.67 )

set( ENABLE_DOCUMENTATION_ONLY FALSE CACHE BOOL "Enable only building of documentation" )

#------------------------------------------------------------------------
#
#------------------------------------------------------------------------

project( ${LDAS_TOOLS_DISKCACHEAPI_PACKAGE_NAME} )

#------------------------------------------------------------------------
# Locate the standard LDAS Tools cmake macro directory
#------------------------------------------------------------------------

find_package(
  LDASToolsCMake
  CONFIG
  REQUIRED
  HINTS ${CMAKE_INSTALL_PREFIX}/share/ldas-tools/cmake/Modules
  )

#========================================================================
# Start pulling in the LDAS Tools macros for easy setup
#========================================================================

find_package(AxLDASTools)

# Define BUILD_STATIC_LIBS option (shared across all LDAS Tools packages)
cx_static_libs_option()

cm_init(
  ${LDAS_TOOLS_DISKCACHEAPI_PACKAGE_NAME}
  "${LDAS_TOOLS_DISKCACHEAPI_VERSION}"
  ${LDAS_TOOLS_BUG_REPORT_URL}
  ${LDAS_TOOLS_DISKCACHEAPI_PACKAGE_NAME}
  ${LDAS_TOOLS_HOMEPAGE_URL}
  DESCRIPTION "${PROJECT_DESCRIPTION}"
  )

set( PROJECT_CONFIG_PATH "${PROJECT_SOURCE_DIR}/config" )

# =======================================================================
#  Prepare a header file to store the glorious configuration info
# =======================================================================
set( PROJECT_CONFIG_FILENAME
  "${CMAKE_CURRENT_BINARY_DIR}/diskcache_config.h" )

ch_top(
  "#ifndef DISKCACHE_CONFIG_H"
  "#define DISKCACHE_CONFIG_H"
  " "
)
ch_bottom(
  " "
  "#endif /* DISKCACHE_CONFIG_H */"
)

cx_scheme_sanitizer(
  ALLOW_WARNINGS
  ENABLE_ASAN
  ENABLE_USE_AFTER_SCOPE
  DISABLE_RECOVERY_ASAN
  DISABLE_LEAK_DETECTION_ASAN
  DISABLE_DETECT_HEAP_USE_AFTER_FREE
  DISABLE_DETECT_ODR_VIOLATION
  DISABLE_STACK_USE_AFTER_RETURN
  DISABLE_USE_AFTER_RETURN
  SUPPRESSION_FILE_ASAN ${CMAKE_SOURCE_DIR}/asan_suppressions.txt
  ENABLE_LSAN
  DISABLE_RECOVERY_LSAN
  SUPPRESSION_FILE_LSAN ${CMAKE_SOURCE_DIR}/lsan_suppressions.txt
  ENABLE_MSAN
  DISABLE_RECOVERY_MSAN
  DISABLE_LEAK_DETECTION_MSAN
  SUPPRESSION_FILE_MSAN ${CMAKE_SOURCE_DIR}/msan_suppressions.txt
  ENABLE_TSAN
  DISABLE_RECOVERY_TSAN
  SUPPRESSION_FILE_TSAN ${CMAKE_SOURCE_DIR}/tsan_suppressions.txt
  ENABLE_UBSAN
  DISABLE_RECOVERY_UBSAN
  SUPPRESSION_FILE_UBSAN ${CMAKE_SOURCE_DIR}/ubsan_suppressions.txt
)

message( WARNING "ASAN_OPTIONS: $ENV{ASAN_OPTIONS}" )

cx_check_compile_hardening( )
cx_cxx_warnings_as_errors( Debug )

# =======================================================================
#  Check for key programs
# =======================================================================

# Use cx_boost_* for conda-compatible Boost detection
cx_boost_base(${BOOST_MINIMUM_VERSION} REQUIRED)
cx_boost_filesystem()
cx_boost_regex()
cx_boost_system()

# =======================================================================
# Check for certain header files
# =======================================================================

cm_check_headers(sys/param.h)
cm_check_headers(sys/ucred.h)
cm_check_headers(sys/mnttab.h)
cm_check_headers(sys/mount.h)
cm_check_headers(mntent.h)

#dnl =====================================================================
#dnl Check for system functions
#dnl =====================================================================

cm_check_funcs(getmntent)
cm_check_funcs(getmntent_r)
cm_check_funcs(getmntinfo)
cm_check_funcs(getfsstat)
cm_check_funcs(setmntent)

# =======================================================================
# Look for additional ldas-tools pieces
# =======================================================================

if( NOT ENABLE_DOCUMENTATION_ONLY )
  cm_pkg_check_modules(
    PREFIX LDASTOOLS
    REQUIRED
    MODULES
      ldastoolsal>=${LDAS_TOOLS_AL_VERSION}
      ldastools-ldasgen>=${LDAS_TOOLS_LDASGEN_VERSION}
      ldastools-stat>=${LDAS_TOOLS_LDASGEN_VERSION}
    )
endif( )

#dnl =====================================================================
#dnl  Check init.d characteristics
#dnl =====================================================================

set( WITH_SYSTEMDSYSTEMUNITDIR auto CACHE PATH "Directory for systemd service files" )
set( yes_pattern "([Yy][Ee][Ss]|[Tt][Rr][Uu][Ee])" )
if ( WITH_SYSTEMDSYSTEMUNITDIR STREQUAL "auto" OR WITH_SYSTEMDSYSTEMUNITDIR MATCHES  "${yes_pattern}" )
  #     def_systemdsystemunitdir=$($PKG_CONFIG --variable=systemdsystemunitdir systemd)
  if ( PKG_CONFIG_FOUND )
    execute_process(
      COMMAND ${PKG_CONFIG_EXECUTABLE} --variable=systemdsystemunitdir systemd
      OUTPUT_VARIABLE def_systemdsystemunitdir
      OUTPUT_STRIP_TRAILING_WHITESPACE
      )
  endif( )
  if ( "${def_systemdsystemunitdir}" STREQUAL "" )
    if ( "${WITH_SYSTEMDSYSTEMUNITDIR}" MATCHES "${yes_pattern}" )
      cm_msg_error( "systemd support requested but pkg-config unable to query systemd package" )
    else( )
      set( WITH_SYSTEMDSYSTEMUNITDIR "no" CACHE PATH "" FORCE )
    endif( )
  else( )
    set( WITH_SYSTEMDSYSTEMUNITDIR "${def_systemdsystemunitdir}" CACHE PATH "" FORCE )
  endif( )
endif( )
if ( NOT WITH_SYSTEMDSYSTEMUNITDIR STREQUAL "no" )
  cm_subst( systemdsystemunitdir ${WITH_SYSTEMDSYSTEMUNITDIR} )
endif( )
cm_conditional(COND_HAVE_SYSTEMD EXPRESSION NOT "${WITH_SYSTEMDSYSTEMUNITDIR}" STREQUAL "no")

cm_conditional( RED_HAT_INITD EXISTS /etc/redhat-release )
cm_conditional( DEBIAN_INITD EXISTS /etc/debian_version )
cm_conditional( OSX_INITD EXISTS /Library/LaunchDaemons )
cm_conditional( SOLARIS_INITD EXISTS /usr/sbin/svccfg )

#========================================================================
# Traverse the tree
#========================================================================

add_subdirectory(src)
add_subdirectory(test)
add_subdirectory(doc)

#========================================================================
# Commit configuration information to the file
#========================================================================

cm_config_headers( ${PROJECT_CONFIG_FILENAME} )

#========================================================================
# Prepare the distribution
#========================================================================

cx_cpack(
  MACPORTS_CATEGORY science
  MAINTAINER "Edward Maros <ed.maros@ligo.org>"
  )

#========================================================================
# Report configuration information
#  CMAKE_MAKE_PROGRAM:FILEPATH
#========================================================================
cm_msg_notice( "=========================================================")
cm_msg_notice( "  Project ${PROJECT_NAME} has now ben successfully configured:")
cm_msg_notice( " " )
cm_msg_notice( "  installation directories:" )
cm_msg_notice( "      CMAKE_INSTALL_BINDIR           ${CMAKE_INSTALL_BINDIR}" )
cm_msg_notice( "      CMAKE_INSTALL_DATADIR          ${CMAKE_INSTALL_DATADIR}" )
cm_msg_notice( "      CMAKE_INSTALL_PREFIX           ${CMAKE_INSTALL_PREFIX}" )
if ( LDASTOOLS_FOUND )
cm_msg_notice( " ")
cm_msg_notice( "  LDASTools Package info:" )
cm_msg_notice( "      LDASTOOLS_INCLUDE_DIRS         ${LDASTOOLS_INCLUDE_DIRS}" )
cm_msg_notice( "      LDASTOOLS_LIBRARIES            ${LDASTOOLS_LIBRARIES}" )
cm_msg_notice( "      LDASTOOLS_LIBRARIES_FULL_PATH  ${LDASTOOLS_LIBRARIES_FULL_PATH}" )
endif( )
cm_msg_notice( " ")
cm_msg_notice( "  Initd configurations:" )
cm_msg_notice( "     SystemD:                       ${COND_HAVE_SYSTEMD}" )
cm_msg_notice( "     Red Hat:                       ${RED_HAT_INITD}" )
cm_msg_notice( "     DEBIAN:                        ${DEBIAN_INITD}" )
cm_msg_notice( "     OSX:                           ${OSX_INITD}" )
cm_msg_notice( "     SOLARIS:                       ${SOLARIS_INITD}" )
cm_msg_notice( " " )
cm_msg_notice( "  Sanitizers:" )
cm_msg_notice( "      Active:                         ${CX_SCHEME_SANITIZERS_ACTIVE}")
cm_msg_notice( " ")
cm_msg_notice( "  Now run 'cmake --build .' to build ${PROJECT_NAME}")
cm_msg_notice( "  and run 'cpack' to package ${PROJECT_NAME}")
cm_msg_notice( "=========================================================")
