blob: 51e21ebb45846a40be4060d795280cfee08f8dff [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#----------------------------------------------------------------------------
alexander31ae9f02022-02-10 16:15:54 +000017cmake_minimum_required(VERSION 3.15.6)
18include(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)
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")
alexander31ae9f02022-02-10 16:15:54 +000048set(HAL_PLATFORM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/source/hal/platform)
Kshitij Sisodiaddcb56d2021-05-11 14:46:01 +010049
alexander3c798932021-03-26 21:42:19 +000050include(${CMAKE_SCRIPTS_DIR}/source_gen_utils.cmake)
alexander3c798932021-03-26 21:42:19 +000051
52if (${CMAKE_BINARY_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
53 message(FATAL_ERROR "Source and build are in the same directory")
54else()
55 message(STATUS "Source directory: ${CMAKE_SOURCE_DIR}")
56 message(STATUS "Binary directory: ${CMAKE_BINARY_DIR}")
57endif()
58
Kshitij Sisodiab59ba682021-11-23 17:19:52 +000059include(${CMAKE_SCRIPTS_DIR}/common_user_options.cmake)
alexandercb8a9872022-02-11 13:23:22 +000060
61add_platform_build_configuration(TARGET_PLATFORM ${TARGET_PLATFORM})
alexander31ae9f02022-02-10 16:15:54 +000062
63set_platform_global_defaults()
64
65message(STATUS "Using CMAKE_TOOLCHAIN_FILE: ${CMAKE_TOOLCHAIN_FILE}")
alexander3c798932021-03-26 21:42:19 +000066
Kshitij Sisodiab59ba682021-11-23 17:19:52 +000067# Make sure the following options are defined before proceeding:
68assert_defined(LOG_LEVEL)
69assert_defined(TENSORFLOW_SRC_PATH)
70assert_defined(TARGET_PLATFORM)
71assert_defined(TARGET_SUBSYSTEM)
72assert_defined(ETHOS_U_NPU_ENABLED)
73assert_defined(USE_CASE_BUILD)
74assert_defined(CPU_PROFILE_ENABLED)
75assert_defined(CMAKE_TOOLCHAIN_FILE)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010076
77project(arm_ml_embedded_evaluation_kit
alexander31ae9f02022-02-10 16:15:54 +000078 VERSION 21.11.1
79 DESCRIPTION "ARM ML Embedded Evaluation Kit"
80 LANGUAGES C CXX ASM)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010081
alexander3c798932021-03-26 21:42:19 +000082enforce_compiler_version()
83setup_source_generator()
84
85set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
86set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
87set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
88set(SRC_PATH ${CMAKE_CURRENT_SOURCE_DIR}/source)
Isabella Gottardi85209832021-04-20 14:08:52 +010089set(TEST_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/tests)
90list(APPEND USE_CASES_TESTS_SEARCH_DIR_LIST ${TEST_SRCS}/use_case)
alexander3c798932021-03-26 21:42:19 +000091
alexander31ae9f02022-02-10 16:15:54 +000092# We include log target
93add_subdirectory(${SRC_PATH}/log ${CMAKE_BINARY_DIR}/log)
alexander3c798932021-03-26 21:42:19 +000094
alexander31ae9f02022-02-10 16:15:54 +000095# We include arm_math target
96add_subdirectory(${SRC_PATH}/math ${CMAKE_BINARY_DIR}/math)
alexander3c798932021-03-26 21:42:19 +000097
alexander31ae9f02022-02-10 16:15:54 +000098# We include the hal target
99add_subdirectory(${SRC_PATH}/hal ${CMAKE_BINARY_DIR}/hal)
alexander3c798932021-03-26 21:42:19 +0000100
alexander31ae9f02022-02-10 16:15:54 +0000101# Include the tensorflow build target
alexander3c798932021-03-26 21:42:19 +0000102include(${CMAKE_SCRIPTS_DIR}/tensorflow.cmake)
103
Isabella Gottardic64f5062022-01-21 15:27:13 +0000104# Profiler
105if (NOT DEFINED PROFILER_DIR)
106 set(PROFILER_DIR ${SRC_PATH}/application/profiler)
107endif ()
108
alexander31ae9f02022-02-10 16:15:54 +0000109# Include directories for application module:
110set(APPLICATION_INCLUDE_DIRS
alexander3c798932021-03-26 21:42:19 +0000111 ${SRC_PATH}/application/tensorflow-lite-micro/include
112 ${SRC_PATH}/application/main/include
alexander31ae9f02022-02-10 16:15:54 +0000113 ${PROFILER_DIR}/include)
alexander3c798932021-03-26 21:42:19 +0000114
alexander31ae9f02022-02-10 16:15:54 +0000115# Source files for application module:
alexander3c798932021-03-26 21:42:19 +0000116file(GLOB_RECURSE SRC_APPLICATION
117 "${SRC_PATH}/application/main/*.cc"
118 "${SRC_PATH}/application/main/*.cpp"
119 "${SRC_PATH}/application/main/*.c"
120 "${SRC_PATH}/application/main/**/*.cc"
121 "${SRC_PATH}/application/main/**/*.cpp"
122 "${SRC_PATH}/application/main/**/*.c"
alexander31ae9f02022-02-10 16:15:54 +0000123 "${SRC_PATH}/application/tensorflow-lite-micro/**/*.cc"
124 "${SRC_PATH}/application/tensorflow-lite-micro/*.cc"
Isabella Gottardic64f5062022-01-21 15:27:13 +0000125 "${PROFILER_DIR}/*.cc"
126 "${PROFILER_DIR}/*.cpp"
127 "${PROFILER_DIR}/*.c"
alexander3c798932021-03-26 21:42:19 +0000128 )
129list(FILTER SRC_APPLICATION EXCLUDE REGEX ".*main\\.c.*$")
alexander31ae9f02022-02-10 16:15:54 +0000130set(SRC_MAIN "${SRC_PATH}/application/main/Main.cc")
131set_source_files_properties(${SRC_MAIN}
132 PROPERTIES COMPILE_DEFINITIONS
133 "PRJ_VER_STR=\"${PROJECT_VERSION}\";PRJ_DES_STR=\"${PROJECT_DESCRIPTION}\"")
alexander3c798932021-03-26 21:42:19 +0000134
135list(JOIN USE_CASE_BUILD "" USE_CASE_BUILD_STR)
Isabella Gottardi85209832021-04-20 14:08:52 +0100136list(APPEND USE_CASES_SEARCH_DIR_LIST ${SRC_PATH}/use_case)
137message(STATUS "Use-cases source paths: ${USE_CASES_SEARCH_DIR_LIST}.")
alexander31ae9f02022-02-10 16:15:54 +0000138
alexander3c798932021-03-26 21:42:19 +0000139if (${USE_CASE_BUILD_STR} STREQUAL all)
Isabella Gottardi85209832021-04-20 14:08:52 +0100140 foreach(USE_CASES_SEARCH_DIR ${USE_CASES_SEARCH_DIR_LIST})
141 SUBDIRLIST(USE_CASES_SUBDIRS ${USE_CASES_SEARCH_DIR})
142 list(APPEND USE_CASES ${USE_CASES_SUBDIRS})
143 endforeach()
alexander3c798932021-03-26 21:42:19 +0000144else()
145 set(USE_CASES ${USE_CASE_BUILD})
146endif()
147
alexandercb8a9872022-02-11 13:23:22 +0000148list(REMOVE_ITEM USE_CASES "" ${EXCLUDED_USE_CASES})
149message(STATUS "Use-cases excluded by platform configuration: ${EXCLUDED_USE_CASES}")
alexander3c798932021-03-26 21:42:19 +0000150message(STATUS "Building use-cases: ${USE_CASES}.")
151foreach(use_case ${USE_CASES})
152
Isabella Gottardi85209832021-04-20 14:08:52 +0100153 set(SRC_USE_CASE "")
154 foreach(USE_CASES_SEARCH_DIR ${USE_CASES_SEARCH_DIR_LIST})
155 if (EXISTS ${USE_CASES_SEARCH_DIR}/${use_case})
156 message(STATUS "Found sources for use-case ${use_case}")
157 set(SRC_USE_CASE ${USE_CASES_SEARCH_DIR})
158 break()
159 endif ()
160 endforeach()
161
162 if (${SRC_USE_CASE} STREQUAL "")
163 message(FATAL_ERROR "Failed to find sources for ${use_case}!")
alexander3c798932021-03-26 21:42:19 +0000164 endif ()
Kshitij Sisodia661959c2021-11-24 10:39:52 +0000165
alexander3c798932021-03-26 21:42:19 +0000166 # Executable application:
167 set(TARGET_NAME "ethos-u-${use_case}")
168
alexander31ae9f02022-02-10 16:15:54 +0000169 set(DEFAULT_MODEL_DIR ${RESOURCES_DIR}/${use_case})
170 set(DEFAULT_TEST_DATA_DIR ${DEFAULT_MODEL_DIR})
alexander3c798932021-03-26 21:42:19 +0000171 set(SRC_GEN_DIR ${CMAKE_BINARY_DIR}/generated/${use_case}/src)
172 set(INC_GEN_DIR ${CMAKE_BINARY_DIR}/generated/${use_case}/include)
173
174 # Remove old files and recreate dirs
175 file(REMOVE_RECURSE ${SRC_GEN_DIR} ${INC_GEN_DIR})
176 file(MAKE_DIRECTORY ${SRC_GEN_DIR} ${INC_GEN_DIR})
177
alexander31ae9f02022-02-10 16:15:54 +0000178 file(GLOB UC_CMAKE_FILE
179 "${SRC_USE_CASE}/${use_case}/*.cmake")
180
181 include(${UC_CMAKE_FILE})
182
alexander3c798932021-03-26 21:42:19 +0000183 file(GLOB_RECURSE UC_SRC
Isabella Gottardi85209832021-04-20 14:08:52 +0100184 "${SRC_USE_CASE}/${use_case}/src/*.cpp"
185 "${SRC_USE_CASE}/${use_case}/src/*.cc"
186 "${SRC_USE_CASE}/${use_case}/src/*.c"
187 "${SRC_USE_CASE}/${use_case}/src/**/*.cpp"
188 "${SRC_USE_CASE}/${use_case}/src/**/*.cc"
alexander31ae9f02022-02-10 16:15:54 +0000189 "${SRC_USE_CASE}/${use_case}/src/**/*.c")
alexander3c798932021-03-26 21:42:19 +0000190
191 file(GLOB_RECURSE SRC_GEN
192 "${SRC_GEN_DIR}/*.cc"
193 "${SRC_GEN_DIR}/*.cpp"
alexander31ae9f02022-02-10 16:15:54 +0000194 "${SRC_GEN_DIR}/*.c")
alexander3c798932021-03-26 21:42:19 +0000195
alexander31ae9f02022-02-10 16:15:54 +0000196 set(UC_INCLUDE ${SRC_USE_CASE}/${use_case}/include)
alexander3c798932021-03-26 21:42:19 +0000197
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100198 if (DEFINED ${use_case}_COMPILE_DEFS)
199 message(STATUS "Additional compilation flags for ${use_case}: ${${use_case}_COMPILE_DEFS}")
200 set_source_files_properties(${UC_SRC}
201 PROPERTIES COMPILE_DEFINITIONS
202 "${${use_case}_COMPILE_DEFS}")
203 endif()
204
alexander3c798932021-03-26 21:42:19 +0000205 set(UC_LIB_NAME lib${TARGET_NAME})
206
207 # Consolidated application static lib:
208 add_library(${UC_LIB_NAME} STATIC
209 ${SRC_APPLICATION}
alexander3c798932021-03-26 21:42:19 +0000210 ${UC_SRC}
alexander31ae9f02022-02-10 16:15:54 +0000211 ${SRC_GEN})
212
alexander3c798932021-03-26 21:42:19 +0000213 target_include_directories(${UC_LIB_NAME} PUBLIC
214 ${APPLICATION_INCLUDE_DIRS}
alexander3c798932021-03-26 21:42:19 +0000215 ${UC_INCLUDE}
216 ${INC_GEN_DIR}
alexander31ae9f02022-02-10 16:15:54 +0000217 ${TENSORFLOW_SRC_PATH}/tensorflow/lite/micro/tools/make/downloads/flatbuffers/include)
alexander3c798932021-03-26 21:42:19 +0000218
219 # Set the activation buffer size
220 target_compile_definitions(${UC_LIB_NAME} PUBLIC
221 "ACTIVATION_BUF_SZ=${${use_case}_ACTIVATION_BUF_SZ}")
222
alexandercb8a9872022-02-11 13:23:22 +0000223 # Set the CPU profiling defintiion
224 target_compile_definitions(${UC_LIB_NAME} PRIVATE
225 $<$<BOOL:${CPU_PROFILE_ENABLED}>:CPU_PROFILE_ENABLED>)
226
alexander31ae9f02022-02-10 16:15:54 +0000227 target_link_libraries(${UC_LIB_NAME} PUBLIC
228 log
229 arm_math
230 hal
231 tensorflow-lite-micro
232 $<$<BOOL:${APPLICATION_EXTRA_LIBS}>:${APPLICATION_EXTRA_LIBS}>
233 )
alexander3c798932021-03-26 21:42:19 +0000234
alexander31ae9f02022-02-10 16:15:54 +0000235 add_executable(${TARGET_NAME} ${SRC_MAIN})
alexander3c798932021-03-26 21:42:19 +0000236
alexander31ae9f02022-02-10 16:15:54 +0000237 target_link_libraries(${TARGET_NAME} PUBLIC ${UC_LIB_NAME})
alexander3c798932021-03-26 21:42:19 +0000238
alexander31ae9f02022-02-10 16:15:54 +0000239 platform_custom_post_build(TARGET_NAME ${TARGET_NAME})
alexander3c798932021-03-26 21:42:19 +0000240
alexander3c798932021-03-26 21:42:19 +0000241endforeach()
242
alexander31ae9f02022-02-10 16:15:54 +0000243print_useroptions()