#
#
#
cmake_minimum_required(VERSION 3.21.0 FATAL_ERROR)

project(
    rocprofiler-sdk-tests-tools
    LANGUAGES C CXX
    VERSION 0.0.0)

find_package(rocprofiler-sdk REQUIRED)

# tool library supporting JSON and perfetto output
add_library(rocprofiler-sdk-json-tool SHARED)
target_sources(rocprofiler-sdk-json-tool PRIVATE json-tool.cpp)
target_link_libraries(
    rocprofiler-sdk-json-tool
    PRIVATE rocprofiler-sdk::rocprofiler-sdk rocprofiler-sdk::rocprofiler-sdk-cereal
            rocprofiler-sdk::tests-build-flags rocprofiler-sdk::tests-common-library
            rocprofiler-sdk::tests-perfetto)
set_target_properties(
    rocprofiler-sdk-json-tool
    PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/rocprofiler-sdk"
               SOVERSION ${PROJECT_VERSION_MINOR}
               VERSION
               ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}
               INSTALL_RPATH "\$ORIGIN:\$ORIGIN/..")

# tool library supporting JSON and perfetto output with late/anytime initialization
add_library(rocprofiler-sdk-json-tool-late SHARED)
target_sources(rocprofiler-sdk-json-tool-late PRIVATE json-tool.cpp)
# Rename this library's rocprofiler_configure so it does not collide with the early
# (LD_PRELOAD'd) json-tool's exported symbol of the same name. Both libraries are built
# from the same source, and when both are loaded in one process with RTLD_GLOBAL (an early
# preloaded tool plus this late dlopen'd tool), the early definition would interpose the
# late tool's internal call -- causing json_tool_force_configure() to register the EARLY
# tool's configure (rejected as already-invoked) instead of its own. The late tool is
# loaded by dlopen + json_tool_force_configure(), never symbol-scanned, so it does not
# need to export rocprofiler_configure at all. Whole-token macro replacement leaves
# rocprofiler_configure_* API calls untouched.
target_compile_definitions(rocprofiler-sdk-json-tool-late
                           PRIVATE rocprofiler_configure=rocprofiler_configure_late)
target_link_libraries(
    rocprofiler-sdk-json-tool-late
    PRIVATE rocprofiler-sdk::rocprofiler-sdk rocprofiler-sdk::rocprofiler-sdk-cereal
            rocprofiler-sdk::tests-build-flags rocprofiler-sdk::tests-common-library
            rocprofiler-sdk::tests-perfetto)
set_target_properties(
    rocprofiler-sdk-json-tool-late
    PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/rocprofiler-sdk"
               SOVERSION ${PROJECT_VERSION_MINOR}
               VERSION
               ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}
               INSTALL_RPATH "\$ORIGIN:\$ORIGIN/..")

# A SECOND late-init json-tool library. Each json-tool .so holds all of its state in
# namespace globals, so two simultaneously-active instances require two distinct .so
# files. This is the companion to rocprofiler-sdk-json-tool-late used by variations that
# register two tools late in the same process. Its rocprofiler_configure is renamed to a
# third distinct symbol so neither the early tool nor the first late tool interposes it.
add_library(rocprofiler-sdk-json-tool-late2 SHARED)
target_sources(rocprofiler-sdk-json-tool-late2 PRIVATE json-tool.cpp)
target_compile_definitions(rocprofiler-sdk-json-tool-late2
                           PRIVATE rocprofiler_configure=rocprofiler_configure_late2)
target_link_libraries(
    rocprofiler-sdk-json-tool-late2
    PRIVATE rocprofiler-sdk::rocprofiler-sdk rocprofiler-sdk::rocprofiler-sdk-cereal
            rocprofiler-sdk::tests-build-flags rocprofiler-sdk::tests-common-library
            rocprofiler-sdk::tests-perfetto)
set_target_properties(
    rocprofiler-sdk-json-tool-late2
    PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/rocprofiler-sdk"
               SOVERSION ${PROJECT_VERSION_MINOR}
               VERSION
               ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}
               INSTALL_RPATH "\$ORIGIN:\$ORIGIN/..")

# tool library which just checks that tools can be compiled with C language
add_library(rocprofiler-sdk-c-tool SHARED)
target_sources(rocprofiler-sdk-c-tool PRIVATE c-tool.c)
target_link_libraries(rocprofiler-sdk-c-tool PRIVATE rocprofiler-sdk::rocprofiler-sdk
                                                     rocprofiler-sdk::tests-build-flags)
