# NCCL Inspector profiler plugin (librccl-profiler-inspector.so)
#
# Can be built two ways:
#   1. Standalone:   cmake -S . -B build && cmake --build build
#   2. As part of the main RCCL build via add_subdirectory(plugins/profiler/inspector)
#
# The legacy Makefile in this directory remains for CUDA-only standalone builds.
# This target additionally supports ROCm: inspector_gpu_compat.h maps the CUDA
# spellings used by the sources onto their HIP equivalents.

# Detect standalone vs. in-tree configuration.
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
    cmake_minimum_required(VERSION 3.16)
    project(rccl-profiler-inspector LANGUAGES CXX)
    set(INSPECTOR_STANDALONE ON)
else()
    set(INSPECTOR_STANDALONE OFF)
endif()

option(NCCL_INSPECTOR_ENABLE_WARN "Emit inspector messages at WARN level (default: INFO)" OFF)

# hip::host supplies the HIP headers, libamdhip64 and __HIP_PLATFORM_AMD__.
# The main RCCL build already ran find_package(hip); resolve it for standalone.
if(NOT TARGET hip::host)
    find_package(hip QUIET)
endif()

# version.cc is generated rather than checked in, matching the Makefile.
find_package(Git QUIET)
if(GIT_FOUND)
    execute_process(
        COMMAND ${GIT_EXECUTABLE} describe --always --dirty
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        OUTPUT_VARIABLE INSPECTOR_GIT_VERSION
        OUTPUT_STRIP_TRAILING_WHITESPACE
        ERROR_QUIET
    )
endif()
if(NOT INSPECTOR_GIT_VERSION)
    set(INSPECTOR_GIT_VERSION "unknown")
endif()

set(INSPECTOR_VERSION_SRC "${CMAKE_CURRENT_BINARY_DIR}/version.cc")
file(WRITE "${INSPECTOR_VERSION_SRC}"
"// Auto-generated by CMake; do not edit.
extern \"C\" const char* get_git_version_info() {
  return \"${INSPECTOR_GIT_VERSION}\";
}
")

add_library(rccl-profiler-inspector SHARED
    ${CMAKE_CURRENT_SOURCE_DIR}/inspector.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/inspector_json.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/inspector_prom.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/inspector_plugin.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/inspector_cudawrap.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/inspector_ring.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/inspector_event_pool.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/json.cc
    ${INSPECTOR_VERSION_SRC}
)

target_include_directories(rccl-profiler-inspector PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/nccl
)

target_compile_definitions(rccl-profiler-inspector PRIVATE
    NCCL_INSPECTOR_ENABLE_WARN=$<BOOL:${NCCL_INSPECTOR_ENABLE_WARN}>
)

target_compile_features(rccl-profiler-inspector PRIVATE cxx_std_17)

target_compile_options(rccl-profiler-inspector PRIVATE -Wall -Wextra)

if(TARGET hip::host)
    target_link_libraries(rccl-profiler-inspector PRIVATE hip::host)
endif()
target_link_libraries(rccl-profiler-inspector PRIVATE ${CMAKE_DL_LIBS} pthread)

# The loader resolves ncclProfiler_v5 with dlsym(), so it must stay visible even
# when the surrounding build defaults to hidden visibility.
set_target_properties(rccl-profiler-inspector PROPERTIES
    OUTPUT_NAME "rccl-profiler-inspector"
    PREFIX "lib"
    POSITION_INDEPENDENT_CODE ON
    CXX_VISIBILITY_PRESET default
    VISIBILITY_INLINES_HIDDEN OFF
)

if(NOT INSPECTOR_STANDALONE)
    set_target_properties(rccl-profiler-inspector PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
    )
endif()
