cmake_minimum_required(VERSION 3.19)

project(qEmby VERSION 0.0.6 LANGUAGES CXX)

if(APPLE)
    set(CMAKE_OSX_DEPLOYMENT_TARGET 13.0)
endif()

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(GNUInstallDirs)
include(QEmbyPlatformDeps)

set(QEMBY_RUNTIME_LIB_SUBDIR "qemby" CACHE STRING
    "Subdirectory under the install libdir for bundled private runtime libraries")

option(QEMBY_USE_SYSTEM_DEPS
    "Prefer system packages for third-party dependencies" ON)
option(QEMBY_FETCHCONTENT_FALLBACK
    "Allow FetchContent fallback when a system dependency is missing" ON)
option(QEMBY_NATIVE_OPTIMIZE
    "Build qEmby targets with -march=native -mtune=native in optimized configs" OFF)
option(QEMBY_ENABLE_IPO
    "Enable interprocedural optimization/LTO for qEmby targets" OFF)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")


set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

if(MSVC)
    add_compile_options(/MP)
endif()

if(NOT QEMBY_USE_SYSTEM_DEPS AND NOT QEMBY_FETCHCONTENT_FALLBACK)
    message(FATAL_ERROR
        "At least one dependency source must be enabled: "
        "QEMBY_USE_SYSTEM_DEPS or QEMBY_FETCHCONTENT_FALLBACK")
endif()

add_library(qemby_build_options INTERFACE)

