# SPDX-FileCopyrightText: none
# SPDX-License-Identifier: BSD-3-Clause
cmake_minimum_required(VERSION 3.27)
set(PIM_VERSION "6.6.40")

project(calendarsupport VERSION ${PIM_VERSION})

set(KF_MIN_VERSION "6.21.0")
set(QT_REQUIRED_VERSION "6.9.0")
set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
find_package(ECM ${KF_MIN_VERSION} CONFIG REQUIRED)

set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH})

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

include(ECMGenerateExportHeader)
include(ECMSetupVersion)
include(ECMGenerateHeaders)

file(
    GLOB_RECURSE ALL_CLANG_FORMAT_SOURCE_FILES
    *.cpp
    *.h
    *.c
)
set(PRE_COMMIT_INSTALLED FALSE)
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})
    find_program(PRE_COMMIT_EXECUTABLE pre-commit)
    if(PRE_COMMIT_EXECUTABLE)
        message(
            WARNING
            "This module has pre-commit support. pre-commit binary was found. Please execute pre-commit install -f"
        )
    endif()

    include(KDEGitCommitHooks)
    include(KDEClangFormat)
    kde_clang_format(${ALL_CLANG_FORMAT_SOURCE_FILES})
endif()

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

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

include(ECMGenerateQDoc)

set(CALENDARSUPPORT_LIB_VERSION ${PIM_VERSION})
set(KMIME_LIB_VERSION "6.6.40")
set(CALENDARUTILS_LIB_VERSION "6.6.40")
set(IDENTITYMANAGEMENT_LIB_VERSION "6.6.40")
set(AKONADICALENDAR_LIB_VERSION "6.6.40")
set(AKONADI_VERSION "6.6.40")
set(KTEXTADDONS_MIN_VERSION "1.9.1")
find_package(KPim6Akonadi ${AKONADI_VERSION} CONFIG REQUIRED)
find_package(
    Qt6
    ${QT_REQUIRED_VERSION}
    CONFIG
    REQUIRED
        Widgets
        Test
        PrintSupport
)
find_package(KF6I18n ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6GuiAddons ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6KIO ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KPim6Mime ${KMIME_LIB_VERSION} CONFIG REQUIRED)
find_package(KF6Codecs ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KPim6CalendarUtils ${CALENDARUTILS_LIB_VERSION} CONFIG REQUIRED)
find_package(KF6CalendarCore ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KPim6IdentityManagementCore ${IDENTITYMANAGEMENT_LIB_VERSION} CONFIG REQUIRED)
find_package(KF6Holidays ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KPim6AkonadiCalendar ${AKONADICALENDAR_LIB_VERSION} CONFIG REQUIRED)
find_package(KF6TextCustomEditor ${KTEXTADDONS_MIN_VERSION} CONFIG REQUIRED)

ecm_setup_version(PROJECT VARIABLE_PREFIX CALENDARSUPPORT
                        VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/src/calendarsupport_version.h"
                        PACKAGE_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/KPim6CalendarSupportConfigVersion.cmake"
                        SOVERSION 6
)

########### Targets ###########

add_definitions(-DQT_NO_CONTEXTLESS_CONNECT)
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()
ecm_set_disabled_deprecation_versions(QT 6.11.0 KF 6.22.0 KMIME 6.7.0)

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

########### CMake Config Files ###########
set(CMAKECONFIG_INSTALL_DIR "${KDE_INSTALL_CMAKEPACKAGEDIR}/KPim6CalendarSupport")

option(WARNINGS_AS_ERRORS "Warnings are errors -Werror" OFF)
if(WARNINGS_AS_ERRORS)
    if(MSVC)
        add_compile_options(/WX)
        add_compile_options(/wd4996)
    else()
        add_compile_options(-Werror)
        add_compile_options(-Wno-error=deprecated-declarations)
    endif()
endif()

add_subdirectory(src)

if(DEFINED kde_configure_git_pre_commit_hook)
    kde_configure_git_pre_commit_hook(CHECKS CLANG_FORMAT)
endif()
ki18n_install(po)

configure_package_config_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/KPimCalendarSupportConfig.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/KPim6CalendarSupportConfig.cmake"
    INSTALL_DESTINATION ${CMAKECONFIG_INSTALL_DIR}
)

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

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

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

ecm_feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES)
