cmake_minimum_required(VERSION 3.12)
# OBJC/OBJCXX only exist on Apple (Metal path) — enabling them unconditionally
# breaks CMake configure on Linux/Windows runners.
project(axim_gfx LANGUAGES CXX)
if(APPLE)
    enable_language(OBJC OBJCXX)
endif()

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# ─────────────────────────────────────────────────────────────
# Vendor-neutral graphics backend selection (no CUDA anywhere):
#   macOS  → Metal offscreen renderer (libaxim_gfx.dylib)
#   Linux  → Vulkan offscreen renderer (libaxim_gfx.so)
#   Windows→ Vulkan offscreen renderer (axim_gfx.dll)
#   Windows→ DX12 offscreen renderer (optional; AXIM_GFX_BACKEND=dx12)
#
# AXIM_FORCE_VULKAN=ON builds the Vulkan path on macOS too (MoltenVK).
# AXIM_GFX_BACKEND=dx12 selects the DirectX 12 backend on Windows. It
# consumes the SAME SPIR-V shaders as Vulkan, cross-compiled offline to
# DXIL by shaders/build_shaders.sh (spirv-cross + dxc).
# ─────────────────────────────────────────────────────────────

option(AXIM_FORCE_VULKAN "Build the Vulkan backend even on macOS (MoltenVK)" OFF)
set(AXIM_GFX_BACKEND "" CACHE STRING "Graphics backend override: dx12 (Windows only)")

if(APPLE AND NOT AXIM_FORCE_VULKAN)
    add_library(axim_gfx SHARED src/axim_gfx_metal.mm)
    target_compile_options(axim_gfx PRIVATE -O3 -fobjc-arc)
    target_include_directories(axim_gfx PUBLIC include)
    target_link_libraries(axim_gfx PRIVATE
        "-framework Metal"
        "-framework Foundation"
    )
    set_target_properties(axim_gfx PROPERTIES
        OUTPUT_NAME "axim_gfx"
        PREFIX "lib"
    )
    message(STATUS "AximOS Graphics backend: Metal (Apple silicon)")
elseif(WIN32 AND AXIM_GFX_BACKEND STREQUAL "dx12")
    # DirectX 12 — the optional Windows path. Consumes the same SPIR-V
    # shaders as Vulkan, cross-compiled offline to DXIL (.cso) by
    # shaders/build_shaders.sh. Links only Windows SDK libraries.
    add_library(axim_gfx SHARED src/axim_gfx_dx12.cpp)
    target_compile_options(axim_gfx PRIVATE -O3)
    target_compile_definitions(axim_gfx PRIVATE _CRT_SECURE_NO_WARNINGS)
    target_include_directories(axim_gfx PUBLIC include)
    target_link_libraries(axim_gfx PRIVATE d3d12 dxgi dxguid)
    set_target_properties(axim_gfx PROPERTIES
        OUTPUT_NAME "axim_gfx"
        PREFIX ""
    )
    message(STATUS "AximOS Graphics backend: DirectX 12 (SPIR-V via DXIL)")
else()
    find_package(Vulkan QUIET)
    if(Vulkan_FOUND)
        add_library(axim_gfx SHARED src/axim_gfx_vulkan.cpp)
        target_compile_options(axim_gfx PRIVATE -O3)
        target_include_directories(axim_gfx PUBLIC include ${Vulkan_INCLUDE_DIRS})
        target_link_libraries(axim_gfx PRIVATE ${Vulkan_LIBRARIES})
        set_target_properties(axim_gfx PROPERTIES
            OUTPUT_NAME "axim_gfx"
            PREFIX "lib"
        )
        message(STATUS "AximOS Graphics backend: Vulkan (${Vulkan_VERSION})")
    else()
        message(WARNING
            "Vulkan SDK not found — libaxim_gfx will NOT be built. "
            "Graphics tests will be skipped on this host. "
            "Install the Vulkan SDK (https://vulkan.lunarg.com) and re-run CMake.")
    endif()
endif()
