# src/KMS/CMakeLists.txt
cmake_minimum_required(VERSION 3.0)

# 设置KMS库项目
set(KMS_TARGET kms)

# 收集KMS相关的源文件
file(GLOB_RECURSE KMS_Sources 
    CONFIGURE_DEPENDS
    "*.cpp" "*.c"
)

# 移除不需要的文件（如果有的话）
list(REMOVE_ITEM KMS_Sources "${CMAKE_CURRENT_SOURCE_DIR}/kms_core_huawei.cpp")

# 创建静态库
add_library(${KMS_TARGET} STATIC ${KMS_Sources})

# 查找必要的依赖包
find_package(jsoncpp REQUIRED)
find_package(OpenSSL REQUIRED)

# 设置包含目录
target_include_directories(${KMS_TARGET} 
    PUBLIC 
        ${CMAKE_CURRENT_SOURCE_DIR}/../../include/kms  # KMS头文件目录
    PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/../../include      # 项目include目录
        ${CMAKE_CURRENT_SOURCE_DIR}/../../pg_include   # PostgreSQL头文件
        ${CMAKE_CURRENT_SOURCE_DIR}/../../db_include
        ${CMAKE_CURRENT_SOURCE_DIR}/../utils           # utils目录
        ${CMAKE_CURRENT_SOURCE_DIR}/../encryptsql      # encryptsql目录
        ${CMAKE_CURRENT_SOURCE_DIR}/../crypto          # crypto目录
)


# 链接必要的库
target_link_libraries(${KMS_TARGET} 
    PUBLIC
        ${OPENSSL_LIBRARIES}
        jsoncpp_lib
    PRIVATE
)

# 设置编译选项
target_compile_options(${KMS_TARGET} PRIVATE
    -std=c++14
    -fvisibility=hidden
    -fPIC # 位置无关代码选项，应用于静态库链接到共享库
)

# 如果使用了条件编译宏，需要传递给KMS库
if(${USE_ENCRYPT})
    target_compile_definitions(${KMS_TARGET} PRIVATE USE_ENCRYPT)
endif()

if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
    target_compile_definitions(${KMS_TARGET} PRIVATE EDEBUG)
else()
    target_compile_definitions(${KMS_TARGET} PRIVATE NDEBUG)
endif()

# 导出库的信息供外部使用
set(KMS_LIBRARY ${KMS_TARGET} PARENT_SCOPE)
set(KMS_INCLUDE_DIRS 
    ${CMAKE_CURRENT_SOURCE_DIR}/../../include/kms
    PARENT_SCOPE)

message(STATUS "KMS library configured: ${KMS_TARGET}")


# --------------------------------------
# kms_cli 工具
# 可执行文件
# add_executable(kms_cli
#     main.cpp
# )

# target_include_directories(kms_cli
#     PRIVATE
#         ${CMAKE_CURRENT_SOURCE_DIR}/../../include
#         ${CMAKE_CURRENT_SOURCE_DIR}
# )

# set(PQ_LIB /usr/local/postgresql/lib/libpq.so)

# target_link_libraries(kms_cli
#     PRIVATE
#         ${KMS_TARGET}
#         ${PQ_LIB}
# )
