project(test C)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=deprecated-declarations -Wno-deprecated-declarations")

if (BUILD_TEST)
message(STATUS "Start generating code")
execute_process(COMMAND lbk gen RESULT_VARIABLE ret)
if(ret AND NOT ret EQUAL 0)
    message(FATAL_ERROR "Generating code failed")
endif()
add_definitions(-DBUILD_TEST)
endif()

aux_source_directory(test_interface/client TEST_SRC)
aux_source_directory(test_interface/server TEST_SRC)
aux_source_directory(test_interface/public TEST_SRC)
list(SORT TEST_SRC)

add_executable(test test_service.c ${TEST_SRC})

pkg_check_modules(LB_CORE REQUIRED lb_core)
pkg_check_modules(LB_BASE REQUIRED lb_base)
pkg_check_modules(GIO REQUIRED gio-2.0)
pkg_check_modules(GLIB2 REQUIRED glib-2.0)
pkg_check_modules(LBFDT_PUB REQUIRED lbFdt-public)
pkg_check_modules(LBFDT_CLI REQUIRED lbFdt-client)

target_include_directories(test PUBLIC
    test_interface/mock
    inc
    ${GLIB2_INCLUDE_DIRS}
    ${LB_BASE_INCLUDE_DIRS}
    ${LB_CORE_INCLUDE_DIRS}
    ${LBFDT_CLI_INCLUDE_DIRS}
    ${LBFDT_PUB_INCLUDE_DIRS}
    test_interface/
)
target_link_directories(test PUBLIC
    ${GLIB2_LIBRARY_DIRS}
    ${LB_CORE_LIBRARY_DIRS}
    ${LBFDT_PUB_LIBRARY_DIRS}
    ${LBFDT_CLI_LIBRARY_DIRS}
)
target_link_libraries(test PUBLIC
    ${GLIB2_LIBRARIES}
    ${LB_CORE_LIBRARIES}
    ${GIO_LIBRARIES}
    ${LBFDT_PUB_LIBRARIES}
    ${LBFDT_CLI_LIBRARIES}
)

execute_process(
    COMMAND dtc -I dts -O dtb -o test_package/test.dtb test_package/test.dts
    COMMAND_ERROR_IS_FATAL ANY
)
execute_process(
    COMMAND ${CMAKE_COMMAND} -E copy test_package/test.dtb ${CMAKE_INSTALL_PREFIX}/sys/firmware/fdt
    COMMAND_ERROR_IS_FATAL ANY
)

install(TARGETS test DESTINATION opt/litebmc/apps/lb_hwmon_test)
