cmake_minimum_required(VERSION 4.0)
project(tsl-server)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

message(STATUS "CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")

# 设置默认构建类型
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  set(CMAKE_BUILD_TYPE
      "Release"
      CACHE STRING "Build type" FORCE)
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
                                               "MinSizeRel" "RelWithDebInfo")
endif()

if(MSVC)
  add_compile_options(/W4 /permissive- /Zc:__cplusplus /utf-8)
  set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
else()
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# MinGW/MSYS2 静态链接
if(MINGW)
  add_link_options(-static -static-libgcc -static-libstdc++)
endif()

# Linux 静态链接
if(UNIX AND NOT APPLE)
  add_link_options(-static-libgcc -static-libstdc++)
  find_package(Threads REQUIRED)
endif()

if(DEFINED VCPKG_DIR)
  if(NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(CMAKE_TOOLCHAIN_FILE
        ${VCPKG_DIR}/scripts/buildsystems/vcpkg.cmake
        CACHE PATH "")
  endif()
  message(STATUS ">>> Vcpkg: ${CMAKE_TOOLCHAIN_FILE}")

  if(NOT DEFINED VCPKG_TARGET_TRIPLET)
    set(VCPKG_TARGET_TRIPLET
        "x64-windows-static"
        CACHE STRING "")
  endif()

  list(APPEND CMAKE_INCLUDE_PATH
       ${VCPKG_DIR}/installed/x64-windows-static/include)
  list(APPEND CMAKE_PREFIX_PATH ${VCPKG_DIR}/installed/x64-windows-static)
endif()

set(CMAKE_FIND_LIBRARY_SUFFIXES ".a" ".lib" ".so")

find_package(glaze CONFIG REQUIRED)
find_package(spdlog CONFIG REQUIRED)
find_package(fmt CONFIG REQUIRED)
find_package(Taskflow REQUIRED)

if(NOT TARGET spdlog::spdlog_header_only)
  message(WARNING "spdlog header-only target not found, using shared library")
endif()
if(NOT TARGET fmt::fmt-header-only)
  message(WARNING "fmt header-only target not found, using shared library")
endif()

if(UNIX AND NOT APPLE)
  find_package(Threads REQUIRED)
endif()

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
set(SOURCES
    src/main.cpp
    src/utils/args_parser.cpp
    src/language/tsl_keywords.cpp
    src/core/dispacther.cpp
    src/core/server.cpp
    src/scheduler/request_scheduler.cpp
    src/provider/base/provider_registry.cpp
    src/provider/base/provider_interface.cpp
    src/provider/initialize/initialize_provider.cpp
    src/provider/initialized/initialized_provider.cpp
    src/provider/text_document/did_open_provider.cpp
    src/provider/text_document/did_change_provider.cpp
    src/provider/text_document/completion_provider.cpp
    src/provider/shutdown/shutdown_provider.cpp
    src/provider/exit/exit_provider.cpp
    src/provider/cancel_request/cancel_request_provider.cpp
    src/provider/trace/set_trace_provider.cpp)

add_executable(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PRIVATE src)

target_compile_definitions(${PROJECT_NAME} PRIVATE SPDLOG_HEADER_ONLY
                                                   FMT_HEADER_ONLY)

target_link_libraries(
  ${PROJECT_NAME} PRIVATE glaze::glaze Taskflow::Taskflow
                          spdlog::spdlog_header_only fmt::fmt-header-only)

# Linux 需要链接 pthread
if(UNIX AND NOT APPLE)
  target_link_libraries(${PROJECT_NAME} PRIVATE Threads::Threads)
endif()
