# DetectOS.cmake

function(add_os_definitions target)
    # 初始化所有平台宏为 0
    set(PLATFORMS MIRAGE_PLATFORM_WINDOWS MIRAGE_PLATFORM_MACOS MIRAGE_PLATFORM_LINUX MIRAGE_PLATFORM_FREEBSD MIRAGE_PLATFORM_IOS MIRAGE_PLATFORM_ANDROID)

    # 检测操作系统并设置相应的宏为 1
    if(WIN32)
        target_compile_definitions(${target} PUBLIC MIRAGE_PLATFORM_WINDOWS=1)
        message(STATUS "检测到 Windows 操作系统")
        list(REMOVE_ITEM PLATFORMS MIRAGE_PLATFORM_WINDOWS)
    elseif(APPLE AND NOT IOS)
        target_compile_definitions(${target} PUBLIC MIRAGE_PLATFORM_MACOS=1)
        message(STATUS "检测到 macOS 操作系统")
        list(REMOVE_ITEM PLATFORMS MIRAGE_PLATFORM_MACOS)
    elseif(UNIX)
        if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
            target_compile_definitions(${target} PUBLIC MIRAGE_PLATFORM_LINUX=1)
            message(STATUS "检测到 Linux 操作系统")
            list(REMOVE_ITEM PLATFORMS MIRAGE_PLATFORM_LINUX)
        elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
            target_compile_definitions(${target} PUBLIC MIRAGE_PLATFORM_FREEBSD=1)
            message(STATUS "检测到 FreeBSD 操作系统")
            list(REMOVE_ITEM PLATFORMS MIRAGE_PLATFORM_FREEBSD)
        else()
            message(WARNING "检测到未知的 类Unix 操作系统")
        endif()
    elseif(ANDROID)
        target_compile_definitions(${target} PUBLIC MIRAGE_PLATFORM_ANDROID=1)
        message(STATUS "检测到 Android 操作系统")
        list(REMOVE_ITEM PLATFORMS MIRAGE_PLATFORM_ANDROID)
    elseif(IOS)
        target_compile_definitions(${target} PUBLIC MIRAGE_PLATFORM_IOS=1)
        message(STATUS "检测到 iOS 操作系统")
        list(REMOVE_ITEM PLATFORMS MIRAGE_PLATFORM_IOS)
    else()
        message(WARNING "检测到未知的操作系统")
    endif()

    foreach(PLATFORM ${PLATFORMS})
        target_compile_definitions(${target} PUBLIC ${PLATFORM}=0)
    endforeach()

    # 检测并设置架构宏
    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
        target_compile_definitions(${target} PUBLIC MIRAGE_PLATFORM_ARCH_64BIT=1 MIRAGE_PLATFORM_ARCH_32BIT=0)
        message(STATUS "检测到 64-bit 架构")
    else()
        target_compile_definitions(${target} PUBLIC MIRAGE_PLATFORM_ARCH_64BIT=0 MIRAGE_PLATFORM_ARCH_32BIT=1)
        message(STATUS "检测到 32-bit 架构")
    endif()

    # 设置通用的 UNIX 宏
    if(UNIX)
        target_compile_definitions(${target} PUBLIC MIRAGE_PLATFORM_UNIX=1)
    else()
        target_compile_definitions(${target} PUBLIC MIRAGE_PLATFORM_UNIX=0)
    endif()

    # 设置通用的 POSIX 宏
    if(UNIX OR APPLE)
        target_compile_definitions(${target} PUBLIC MIRAGE_PLATFORM_POSIX=1)
    else()
        target_compile_definitions(${target} PUBLIC MIRAGE_PLATFORM_POSIX=0)
    endif()

    # 设置IS_MOBILE宏
    if(ANDROID OR IOS)
        target_compile_definitions(${target} PUBLIC MIRAGE_PLATFORM_IS_MOBILE=1)
    else()
        target_compile_definitions(${target} PUBLIC MIRAGE_PLATFORM_IS_MOBILE=0)
    endif()
endfunction()
