blob: 65119fd97ddec85074452b0d134271c61f6d7edd [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001#----------------------------------------------------------------------------
Kshitij Sisodia774c7ca2024-04-12 11:30:02 +01002# SPDX-FileCopyrightText: Copyright 2021-2024 Arm Limited and/or its
3# affiliates <open-source-office@arm.com>
alexander3c798932021-03-26 21:42:19 +00004# SPDX-License-Identifier: Apache-2.0
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://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,
14# WITHOUT 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#----------------------------------------------------------------------------
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010018cmake_minimum_required(VERSION 3.21.0)
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)
Kshitij Sisodia774c7ca2024-04-12 11:30:02 +010041set(CMAKE_TOOLCHAIN_DIR ${SCRIPTS_DIR}/cmake/toolchains)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010042set(DOWNLOAD_DEP_DIR ${CMAKE_BINARY_DIR}/dependencies)
alexander31ae9f02022-02-10 16:15:54 +000043set(DEPENDENCY_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/dependencies)
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +000044set(CORE_PLATFORM_DIR ${DEPENDENCY_ROOT_DIR}/core-platform)
alexander3c798932021-03-26 21:42:19 +000045
Kshitij Sisodiaddcb56d2021-05-11 14:46:01 +010046set(RESOURCES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/resources_downloaded
47 CACHE PATH "Resources directory")
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +000048set(HAL_PLATFORM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/source/hal/source/platform)
Kshitij Sisodia774c7ca2024-04-12 11:30:02 +010049list(APPEND CMAKE_MODULE_PATH
50 "${SCRIPTS_DIR}/cmake"
51 "${SCRIPTS_DIR}/cmake/configuration_options"
52 "${SCRIPTS_DIR}/cmake/timing_adapter")
Kshitij Sisodiaddcb56d2021-05-11 14:46:01 +010053
Kshitij Sisodia774c7ca2024-04-12 11:30:02 +010054include(source_gen_utils)
alexander3c798932021-03-26 21:42:19 +000055
Eanna O Cathain7bc68322022-05-31 14:14:32 +010056enable_testing()
57
alexander3c798932021-03-26 21:42:19 +000058if (${CMAKE_BINARY_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
59 message(FATAL_ERROR "Source and build are in the same directory")
60else()
61 message(STATUS "Source directory: ${CMAKE_SOURCE_DIR}")
62 message(STATUS "Binary directory: ${CMAKE_BINARY_DIR}")
63endif()
64
Kshitij Sisodia774c7ca2024-04-12 11:30:02 +010065# Include common configuration options
66include(common_opts)
alexandercb8a9872022-02-11 13:23:22 +000067
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000068# Check if the resources_downloaded needs update
69check_update_public_resources(${RESOURCES_DIR})
70
alexandercb8a9872022-02-11 13:23:22 +000071add_platform_build_configuration(TARGET_PLATFORM ${TARGET_PLATFORM})
alexander31ae9f02022-02-10 16:15:54 +000072
73set_platform_global_defaults()
74
75message(STATUS "Using CMAKE_TOOLCHAIN_FILE: ${CMAKE_TOOLCHAIN_FILE}")
alexander3c798932021-03-26 21:42:19 +000076
Kshitij Sisodiab59ba682021-11-23 17:19:52 +000077# Make sure the following options are defined before proceeding:
78assert_defined(LOG_LEVEL)
79assert_defined(TENSORFLOW_SRC_PATH)
80assert_defined(TARGET_PLATFORM)
Kshitij Sisodiab59ba682021-11-23 17:19:52 +000081assert_defined(USE_CASE_BUILD)
82assert_defined(CPU_PROFILE_ENABLED)
83assert_defined(CMAKE_TOOLCHAIN_FILE)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010084
Nina Drozd59169522022-02-10 13:33:20 +000085if(POLICY CMP0123)
86 cmake_policy(SET CMP0123 NEW)
87endif()
88
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010089project(arm_ml_embedded_evaluation_kit
Kshitij Sisodia4cef9ac2024-04-08 09:58:46 +010090 VERSION 24.02.0
alexander31ae9f02022-02-10 16:15:54 +000091 DESCRIPTION "ARM ML Embedded Evaluation Kit"
92 LANGUAGES C CXX ASM)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010093
alexander3c798932021-03-26 21:42:19 +000094enforce_compiler_version()
95setup_source_generator()
96
97set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
98set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
99set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
100set(SRC_PATH ${CMAKE_CURRENT_SOURCE_DIR}/source)
Isabella Gottardi85209832021-04-20 14:08:52 +0100101set(TEST_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/tests)
102list(APPEND USE_CASES_TESTS_SEARCH_DIR_LIST ${TEST_SRCS}/use_case)
alexander3c798932021-03-26 21:42:19 +0000103
alexander31ae9f02022-02-10 16:15:54 +0000104# We include log target
105add_subdirectory(${SRC_PATH}/log ${CMAKE_BINARY_DIR}/log)
alexander3c798932021-03-26 21:42:19 +0000106
alexander31ae9f02022-02-10 16:15:54 +0000107# We include arm_math target
108add_subdirectory(${SRC_PATH}/math ${CMAKE_BINARY_DIR}/math)
alexander3c798932021-03-26 21:42:19 +0000109
alexander31ae9f02022-02-10 16:15:54 +0000110# We include the hal target
111add_subdirectory(${SRC_PATH}/hal ${CMAKE_BINARY_DIR}/hal)
alexander3c798932021-03-26 21:42:19 +0000112
Kshitij Sisodiae2da7ee2022-02-14 11:22:58 +0000113# Add the profiler target
114if (NOT DEFINED PROFILER_DIR)
115 set(PROFILER_DIR ${SRC_PATH}/profiler)
116endif ()
117add_subdirectory(${PROFILER_DIR} ${CMAKE_BINARY_DIR}/profiler)
118
alexander31ae9f02022-02-10 16:15:54 +0000119# Include the tensorflow build target
Kshitij Sisodia774c7ca2024-04-12 11:30:02 +0100120include(tensorflow_lite_micro)
alexander3c798932021-03-26 21:42:19 +0000121
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100122# Add the common API library target (tensorflow-lite-micro target is needed)
123add_subdirectory(${SRC_PATH}/application/api/common ${CMAKE_BINARY_DIR}/api/common)
124
alexander31ae9f02022-02-10 16:15:54 +0000125# Include directories for application module:
126set(APPLICATION_INCLUDE_DIRS
Kshitij Sisodiae2da7ee2022-02-14 11:22:58 +0000127 ${SRC_PATH}/application/main/include)
alexander3c798932021-03-26 21:42:19 +0000128
alexander31ae9f02022-02-10 16:15:54 +0000129# Source files for application module:
alexander3c798932021-03-26 21:42:19 +0000130file(GLOB_RECURSE SRC_APPLICATION
131 "${SRC_PATH}/application/main/*.cc"
132 "${SRC_PATH}/application/main/*.cpp"
133 "${SRC_PATH}/application/main/*.c"
alexander3c798932021-03-26 21:42:19 +0000134 )
135list(FILTER SRC_APPLICATION EXCLUDE REGEX ".*main\\.c.*$")
alexander31ae9f02022-02-10 16:15:54 +0000136set(SRC_MAIN "${SRC_PATH}/application/main/Main.cc")
137set_source_files_properties(${SRC_MAIN}
138 PROPERTIES COMPILE_DEFINITIONS
139 "PRJ_VER_STR=\"${PROJECT_VERSION}\";PRJ_DES_STR=\"${PROJECT_DESCRIPTION}\"")
alexander3c798932021-03-26 21:42:19 +0000140
141list(JOIN USE_CASE_BUILD "" USE_CASE_BUILD_STR)
Isabella Gottardi85209832021-04-20 14:08:52 +0100142list(APPEND USE_CASES_SEARCH_DIR_LIST ${SRC_PATH}/use_case)
143message(STATUS "Use-cases source paths: ${USE_CASES_SEARCH_DIR_LIST}.")
alexander31ae9f02022-02-10 16:15:54 +0000144
alexander3c798932021-03-26 21:42:19 +0000145if (${USE_CASE_BUILD_STR} STREQUAL all)
Isabella Gottardi85209832021-04-20 14:08:52 +0100146 foreach(USE_CASES_SEARCH_DIR ${USE_CASES_SEARCH_DIR_LIST})
147 SUBDIRLIST(USE_CASES_SUBDIRS ${USE_CASES_SEARCH_DIR})
148 list(APPEND USE_CASES ${USE_CASES_SUBDIRS})
149 endforeach()
alexander3c798932021-03-26 21:42:19 +0000150else()
151 set(USE_CASES ${USE_CASE_BUILD})
152endif()
153
alexandercb8a9872022-02-11 13:23:22 +0000154list(REMOVE_ITEM USE_CASES "" ${EXCLUDED_USE_CASES})
155message(STATUS "Use-cases excluded by platform configuration: ${EXCLUDED_USE_CASES}")
alexander3c798932021-03-26 21:42:19 +0000156message(STATUS "Building use-cases: ${USE_CASES}.")
Eanna O Cathain7bc68322022-05-31 14:14:32 +0100157
alexander3c798932021-03-26 21:42:19 +0000158foreach(use_case ${USE_CASES})
159
Isabella Gottardi85209832021-04-20 14:08:52 +0100160 set(SRC_USE_CASE "")
161 foreach(USE_CASES_SEARCH_DIR ${USE_CASES_SEARCH_DIR_LIST})
162 if (EXISTS ${USE_CASES_SEARCH_DIR}/${use_case})
163 message(STATUS "Found sources for use-case ${use_case}")
164 set(SRC_USE_CASE ${USE_CASES_SEARCH_DIR})
165 break()
166 endif ()
167 endforeach()
168
169 if (${SRC_USE_CASE} STREQUAL "")
170 message(FATAL_ERROR "Failed to find sources for ${use_case}!")
alexander3c798932021-03-26 21:42:19 +0000171 endif ()
Kshitij Sisodia661959c2021-11-24 10:39:52 +0000172
alexander3c798932021-03-26 21:42:19 +0000173 # Executable application:
174 set(TARGET_NAME "ethos-u-${use_case}")
175
alexander31ae9f02022-02-10 16:15:54 +0000176 set(DEFAULT_MODEL_DIR ${RESOURCES_DIR}/${use_case})
177 set(DEFAULT_TEST_DATA_DIR ${DEFAULT_MODEL_DIR})
alexander3c798932021-03-26 21:42:19 +0000178 set(SRC_GEN_DIR ${CMAKE_BINARY_DIR}/generated/${use_case}/src)
179 set(INC_GEN_DIR ${CMAKE_BINARY_DIR}/generated/${use_case}/include)
180
181 # Remove old files and recreate dirs
182 file(REMOVE_RECURSE ${SRC_GEN_DIR} ${INC_GEN_DIR})
183 file(MAKE_DIRECTORY ${SRC_GEN_DIR} ${INC_GEN_DIR})
184
alexander31ae9f02022-02-10 16:15:54 +0000185 file(GLOB UC_CMAKE_FILE
186 "${SRC_USE_CASE}/${use_case}/*.cmake")
187
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100188 # Include the use case cmake file.
alexander31ae9f02022-02-10 16:15:54 +0000189 include(${UC_CMAKE_FILE})
190
alexander3c798932021-03-26 21:42:19 +0000191 file(GLOB_RECURSE UC_SRC
Isabella Gottardi85209832021-04-20 14:08:52 +0100192 "${SRC_USE_CASE}/${use_case}/src/*.cpp"
193 "${SRC_USE_CASE}/${use_case}/src/*.cc"
194 "${SRC_USE_CASE}/${use_case}/src/*.c"
195 "${SRC_USE_CASE}/${use_case}/src/**/*.cpp"
196 "${SRC_USE_CASE}/${use_case}/src/**/*.cc"
alexander31ae9f02022-02-10 16:15:54 +0000197 "${SRC_USE_CASE}/${use_case}/src/**/*.c")
alexander3c798932021-03-26 21:42:19 +0000198
199 file(GLOB_RECURSE SRC_GEN
200 "${SRC_GEN_DIR}/*.cc"
201 "${SRC_GEN_DIR}/*.cpp"
alexander31ae9f02022-02-10 16:15:54 +0000202 "${SRC_GEN_DIR}/*.c")
alexander3c798932021-03-26 21:42:19 +0000203
alexander31ae9f02022-02-10 16:15:54 +0000204 set(UC_INCLUDE ${SRC_USE_CASE}/${use_case}/include)
alexander3c798932021-03-26 21:42:19 +0000205
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100206 if (DEFINED ${use_case}_COMPILE_DEFS)
207 message(STATUS "Additional compilation flags for ${use_case}: ${${use_case}_COMPILE_DEFS}")
208 set_source_files_properties(${UC_SRC}
209 PROPERTIES COMPILE_DEFINITIONS
210 "${${use_case}_COMPILE_DEFS}")
211 endif()
212
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100213 set(UC_LIB_NAME ${use_case})
alexander3c798932021-03-26 21:42:19 +0000214
215 # Consolidated application static lib:
216 add_library(${UC_LIB_NAME} STATIC
217 ${SRC_APPLICATION}
alexander3c798932021-03-26 21:42:19 +0000218 ${UC_SRC}
alexander31ae9f02022-02-10 16:15:54 +0000219 ${SRC_GEN})
220
alexander3c798932021-03-26 21:42:19 +0000221 target_include_directories(${UC_LIB_NAME} PUBLIC
222 ${APPLICATION_INCLUDE_DIRS}
alexander3c798932021-03-26 21:42:19 +0000223 ${UC_INCLUDE}
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100224 ${INC_GEN_DIR})
alexander3c798932021-03-26 21:42:19 +0000225
226 # Set the activation buffer size
227 target_compile_definitions(${UC_LIB_NAME} PUBLIC
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100228 "ACTIVATION_BUF_SZ=${${use_case}_ACTIVATION_BUF_SZ}")
alexander3c798932021-03-26 21:42:19 +0000229
alexander31ae9f02022-02-10 16:15:54 +0000230 target_link_libraries(${UC_LIB_NAME} PUBLIC
231 log
232 arm_math
233 hal
Kshitij Sisodiae2da7ee2022-02-14 11:22:58 +0000234 profiler
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +0100235 tensorflow-lite-micro
236 common_api)
alexander3c798932021-03-26 21:42:19 +0000237
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100238 # If an API exists for this use case, include the projects here and add to
239 # the library list.
240 foreach(API_TO_USE ${${use_case}_API_LIST})
241
242 # If the required target doesn't yet exist, include the project here:
243 if (NOT TARGET ${API_TO_USE}_api)
244 add_subdirectory(
245 ${SRC_PATH}/application/api/use_case/${API_TO_USE} # Source path
Isabella Gottardi8ce2be82023-03-20 15:35:42 +0000246 ${CMAKE_BINARY_DIR}/api/use_case/${API_TO_USE}) # Binary path
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100247 endif()
248
249 # Check if the target now exists
250 if (TARGET ${API_TO_USE}_api)
251 message(STATUS "Using ${API_TO_USE}_api for ${use_case}")
252 target_link_libraries(${UC_LIB_NAME} PUBLIC ${API_TO_USE}_api)
253 else()
254 message(FATAL_ERROR "${API_TO_USE}_api target not found!")
255 endif()
256 endforeach()
257
alexander31ae9f02022-02-10 16:15:54 +0000258 add_executable(${TARGET_NAME} ${SRC_MAIN})
alexander3c798932021-03-26 21:42:19 +0000259
alexander31ae9f02022-02-10 16:15:54 +0000260 target_link_libraries(${TARGET_NAME} PUBLIC ${UC_LIB_NAME})
alexander3c798932021-03-26 21:42:19 +0000261
alexander31ae9f02022-02-10 16:15:54 +0000262 platform_custom_post_build(TARGET_NAME ${TARGET_NAME})
alexander3c798932021-03-26 21:42:19 +0000263
alexander3c798932021-03-26 21:42:19 +0000264endforeach()
265
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +0000266print_useroptions()