cmake_minimum_required(VERSION 4.0)
project(TestFindOpenMP NONE)
include(CTest)

macro(source_code_mapper_helper LANG_NAME SRC_FILE_NAME)
  if("${LANG_NAME}" STREQUAL "C")
    set(OpenMPTEST_SOURCE_FILE "${SRC_FILE_NAME}.c")
  elseif("${LANG_NAME}" STREQUAL "CXX")
    configure_file("${SRC_FILE_NAME}.c" "${SRC_FILE_NAME}.cxx" COPYONLY)
    set(OpenMPTEST_SOURCE_FILE "${SRC_FILE_NAME}.cxx")
  elseif("${LANG_NAME}" STREQUAL "CUDA")
    configure_file("${SRC_FILE_NAME}.c" "${SRC_FILE_NAME}.cu" COPYONLY)
    set(OpenMPTEST_SOURCE_FILE "${SRC_FILE_NAME}.cu")
  elseif("${LANG_NAME}" STREQUAL "Fortran")
    set(OpenMPTEST_SOURCE_FILE "${CMAKE_CURRENT_BINARY_DIR}/${SRC_FILE_NAME}.f90")
    if(OpenMP_Fortran_HAVE_OMPLIB_MODULE)
      set(OpenMP_Fortran_INCLUDE_LINE "use omp_lib\n      implicit none")
    else()
      set(OpenMP_Fortran_INCLUDE_LINE "implicit none\n      include 'omp_lib.h'")
    endif()
    configure_file("${SRC_FILE_NAME}.f90.in" "${OpenMPTEST_SOURCE_FILE}" @ONLY)
  endif()
endmacro()

foreach(c C CXX CUDA Fortran)
  if("${OpenMP_TEST_${c}}")
    message("Testing ${c}")
    enable_language(${c})
  endif()
endforeach()

if(CMAKE_C_COMPILER_ID STREQUAL "MSVC"
    AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 19.30
    AND NOT CMAKE_C_COMPILER_ARCHITECTURE_ID STREQUAL "ARM64")
  set(test_msvc_runtime 1)
  set(OpenMP_RUNTIME_MSVC "llvm")
endif()

find_package(OpenMP REQUIRED)

if(test_msvc_runtime)
  if(NOT OpenMP_C_FLAGS STREQUAL "-openmp:llvm")
    message(FATAL_ERROR "OpenMP_RUNTIME_MSVC='${OpenMP_RUNTIME_MSVC}' not honored: '${OpenMP_C_FLAGS}'")
  endif()
endif()

# FIXME(#26701) Needed to check target offload conditions
foreach(c C CXX Fortran)
  if(NOT "${OpenMP_TEST_${c}}")
    continue()
  endif()
  if(CMAKE_${c}_COMPILER_ID MATCHES "^(Cray|CrayClang)$")
    get_target_property(link_options OpenMP::OpenMP_${c} INTERFACE_LINK_OPTIONS)
    if(NOT link_options)
      message(FATAL_ERROR "OpenMP_${c} Interface Link Options not set for Cray Compiler")
    endif()
  endif()
endforeach()

foreach(c C CXX CUDA Fortran)
  if(NOT "${OpenMP_TEST_${c}}")
    continue()
  endif()
  source_code_mapper_helper(${c} main)
  add_executable(test_tgt_${c} ${OpenMPTEST_SOURCE_FILE})
  target_link_libraries(test_tgt_${c} PRIVATE OpenMP::OpenMP_${c})
  set_property(TARGET test_tgt_${c} PROPERTY LINKER_LANGUAGE ${c})
  add_test(NAME test_tgt_${c} COMMAND test_tgt_${c})

  add_executable(test_var_${c} ${OpenMPTEST_SOURCE_FILE})
  separate_arguments(_OpenMP_${c}_OPTIONS NATIVE_COMMAND "${OpenMP_${c}_FLAGS}")
  target_compile_options(test_var_${c} PRIVATE "${_OpenMP_${c}_OPTIONS}")
  target_link_libraries(test_var_${c} PRIVATE ${OpenMP_${c}_LIBRARIES})
  target_include_directories(test_var_${c} PRIVATE ${OpenMP_${c}_INCLUDE_DIRS})
  set_property(TARGET test_var_${c} PROPERTY LINKER_LANGUAGE ${c})
  add_test(NAME test_var_${c} COMMAND test_var_${c})

  source_code_mapper_helper(${c} scalprod)
  add_library(scalprod_${c} STATIC ${OpenMPTEST_SOURCE_FILE})
  target_link_libraries(scalprod_${c} PRIVATE OpenMP::OpenMP_${c})
  set_property(TARGET scalprod_${c} PROPERTY LINKER_LANGUAGE ${c})
endforeach()

foreach(c C CXX CUDA Fortran)
  if(NOT "${OpenMP_TEST_${c}}")
    continue()
  endif()
  foreach(d C CXX CUDA Fortran)
    if(NOT "${OpenMP_TEST_${d}}")
      continue()
    endif()
    source_code_mapper_helper(${c} scaltest)
    add_executable(scaltest_${c}_${d} ${OpenMPTEST_SOURCE_FILE})
    target_link_libraries(scaltest_${c}_${d} PRIVATE scalprod_${d})
    set_property(TARGET scaltest_${c}_${d} PROPERTY LINKER_LANGUAGE ${c})
    add_test(NAME test_omp_${c}_${d} COMMAND scaltest_${c}_${d})
    set_property(TEST test_omp_${c}_${d} PROPERTY PASS_REGULAR_EXPRESSION "^[ \t]*70\\.?0*")
  endforeach()
endforeach()
