# SPDX-License-Identifier: BSD-2-Clause-Patent
#
# SPDX-FileCopyrightText: 2020 the prplMesh contributors (see AUTHORS.md)
#
# This code is subject to the terms of the BSD+Patent license.
# See LICENSE file for more details.

project(nbapi VERSION ${prplmesh_VERSION})

message("${BoldWhite}Preparing ${BoldGreen}${PROJECT_NAME}${BoldWhite} for the ${BoldGreen}${TARGET_PLATFORM}${BoldWhite} platform${ColourReset}")

# Set the base path for the current module
set(MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})


# Common Sources
set(nbapi_sources ${MODULE_PATH}/ambiorix_dummy.cpp)

# Add several imports from tlfv for sMacAddr support
set(TLVF_PATH ${CMAKE_CURRENT_LIST_DIR}/../../tlvf)
set(tlvf_sources ${TLVF_PATH}/AutoGenerated/include/tlvf/common/sMacAddr.h 
                      ${TLVF_PATH}/src/include/tlvf/tlvftypes.h
                      )

# Include AMBIORIX in the build
if (ENABLE_NBAPI)
    find_package(amxb REQUIRED)
    find_package(amxc REQUIRED)
    find_package(amxd REQUIRED)
    find_package(amxp REQUIRED)
    find_package(amxo REQUIRED)
    find_package(libamxrt REQUIRED)
    list(APPEND nbapi_sources ${MODULE_PATH}/ambiorix_impl.cpp)
    list(APPEND NBAPI_LIBS amxb amxc amxd amxp amxo libamxrt)
endif()

# Build the library
add_library(${PROJECT_NAME} ${nbapi_sources} ${tlvf_sources})

set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${prplmesh_VERSION} SOVERSION ${prplmesh_VERSION_MAJOR})
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-Wl,-z,defs")

if (ENABLE_NBAPI)
    target_link_libraries(${PROJECT_NAME} PRIVATE ${NBAPI_LIBS} mapfcommon elpp bcl event)
else ()
    target_link_libraries(${PROJECT_NAME} PRIVATE mapfcommon elpp bcl)
endif()

# Install
target_include_directories(${PROJECT_NAME}
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    )

install(TARGETS ${PROJECT_NAME}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    )

# Tests are only available if NBAPI is actually built.
if (ENABLE_NBAPI AND BUILD_TESTS)
    set(TEST_PROJECT_NAME ${PROJECT_NAME}_unit_tests)
    set(unit_tests_sources
        ${nbapi_sources}
        ${MODULE_PATH}/unit_tests/ambiorix_test.cpp
        ${MODULE_PATH}/unit_tests/amxb_wrapper.cpp
    )
    add_executable(${TEST_PROJECT_NAME}
        ${unit_tests_sources}
    )
    if (COVERAGE)
        set_target_properties(${TEST_PROJECT_NAME} PROPERTIES COMPILE_FLAGS "--coverage -fPIC -O0")
        set_target_properties(${TEST_PROJECT_NAME} PROPERTIES LINK_FLAGS "--coverage")
    endif()
    target_include_directories(${TEST_PROJECT_NAME}
        PRIVATE
            ${PLATFORM_INCLUDE_DIR}
        PUBLIC
            $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    )
    list(REMOVE_ITEM NBAPI_LIBS amxb)
    target_link_libraries(${TEST_PROJECT_NAME} ${NBAPI_LIBS} mapfcommon elpp bcl)
    target_link_libraries(${TEST_PROJECT_NAME} gtest_main gmock)

    install(TARGETS ${TEST_PROJECT_NAME} DESTINATION tests)
    add_test(NAME ${TEST_PROJECT_NAME} COMMAND $<TARGET_FILE:${TEST_PROJECT_NAME}>)
endif()
