cmake_minimum_required(VERSION 3.10)

project(dsplib_tests ASM C CXX)

# Build dsplib tests
find_package(elcore50-dsplib QUIET)

list(APPEND DSPLIBRARY_ARGS
    -DELCORE50_TARGET=${ELCORE50_TARGET})

option(ELCORE50_TARGET "ELCORE50_TARGET" Sim3x)

macro(build_test_c TEST_SOURCES DSP_LIB postfix)
    foreach(SRC ${TEST_SOURCES})
        get_filename_component(FUNC_NAME ${SRC} NAME_WE)
        add_executable(${FUNC_NAME}${postfix} ${SRC})
        set_target_properties(${FUNC_NAME}${postfix} PROPERTIES LINK_FLAGS "-T${LD_SCRIPT} -g")

        if(ELCORE50_CL_BUILD)
            target_link_libraries(${FUNC_NAME}${postfix} ${DSP_LIB} common elcore-runtime)
            target_compile_definitions(${FUNC_NAME}${postfix} PUBLIC
                EPS=0.05 MAX_TEST_SIZE=${MAX_TEST_SIZE_VAR})
        else()
            target_link_libraries(${FUNC_NAME}${postfix} ${DSP_LIB} common)
            target_compile_definitions(${FUNC_NAME}${postfix} PUBLIC
                EPS=0.05 MAX_TEST_SIZE=${MAX_TEST_SIZE_VAR} BARE_METAL)
        endif()

        add_test(NAME ${FUNC_NAME}${postfix} COMMAND ${FUNC_NAME}${postfix})
        install(TARGETS ${FUNC_NAME}${postfix} DESTINATION libraries/tests/dsplib/elf)
    endforeach()
endmacro()

set(ELCORE50_TARGET_DEFINITIONS "-D__${ELCORE50_TARGET}__")
add_definitions(${ELCORE50_TARGET_DEFINITIONS})
if("${ELCORE50_TARGET}" STREQUAL Solaris)
    set(MAX_TEST_SIZE_VAR 1048576)
    set(LD_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/common/dsp_solaris.ld")
elseif("${ELCORE50_TARGET}" STREQUAL MCom03)
    set(MAX_TEST_SIZE_VAR 1048576)
    set(LD_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/common/dsp_MCom03.ld")
elseif("${ELCORE50_TARGET}" STREQUAL Sim3x)
    set(MAX_TEST_SIZE_VAR 32768)
    set(LD_SCRIPT "${CMAKE_CURRENT_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()

include_directories(include/ common/)
file(GLOB SOURCES common/*)
add_library(common STATIC ${SOURCES})

file(GLOB TEST_SOURCES_C src/*)

build_test_c("${TEST_SOURCES_C}" elcore50-dsplib ".elf")
enable_testing()
include(CTest)
