CMakeLists.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #
  2. # Copyright (c) 2019-2021 Arm Limited. All rights reserved.
  3. #
  4. # SPDX-License-Identifier: Apache-2.0
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the License); you may
  7. # not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. cmake_minimum_required(VERSION 3.15.6)
  19. project(cmsis_nn_unit_tests VERSION 0.0.1)
  20. set(CMSIS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../..")
  21. option(BUILD_CMSIS_NN_UNIT "If building the unit tests from another project, i.e. \
  22. platform dependencies need to be provided externally." OFF)
  23. if(NOT BUILD_CMSIS_NN_UNIT)
  24. set(BUILD_CMSIS_NN_UNIT_TESTS_FOR_FVP_BASED_CORSTONE_300 ON)
  25. else()
  26. set(BUILD_CMSIS_NN_UNIT_TESTS_FOR_FVP_BASED_CORSTONE_300 OFF)
  27. endif()
  28. if(BUILD_CMSIS_NN_UNIT_TESTS_FOR_FVP_BASED_CORSTONE_300)
  29. set(FVP_CORSTONE_300_PATH "${CMAKE_CURRENT_SOURCE_DIR}/Corstone-300" CACHE PATH
  30. "Dependencies for using FVP based on Arm Corstone-300 software.")
  31. set(CMAKE_EXECUTABLE_SUFFIX ".elf")
  32. endif()
  33. # Build the functions to be tested.
  34. set(BUILD_CMSIS_NN_FUNCTIONS ON)
  35. add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../.. cmsis-nn)
  36. # Target for all unit tests.
  37. add_custom_target(cmsis_nn_unit_tests)
  38. # This function should be used instead of add_executable.
  39. set_property(GLOBAL PROPERTY cmsis_nn_unit_test_executables)
  40. function(add_cmsis_nn_unit_test_executable)
  41. get_property(tmp GLOBAL PROPERTY cmsis_nn_unit_test_executables)
  42. foreach(target ${ARGV})
  43. set(tmp "${tmp} ${target}")
  44. add_executable(${target})
  45. if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  46. target_link_options(${target} PRIVATE "--specs=nosys.specs")
  47. endif()
  48. add_dependencies(cmsis_nn_unit_tests ${target})
  49. endforeach()
  50. set_property(GLOBAL PROPERTY cmsis_nn_unit_test_executables "${tmp}")
  51. endfunction(add_cmsis_nn_unit_test_executable)
  52. add_subdirectory(TestCases/test_arm_avgpool_s8)
  53. add_subdirectory(TestCases/test_arm_convolve_1x1_s8_fast)
  54. add_subdirectory(TestCases/test_arm_convolve_s8)
  55. add_subdirectory(TestCases/test_arm_depthwise_conv_3x3_s8)
  56. add_subdirectory(TestCases/test_arm_depthwise_conv_s8)
  57. add_subdirectory(TestCases/test_arm_depthwise_conv_s8_opt)
  58. add_subdirectory(TestCases/test_arm_fully_connected_s8)
  59. add_subdirectory(TestCases/test_arm_max_pool_s8)
  60. add_subdirectory(TestCases/test_arm_softmax_s8)
  61. add_subdirectory(TestCases/test_arm_svdf_s8)
  62. set(MAKE_CMD "python3")
  63. set(MAKE_CMD_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/unittest_targets.py")
  64. set(MAKE_CMD_SCRIPT_OPTION "--download-and-generate-test-runners")
  65. MESSAGE(STATUS "Downloading Unity and generating test runners for CMSIS-NN unit tests if needed..")
  66. execute_process(COMMAND ${MAKE_CMD} ${MAKE_CMD_SCRIPT} ${MAKE_CMD_SCRIPT_OPTION}
  67. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
  68. add_subdirectory(Unity)
  69. # Link common dependencies.
  70. get_property(executables GLOBAL PROPERTY cmsis_nn_unit_test_executables)
  71. string(REPLACE " " ";" cmsis_nn_unit_test_list_of_executables ${executables})
  72. foreach(target ${cmsis_nn_unit_test_list_of_executables})
  73. target_link_libraries(${target} LINK_PUBLIC unity)
  74. target_link_libraries(${target} LINK_PUBLIC CMSISNN)
  75. endforeach()
  76. if(BUILD_CMSIS_NN_UNIT_TESTS_FOR_FVP_BASED_CORSTONE_300)
  77. add_library(retarget STATIC
  78. ${FVP_CORSTONE_300_PATH}/retarget.c
  79. ${FVP_CORSTONE_300_PATH}/uart.c)
  80. # Build CMSIS startup dependencies based on TARGET_CPU.
  81. string(REGEX REPLACE "^cortex-m([0-9]+)$" "ARMCM\\1" ARM_CPU ${CMAKE_SYSTEM_PROCESSOR})
  82. if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "cortex-m33")
  83. set(ARM_FEATURES "_DSP_FP")
  84. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "cortex-m4")
  85. set(ARM_FEATURES "_FP")
  86. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "cortex-m7")
  87. set(ARM_FEATURES "_DP")
  88. else()
  89. set(ARM_FEATURES "")
  90. endif()
  91. add_library(cmsis_startup STATIC)
  92. target_sources(cmsis_startup PRIVATE
  93. ${CMSIS_PATH}/Device/ARM/${ARM_CPU}/Source/startup_${ARM_CPU}.c
  94. ${CMSIS_PATH}/Device/ARM/${ARM_CPU}/Source/system_${ARM_CPU}.c)
  95. target_include_directories(cmsis_startup PUBLIC
  96. ${CMSIS_PATH}/Device/ARM/${ARM_CPU}/Include
  97. ${CMSIS_PATH}/CMSIS/Core/Include)
  98. target_compile_options(cmsis_startup INTERFACE -include${ARM_CPU}${ARM_FEATURES}.h)
  99. target_compile_definitions(cmsis_startup PRIVATE ${ARM_CPU}${ARM_FEATURES})
  100. # Linker file settings.
  101. set(LINK_FILE "${FVP_CORSTONE_300_PATH}/linker" CACHE PATH "Linker file.")
  102. if (CMAKE_CXX_COMPILER_ID STREQUAL "ARMClang")
  103. set(LINK_FILE "${FVP_CORSTONE_300_PATH}/linker.scatter")
  104. set(LINK_FILE_OPTION "--scatter")
  105. set(LINK_ENTRY_OPTION "--entry")
  106. set(LINK_ENTRY "Reset_Handler")
  107. elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  108. set(LINK_FILE "${FVP_CORSTONE_300_PATH}/linker.ld")
  109. set(LINK_FILE_OPTION "-T")
  110. set(LINK_ENTRY_OPTION "")
  111. set(LINK_ENTRY "")
  112. endif()
  113. # Link in FVP dependencies to every unit test.
  114. get_property(executables GLOBAL PROPERTY cmsis_nn_unit_test_executables)
  115. string(REPLACE " " ";" cmsis_nn_unit_test_list_of_executables ${executables})
  116. foreach(target ${cmsis_nn_unit_test_list_of_executables})
  117. target_link_libraries(${target} PRIVATE retarget)
  118. target_link_libraries(${target} PRIVATE $<TARGET_OBJECTS:cmsis_startup> cmsis_startup)
  119. add_dependencies(${target} retarget cmsis_startup)
  120. target_compile_definitions(${target} PUBLIC USING_FVP_CORSTONE_300)
  121. target_link_options(${target} PRIVATE ${LINK_FILE_OPTION} ${LINK_FILE} ${LINK_ENTRY_OPTION} ${LINK_ENTRY})
  122. set_target_properties(${target} PROPERTIES LINK_DEPENDS ${LINK_FILE})
  123. endforeach()
  124. endif()