cmake_minimum_required(VERSION 3.8)
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 "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 Fortran)
  if("${OpenMP_TEST_${c}}")
    message("Testing ${c}")
    enable_language(${c})
  endif()
endforeach()

find_package(OpenMP REQUIRED)

foreach(c C CXX 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}_FLAGS}")
  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 Fortran)
  if(NOT "${OpenMP_TEST_${c}}")
    continue()
  endif()
  foreach(d C CXX 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()
