#===============================================================================
# Copyright 2020-2022 Intel Corporation.
#
# This software and the related documents are Intel copyrighted  materials,  and
# your use of  them is  governed by the  express license  under which  they were
# provided to you (License).  Unless the License provides otherwise, you may not
# use, modify, copy, publish, distribute,  disclose or transmit this software or
# the related documents without Intel's prior written permission.
#
# This software and the related documents  are provided as  is,  with no express
# or implied  warranties,  other  than those  that are  expressly stated  in the
# License.
#===============================================================================

cmake_minimum_required(VERSION 3.13)
enable_testing()

# Add cmake scripts and modules to CMake search path
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

# Define language and compiler
set(ENABLE_OMP_OFFLOAD ON)
set(TEST_LANG Fortran)
set(TEST_EXT f90)
include(setup_examples)
set(CMAKE_SKIP_BUILD_RPATH TRUE)

project(MKL_Examples LANGUAGES ${TEST_LANG})
find_package(MKL CONFIG REQUIRED)

# Generate domainList and ${domain}_funcList
include(generate_examples_list)

if(WIN32)
  set(CMAKE_CREATE_CONSOLE_EXE "")
  set(CMAKE_EXE_LINKER_FLAGS "/MANIFEST:NO")
  set(CMAKE_EXE_LINKER_FLAGS_RELEASE "")
  if(CMAKE_VERSION VERSION_LESS "3.25.2")
    set(CMAKE_Fortran_COMPILE_OBJECT "<CMAKE_Fortran_COMPILER> <DEFINES> <INCLUDES> <FLAGS> /Fo<OBJECT> -c <SOURCE>")
    set(CMAKE_Fortran_LINK_EXECUTABLE "<CMAKE_Fortran_COMPILER> <OBJECTS> /Fe<TARGET> <LINK_FLAGS> <LINK_LIBRARIES>")
  endif()
endif()

# Define target for each function from each domain
if(domainList)
foreach(domain IN LISTS domainList)
  set(TEST_INCLUDE "")
  set(TEST_LOPT "")
  set(TEST_COPT "")

  list(APPEND TEST_INCLUDE "${PROJECT_SOURCE_DIR}/common" "${PROJECT_SOURCE_DIR}/${domain}")
  list(APPEND TEST_COPT "-fpp")
  if(WIN32)
    list(APPEND TEST_COPT "/Qdiag-disable:8980")
  else()
    list(APPEND TEST_COPT "-diag-disable=8980")
  endif()

  # Build target for each example
  message(STATUS "Functions list ${domain}: ${${domain}_funcList}")
  foreach(func IN LISTS ${domain}_funcList)
    string(STRIP ${func} func_name)
    set(func "${domain}-${func_name}")

    file(GLOB_RECURSE ${domain}_${func}_SRC ${PROJECT_SOURCE_DIR}/${domain}/*/${func_name}.${TEST_EXT})
    if(NOT ${domain}_${func}_SRC)
      message(FATAL_ERROR "${domain} source file ${func_name}.${TEST_EXT} was not found")
    endif()

    # Store .mod files in unique folders to enable parallelization
    file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${func}_mod)
    set(CMAKE_Fortran_MODULE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${func}_mod")

    add_executable(${func} ${${domain}_${func}_SRC})
    target_include_directories(${func} PUBLIC ${TEST_INCLUDE} $<TARGET_PROPERTY:MKL::MKL,INTERFACE_INCLUDE_DIRECTORIES>
        ${CMAKE_CURRENT_BINARY_DIR}/${func}_mod)
    target_compile_options(${func} PUBLIC ${TEST_COPT} $<TARGET_PROPERTY:MKL::MKL,INTERFACE_COMPILE_OPTIONS>)
    target_link_libraries(${func} PUBLIC ${TEST_LOPT} $<LINK_ONLY:MKL::MKL>)

    # Register example as ctest
    add_test(NAME ${func} COMMAND ${func})

    # Add Environment variables
    if(MKL_ENV)
      set_tests_properties(${func} PROPERTIES ENVIRONMENT "${MKL_ENV}")
    endif()
  endforeach() #${domain}_funcList
endforeach() #domainList
endif() #not empty domainList
