cmake_minimum_required(VERSION 3.15...3.27)
project(multigedi VERSION 0.1.0 LANGUAGES CXX)

# C++ standard
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Position independent code (required for shared libraries)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Find Python
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)

# Find pybind11
find_package(pybind11 CONFIG REQUIRED)

# Find Eigen3
find_package(Eigen3 3.3 REQUIRED NO_MODULE)

# Find OpenMP (optional)
find_package(OpenMP)

# Add the C++ source directory
add_subdirectory(src/_multigedi_cpp)

# =============================================================================
# Optional CUDA backend (libmultigedi_gpu.so)
# =============================================================================
#
# OFF by default so the standard `pip install .` produces a CPU-only wheel
# even on machines without an NVCC toolchain.  Turn on at build time with
#
#     pip install --config-settings=cmake.define.MULTIGEDI_BUILD_GPU=ON .
#
# When ON, the CUDA shared library is bundled inside the wheel at
# multigedi/_gpu/libmultigedi_gpu.so with INSTALL_RPATH "$ORIGIN" so the
# Python ctypes wrapper finds it without an env-var override.
option(MULTIGEDI_BUILD_GPU "Build the CUDA backend (libmultigedi_gpu.so)" OFF)

if(MULTIGEDI_BUILD_GPU)
    message(STATUS "MULTIGEDI_BUILD_GPU=ON: building CUDA backend")
    add_subdirectory(src/_multigedi_gpu)
    # Phase 3 zero-copy pybind11 bindings (replaces ctypes + on-disk
    # serializer in v1.1).  Lives alongside the C-ABI shared library;
    # both ship inside the same wheel during the v1.1 transition.
    add_subdirectory(src/_multigedi_gpu_py)
else()
    message(STATUS "MULTIGEDI_BUILD_GPU=OFF: CPU-only build (pass "
        "-DMULTIGEDI_BUILD_GPU=ON to enable the CUDA backend)")
endif()
