cmake_minimum_required(VERSION 3.10)

project(matrix_lib_tests)

# Build matrix-lib tests
find_package(elcore50-matrix-lib QUIET)

set(ELCORE50_TARGET_DEFINITIONS "-D__${ELCORE50_TARGET}__")
add_definitions(${ELCORE50_TARGET_DEFINITIONS})
if("${ELCORE50_TARGET}" STREQUAL Solaris)
    set(LD_SCRIPT "${PROJECT_SOURCE_DIR}/common/dsp_solaris.ld")
elseif("${ELCORE50_TARGET}" STREQUAL MCom03)
    set(LD_SCRIPT "${PROJECT_SOURCE_DIR}/common/dsp_MCom03.ld")
elseif("${ELCORE50_TARGET}" STREQUAL Sim3x)
    set(LD_SCRIPT "${PROJECT_SOURCE_DIR}/common/dsp_solaris.ld")
else()
    message(FATAL_ERROR "Wrong value of ELCORE50_TARGET: ${ELCORE50_TARGET}.\
    \nSet\n-DELCORE50_TARGET=Solaris\n   \
    or\n-DELCORE50_TARGET=MCom03\n   \
    or\n-DELCORE50_TARGET=Sim3x")
endif()
set(LD_SCRIPT_MATMUL "${PROJECT_SOURCE_DIR}/common/dsp_MCom03_xyram_ddr.ld")

if(${ELCORE50_CL_BUILD})
    find_package(ElcoreRuntime REQUIRED)
endif()

macro(build_test_cpp SRC postfix SCRIPT)
    add_executable(${FUNC_NAME}${postfix} ${SRC})
    set_target_properties(${FUNC_NAME}${postfix} PROPERTIES LINK_FLAGS
        "-T${SCRIPT} -g -lc++ -lc++abi -lunwind -lpyapi -lc -lcrt -lm")

    if(${ELCORE50_CL_BUILD})
        target_link_libraries(${FUNC_NAME}${postfix}
            elcore50-matrix-lib elcore50-matrix-lib-test elcore-runtime)
    else()
        target_link_libraries(${FUNC_NAME}${postfix}
            elcore50-matrix-lib elcore50-matrix-lib-test)
    endif()

    add_test(NAME ${FUNC_NAME}${postfix} COMMAND ${FUNC_NAME}${postfix})
    install(TARGETS ${FUNC_NAME}${postfix} DESTINATION libraries/tests/matrix-lib/elf)
endmacro()

include_directories(include)

file(GLOB LIBTEST_SOURCES common/*)

add_library(elcore50-matrix-lib-test ${LIBTEST_SOURCES})
target_link_libraries(elcore50-matrix-lib-test elcore50-matrix-lib)
file(GLOB TESTS src/*.c src/*.cpp)

set(MATMUL_XYRAM_TESTS "test_matmul" "test_matmul_perf_f16")
foreach(SRC ${TESTS})
    get_filename_component(FUNC_NAME ${SRC} NAME_WE)
    if(${FUNC_NAME} IN_LIST MATMUL_XYRAM_TESTS AND ${ELCORE50_CL_BUILD})
        build_test_cpp("${SRC}" ".elf" "${LD_SCRIPT_MATMUL}")
    elseif(NOT ${FUNC_NAME} IN_LIST MATMUL_XYRAM_TESTS)
        build_test_cpp("${SRC}" ".elf" "${LD_SCRIPT}")
    endif()
endforeach()

enable_testing()
include(CTest)
