# SPDX-FileCopyrightText: none
# SPDX-License-Identifier: BSD-3-Clause
cmake_minimum_required(VERSION 3.29)

set(PIM_VERSION "6.8.0")
project(KCalUtils VERSION ${PIM_VERSION})

# ECM setup
set(KF_MIN_VERSION "6.25.0")
set(QT_REQUIRED_VERSION "6.9.0")
find_package(ECM ${KF_MIN_VERSION} CONFIG REQUIRED)
set(CMAKE_MODULE_PATH
    ${KCalUtils_SOURCE_DIR}/cmake
    ${ECM_MODULE_PATH}
)

include(KDEInstallDirs)
include(KDECMakeSettings)
include(KDECompilerSettings NO_POLICY_SCOPE)

include(ECMGenerateHeaders)
include(ECMGenerateExportHeader)

include(ECMSetupVersion)
file(
    GLOB_RECURSE ALL_CLANG_FORMAT_SOURCE_FILES
    src/*.cpp
    src/*.h
    autotests/*.cpp
    autotests/*.h
)
if(EXISTS "${PROJECT_SOURCE_DIR}/.git/")
    set(GIT_SOURCE_TARBALL TRUE)
endif()

if(GIT_SOURCE_TARBALL)
    set(PRE_COMMIT_INSTALLED FALSE)
    include(KDEGitCommitHooks)
    include(KDEClangFormat)
    kde_clang_format(${ALL_CLANG_FORMAT_SOURCE_FILES})
    if(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR})
        if(EXISTS "${PROJECT_SOURCE_DIR}/.git/hooks/pre-commit")
            file(READ "${PROJECT_SOURCE_DIR}/.git/hooks/pre-commit" FILE_CONTENTS)
            string(
                FIND "${FILE_CONTENTS}"
                "File generated by pre-commit"
                INDEX
            )
            if(${INDEX} GREATER_EQUAL 0)
                set(PRE_COMMIT_INSTALLED TRUE)
            endif()
        endif()
    endif()
    if(NOT ${PRE_COMMIT_INSTALLED})
        function(install_hook executable hook_name)
            message(
                STATUS
                "This module has ${hook_name} support. ${hook_name} binary was found. Installing ${hook_name} hook"
            )
            execute_process(
                COMMAND
                    "${executable}" install -f
                ERROR_QUIET
                RESULT_VARIABLE _precommitresult
                WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
            )
            if(_precommitresult EQUAL 0)
                message(STATUS "${hook_name} installed.")
            else()
                message(
                    WARNING
                    "Error during installing ${hook_name} hook: ${executable} project source dir ${PROJECT_SOURCE_DIR}"
                )
            endif()
        endfunction()

        find_program(PREK_EXECUTABLE prek)
        if(PREK_EXECUTABLE)
            install_hook(${PREK_EXECUTABLE} "Prek")
        else()
            find_program(PRE_COMMIT_EXECUTABLE pre-commit)
            if(PRE_COMMIT_EXECUTABLE)
                install_hook(${PRE_COMMIT_EXECUTABLE} "pre-commit")
            else()
                message(WARNING "This module has pre-commit support. pre-commit binary was not found.")
            endif()
        endif()
    endif()
endif()

include(ECMQtDeclareLoggingCategory)
include(ECMDeprecationSettings)
include(ECMFeatureSummary)

include(ECMCheckOutboundLicense)
file(
    GLOB_RECURSE ALL_SOURCE_FILES
    src/*.cpp
    src/*.h
    autotests/*.cpp
    autotests/*.h
)
ecm_check_outbound_license(LICENSES GPL-2.0-only  FILES ${ALL_SOURCE_FILES})

include(ECMGenerateQDoc)

set(IDENTITYMANAGER_LIB_VERSION "6.8.0")
ecm_setup_version(PROJECT VARIABLE_PREFIX KCALUTILS
                        VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/src/kcalutils_version.h"
                        PACKAGE_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/KPim6CalendarUtilsConfigVersion.cmake"
                        SOVERSION 6
)

########### Find packages ###########
find_package(KF6CoreAddons ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6I18n ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6Codecs ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6WidgetsAddons ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6IconThemes ${KF_MIN_VERSION} CONFIG REQUIRED)

find_package(KF6TextTemplate CONFIG REQUIRED)

find_package(KF6CalendarCore ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KPim6IdentityManagementCore ${IDENTITYMANAGER_LIB_VERSION} CONFIG REQUIRED)
add_definitions(-DTRANSLATION_DOMAIN=\"libkcalutils6\")

add_definitions(-DQT_NO_CONTEXTLESS_CONNECT)
ecm_set_disabled_deprecation_versions(QT 6.12.0 KF 6.28.0)

if(BUILD_TESTING)
    add_definitions(-DBUILD_TESTING)
endif()
option(USE_UNITY_CMAKE_SUPPORT "Use UNITY cmake support (speedup compile time)" OFF)

set(COMPILE_WITH_UNITY_CMAKE_SUPPORT OFF)
if(USE_UNITY_CMAKE_SUPPORT)
    set(COMPILE_WITH_UNITY_CMAKE_SUPPORT ON)
    add_definitions(-DCOMPILE_WITH_UNITY_CMAKE_SUPPORT)
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    # Not setting for GNU due to too many warnings related to private members of base classes or around lambdas
    # see e.g. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56556 or https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79328
    string(APPEND CMAKE_CXX_FLAGS " -Wshadow")
endif()

option(WARNINGS_AS_ERRORS "Warnings are errors -Werror" OFF)
if(WARNINGS_AS_ERRORS)
    if(MSVC)
        # FIXME Disabled because of inconsistent dll linkage warnings
        # add_compile_options(/WX)
        # add_compile_options(/wd4996)
    else()
        add_compile_options(-Werror)
        add_compile_options(-Wno-error=deprecated-declarations)
    endif()
endif()
add_compile_definitions(QT_ENABLE_STRICT_MODE_UP_TO=0x060B00)
add_subdirectory(src)

if(BUILD_TESTING)
    add_subdirectory(autotests)
endif()

########### CMake Config Files ###########
set(CMAKECONFIG_INSTALL_DIR "${KDE_INSTALL_CMAKEPACKAGEDIR}/KPim6CalendarUtils")
configure_package_config_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/KPimCalendarUtilsConfig.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/KPim6CalendarUtilsConfig.cmake"
    INSTALL_DESTINATION ${CMAKECONFIG_INSTALL_DIR}
)

install(
    FILES
        "${CMAKE_CURRENT_BINARY_DIR}/KPim6CalendarUtilsConfig.cmake"
        "${CMAKE_CURRENT_BINARY_DIR}/KPim6CalendarUtilsConfigVersion.cmake"
    DESTINATION "${CMAKECONFIG_INSTALL_DIR}"
    COMPONENT Devel
)

install(
    EXPORT KPim6CalendarUtilsTargets
    DESTINATION "${CMAKECONFIG_INSTALL_DIR}"
    FILE KPim6CalendarUtilsTargets.cmake
    NAMESPACE KPim6::
)

install(
    FILES
        ${CMAKE_CURRENT_BINARY_DIR}/src/kcalutils_version.h
    DESTINATION ${KDE_INSTALL_INCLUDEDIR}/KPim6/KCalUtils/
    COMPONENT Devel
)

if(GIT_SOURCE_TARBALL)
    if(DEFINED kde_configure_git_pre_commit_hook)
        kde_configure_git_pre_commit_hook(CHECKS CLANG_FORMAT)
    endif()
endif()
ki18n_install(po)
ecm_feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES)
