IF (NOT HAVE_TSAN OR NOT ENABLE_TSAN)
    RETURN()
ENDIF ()

IF(TEST_PREBUILDS)
    MUST_TEST_PREBUILD_CONFIGURATION(
        prebuild-MpiTsan
        "--must:tsan \
         ${MPIEXEC_NUMPROC_FLAG} 2"
        )
    MUST_TEST_PREBUILD_CONFIGURATION(
        prebuild-MpiTsan-distributed
        "--must:tsan --must:distributed \
         ${MPIEXEC_NUMPROC_FLAG} 2"
        )
ENDIF()

MACRO(addMpiTSanTest
    testName    # name used for this test (must be unique)
    sourceList   # sources needed for the test executable
    numProcs   # number of application processes, all other layers have 1 process and no modules
    )
    addHybridTest(${testName} ${sourceList} ${numProcs}
        "data race"
        ""
        "-fsanitize=thread"
        ${linkerFlags}
        DEPENDS onReportLoader
    )
ENDMACRO(addMpiTSanTest)

add_compile_options("-g" "-fsanitize=thread")
set (linkerFlags "-Wl,--whole-archive ${CMAKE_BINARY_DIR}/lib/libonReportLoader.a -Wl,--no-whole-archive -ldl -fsanitize=thread")

addMpiTSanTest("MpiTSanBcast" "bcast.c" 2)
addMpiTSanTest("MpiTSanIbcast" "ibcast.c" 2)
addMpiTSanTest("MpiTSanRecv" "recv.c" 2)
addMpiTSanTest("MpiTSanIrecv" "irecv.c" 2)
addMpiTSanTest("MpiTSanScatter" "scatter.c" 2)
addMpiTSanTest("MpiTSanIscatter" "iscatter.c" 2)
addMpiTSanTest("MpiTSanIsendSingle" "isend_single.c" 2)
addMpiTSanTest("MpiTSanIsendNo" "isend_no.c" 2)
addMpiTSanTest("MpiTSanIsendIrecv" "isend_irecv.c" 2)
addMpiTSanTest("MpiTSanIsend" "isend.c" 2)
addMpiTSanTest("MpiTSanSend" "send.c" 2)


FILE(GLOB SUBDIRECTORIES "*")
FOREACH (DIR ${SUBDIRECTORIES})
    IF (EXISTS "${DIR}/CMakeLists.txt")
        ADD_SUBDIRECTORY(${DIR})
    ENDIF ()
ENDFOREACH ()

