cmake_minimum_required(VERSION 3.16)
project(LazyLLMCPP LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

if (MSVC)
    add_compile_options(/utf-8)
endif()

function(lazyllm_enable_strict_warnings target_name)
    if (MSVC)
        target_compile_options(${target_name} PRIVATE /W4 /WX)
    else ()
        target_compile_options(${target_name} PRIVATE -Werror -Wshadow)
    endif ()
endfunction()

# Third party libs
include(cmake/third_party.cmake)
include(cmake/download_tokenizers.cmake)

# Config lazyllm_core lib with pure cpp code.
file(GLOB_RECURSE LAZYLLM_CORE_SOURCES CONFIGURE_DEPENDS
    "${CMAKE_CURRENT_SOURCE_DIR}/core/src/*.cpp")
add_library(lazyllm_core STATIC ${LAZYLLM_CORE_SOURCES})
target_include_directories(lazyllm_core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/core/include)
target_link_libraries(lazyllm_core PUBLIC xxhash)
target_link_libraries(lazyllm_core PUBLIC tiktoken)
target_link_libraries(lazyllm_core PUBLIC utf8proc)
target_link_libraries(lazyllm_core PUBLIC ${CMAKE_DL_LIBS})
lazyllm_enable_strict_warnings(lazyllm_core)

# Config lazyllm_cpp lib with binding informations.
file(GLOB_RECURSE LAZYLLM_BINDING_SOURCES CONFIGURE_DEPENDS
    "${CMAKE_CURRENT_SOURCE_DIR}/binding/*.cpp")
set(INTERFACE_TARGET_NAME lazyllm_cpp)
pybind11_add_module(${INTERFACE_TARGET_NAME} ${LAZYLLM_BINDING_SOURCES})
target_include_directories(${INTERFACE_TARGET_NAME} PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/binding
    ${CMAKE_CURRENT_SOURCE_DIR}/core/include
)
target_link_libraries(${INTERFACE_TARGET_NAME} PRIVATE lazyllm_core)
lazyllm_enable_strict_warnings(${INTERFACE_TARGET_NAME})

# Runtime loader configuration per platform.
set(_lazyllm_cpp_rpath "")
if (WIN32)
    # Windows has no ELF rpath; loader resolution is driven by PATH and DLL search order.
    # Keep test runtime env empty by default.
elseif (APPLE)
    # Ensure lazyllm_cpp can find third-party dylibs under lazyllm/cpp_lib.
    list(APPEND _lazyllm_cpp_rpath "@loader_path/cpp_lib")
else ()
    # Ensure lazyllm_cpp can find third-party shared libraries under lazyllm/cpp_lib.
    list(APPEND _lazyllm_cpp_rpath "$ORIGIN/cpp_lib")
    # Use DT_RPATH (instead of DT_RUNPATH) so the extension's own runtime
    # search path can take precedence over host interpreter bundled libs.
    target_link_options(${INTERFACE_TARGET_NAME} PRIVATE -Wl,--disable-new-dtags)

    # Resolve libstdc++ from the active C++ compiler and include it in rpath.
    execute_process(
        COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libstdc++.so.6
        OUTPUT_VARIABLE LIBSTDCPP_PATH
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    if (LIBSTDCPP_PATH AND NOT LIBSTDCPP_PATH STREQUAL "libstdc++.so.6")
        get_filename_component(LIBSTDCPP_DIR "${LIBSTDCPP_PATH}" DIRECTORY)
        if (LIBSTDCPP_DIR)
            list(APPEND _lazyllm_cpp_rpath "${LIBSTDCPP_DIR}")
            set(LAZYLLM_LIBSTDCPP_DIR "${LIBSTDCPP_DIR}"
                CACHE INTERNAL "libstdc++ directory for C++ tests" FORCE)
        endif ()
    endif ()
endif ()

if (_lazyllm_cpp_rpath)
    set_target_properties(${INTERFACE_TARGET_NAME} PROPERTIES
        BUILD_RPATH "${_lazyllm_cpp_rpath}"
        INSTALL_RPATH "${_lazyllm_cpp_rpath}"
    )
endif ()

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    # SHOW_SYMBOL
    set_target_properties(${INTERFACE_TARGET_NAME} PROPERTIES CXX_VISIBILITY_PRESET default)
    set_target_properties(${INTERFACE_TARGET_NAME} PROPERTIES CUDA_VISIBILITY_PRESET default)
endif()

# Install
install(TARGETS ${INTERFACE_TARGET_NAME}
    ARCHIVE DESTINATION lazyllm COMPONENT lazyllm_cpp
    LIBRARY DESTINATION lazyllm COMPONENT lazyllm_cpp
    RUNTIME DESTINATION lazyllm COMPONENT lazyllm_cpp
)
install(TARGETS xxhash tiktoken utf8proc
    ARCHIVE DESTINATION lazyllm/cpp_lib COMPONENT lazyllm_cpp
    LIBRARY DESTINATION lazyllm/cpp_lib COMPONENT lazyllm_cpp
    RUNTIME DESTINATION lazyllm/cpp_lib COMPONENT lazyllm_cpp
)
install(DIRECTORY ${CMAKE_BINARY_DIR}/tokenizers DESTINATION lazyllm COMPONENT lazyllm_cpp)


# TESTS
option(BUILD_TESTS "Build C++ tests" OFF)
if (BUILD_TESTS)
    include(cmake/tests.cmake)
endif ()
