# MIT License
#
# Copyright (c) 2026 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

#
cmake_minimum_required(VERSION 3.21.0 FATAL_ERROR)

project(
    rocprofiler-sdk-tests-openmp-init-deadlock
    LANGUAGES CXX
    VERSION 0.0.0)

find_package(rocprofiler-sdk REQUIRED)

set(IS_DISABLED OFF)
if(NOT TARGET openmp-init-deadlock)
    set(IS_DISABLED ON)
endif()
if(NOT OMP_TARGET_COMPILER)
    set(IS_DISABLED ON)
endif()

# Regression test verifying that rocprofiler-sdk does not act as an OMPT tool on its own.
#
# The workload links librocprofiler-sdk (via the client library) but the rocprofv3 tool
# library is not loaded. libomp discovers an OMPT tool by searching loaded libraries for
# an ompt_start_tool symbol; because rocprofiler-sdk no longer exports that symbol, libomp
# finds no tool, performs no client discovery, and the linked libraries' DT_INIT
# constructors (which call into libomp) run to completion. This models the case of a plain
# application that merely has rocprofiler-sdk in its address space (for example, importing
# a library that pulls in OpenMP) with no profiler active: it must not deadlock.
#
# The assertion is twofold: the TIMEOUT guards against the deadlock (a hang fails the
# test), and PASS_REGULAR_EXPRESSION ensures the application actually reached main (so an
# unrelated early exit cannot pass silently).
rocprofiler_add_integration_execute_test(
    openmp-init-deadlock
    TARGET openmp-init-deadlock
    TIMEOUT 60
    LABELS "integration-tests;openmp-init-deadlock"
    DISABLED "${IS_DISABLED}"
    ENVIRONMENT
        "OMP_TOOL=enabled"
        "OMP_NUM_THREADS=2"
        "LD_LIBRARY_PATH=$<TARGET_FILE_DIR:rocprofiler-sdk::rocprofiler-sdk-shared-library>:$ENV{LD_LIBRARY_PATH}"
    PASS_REGULAR_EXPRESSION "reached main: no OMPT-init deadlock")
