CMakeLists.txt 6.8 KB

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