# Suppress -Wcpp warning from rccl/rccl.h which emits a #warning when compiled with C
# compiler This warning is treated as an error with -Werror but is harmless for this C
# compatibility test
target_compile_options(rocprofiler-sdk-c-tool PRIVATE -Wno-cpp)
set_target_properties(
    rocprofiler-sdk-c-tool
    PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/rocprofiler-sdk"
               SOVERSION ${PROJECT_VERSION_MINOR}
               VERSION
               ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}
               INSTALL_RPATH "\$ORIGIN:\$ORIGIN/..")

if(NOT CMAKE_STRIP)
    find_program(CMAKE_STRIP NAMES strip REQUIRED)
endif()

add_custom_command(
    TARGET rocprofiler-sdk-c-tool
    POST_BUILD
    COMMAND ${CMAKE_STRIP} $<TARGET_FILE:rocprofiler-sdk-c-tool>
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
    COMMENT "Stripping rocprofiler-sdk-c-tool...")

# thread-trace tool libraries
set(THREAD_TRACE_TOOL_LINK_LIBRARIES
    rocprofiler-sdk::rocprofiler-sdk rocprofiler-sdk::tests-build-flags
    rocprofiler-sdk::tests-common-library)

# single dispatch thread trace tool
add_library(thread-trace-single-tool SHARED)
target_sources(thread-trace-single-tool PRIVATE thread-trace-single-tool.cpp
                                                thread-trace-callbacks.cpp)
target_link_libraries(thread-trace-single-tool
                      PRIVATE ${THREAD_TRACE_TOOL_LINK_LIBRARIES})
set_target_properties(
    thread-trace-single-tool
    PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/rocprofiler-sdk"
               INSTALL_RPATH "\$ORIGIN:\$ORIGIN/..")

# multi dispatch thread trace tool
add_library(thread-trace-multi-tool SHARED)
target_sources(thread-trace-multi-tool PRIVATE thread-trace-multi-tool.cpp
                                               thread-trace-callbacks.cpp)
target_link_libraries(thread-trace-multi-tool PRIVATE ${THREAD_TRACE_TOOL_LINK_LIBRARIES})
set_target_properties(
    thread-trace-multi-tool
    PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/rocprofiler-sdk"
               INSTALL_RPATH "\$ORIGIN:\$ORIGIN/..")

# agent (device) thread trace tool
add_library(thread-trace-agent-tool SHARED)
target_sources(thread-trace-agent-tool PRIVATE thread-trace-agent-tool.cpp
                                               thread-trace-callbacks.cpp)
target_link_libraries(thread-trace-agent-tool PRIVATE ${THREAD_TRACE_TOOL_LINK_LIBRARIES})
set_target_properties(
    thread-trace-agent-tool
    PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/rocprofiler-sdk"
               INSTALL_RPATH "\$ORIGIN:\$ORIGIN/..")

# triple buffer thread trace tool
add_library(thread-trace-triple-buffer-tool SHARED)
target_sources(thread-trace-triple-buffer-tool
               PRIVATE thread-trace-triple-buffer-tool.cpp)
target_link_libraries(thread-trace-triple-buffer-tool
                      PRIVATE ${THREAD_TRACE_TOOL_LINK_LIBRARIES})
set_target_properties(
    thread-trace-triple-buffer-tool
    PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/rocprofiler-sdk"
               INSTALL_RPATH "\$ORIGIN:\$ORIGIN/..")

# tool library supporting late initialization in the application
add_library(rocprofiler-sdk-late-start-tool SHARED)
target_sources(rocprofiler-sdk-late-start-tool PRIVATE late-start-tool.cpp)
target_link_libraries(
    rocprofiler-sdk-late-start-tool
    PRIVATE rocprofiler-sdk::rocprofiler-sdk rocprofiler-sdk::rocprofiler-sdk-cereal
            rocprofiler-sdk::tests-build-flags rocprofiler-sdk::tests-common-library)
set_target_properties(
    rocprofiler-sdk-late-start-tool
    PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/rocprofiler-sdk"
               SOVERSION ${PROJECT_VERSION_MINOR}
               VERSION
               ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}
               INSTALL_RPATH "\$ORIGIN:\$ORIGIN/..")
