#===============================================================================
# Copyright (C) 2020 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()

# Set MKL_ROOT directory
function(define_mkl_root POTENTIAL_MKL_ROOT)
  if(EXISTS "${POTENTIAL_MKL_ROOT}/include/mkl.h")
    set(MKL_ROOT "${POTENTIAL_MKL_ROOT}" PARENT_SCOPE)
  else()
    set(MKL_ROOT "" PARENT_SCOPE)
  endif()
endfunction()

if("${MKL_ROOT}" STREQUAL "")
  if(NOT "$ENV{MKLROOT}" STREQUAL "")
    file(TO_CMAKE_PATH "$ENV{MKLROOT}" POTENTIAL_MKL_ROOT)
    define_mkl_root("${POTENTIAL_MKL_ROOT}")
  endif()
  if("${MKL_ROOT}" STREQUAL "" AND (NOT "${MKL_DIR}" STREQUAL ""))
    get_filename_component(POTENTIAL_MKL_ROOT "${MKL_DIR}/../../../" ABSOLUTE)
    define_mkl_root("${POTENTIAL_MKL_ROOT}")
  endif()
  if("${MKL_ROOT}" STREQUAL "")
    get_filename_component(MKL_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}" REALPATH)
    get_filename_component(POTENTIAL_MKL_ROOT "${MKL_CMAKE_PATH}/../../../../../" ABSOLUTE)
    define_mkl_root("${POTENTIAL_MKL_ROOT}")
  endif()
  if("${MKL_ROOT}" STREQUAL "")
    message(STATUS "Cannot infer MKL_ROOT from the MKLROOT environment variable, MKL_DIR variable, or the directory containing CMakeLists.txt.")
  endif()
endif()
if(NOT "${MKL_ROOT}" STREQUAL "")
  file(TO_CMAKE_PATH "${MKL_ROOT}" MKL_ROOT)
  message(STATUS "MKL_ROOT: ${MKL_ROOT}")
endif()

# 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")
if(NOT "${MKL_ROOT}" STREQUAL "")
  list(APPEND CMAKE_MODULE_PATH "${MKL_ROOT}/share/doc/mkl/examples/cmake")
endif()

# Define language and compiler
set(TEST_LANG C)
set(TEST_EXT c)
set(DATA_EXT d)
include(setup_examples)

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

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

if(WIN32)
  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MANIFEST:NO")
endif()

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

  # Suppress CRT warning on Windows
  if(WIN32)
    list(APPEND TEST_COPT "-D_CRT_SECURE_NO_WARNINGS")
    if (CMAKE_${TEST_LANG}_COMPILER_ID STREQUAL "ICL")
      list(APPEND TEST_COPT "/Qdiag-disable:1478")
    endif()
  endif()

  # Domain specific options and targets
  if(domain STREQUAL "blas")
    add_library(blas_common ${CMAKE_CURRENT_SOURCE_DIR}/blas/source/common_func.c)
    target_include_directories(blas_common PUBLIC $<TARGET_PROPERTY:MKL::MKL,INTERFACE_INCLUDE_DIRECTORIES>)
    target_compile_options(blas_common PUBLIC ${TEST_COPT} $<TARGET_PROPERTY:MKL::MKL,INTERFACE_COMPILE_OPTIONS>)
    list(APPEND TEST_LOPT blas_common)
  endif()
  if(domain STREQUAL "fftw3x")
    list(APPEND TEST_INCLUDE "${MKL_INCLUDE}/fftw")
  endif()
  if(domain STREQUAL "trans")
    add_library(common_func ${CMAKE_CURRENT_SOURCE_DIR}/trans/source/common_func.c)
    target_include_directories(common_func PUBLIC $<TARGET_PROPERTY:MKL::MKL,INTERFACE_INCLUDE_DIRECTORIES>)
    target_compile_options(common_func PUBLIC ${TEST_COPT} $<TARGET_PROPERTY:MKL::MKL,INTERFACE_COMPILE_OPTIONS>)
    set(TEST_LOPT common_func)
  endif()
  if(domain STREQUAL "sparse_directsolvers")
    if("pardiso_matrix_file" IN_LIST ${domain}_funcList)
      list(APPEND TEST_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/common/sparse")
      add_library(sparse_common ${CMAKE_CURRENT_SOURCE_DIR}/common/sparse/matrix_reader.c ${CMAKE_CURRENT_SOURCE_DIR}/common/sparse/mmio.c)
      target_include_directories(sparse_common PUBLIC $<TARGET_PROPERTY:MKL::MKL,INTERFACE_INCLUDE_DIRECTORIES>)
      target_compile_options(sparse_common PUBLIC ${TEST_COPT} $<TARGET_PROPERTY:MKL::MKL,INTERFACE_COMPILE_OPTIONS>)
      set(TEST_LOPT sparse_common)
    endif()
  endif()

  # Build target for each example

  if(NOT "${${domain}_funcList}" STREQUAL "")
    message(STATUS "Functions list ${domain}: ${${domain}_funcList}")
    foreach(func IN LISTS ${domain}_funcList)
      set(executable "${domain}-${func}")

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

      if(domain STREQUAL "vsl_essl")
        string(REPLACE "source/example_" "vsl_wrappers/sample_" wrapper_func_name ${${domain}_${func}_SRC})
        list(APPEND ${domain}_${func}_SRC ${wrapper_func_name})
      endif()

      add_executable(${executable} ${${domain}_${func}_SRC})
      set_property(TARGET ${executable} PROPERTY C_STANDARD 99)
      target_include_directories(${executable} PUBLIC ${TEST_INCLUDE} $<TARGET_PROPERTY:MKL::MKL,INTERFACE_INCLUDE_DIRECTORIES>)
      target_compile_options(${executable} PUBLIC ${TEST_COPT} $<TARGET_PROPERTY:MKL::MKL,INTERFACE_COMPILE_OPTIONS>)
      target_link_libraries(${executable} PUBLIC ${TEST_LOPT} $<LINK_ONLY:MKL::MKL>)

      # Register example as ctest
      file(GLOB_RECURSE ${domain}_${func}_DATA ${PROJECT_SOURCE_DIR}/${domain}/*/${func}.${DATA_EXT})
      if(EXISTS ${${domain}_${func}_DATA})
        if(domain STREQUAL "blas")
          add_test(NAME ${executable} COMMAND ${executable} ${${domain}_${func}_DATA})
        elseif(domain STREQUAL "vsl")
          add_test(NAME ${executable} COMMAND cmake -E echo \"${${domain}_${func}_DATA}\" | ${executable})
        endif()
      else()
        if( (domain STREQUAL "sparse_directsolvers") AND (func STREQUAL "pardiso_matrix_file") )
          add_test(NAME ${executable} COMMAND ${executable} ${PROJECT_SOURCE_DIR}/common/sparse/matrices/sym.mtx)
        else()
          add_test(NAME ${executable} COMMAND ${executable})
        endif()
      endif()

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