if(QEMBY_NATIVE_OPTIMIZE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    target_compile_options(qemby_build_options INTERFACE
        "$<$<CONFIG:Release>:-march=native>"
        "$<$<CONFIG:Release>:-mtune=native>"
        "$<$<CONFIG:RelWithDebInfo>:-march=native>"
        "$<$<CONFIG:RelWithDebInfo>:-mtune=native>"
    )
endif()

set(_qemby_ipo_supported FALSE)
if(QEMBY_ENABLE_IPO)
    include(CheckIPOSupported)
    check_ipo_supported(RESULT _qemby_ipo_supported
        OUTPUT _qemby_ipo_error
        LANGUAGES CXX)
    if(NOT _qemby_ipo_supported)
        message(WARNING
            "IPO/LTO was requested but is not supported: ${_qemby_ipo_error}")
    endif()
endif()

function(qemby_apply_build_options target)
    if(TARGET qemby_build_options)
        target_link_libraries("${target}" PRIVATE qemby_build_options)
    endif()

    if(QEMBY_ENABLE_IPO AND _qemby_ipo_supported)
        set_target_properties("${target}" PROPERTIES
            INTERPROCEDURAL_OPTIMIZATION TRUE)
    endif()
endfunction()

# ==========================================
# 允许通过缓存变量或环境变量提供 Qt 安装路径
# Windows 未显式指定时仍会尝试兼容现有本地默认路径
# ==========================================
set(QEMBY_QT_ROOT "" CACHE PATH
    "Optional Qt installation root or CMake prefix path")
qemby_append_default_qt_prefixes()

# 寻找 Qt6 (不再允许降级)
find_package(Qt6 REQUIRED COMPONENTS Widgets Core Network Concurrent OpenGLWidgets LinguistTools WebSockets Svg)
if(UNIX AND NOT APPLE)
    find_package(Qt6 QUIET COMPONENTS DBus)
endif()

# ==========================================
# 【关键修复】: 为第三方子模块 (qwindowkit/qmsetup) 注入所需的 Qt 版本环境变量
# ==========================================
set(QT_VERSION_MAJOR ${Qt6_VERSION_MAJOR})
set(QT_VERSION_MINOR ${Qt6_VERSION_MINOR})
set(QT_VERSION ${Qt6_VERSION})

# ==========================================
# 引入 QCoro (C++20 Coroutines for Qt)
# ==========================================
include(FetchContent)

function(qemby_ensure_noop_install_script build_dir)
    if(NOT build_dir)
        return()
    endif()

    file(MAKE_DIRECTORY "${build_dir}")
    file(WRITE "${build_dir}/cmake_install.cmake"
        "# qEmby placeholder install script for third-party dependency\n")
endfunction()

set(_qemby_saved_skip_install_rules ${CMAKE_SKIP_INSTALL_RULES})
set(CMAKE_SKIP_INSTALL_RULES ON)
if(QEMBY_USE_SYSTEM_DEPS)
    find_package(QCoro6 CONFIG QUIET COMPONENTS Core Network)
endif()

if(QCoro6_FOUND)
    message(STATUS "Using system QCoro6")
else()
    if(NOT QEMBY_FETCHCONTENT_FALLBACK)
        message(FATAL_ERROR
            "QCoro6 was not found. Install the system package or enable "
            "QEMBY_FETCHCONTENT_FALLBACK.")
    endif()

    message(STATUS "System QCoro6 not found; fetching bundled QCoro")
    FetchContent_Declare(
        QCoro
        GIT_REPOSITORY https://github.com/danvratil/qcoro.git
        GIT_TAG v0.10.0
    )

    # 强制 QCoro 使用 Qt6 进行编译，并关闭不必要的测试与示例构建以加快编译速度
    set(QCORO_QT_MAJOR_VERSION 6 CACHE STRING "Use Qt6 for QCoro" FORCE)
    set(QCORO_BUILD_EXAMPLES OFF CACHE BOOL "Disable QCoro examples" FORCE)
    set(QCORO_WITH_QML OFF CACHE BOOL "Disable QCoro QML module" FORCE)
    set(QCORO_WITH_QTQUICK OFF CACHE BOOL "Disable QCoro QtQuick module" FORCE)
    set(BUILD_TESTING OFF CACHE BOOL "Disable QCoro tests" FORCE)

    FetchContent_MakeAvailable(QCoro)
    FetchContent_GetProperties(QCoro)
    qemby_ensure_noop_install_script("${qcoro_BINARY_DIR}")
endif()

# ==========================================
# 引入 spdlog (高性能线程安全日志库, MIT License)
# ==========================================
if(QEMBY_USE_SYSTEM_DEPS)
    find_package(spdlog CONFIG QUIET)
endif()

if(spdlog_FOUND)
    message(STATUS "Using system spdlog")
else()
    if(NOT QEMBY_FETCHCONTENT_FALLBACK)
        message(FATAL_ERROR
            "spdlog was not found. Install the system package or enable "
            "QEMBY_FETCHCONTENT_FALLBACK.")
    endif()

    message(STATUS "System spdlog not found; fetching bundled spdlog")
    FetchContent_Declare(
        spdlog
        GIT_REPOSITORY https://github.com/gabime/spdlog.git
        GIT_TAG v1.15.3
    )
    set(SPDLOG_BUILD_EXAMPLE OFF CACHE BOOL "Disable spdlog examples" FORCE)
    set(SPDLOG_BUILD_TESTS OFF CACHE BOOL "Disable spdlog tests" FORCE)
    set(SPDLOG_INSTALL OFF CACHE BOOL "Disable spdlog install rules" FORCE)
    FetchContent_MakeAvailable(spdlog)
    FetchContent_GetProperties(spdlog)
    qemby_ensure_noop_install_script("${spdlog_BINARY_DIR}")
endif()
set(CMAKE_SKIP_INSTALL_RULES ${_qemby_saved_skip_install_rules})
unset(_qemby_saved_skip_install_rules)
# ==========================================

set(QWINDOWKIT_INSTALL OFF CACHE BOOL
    "Disable QWindowKit development install rules inside qEmby" FORCE)

add_subdirectory(libs/qwindowkit)
add_subdirectory(libs/qwindowkit/examples/shared)
add_subdirectory(src/qEmbyCore)
add_subdirectory(src/qEmbyApp)

include(QEmbyPackaging)
