# krb5 module CMake configuration
#
# Copyright (C) 2026 Qore Technologies, s.r.o.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

# Start of hacks to simulate installed QoreConfig.cmake
if (NOT QORE_MODULES_DIR)
    set(QORE_MODULES_DIR ${MODULE_DIR})
endif()

if (NOT QORE_LIBRARY)
    set(QORE_LIBRARY libqore)
endif()

set(QORE_API_VERSION ${MODULE_API_MAJOR}.${MODULE_API_MINOR})

include(${CMAKE_SOURCE_DIR}/cmake/QoreMacros.cmake)
### end of hacks

include(CheckCXXCompilerFlag)
include(CheckSymbolExists)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_BINARY_DIR}/include)
include_directories(${CMAKE_SOURCE_DIR}/include)

check_cxx_compiler_flag(-fvisibility=hidden HAVE_GCC_VISIBILITY)

set(CMAKE_REQUIRED_INCLUDES ${QORE_KRB5_INCLUDE_DIRS})
string(REPLACE ";" " " QORE_KRB5_CFLAGS_STRING "${QORE_KRB5_CFLAGS}")
set(CMAKE_REQUIRED_FLAGS ${QORE_KRB5_CFLAGS_STRING})
set(CMAKE_REQUIRED_LIBRARIES ${QORE_KRB5_LINK_LIBRARIES})
check_symbol_exists(KRB5_GC_CONSTRAINED_DELEGATION "krb5.h" HAVE_KRB5_GC_CONSTRAINED_DELEGATION)
check_symbol_exists(gss_acquire_cred_impersonate_name "gssapi/gssapi_ext.h"
    HAVE_GSS_ACQUIRE_CRED_IMPERSONATE_NAME)
unset(CMAKE_REQUIRED_INCLUDES)
unset(CMAKE_REQUIRED_FLAGS)
unset(CMAKE_REQUIRED_LIBRARIES)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/krb5-config.h.cmake
    ${CMAKE_CURRENT_BINARY_DIR}/krb5-config.h)

set(KRB5_QPP_SRC
    src/QC_Krb5Context.qpp
    src/QC_Krb5Credentials.qpp
    src/QC_Krb5CredentialCache.qpp
    src/QC_Krb5Keytab.qpp
    src/QC_Krb5Principal.qpp
    src/QC_GssCredential.qpp
    src/QC_GssClientContext.qpp
    src/QC_GssAcceptorContext.qpp
)

set(KRB5_CPP_SRC
    src/krb5-module.cpp
)

set(QORE_DOX_KRB5_TMPL_SRC
    docs/mainpage.doxygen.tmpl
    docs/usecases.doxygen.tmpl
    docs/security.doxygen.tmpl
    docs/examples.doxygen.tmpl
    docs/release-notes.doxygen.tmpl
)

if (DOXYGEN_FOUND)
    qore_wrap_dox(QORE_DOX_KRB5_SRC ${QORE_DOX_KRB5_TMPL_SRC})
endif()

qore_wrap_qpp_value(KRB5_QPP_SOURCES DOXLIST _dox_src METALIST KRB5_QPP_META_SRC ${KRB5_QPP_SRC})
add_custom_target(KRB5_QPP_GENERATED_FILES DEPENDS qpp ${KRB5_QPP_SOURCES})

set(krb5_module_name "krb5")

if (SINGLE_COMPILATION_UNIT)
    set(KRB5_SCU_PATH ${CMAKE_CURRENT_BINARY_DIR}/krb5_scu.cpp)

    set(_scu_content "// Auto-generated krb5 single-compilation-unit file\n")
    set(_scu_content "${_scu_content}// Generated by cmake; do not edit manually\n\n")
    foreach(_f ${KRB5_CPP_SRC})
        get_filename_component(_abs ${_f} ABSOLUTE)
        set(_scu_content "${_scu_content}#include \"${_abs}\"\n")
    endforeach()
    set(_scu_content "${_scu_content}\n")
    foreach(_f ${KRB5_QPP_SOURCES})
        set(_scu_content "${_scu_content}#include \"${_f}\"\n")
    endforeach()
    file(WRITE ${KRB5_SCU_PATH} "${_scu_content}")

    add_library(${krb5_module_name} MODULE ${KRB5_SCU_PATH})

    set(_scu_deps ${KRB5_QPP_SOURCES})
    foreach(_f ${KRB5_CPP_SRC})
        get_filename_component(_abs ${_f} ABSOLUTE)
        list(APPEND _scu_deps ${_abs})
    endforeach()
    set_source_files_properties(${KRB5_SCU_PATH} PROPERTIES
        OBJECT_DEPENDS "${_scu_deps}"
    )
else()
    add_library(${krb5_module_name} MODULE ${KRB5_CPP_SRC} ${KRB5_QPP_SOURCES})
endif()

target_include_directories(${krb5_module_name} PRIVATE ${QORE_KRB5_INCLUDE_DIRS})
if (QORE_KRB5_CFLAGS)
    target_compile_options(${krb5_module_name} PRIVATE ${QORE_KRB5_CFLAGS})
endif()
if (QORE_KRB5_LIBRARY_DIRS)
    target_link_directories(${krb5_module_name} PRIVATE ${QORE_KRB5_LIBRARY_DIRS})
endif()
target_link_libraries(${krb5_module_name} ${QORE_KRB5_LINK_LIBRARIES})

add_dependencies(${krb5_module_name} libqore KRB5_QPP_GENERATED_FILES)
set(MODULE_DOX_INPUT ${QORE_DOX_KRB5_SRC} ${_dox_src})
qore_binary_module_qore(${krb5_module_name} "${VERSION}" "/${VERSION}")

if (COMMAND qore_install_qpp_metadata)
    qore_install_qpp_metadata(${krb5_module_name} ${KRB5_QPP_META_SRC})
endif()

install(PROGRAMS
    bin/qklist
    DESTINATION ${CMAKE_INSTALL_BINDIR}
    COMPONENT ${QORE_BINARY_MODULE_INSTALL_COMPONENT}
)

install(FILES
    man/qklist.1
    DESTINATION ${CMAKE_INSTALL_MANDIR}/man1
    COMPONENT ${QORE_BINARY_MODULE_INSTALL_COMPONENT}
)

if (DOXYGEN_FOUND)
    add_custom_target(QORE_KRB5_MOD_DOX_FILES DEPENDS ${QORE_DOX_KRB5_SRC})
    add_dependencies(docs-krb5 QORE_KRB5_MOD_DOX_FILES)
endif()
