#[[
CMake build script for ACM Collected Algorithms: DCUHRE and DQUAD
Copyright (C) 2022  Tim Haase

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program.  If not, see
<https://www.gnu.org/licenses/>.
]]

cmake_minimum_required(VERSION 3.20)

if (NOT ${CMAKE_VERSION} VERSION_LESS 3.24)
    cmake_policy(VERSION 3.24)
endif()

project(CalgoIntegrators VERSION 1.0.0 LANGUAGES C)

include(GNUInstallDirs)
include(FetchContent)

include(CTest)
set(CMAKE_CTEST_ARGUMENTS "--verbose")

# Enable diagnostic output for FetchContent
get_property(FETCHCONTENT_QUIET_HELPSTRING CACHE FETCHCONTENT_QUIET PROPERTY HELPSTRING)
set(FETCHCONTENT_QUIET OFF CACHE BOOL "${FETCHCONTENT_QUIET_HELPSTRING}" FORCE)

option(CalgoIntegrators_SHARED_LIBS "Build DCUHRE and DQUAD as a shared libraries?" OFF)

if (DEFINED CalgoIntegrators_SHARED_LIBS)
    set(BUILD_SHARED_LIBS "${CalgoIntegrators_SHARED_LIBS}")
endif ()

# If using a single target generator
if (NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE "Release" CACHE INTERNAL "Choose the build type.")
endif ()

if (BUILD_SHARED_LIBS)
    include(CheckPIESupported)
    check_pie_supported()
endif ()

FetchContent_Declare(
    LoadStaticSharedTargets
    GIT_REPOSITORY "git@gitlab.aei.uni-hannover.de:cmake/loadstaticsharedtargets.git"
    GIT_TAG v1.2
)

FetchContent_Declare(
    NetlibF2C
    GIT_REPOSITORY "git@gitlab.aei.uni-hannover.de:netlib/netlib-f2c.git"
    GIT_TAG        v1.0
    FIND_PACKAGE_ARGS NAMES NetlibF2C CONFIG
)

set(NetlibF2C_SHARED_LIBS "${BUILD_SHARED_LIBS}")
# Install dependencies, too
set(NetlibF2C_INCLUDE_PACKAGING TRUE)

FetchContent_MakeAvailable(NetlibF2C LoadStaticSharedTargets)

FetchContent_GetProperties(
    LoadStaticSharedTargets
    SOURCE_DIR LoadStaticSharedTargets_SOURCE_DIR
)

option(CalgoIntegrators_LICENSE_ACCEPTED "Did the user accept the ACM license?" NO)

if (NOT CalgoIntegrators_LICENSE_ACCEPTED)
    file(READ "3rdparty/ACM Software License Agreement.txt" ACM_LICENSE_TEXT)

    message(
        WARNING
        "The Collected Algorithms (CALGO) is published by ACM "
        "(Association for Computing Machinery) under the following license:"
    )
    message(NOTICE "${ACM_LICENSE_TEXT}")
    message(
        NOTICE
        "The latest version of the license text can be found here: "
        "<https://www.acm.org/publications/policies/software-copyright-notice>"
    )
    message(
        FATAL_ERROR
        "Please acknowledge the above license for the ACM Collected Algorithms (CALGO) "
        "by setting the configuration variable 'CalgoIntegrators_LICENSE_ACCEPTED' to 'YES'!"
    )
endif ()

add_subdirectory(src)
add_subdirectory(test)

string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" is_top_level)
option(CalgoIntegrators_INCLUDE_PACKAGING "Include packaging rules for Calgo Integrators" "${is_top_level}")

if (CalgoIntegrators_INCLUDE_PACKAGING)
    add_subdirectory(packaging)
endif ()
