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

# If CMake >=3.24 is used, set policies up to v3.24 to NEW
# if (NOT ${CMAKE_VERSION} VERSION_LESS 3.24)
#     cmake_policy(VERSION 3.24)
# endif()

project(CalgoIntegrators VERSION 1.4.0 LANGUAGES C)

include(GNUInstallDirs)
include(FetchContent)

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

if (IS_TOP_LEVEL)
    include(CTest)
    set(CMAKE_CTEST_ARGUMENTS "--verbose")
endif ()

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

# CalgoIntegrators is a third-party tool and library, which we never want to
# debug
# If using a multi config generator
if (GENERATOR_IS_MULTI_CONFIG)
    set(CMAKE_CONFIGURATION_TYPES "Release")
    set(CMAKE_DEFAULT_BUILD_TYPE "Release")
# If using a single config generator
else ()
    set(CMAKE_BUILD_TYPE "Release")
endif ()

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

# Install dependencies, too
set(LoadStaticSharedTargets_INCLUDE_PACKAGING TRUE)

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

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

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_subdirectory(test)
endif ()

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

if (CalgoIntegrators_INCLUDE_PACKAGING)
    add_subdirectory(packaging)
endif ()
