cmake_minimum_required(VERSION 3.18)
project(gsm_cuda LANGUAGES CXX)

# Try to find CUDA
include(CheckLanguage)
check_language(CUDA)

# Find pybind11
find_package(pybind11 REQUIRED)

if(CMAKE_CUDA_COMPILER)
    enable_language(CUDA)
    find_package(CUDAToolkit REQUIRED)

    # Build with CUDA support
    pybind11_add_module(gsm_cuda_core
        gsm_binding.cpp
        gsm_kernel.cu
    )
    target_compile_definitions(gsm_cuda_core PRIVATE GSM_CUDA_ENABLED)
    target_compile_features(gsm_cuda_core PRIVATE cxx_std_14)
    set_target_properties(gsm_cuda_core PROPERTIES CUDA_STANDARD 14)
    target_link_libraries(gsm_cuda_core PRIVATE CUDA::cudart)

    message(STATUS "Building GSM with CUDA support")
else()
    # Build CPU-only version
    # Compile the .cu file as C++ (it has proper #ifdef guards)
    pybind11_add_module(gsm_cuda_core
        gsm_binding.cpp
        gsm_kernel_cpu.cpp
    )
    target_compile_features(gsm_cuda_core PRIVATE cxx_std_14)

    message(STATUS "Building GSM without CUDA (CPU-only fallback)")
endif()

install(TARGETS gsm_cuda_core DESTINATION sparse_approx_gsm/cuda)
