#------------------------------------------------------------------------
# -*- mode: cmake -*-
#------------------------------------------------------------------------
#
# LDASTools Abstraction Library  - General purpose library
#
# Copyright (C) 2018 California Institute of Technology
#
# LDASTools Abstraction 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 Abstraction 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
#
#------------------------------------------------------------------------

cmake_minimum_required(VERSION 3.12...4.0)

set(LDAS_TOOLS_UTILITIES_PACKAGE_NAME ldas-tools-utilities)
set(LDAS_TOOLS_UTILITIES_VERSION      2.7.4)

set(
  PROJECT_DESCRIPTION
  "LDAS Tools utilities derived from multiple LDAS Tools libraries"
  )
set(
  PROJECT_DESCRIPTION_LONG
  "This provides a collection of utilities that are derived from"
  "multiple libraries of the LDAS Tools Suite."
  )

set( BOOST_MINIMUM_VERSION 1.67 )

set(LDAS_TOOLS_CMAKE_VERSION        1.4.1)
set(LDAS_TOOLS_AL_VERSION           2.8.0)
set(LDAS_TOOLS_LDASGEN_VERSION      2.7.5)
set(LDAS_TOOLS_FRAMECPP_VERSION     4.0.1)
set(LDAS_TOOLS_FILTERS_VERSION      2.6.8)
set(LDAS_TOOLS_DISKCACHEAPI_VERSION 2.7.9)
set(LDAS_TOOLS_FRAMEAPI_VERSION     3.0.5)

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

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

if ( NOT CMAKE_OSX_ARCHITECTURES )
  set( CMAKE_OSX_ARCHITECTURES x86_64 )
endif( )


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

project( ${LDAS_TOOLS_UTILITIES_PACKAGE_NAME}
  VERSION ${LDAS_TOOLS_UTILITIES_VERSION}
  DESCRIPTION "${PROJECT_DESCRIPTION}"
)

#------------------------------------------------------------------------
#------------------------------------------------------------------------
function( utilities_target_test target )
  set(options
    )
  set(oneValueArgs
    )
  set(multiValueArgs
    SOURCES
    LDADD
    COMMAND_ARGS
    )

  cmake_parse_arguments( ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )

  if ( NOT ARG_LDADD )
    set( ARG_LDADD ${LDADD} )
  endif( )
  if ( NOT ARG_INCLUDES )
    set( ARG_INCLUDES ${AM_CPPFLAGS} )
  endif( )

  cx_target_executable(
    ${target}
    NOINST
    SOURCES ${ARG_SOURCES}
    LDADD ${ARG_LDADD}
    INCLUDE_DIRECTORIES BEFORE PRIVATE
    ${ARG_INCLUDES}
    )
  if ( NOT TARGET check )
    add_custom_target( check )
  endif( )
  add_dependencies( ${target} check )
  cx_target_test( ${target}
    COMMAND ${target} ${ARG_COMMAND_ARGS}
    ENVIRONMENT ${TESTS_ENVIRONMENT}
    )
endfunction( )

#------------------------------------------------------------------------
# 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)

cm_init(
  "${LDAS_TOOLS_UTILITIES_PACKAGE_NAME}"
  "${LDAS_TOOLS_UTILITIES_VERSION}"
  ${LDAS_TOOLS_BUG_REPORT_URL}
  "${LDAS_TOOLS_UTILITIES_PACKAGE_NAME}"
  ${LDAS_TOOLS_HOMEPAGE_URL}
  DESCRIPTION "${PROJECT_DESCRIPTION}"
  )

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

set( PROJECT_CONFIG_FILENAME
  "${CMAKE_CURRENT_BINARY_DIR}/utilities_config.h" )

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

cx_scheme_sanitizer(
  ALLOW_WARNINGS
  ENABLE_ASAN
  ENABLE_USE_AFTER_SCOPE
  DISABLE_DETECT_ODR_VIOLATION
  DISABLE_RECOVERY_ASAN
  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
  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
)

cx_check_compile_hardening( )
cx_cxx_warnings_as_errors( Debug )

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

# Use cx_boost_* for improved Boost detection
cx_boost_base(${BOOST_MINIMUM_VERSION} REQUIRED)
cx_boost_filesystem()
cx_boost_program_options()

# =====================================================================
#  Check for the operating system
# =====================================================================

# AX_LDAS_OS
# AX_LDAS_CHECK_OS_VARIABLE_LIBRARY_LOAD_PATH

# =====================================================================
#  Check for compilers and language features
# =====================================================================

# AX_LDAS_PROG_CC

# AX_LDAS_PROG_CXX
# AX_LDAS_CXX_CONSTEXPR
# AX_LDAS_CXX_EXCEPTIONS_RETHROW
# AX_LDAS_CXX_TEMPLATE_ALIASES
# AX_LDAS_CXX_TEMPLATE_ALIASES_VIA_USING
# AX_LDAS_CXX_TEMPLATE_AS_TEMPLATE_PARAMETER

# AX_LDAS_ARG_WITH_OPTIMIZATION

# =====================================================================
#  Checking if the OS is 64 bit
# =====================================================================

# AX_LDAS_SYS_64BIT_OS

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

# AX_LDAS_PROG_LIBTOOL
# AX_LDAS_PROG_DOXYGEN
# AX_LDAS_PROG_VALGRIND
# AX_LDAS_PROG_ABI_DUMPER
# AX_LDAS_PROG_ABI_COMPLIANCE_CHECKER
# AX_LDAS_PROG_PKG_CONFIG

# AC_CHECK_PROGS([EXPR],[expr])

# =====================================================================
#  Some programs need to be aware of the installation
# =====================================================================
# AC_DEFINE([LIBEXECDIR],
# 	  ["@libexecdir@"],
# 	  [Directory for installing executables that other other programs run])

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

if( NOT ENABLE_DOCUMENTATION_ONLY )
  # AX_LDAS_PKG_CHECK([ldastoolsal])
  # AX_LDAS_PKG_CHECK([framecpp])
  # AX_LDAS_PKG_CHECK([ldas-tools-filters])
  # AX_LDAS_PKG_CHECK([ldastools-ldasgen])
  # AX_LDAS_PKG_CHECK([ldastools-stat])
  # AX_LDAS_PKG_CHECK([ldastools-ldasframe])
  # AX_LDAS_PKG_CHECK([ldas-tools-diskcache])
endif( )

#========================================================================
# Find required system libraries
#========================================================================

# EL8 requires explicit pthread linking for boost libraries
cx_pthread(REQUIRED)

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

add_subdirectory(src)
add_subdirectory(doc)

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

cm_config_headers( ${PROJECT_CONFIG_FILENAME} )

#========================================================================
# Dummy test to all for ctest to succeed
#========================================================================

add_test( NAME empty_test
  COMMAND ${CMAKE_COMMAND} -E echo Empty test )

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

cx_scheme_sanitizer_set_environment( )

#========================================================================
# 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(" configuration_information:" )
cm_msg_notice("   Boost_PROGRAM_OPTIONS_LIBRARY: ${Boost_PROGRAM_OPTIONS_LIBRARY}" )
cm_msg_notice("   Boost_FILESYSTEM_LIBRARY: ${Boost_FILESYSTEM_LIBRARY}" )
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( " " )
cm_msg_notice( "  Sanitizers:" )
cm_msg_notice( "      Active:                         ${CX_SCHEME_SANITIZERS_ACTIVE}" )
cm_msg_notice( " " )
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("=========================================================")
