CMakeLists.txt 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #
  2. # SPDX-FileCopyrightText: Copyright 2019-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
  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 "</path/to/CMSIS>" CACHE PATH "Path to CMSIS.")
  21. add_compile_options(-fomit-frame-pointer
  22. -Werror
  23. -Wimplicit-function-declaration
  24. -Wunused-variable
  25. -Wunused-function
  26. -Wno-redundant-decls
  27. -Wvla)
  28. option(BUILD_CMSIS_NN_UNIT "If building the unit tests from another project, i.e. \
  29. platform dependencies need to be provided externally." OFF)
  30. if (${CMSIS_PATH} STREQUAL "</path/to/CMSIS>")
  31. message(FATAL_ERROR "CMSIS_PATH not set. Did you provide -DCMSIS_PATH=<path/to/CMSIS>?")
  32. endif()
  33. if(NOT BUILD_CMSIS_NN_UNIT)
  34. set(BUILD_CMSIS_NN_UNIT_TESTS_FOR_FVP_BASED_CORSTONE_300 ON)
  35. else()
  36. set(BUILD_CMSIS_NN_UNIT_TESTS_FOR_FVP_BASED_CORSTONE_300 OFF)
  37. endif()
  38. if(BUILD_CMSIS_NN_UNIT_TESTS_FOR_FVP_BASED_CORSTONE_300)
  39. set(FVP_CORSTONE_300_PATH "${CMAKE_CURRENT_SOURCE_DIR}/Corstone-300" CACHE PATH
  40. "Dependencies for using FVP based on Arm Corstone-300 software.")
  41. set(CMAKE_EXECUTABLE_SUFFIX ".elf")
  42. endif()
  43. # Build the functions to be tested.
  44. add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../.. cmsis-nn)
  45. add_compile_options(${CMSIS_OPTIMIZATION_LEVEL})
  46. # Target for all unit tests.
  47. add_custom_target(cmsis_nn_unit_tests)
  48. # This function should be used instead of add_executable.
  49. set_property(GLOBAL PROPERTY cmsis_nn_unit_test_executables)
  50. function(add_cmsis_nn_unit_test_executable)
  51. get_property(tmp GLOBAL PROPERTY cmsis_nn_unit_test_executables)
  52. foreach(target ${ARGV})
  53. set(tmp "${tmp} ${target}")
  54. add_executable(${target})
  55. if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  56. target_link_options(${target} PRIVATE "--specs=nosys.specs")
  57. endif()
  58. add_dependencies(cmsis_nn_unit_tests ${target})
  59. endforeach()
  60. set_property(GLOBAL PROPERTY cmsis_nn_unit_test_executables "${tmp}")
  61. endfunction(add_cmsis_nn_unit_test_executable)
  62. add_subdirectory(TestCases/test_arm_avgpool_s16)
  63. add_subdirectory(TestCases/test_arm_avgpool_s8)
  64. add_subdirectory(TestCases/test_arm_convolve_1x1_s8_fast)
  65. add_subdirectory(TestCases/test_arm_convolve_1x1_s4_fast)
  66. add_subdirectory(TestCases/test_arm_convolve_fast_s16)
  67. add_subdirectory(TestCases/test_arm_convolve_s16)
  68. add_subdirectory(TestCases/test_arm_convolve_s8)
  69. add_subdirectory(TestCases/test_arm_convolve_s4)
  70. add_subdirectory(TestCases/test_arm_convolve_1_x_n_s8)
  71. add_subdirectory(TestCases/test_arm_depthwise_conv_3x3_s8)
  72. add_subdirectory(TestCases/test_arm_depthwise_conv_fast_s16)
  73. add_subdirectory(TestCases/test_arm_depthwise_conv_s16)
  74. add_subdirectory(TestCases/test_arm_depthwise_conv_s4)
  75. add_subdirectory(TestCases/test_arm_depthwise_conv_s4_opt)
  76. add_subdirectory(TestCases/test_arm_depthwise_conv_s8)
  77. add_subdirectory(TestCases/test_arm_depthwise_conv_s8_opt)
  78. add_subdirectory(TestCases/test_arm_ds_cnn_l_s8)
  79. add_subdirectory(TestCases/test_arm_ds_cnn_s_s8)
  80. add_subdirectory(TestCases/test_arm_elementwise_add_s16)
  81. add_subdirectory(TestCases/test_arm_elementwise_add_s8)
  82. add_subdirectory(TestCases/test_arm_elementwise_mul_s16)
  83. add_subdirectory(TestCases/test_arm_elementwise_mul_s8)
  84. add_subdirectory(TestCases/test_arm_fully_connected_s16)
  85. add_subdirectory(TestCases/test_arm_fully_connected_s8)
  86. add_subdirectory(TestCases/test_arm_fully_connected_s4)
  87. add_subdirectory(TestCases/test_arm_grouped_convolve_s8)
  88. add_subdirectory(TestCases/test_arm_lstm_unidirectional_s8)
  89. add_subdirectory(TestCases/test_arm_max_pool_s16)
  90. add_subdirectory(TestCases/test_arm_max_pool_s8)
  91. add_subdirectory(TestCases/test_arm_softmax_s16)
  92. add_subdirectory(TestCases/test_arm_softmax_s8)
  93. add_subdirectory(TestCases/test_arm_softmax_s8_s16)
  94. add_subdirectory(TestCases/test_arm_svdf_s8)
  95. add_subdirectory(TestCases/test_arm_svdf_state_s16_s8)
  96. add_subdirectory(TestCases/test_arm_transpose_conv_s8)
  97. set(MAKE_CMD "python3")
  98. set(MAKE_CMD_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/unittest_targets.py")
  99. set(MAKE_CMD_SCRIPT_OPTION "--download-and-generate-test-runners")
  100. MESSAGE(STATUS "Downloading Unity and generating test runners for CMSIS-NN unit tests if needed..")
  101. execute_process(COMMAND ${MAKE_CMD} ${MAKE_CMD_SCRIPT} ${MAKE_CMD_SCRIPT_OPTION}
  102. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
  103. add_subdirectory(Unity)
  104. # Link common dependencies.
  105. get_property(executables GLOBAL PROPERTY cmsis_nn_unit_test_executables)
  106. string(REPLACE " " ";" cmsis_nn_unit_test_list_of_executables ${executables})
  107. foreach(target ${cmsis_nn_unit_test_list_of_executables})
  108. target_link_libraries(${target} LINK_PUBLIC unity)
  109. target_link_libraries(${target} LINK_PUBLIC cmsis-nn)
  110. endforeach()
  111. if(BUILD_CMSIS_NN_UNIT_TESTS_FOR_FVP_BASED_CORSTONE_300)
  112. add_library(retarget STATIC
  113. ${FVP_CORSTONE_300_PATH}/retarget.c
  114. ${FVP_CORSTONE_300_PATH}/uart.c)
  115. # Build CMSIS startup dependencies based on TARGET_CPU.
  116. string(REGEX REPLACE "^cortex-m([0-9]+)$" "ARMCM\\1" ARM_CPU ${CMAKE_SYSTEM_PROCESSOR})
  117. if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "cortex-m33")
  118. set(ARM_FEATURES "_DSP_FP")
  119. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "cortex-m4")
  120. set(ARM_FEATURES "_FP")
  121. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "cortex-m7")
  122. set(ARM_FEATURES "_DP")
  123. else()
  124. set(ARM_FEATURES "")
  125. endif()
  126. add_library(cmsis_startup STATIC)
  127. target_sources(cmsis_startup PRIVATE
  128. ${CMSIS_PATH}/Device/ARM/${ARM_CPU}/Source/startup_${ARM_CPU}.c
  129. ${CMSIS_PATH}/Device/ARM/${ARM_CPU}/Source/system_${ARM_CPU}.c)
  130. target_include_directories(cmsis_startup PUBLIC
  131. ${CMSIS_PATH}/Device/ARM/${ARM_CPU}/Include
  132. ${CMSIS_PATH}/CMSIS/Core/Include)
  133. target_compile_options(cmsis_startup INTERFACE -include${ARM_CPU}${ARM_FEATURES}.h)
  134. target_compile_definitions(cmsis_startup PRIVATE ${ARM_CPU}${ARM_FEATURES})
  135. # Linker file settings.
  136. set(LINK_FILE "${FVP_CORSTONE_300_PATH}/linker" CACHE PATH "Linker file.")
  137. if (CMAKE_CXX_COMPILER_ID STREQUAL "ARMClang")
  138. set(LINK_FILE "${FVP_CORSTONE_300_PATH}/linker.scatter")
  139. set(LINK_FILE_OPTION "--scatter")
  140. set(LINK_ENTRY_OPTION "--entry")
  141. set(LINK_ENTRY "Reset_Handler")
  142. elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  143. set(LINK_FILE "${FVP_CORSTONE_300_PATH}/linker.ld")
  144. set(LINK_FILE_OPTION "-T")
  145. set(LINK_ENTRY_OPTION "")
  146. set(LINK_ENTRY "")
  147. endif()
  148. # Link in FVP dependencies to every unit test.
  149. get_property(executables GLOBAL PROPERTY cmsis_nn_unit_test_executables)
  150. string(REPLACE " " ";" cmsis_nn_unit_test_list_of_executables ${executables})
  151. foreach(target ${cmsis_nn_unit_test_list_of_executables})
  152. target_link_libraries(${target} PRIVATE retarget)
  153. target_link_libraries(${target} PRIVATE $<TARGET_OBJECTS:cmsis_startup> cmsis_startup)
  154. add_dependencies(${target} retarget cmsis_startup)
  155. target_compile_definitions(${target} PUBLIC USING_FVP_CORSTONE_300)
  156. target_link_options(${target} PRIVATE ${LINK_FILE_OPTION} ${LINK_FILE} ${LINK_ENTRY_OPTION} ${LINK_ENTRY})
  157. set_target_properties(${target} PROPERTIES LINK_DEPENDS ${LINK_FILE})
  158. endforeach()
  159. endif()