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

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.25)

set(CMAKE_MAXIMUM_SUPPORTED_VERSION 3.29)

include("./cmake/HandlePolicies.cmake" NO_POLICY_SCOPE)

project(CalgoIntegrators VERSION 1.7.0 LANGUAGES C)

include(GNUInstallDirs)
include(FetchContent)

string(COMPARE EQUAL "${CMAKE_PROJECT_NAME}" "${PROJECT_NAME}" IS_TOP_LEVEL)

if (IS_TOP_LEVEL)
    include(CTest)

    if (NOT DEFINED CMAKE_CTEST_ARGUMENTS)
        set(CMAKE_CTEST_ARGUMENTS "--verbose")
    endif ()
endif ()

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

# CalgoIntegrators is a third-party tool and library, which we rarely want to
# debug. Set the default build type to Release.
# If using a multi config generator
get_property(_IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if (_IS_MULTI_CONFIG)
    set(CMAKE_DEFAULT_BUILD_TYPE "Release")
# If using a single config generator
else ()
    if (NOT DEFINED CMAKE_BUILD_TYPE)
        set(CMAKE_BUILD_TYPE "Release")
    endif ()
endif ()

option(CalgoIntegrators_INCLUDE_PACKAGING "Include packaging rules for Calgo Integrators" "${IS_TOP_LEVEL}")

FetchContent_Declare(
    LoadStaticSharedTargets
    GIT_REPOSITORY "git@gitlab.aei.uni-hannover.de:cmake/loadstaticsharedtargets.git"
    GIT_TAG v1.6.0
    SYSTEM
    FIND_PACKAGE_ARGS 1.6.0 CONFIG NAMES LoadStaticSharedTargets
)

# Install dependencies, too
set(LoadStaticSharedTargets_INCLUDE_PACKAGING "${CalgoIntegrators_INCLUDE_PACKAGING}")

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

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

FetchContent_MakeAvailable(NetlibF2C LoadStaticSharedTargets)

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)

if (IS_TOP_LEVEL AND BUILD_TESTING)
    # Only for executables
    if (BUILD_SHARED_LIBS)
        include(CheckPIESupported)
        check_pie_supported()
    endif ()

    add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} -V)

    add_subdirectory(test EXCLUDE_FROM_ALL)
endif ()

if (CalgoIntegrators_INCLUDE_PACKAGING)
    add_subdirectory(packaging)
endif ()
