blob: 150c668621f085df17e6580476c9c7766c5a1d26 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001#----------------------------------------------------------------------------
Isabella Gottardic64f5062022-01-21 15:27:13 +00002# Copyright (c) 2021 - 2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#----------------------------------------------------------------------------
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010017cmake_minimum_required(VERSION 3.21.0)
alexander31ae9f02022-02-10 16:15:54 +000018include(ExternalProject)
alexander3c798932021-03-26 21:42:19 +000019
20# Build in release mode by default
21if (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
22 set(CMAKE_BUILD_TYPE Release CACHE INTERNAL "")
23endif()
24
25message(STATUS "Build type is set to ${CMAKE_BUILD_TYPE}")
26
alexander31ae9f02022-02-10 16:15:54 +000027# Set language standards.
alexander3c798932021-03-26 21:42:19 +000028set(CMAKE_C_STANDARD 99)
ayamas0115f80702021-11-18 14:22:23 +000029set(CMAKE_CXX_STANDARD 14)
alexander3c798932021-03-26 21:42:19 +000030
31# Make the standard a requirement => prevent fallback to previous
32# supported standard
33set(CMAKE_C_STANDARD_REQUIRED ON)
34set(CMAKE_CXX_STANDARD_REQUIRED ON)
35
36# We want to pass standard C/C++ flags, without gnu extensions
37set(CMAKE_C_EXTENSIONS OFF)
38set(CMAKE_CXX_EXTENSIONS OFF)
39
alexander31ae9f02022-02-10 16:15:54 +000040set(SCRIPTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/scripts)
41set(CMAKE_SCRIPTS_DIR ${SCRIPTS_DIR}/cmake)
42set(CMAKE_TOOLCHAIN_DIR ${CMAKE_SCRIPTS_DIR}/toolchains)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010043set(DOWNLOAD_DEP_DIR ${CMAKE_BINARY_DIR}/dependencies)
alexander31ae9f02022-02-10 16:15:54 +000044set(DEPENDENCY_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/dependencies)
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +000045set(CORE_PLATFORM_DIR ${DEPENDENCY_ROOT_DIR}/core-platform)
alexander3c798932021-03-26 21:42:19 +000046
Kshitij Sisodiaddcb56d2021-05-11 14:46:01 +010047set(RESOURCES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/resources_downloaded
48 CACHE PATH "Resources directory")
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +000049set(HAL_PLATFORM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/source/hal/source/platform)
Kshitij Sisodiaddcb56d2021-05-11 14:46:01 +010050
alexander3c798932021-03-26 21:42:19 +000051include(${CMAKE_SCRIPTS_DIR}/source_gen_utils.cmake)
alexander3c798932021-03-26 21:42:19 +000052
53if (${CMAKE_BINARY_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
54 message(FATAL_ERROR "Source and build are in the same directory")
55else()
56 message(STATUS "Source directory: ${CMAKE_SOURCE_DIR}")
57 message(STATUS "Binary directory: ${CMAKE_BINARY_DIR}")
58endif()
59
Kshitij Sisodiab59ba682021-11-23 17:19:52 +000060include(${CMAKE_SCRIPTS_DIR}/common_user_options.cmake)
alexandercb8a9872022-02-11 13:23:22 +000061
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000062# Check if the resources_downloaded needs update
63check_update_public_resources(${RESOURCES_DIR})
64
alexandercb8a9872022-02-11 13:23:22 +000065add_platform_build_configuration(TARGET_PLATFORM ${TARGET_PLATFORM})
alexander31ae9f02022-02-10 16:15:54 +000066
67set_platform_global_defaults()
68
69message(STATUS "Using CMAKE_TOOLCHAIN_FILE: ${CMAKE_TOOLCHAIN_FILE}")
alexander3c798932021-03-26 21:42:19 +000070
Kshitij Sisodiab59ba682021-11-23 17:19:52 +000071# Make sure the following options are defined before proceeding:
72assert_defined(LOG_LEVEL)
73assert_defined(TENSORFLOW_SRC_PATH)
74assert_defined(TARGET_PLATFORM)
75assert_defined(TARGET_SUBSYSTEM)
76assert_defined(ETHOS_U_NPU_ENABLED)
77assert_defined(USE_CASE_BUILD)
78assert_defined(CPU_PROFILE_ENABLED)
79assert_defined(CMAKE_TOOLCHAIN_FILE)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010080
Nina Drozd59169522022-02-10 13:33:20 +000081if(POLICY CMP0123)
82 cmake_policy(SET CMP0123 NEW)
83endif()
84
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010085project(arm_ml_embedded_evaluation_kit
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010086 VERSION 22.05.0
alexander31ae9f02022-02-10 16:15:54 +000087 DESCRIPTION "ARM ML Embedded Evaluation Kit"
88 LANGUAGES C CXX ASM)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010089
alexander3c798932021-03-26 21:42:19 +000090enforce_compiler_version()
91setup_source_generator()
92
93set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
94set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
95set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
96set(SRC_PATH ${CMAKE_CURRENT_SOURCE_DIR}/source)
Isabella Gottardi85209832021-04-20 14:08:52 +010097set(TEST_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/tests)
98list(APPEND USE_CASES_TESTS_SEARCH_DIR_LIST ${TEST_SRCS}/use_case)
alexander3c798932021-03-26 21:42:19 +000099
alexander31ae9f02022-02-10 16:15:54 +0000100# We include log target
101add_subdirectory(${SRC_PATH}/log ${CMAKE_BINARY_DIR}/log)
alexander3c798932021-03-26 21:42:19 +0000102
alexander31ae9f02022-02-10 16:15:54 +0000103# We include arm_math target
104add_subdirectory(${SRC_PATH}/math ${CMAKE_BINARY_DIR}/math)
alexander3c798932021-03-26 21:42:19 +0000105
alexander31ae9f02022-02-10 16:15:54 +0000106# We include the hal target
107add_subdirectory(${SRC_PATH}/hal ${CMAKE_BINARY_DIR}/hal)
alexander3c798932021-03-26 21:42:19 +0000108
Kshitij Sisodiae2da7ee2022-02-14 11:22:58 +0000109# Add the profiler target
110if (NOT DEFINED PROFILER_DIR)
111 set(PROFILER_DIR ${SRC_PATH}/profiler)
112endif ()
113add_subdirectory(${PROFILER_DIR} ${CMAKE_BINARY_DIR}/profiler)
114
alexander31ae9f02022-02-10 16:15:54 +0000115# Include the tensorflow build target
alexander3c798932021-03-26 21:42:19 +0000116include(${CMAKE_SCRIPTS_DIR}/tensorflow.cmake)
117
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100118# Add the common API library target (tensorflow-lite-micro target is needed)
119add_subdirectory(${SRC_PATH}/application/api/common ${CMAKE_BINARY_DIR}/api/common)
120
alexander31ae9f02022-02-10 16:15:54 +0000121# Include directories for application module:
122set(APPLICATION_INCLUDE_DIRS
Kshitij Sisodiae2da7ee2022-02-14 11:22:58 +0000123 ${SRC_PATH}/application/main/include)
alexander3c798932021-03-26 21:42:19 +0000124
alexander31ae9f02022-02-10 16:15:54 +0000125# Source files for application module:
alexander3c798932021-03-26 21:42:19 +0000126file(GLOB_RECURSE SRC_APPLICATION
127 "${SRC_PATH}/application/main/*.cc"
128 "${SRC_PATH}/application/main/*.cpp"
129 "${SRC_PATH}/application/main/*.c"
alexander3c798932021-03-26 21:42:19 +0000130 )
131list(FILTER SRC_APPLICATION EXCLUDE REGEX ".*main\\.c.*$")
alexander31ae9f02022-02-10 16:15:54 +0000132set(SRC_MAIN "${SRC_PATH}/application/main/Main.cc")
133set_source_files_properties(${SRC_MAIN}
134 PROPERTIES COMPILE_DEFINITIONS
135 "PRJ_VER_STR=\"${PROJECT_VERSION}\";PRJ_DES_STR=\"${PROJECT_DESCRIPTION}\"")
alexander3c798932021-03-26 21:42:19 +0000136
137list(JOIN USE_CASE_BUILD "" USE_CASE_BUILD_STR)
Isabella Gottardi85209832021-04-20 14:08:52 +0100138list(APPEND USE_CASES_SEARCH_DIR_LIST ${SRC_PATH}/use_case)
139message(STATUS "Use-cases source paths: ${USE_CASES_SEARCH_DIR_LIST}.")
alexander31ae9f02022-02-10 16:15:54 +0000140
alexander3c798932021-03-26 21:42:19 +0000141if (${USE_CASE_BUILD_STR} STREQUAL all)
Isabella Gottardi85209832021-04-20 14:08:52 +0100142 foreach(USE_CASES_SEARCH_DIR ${USE_CASES_SEARCH_DIR_LIST})
143 SUBDIRLIST(USE_CASES_SUBDIRS ${USE_CASES_SEARCH_DIR})
144 list(APPEND USE_CASES ${USE_CASES_SUBDIRS})
145 endforeach()
alexander3c798932021-03-26 21:42:19 +0000146else()
147 set(USE_CASES ${USE_CASE_BUILD})
148endif()
149
alexandercb8a9872022-02-11 13:23:22 +0000150list(REMOVE_ITEM USE_CASES "" ${EXCLUDED_USE_CASES})
151message(STATUS "Use-cases excluded by platform configuration: ${EXCLUDED_USE_CASES}")
alexander3c798932021-03-26 21:42:19 +0000152message(STATUS "Building use-cases: ${USE_CASES}.")
153foreach(use_case ${USE_CASES})
154
Isabella Gottardi85209832021-04-20 14:08:52 +0100155 set(SRC_USE_CASE "")
156 foreach(USE_CASES_SEARCH_DIR ${USE_CASES_SEARCH_DIR_LIST})
157 if (EXISTS ${USE_CASES_SEARCH_DIR}/${use_case})
158 message(STATUS "Found sources for use-case ${use_case}")
159 set(SRC_USE_CASE ${USE_CASES_SEARCH_DIR})
160 break()
161 endif ()
162 endforeach()
163
164 if (${SRC_USE_CASE} STREQUAL "")
165 message(FATAL_ERROR "Failed to find sources for ${use_case}!")
alexander3c798932021-03-26 21:42:19 +0000166 endif ()
Kshitij Sisodia661959c2021-11-24 10:39:52 +0000167
alexander3c798932021-03-26 21:42:19 +0000168 # Executable application:
169 set(TARGET_NAME "ethos-u-${use_case}")
170
alexander31ae9f02022-02-10 16:15:54 +0000171 set(DEFAULT_MODEL_DIR ${RESOURCES_DIR}/${use_case})
172 set(DEFAULT_TEST_DATA_DIR ${DEFAULT_MODEL_DIR})
alexander3c798932021-03-26 21:42:19 +0000173 set(SRC_GEN_DIR ${CMAKE_BINARY_DIR}/generated/${use_case}/src)
174 set(INC_GEN_DIR ${CMAKE_BINARY_DIR}/generated/${use_case}/include)
175
176 # Remove old files and recreate dirs
177 file(REMOVE_RECURSE ${SRC_GEN_DIR} ${INC_GEN_DIR})
178 file(MAKE_DIRECTORY ${SRC_GEN_DIR} ${INC_GEN_DIR})
179
alexander31ae9f02022-02-10 16:15:54 +0000180 file(GLOB UC_CMAKE_FILE
181 "${SRC_USE_CASE}/${use_case}/*.cmake")
182
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100183 # Include the use case cmake file.
alexander31ae9f02022-02-10 16:15:54 +0000184 include(${UC_CMAKE_FILE})
185
alexander3c798932021-03-26 21:42:19 +0000186 file(GLOB_RECURSE UC_SRC
Isabella Gottardi85209832021-04-20 14:08:52 +0100187 "${SRC_USE_CASE}/${use_case}/src/*.cpp"
188 "${SRC_USE_CASE}/${use_case}/src/*.cc"
189 "${SRC_USE_CASE}/${use_case}/src/*.c"
190 "${SRC_USE_CASE}/${use_case}/src/**/*.cpp"
191 "${SRC_USE_CASE}/${use_case}/src/**/*.cc"
alexander31ae9f02022-02-10 16:15:54 +0000192 "${SRC_USE_CASE}/${use_case}/src/**/*.c")
alexander3c798932021-03-26 21:42:19 +0000193
194 file(GLOB_RECURSE SRC_GEN
195 "${SRC_GEN_DIR}/*.cc"
196 "${SRC_GEN_DIR}/*.cpp"
alexander31ae9f02022-02-10 16:15:54 +0000197 "${SRC_GEN_DIR}/*.c")
alexander3c798932021-03-26 21:42:19 +0000198
alexander31ae9f02022-02-10 16:15:54 +0000199 set(UC_INCLUDE ${SRC_USE_CASE}/${use_case}/include)
alexander3c798932021-03-26 21:42:19 +0000200
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100201 if (DEFINED ${use_case}_COMPILE_DEFS)
202 message(STATUS "Additional compilation flags for ${use_case}: ${${use_case}_COMPILE_DEFS}")
203 set_source_files_properties(${UC_SRC}
204 PROPERTIES COMPILE_DEFINITIONS
205 "${${use_case}_COMPILE_DEFS}")
206 endif()
207
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100208 set(UC_LIB_NAME ${use_case})
alexander3c798932021-03-26 21:42:19 +0000209
210 # Consolidated application static lib:
211 add_library(${UC_LIB_NAME} STATIC
212 ${SRC_APPLICATION}
alexander3c798932021-03-26 21:42:19 +0000213 ${UC_SRC}
alexander31ae9f02022-02-10 16:15:54 +0000214 ${SRC_GEN})
215
alexander3c798932021-03-26 21:42:19 +0000216 target_include_directories(${UC_LIB_NAME} PUBLIC
217 ${APPLICATION_INCLUDE_DIRS}
alexander3c798932021-03-26 21:42:19 +0000218 ${UC_INCLUDE}
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100219 ${INC_GEN_DIR})
alexander3c798932021-03-26 21:42:19 +0000220
221 # Set the activation buffer size
222 target_compile_definitions(${UC_LIB_NAME} PUBLIC
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100223 "ACTIVATION_BUF_SZ=${${use_case}_ACTIVATION_BUF_SZ}")
alexander3c798932021-03-26 21:42:19 +0000224
alexander31ae9f02022-02-10 16:15:54 +0000225 target_link_libraries(${UC_LIB_NAME} PUBLIC
226 log
227 arm_math
228 hal
Kshitij Sisodiae2da7ee2022-02-14 11:22:58 +0000229 profiler
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +0100230 tensorflow-lite-micro
231 common_api)
alexander3c798932021-03-26 21:42:19 +0000232
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100233 # If an API exists for this use case, include the projects here and add to
234 # the library list.
235 foreach(API_TO_USE ${${use_case}_API_LIST})
236
237 # If the required target doesn't yet exist, include the project here:
238 if (NOT TARGET ${API_TO_USE}_api)
239 add_subdirectory(
240 ${SRC_PATH}/application/api/use_case/${API_TO_USE} # Source path
241 ${CMAKE_BINARY_DIR}/api/use_case/${API_TO_USE}) # Binary path
242 endif()
243
244 # Check if the target now exists
245 if (TARGET ${API_TO_USE}_api)
246 message(STATUS "Using ${API_TO_USE}_api for ${use_case}")
247 target_link_libraries(${UC_LIB_NAME} PUBLIC ${API_TO_USE}_api)
248 else()
249 message(FATAL_ERROR "${API_TO_USE}_api target not found!")
250 endif()
251 endforeach()
252
alexander31ae9f02022-02-10 16:15:54 +0000253 add_executable(${TARGET_NAME} ${SRC_MAIN})
alexander3c798932021-03-26 21:42:19 +0000254
alexander31ae9f02022-02-10 16:15:54 +0000255 target_link_libraries(${TARGET_NAME} PUBLIC ${UC_LIB_NAME})
alexander3c798932021-03-26 21:42:19 +0000256
alexander31ae9f02022-02-10 16:15:54 +0000257 platform_custom_post_build(TARGET_NAME ${TARGET_NAME})
alexander3c798932021-03-26 21:42:19 +0000258
alexander3c798932021-03-26 21:42:19 +0000259endforeach()
260
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +0000261print_useroptions()