add_library(nvml_gpu SHARED
    nvml_gpu.cpp
)

target_include_directories(nvml_gpu PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
)

# Find NVML library
find_library(NVML_LIBRARY nvidia-ml 
    PATHS 
        /usr/lib/x86_64-linux-gnu
        /usr/local/cuda/lib64
        /usr/lib64
        /usr/lib
        /opt/cuda/lib64
    DOC "NVIDIA Management Library"
)

# Check for NVML header
find_path(NVML_INCLUDE_DIR nvml.h
    PATHS
        /usr/include
        /usr/local/cuda/include
        /opt/cuda/include
)

if(NVML_LIBRARY AND NVML_INCLUDE_DIR)
    target_link_libraries(nvml_gpu PRIVATE ${NVML_LIBRARY})
    target_include_directories(nvml_gpu PRIVATE ${NVML_INCLUDE_DIR})
    message(STATUS "Found NVML: ${NVML_LIBRARY}")
    message(STATUS "NVML include: ${NVML_INCLUDE_DIR}")
else()
    if(CMAKE_SYSTEM_NAME MATCHES "Linux")
        message(WARNING "NVML not found. NVIDIA GPU monitoring will be disabled.")
    else()
        message(STATUS "NVML not available on ${CMAKE_SYSTEM_NAME}. Using platform-specific GPU monitoring.")
    endif()
    # Create stub library
    target_compile_definitions(nvml_gpu PRIVATE NVML_STUB)
endif()

set_target_properties(nvml_gpu PROPERTIES
    VERSION ${PROJECT_VERSION}
    SOVERSION ${PROJECT_VERSION_MAJOR}
    POSITION_INDEPENDENT_CODE ON
    C_STANDARD 11
    CXX_STANDARD 17
)
