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

project(KIdentityManagement VERSION ${PIM_VERSION})

# ECM setup
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(ECMGenerateQDoc)
include(GenerateExportHeader)
include(ECMGenerateHeaders)

include(ECMSetupVersion)
file(
    GLOB_RECURSE ALL_CLANG_FORMAT_SOURCE_FILES
    src/*.cpp
    src/*.h
)
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(STATUS "This module has pre-commit support. pre-commit binary was found. Installing pre-commit hook")
        execute_process(
            COMMAND
                "${PRE_COMMIT_EXECUTABLE}" install -f
            ERROR_QUIET
            RESULT_VARIABLE _precommitresult
            WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
        )
        if(_precommitresult EQUAL 0)
            message(STATUS "Pre-commit installed.")
        else()
            message(
                WARNING
                "Error during installing pre-commit hook: ${PRE_COMMIT_EXECUTABLE} project source dir ${PROJECT_SOURCE_DIR}"
            )
            include(KDEGitCommitHooks)
            include(KDEClangFormat)
            kde_clang_format(${ALL_CLANG_FORMAT_SOURCE_FILES})
        endif()
    else()
        message(WARNING "This module has pre-commit support. pre-commit binary was not found.")
        include(KDEGitCommitHooks)
        include(KDEClangFormat)
        kde_clang_format(${ALL_CLANG_FORMAT_SOURCE_FILES})
    endif()
endif()

include(ECMQtDeclareLoggingCategory)
include(ECMDeprecationSettings)
include(ECMFeatureSummary)
include(ECMQmlModule)
include(ECMCheckOutboundLicense)

option(BUILD_QUICK_SUPPORT "Build quick support. Needed for merkuro project" ON)

set(KIDENTITYMANAGEMENT_LIB_VERSION ${PIM_VERSION})
set(PIMTEXTEDIT_LIB_VERSION "6.6.40")
set(KTEXTADDONS_MIN_VERSION "1.9.1")

########### Find packages ###########

find_package(Qt6Network NO_MODULE REQUIRED)
find_package(Qt6DBus ${QT_REQUIRED_VERSION} CONFIG REQUIRED)

if(BUILD_QUICK_SUPPORT)
    find_package(Qt6Quick NO_MODULE REQUIRED)
endif()

find_package(KF6CoreAddons ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6XmlGui ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6KIO ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6Config ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6Codecs ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6I18n ${KF_MIN_VERSION} CONFIG REQUIRED)

find_package(KF6TextCustomEditor ${KTEXTADDONS_MIN_VERSION} CONFIG REQUIRED)
find_package(KPim6TextEdit ${PIMTEXTEDIT_LIB_VERSION} CONFIG REQUIRED)

if(BUILD_TESTING)
    find_package(Qt6Test ${QT_REQUIRED_VERSION} CONFIG REQUIRED)
endif()

if(BUILD_QUICK_SUPPORT)
    find_package(KF6KirigamiAddons 1.0 REQUIRED)
    set_package_properties(
        KF6KirigamiAddons
        PROPERTIES
            TYPE
                REQUIRED
            PURPOSE
                "Addons for the Kirigami application UI framework"
    )
endif()
add_definitions(-DTRANSLATION_DOMAIN=\"libkpimidentities6\")

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

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

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

########### Targets ###########
add_subdirectory(src)

if(DEFINED kde_configure_git_pre_commit_hook)
    kde_configure_git_pre_commit_hook(CHECKS CLANG_FORMAT)
endif()
ki18n_install(po)
ecm_feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES)
file(
    GLOB_RECURSE ALL_SOURCE_FILES
    src/*.cpp
    src/*.h
)
ecm_check_outbound_license(LICENSES GPL-2.0-only  FILES ${ALL_SOURCE_FILES})
