# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# Copyright 2026 Arm Limited and/or its affiliates.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

cmake_minimum_required(VERSION 3.19)
project(arm_ethos_u_runtime_tests LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

enable_testing()

if(NOT EXECUTORCH_ROOT)
  message(
    FATAL_ERROR "EXECUTORCH_ROOT must be set to the ExecuTorch repository root."
  )
endif()
get_filename_component(EXECUTORCH_ROOT "${EXECUTORCH_ROOT}" ABSOLUTE)

add_subdirectory(
  "${EXECUTORCH_ROOT}" "${CMAKE_CURRENT_BINARY_DIR}/executorch"
  EXCLUDE_FROM_ALL
)

find_package(GTest QUIET)
if(NOT TARGET GTest::gtest_main)
  set(INSTALL_GTEST
      OFF
      CACHE BOOL "" FORCE
  )
  add_subdirectory(
    "${EXECUTORCH_ROOT}/third-party/googletest"
    "${CMAKE_CURRENT_BINARY_DIR}/googletest" EXCLUDE_FROM_ALL
  )
endif()

add_executable(
  ethosu_planned_scratch_test
  test_ethosu_planned_scratch.cpp
  "${EXECUTORCH_ROOT}/backends/arm/runtime/EthosUBackend.cpp"
  "${EXECUTORCH_ROOT}/backends/arm/runtime/EthosUBackend_IoMemcpy.cpp"
  "${EXECUTORCH_ROOT}/backends/arm/runtime/VelaBinStream.cpp"
)
target_link_libraries(
  ethosu_planned_scratch_test PRIVATE executorch_core GTest::gtest_main
)
add_test(NAME ethosu_planned_scratch_test COMMAND ethosu_planned_scratch_test)
set_tests_properties(
  ethosu_planned_scratch_test PROPERTIES LABELS "arm;ethos-u;runtime"
)

set(_ethos_u_runtime_tests_dir "${CMAKE_CURRENT_SOURCE_DIR}")
set(_ethos_u_runtime_test_output_dir "${CMAKE_CURRENT_BINARY_DIR}/test_run")

message(
  STATUS
    "Ethos-U runtime tests configured. List tests with: ctest --test-dir ${CMAKE_CURRENT_BINARY_DIR} -N"
)
message(
  STATUS
    "Run all tests with output on failure: ctest --test-dir ${CMAKE_CURRENT_BINARY_DIR} --output-on-failure"
)
message(
  STATUS
    "Run one test: ctest --test-dir ${CMAKE_CURRENT_BINARY_DIR} -R '^memory_allocation_planned_fast$' --output-on-failure"
)

add_executable(
  vela_external_blocks_test
  "${EXECUTORCH_ROOT}/backends/arm/runtime/VelaBinStream.cpp"
  "${EXECUTORCH_ROOT}/backends/arm/test/vela_external_blocks_test.cpp"
)
target_link_libraries(
  vela_external_blocks_test PRIVATE executorch_core GTest::gtest_main
)
add_test(NAME vela_external_blocks COMMAND vela_external_blocks_test)
set_tests_properties(
  vela_external_blocks PROPERTIES LABELS "arm;ethos-u;runtime"
)

find_package(Threads REQUIRED)
add_library(
  ethosu_execution_stats_test_library STATIC
  "${EXECUTORCH_ROOT}/backends/arm/runtime/EthosUExecutionStats.cpp"
)
target_include_directories(
  ethosu_execution_stats_test_library
  PRIVATE
    "${EXECUTORCH_ROOT}/.."
    "${EXECUTORCH_ROOT}/examples/arm/arm-scratch/ethos-u/core_software/core_driver/include"
)

add_executable(
  ethosu_execution_stats_test
  "${EXECUTORCH_ROOT}/backends/arm/test/ethosu_pmu_test_stubs.cpp"
  "${EXECUTORCH_ROOT}/backends/arm/test/ethosu_execution_stats_test.cpp"
)
target_include_directories(
  ethosu_execution_stats_test
  PRIVATE
    "${EXECUTORCH_ROOT}/.."
    "${EXECUTORCH_ROOT}/examples/arm/arm-scratch/ethos-u/core_software/core_driver/include"
)
target_link_libraries(
  ethosu_execution_stats_test PRIVATE ethosu_execution_stats_test_library
                                      Threads::Threads
)
add_test(NAME ethosu_execution_stats COMMAND ethosu_execution_stats_test)
set_tests_properties(
  ethosu_execution_stats PROPERTIES LABELS "arm;ethos-u;runtime"
)

add_executable(
  ethosu_pmu_callbacks_test
  "${EXECUTORCH_ROOT}/backends/arm/test/ethosu_pmu_test_stubs.cpp"
  "${EXECUTORCH_ROOT}/backends/arm/test/ethosu_pmu_callbacks_test.cpp"
)
target_include_directories(
  ethosu_pmu_callbacks_test
  PRIVATE
    "${EXECUTORCH_ROOT}/.."
    "${EXECUTORCH_ROOT}/examples/arm/arm-scratch/ethos-u/core_software/core_driver/include"
)
target_link_libraries(
  ethosu_pmu_callbacks_test PRIVATE ethosu_execution_stats_test_library
                                    Threads::Threads
)
add_test(NAME ethosu_pmu_callbacks COMMAND ethosu_pmu_callbacks_test)
set_tests_properties(
  ethosu_pmu_callbacks PROPERTIES LABELS "arm;ethos-u;runtime"
)

add_test(
  NAME memory_allocation_baseline
  COMMAND "${_ethos_u_runtime_tests_dir}/test_memory_allocation_baseline.sh"
          "${EXECUTORCH_ROOT}" "${_ethos_u_runtime_test_output_dir}"
)
set_tests_properties(
  memory_allocation_baseline
  PROPERTIES LABELS "arm;ethos-u;runtime;memory_allocation;fvp" RESOURCE_LOCK
             arm_ethos_u_runtime
)

add_test(
  NAME runner_size_optimization
  COMMAND "${_ethos_u_runtime_tests_dir}/test_runner_size_optimization.sh"
          "${EXECUTORCH_ROOT}" "${_ethos_u_runtime_test_output_dir}"
)
set_tests_properties(
  runner_size_optimization
  PROPERTIES LABELS "arm;ethos-u;runtime;memory_allocation;runner"
             RESOURCE_LOCK arm_ethos_u_runtime
)

add_test(
  NAME runner_input_restoration
  COMMAND "${_ethos_u_runtime_tests_dir}/test_runner_input_restoration.sh"
          "${EXECUTORCH_ROOT}" "${_ethos_u_runtime_test_output_dir}"
)
set_tests_properties(
  runner_input_restoration PROPERTIES LABELS "arm;ethos-u;runtime;runner;fvp"
                                      RESOURCE_LOCK arm_ethos_u_runtime
)

add_test(
  NAME memory_allocation_planned_fast
  COMMAND "${_ethos_u_runtime_tests_dir}/test_memory_allocation_planned_fast.sh"
          "${EXECUTORCH_ROOT}" "${_ethos_u_runtime_test_output_dir}"
)
set_tests_properties(
  memory_allocation_planned_fast
  PROPERTIES LABELS "arm;ethos-u;runtime;memory_allocation;fvp" RESOURCE_LOCK
             arm_ethos_u_runtime
)
