cmake_minimum_required(VERSION 3.5)
project(srp LANGUAGES C VERSION 0.0.2)

# CMP0077: option() 接受来自 conan_toolchain 的 normal 变量覆盖
# 配合 tools.cmake.cmaketoolchain:extra_variables={"FULL_STATIC": "ON"}
cmake_policy(SET CMP0077 NEW)

include(GNUInstallDirs)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/usr/bin)

option(BUILD_TEST "Build test_package" OFF)

if (BUILD_TEST)
add_definitions(-DBUILD_TEST)
endif()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__FILENAME__='\"$(notdir $(abspath $(patsubst %.o,%,$@)))\"'")

set(CMAKE_SKIP_RPATH On)

# ============================================================
# 静态链接配置
#
# FULL_STATIC=OFF (默认): glibc 半静态 — libevent/openssl/libgcc 静态，
#   libc/pthread 动态，用于本地开发调试
# FULL_STATIC=ON: musl 全静态 (-static) — 零动态依赖，
#   用于 Linux 发布构建 (./scripts/build-static.sh)
#
# FULL_STATIC 由 conan profile 的 CMakeToolchain extra_variables 注入
# ============================================================
option(FULL_STATIC "Enable full static linking (requires musl)" OFF)

if (FULL_STATIC)
    if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
        set(STATIC_LINK_OPTIONS -static)
    elseif (CMAKE_C_COMPILER_ID STREQUAL "Clang")
        set(STATIC_LINK_OPTIONS -static)
    endif()
else()
    # 半静态: libgcc 静态, libc/pthread 动态
    if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
        set(STATIC_LINK_OPTIONS -static-libgcc -Wl,--as-needed)
    elseif (CMAKE_C_COMPILER_ID STREQUAL "Clang")
        set(STATIC_LINK_OPTIONS -static-libgcc -Wl,--as-needed)
    endif()
endif()

set(PUB_SRC src/common.c src/buffer.c src/connect.c src/crypto.c src/srp.c src/network.c src/config.c src/access_control.c)
set(NODE_SRC ${PUB_SRC} src/node.c srpn.c)
set(SRV_SRC ${PUB_SRC} src/server.c srps.c)

find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBEV REQUIRED libevent)
pkg_check_modules(CRYPTO REQUIRED libcrypto)

add_executable(srpn ${NODE_SRC})
add_executable(srps ${SRV_SRC})

target_compile_definitions(srpn PUBLIC _NODE)
target_compile_definitions(srps PUBLIC _SERVER)

target_include_directories(srpn PUBLIC
    ./src
    ${LIBEV_INCLUDE_DIRS}
    ${CRYPTO_INCLUDE_DIRS}
)
target_link_directories(srpn PUBLIC
    ${LIBEV_LIBRARY_DIRS}
    ${CRYPTO_LIBRARY_DIRS}
)
target_link_libraries(srpn PUBLIC
    ${LIBEV_LIBRARIES}
    ${CRYPTO_LIBRARIES}
    pthread
)
target_link_options(srpn PRIVATE ${STATIC_LINK_OPTIONS})

target_include_directories(srps PUBLIC
    ./src
    ${LIBEV_INCLUDE_DIRS}
    ${CRYPTO_INCLUDE_DIRS}
)
target_link_directories(srps PUBLIC
    ${LIBEV_LIBRARY_DIRS}
    ${CRYPTO_LIBRARY_DIRS}
)
target_link_libraries(srps PUBLIC
    ${LIBEV_LIBRARIES}
    ${CRYPTO_LIBRARIES}
    pthread
)
target_link_options(srps PRIVATE ${STATIC_LINK_OPTIONS})

if (BUILD_TEST)
add_subdirectory(test_package)
endif()

install(TARGETS srps DESTINATION usr/bin/)
install(TARGETS srpn DESTINATION usr/bin/)
install(FILES scripts/srps.conf DESTINATION etc/srp/)
install(FILES scripts/srpn.conf DESTINATION etc/srp/)
install(FILES scripts/srpn.service DESTINATION usr/lib/systemd/system/)
install(FILES scripts/srps.service DESTINATION usr/lib/systemd/system/)
