alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | #---------------------------------------------------------------------------- |
Isabella Gottardi | c64f506 | 2022-01-21 15:27:13 +0000 | [diff] [blame] | 2 | # Copyright (c) 2021 - 2022 Arm Limited. All rights reserved. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 3 | # 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 | #---------------------------------------------------------------------------- |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 17 | cmake_minimum_required(VERSION 3.15.6) |
| 18 | include(ExternalProject) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 19 | |
| 20 | # Build in release mode by default |
| 21 | if (NOT CMAKE_BUILD_TYPE STREQUAL Debug) |
| 22 | set(CMAKE_BUILD_TYPE Release CACHE INTERNAL "") |
| 23 | endif() |
| 24 | |
| 25 | message(STATUS "Build type is set to ${CMAKE_BUILD_TYPE}") |
| 26 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 27 | # Set language standards. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 28 | set(CMAKE_C_STANDARD 99) |
ayamas01 | 15f8070 | 2021-11-18 14:22:23 +0000 | [diff] [blame] | 29 | set(CMAKE_CXX_STANDARD 14) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 30 | |
| 31 | # Make the standard a requirement => prevent fallback to previous |
| 32 | # supported standard |
| 33 | set(CMAKE_C_STANDARD_REQUIRED ON) |
| 34 | set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 35 | |
| 36 | # We want to pass standard C/C++ flags, without gnu extensions |
| 37 | set(CMAKE_C_EXTENSIONS OFF) |
| 38 | set(CMAKE_CXX_EXTENSIONS OFF) |
| 39 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 40 | set(SCRIPTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/scripts) |
| 41 | set(CMAKE_SCRIPTS_DIR ${SCRIPTS_DIR}/cmake) |
| 42 | set(CMAKE_TOOLCHAIN_DIR ${CMAKE_SCRIPTS_DIR}/toolchains) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 43 | set(DOWNLOAD_DEP_DIR ${CMAKE_BINARY_DIR}/dependencies) |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 44 | set(DEPENDENCY_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/dependencies) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 45 | |
Kshitij Sisodia | ddcb56d | 2021-05-11 14:46:01 +0100 | [diff] [blame] | 46 | set(RESOURCES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/resources_downloaded |
| 47 | CACHE PATH "Resources directory") |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 48 | set(HAL_PLATFORM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/source/hal/platform) |
Kshitij Sisodia | ddcb56d | 2021-05-11 14:46:01 +0100 | [diff] [blame] | 49 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 50 | include(${CMAKE_SCRIPTS_DIR}/source_gen_utils.cmake) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 51 | |
| 52 | if (${CMAKE_BINARY_DIR} STREQUAL ${CMAKE_SOURCE_DIR}) |
| 53 | message(FATAL_ERROR "Source and build are in the same directory") |
| 54 | else() |
| 55 | message(STATUS "Source directory: ${CMAKE_SOURCE_DIR}") |
| 56 | message(STATUS "Binary directory: ${CMAKE_BINARY_DIR}") |
| 57 | endif() |
| 58 | |
Kshitij Sisodia | b59ba68 | 2021-11-23 17:19:52 +0000 | [diff] [blame] | 59 | include(${CMAKE_SCRIPTS_DIR}/common_user_options.cmake) |
alexander | cb8a987 | 2022-02-11 13:23:22 +0000 | [diff] [blame] | 60 | |
| 61 | add_platform_build_configuration(TARGET_PLATFORM ${TARGET_PLATFORM}) |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 62 | |
| 63 | set_platform_global_defaults() |
| 64 | |
| 65 | message(STATUS "Using CMAKE_TOOLCHAIN_FILE: ${CMAKE_TOOLCHAIN_FILE}") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 66 | |
Kshitij Sisodia | b59ba68 | 2021-11-23 17:19:52 +0000 | [diff] [blame] | 67 | # Make sure the following options are defined before proceeding: |
| 68 | assert_defined(LOG_LEVEL) |
| 69 | assert_defined(TENSORFLOW_SRC_PATH) |
| 70 | assert_defined(TARGET_PLATFORM) |
| 71 | assert_defined(TARGET_SUBSYSTEM) |
| 72 | assert_defined(ETHOS_U_NPU_ENABLED) |
| 73 | assert_defined(USE_CASE_BUILD) |
| 74 | assert_defined(CPU_PROFILE_ENABLED) |
| 75 | assert_defined(CMAKE_TOOLCHAIN_FILE) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 76 | |
| 77 | project(arm_ml_embedded_evaluation_kit |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 78 | VERSION 21.11.1 |
| 79 | DESCRIPTION "ARM ML Embedded Evaluation Kit" |
| 80 | LANGUAGES C CXX ASM) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 81 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 82 | enforce_compiler_version() |
| 83 | setup_source_generator() |
| 84 | |
| 85 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) |
| 86 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) |
| 87 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) |
| 88 | set(SRC_PATH ${CMAKE_CURRENT_SOURCE_DIR}/source) |
Isabella Gottardi | 8520983 | 2021-04-20 14:08:52 +0100 | [diff] [blame] | 89 | set(TEST_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/tests) |
| 90 | list(APPEND USE_CASES_TESTS_SEARCH_DIR_LIST ${TEST_SRCS}/use_case) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 91 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 92 | # We include log target |
| 93 | add_subdirectory(${SRC_PATH}/log ${CMAKE_BINARY_DIR}/log) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 94 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 95 | # We include arm_math target |
| 96 | add_subdirectory(${SRC_PATH}/math ${CMAKE_BINARY_DIR}/math) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 97 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 98 | # We include the hal target |
| 99 | add_subdirectory(${SRC_PATH}/hal ${CMAKE_BINARY_DIR}/hal) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 100 | |
Kshitij Sisodia | e2da7ee | 2022-02-14 11:22:58 +0000 | [diff] [blame] | 101 | # Add the profiler target |
| 102 | if (NOT DEFINED PROFILER_DIR) |
| 103 | set(PROFILER_DIR ${SRC_PATH}/profiler) |
| 104 | endif () |
| 105 | add_subdirectory(${PROFILER_DIR} ${CMAKE_BINARY_DIR}/profiler) |
| 106 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 107 | # Include the tensorflow build target |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 108 | include(${CMAKE_SCRIPTS_DIR}/tensorflow.cmake) |
| 109 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 110 | # Include directories for application module: |
| 111 | set(APPLICATION_INCLUDE_DIRS |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 112 | ${SRC_PATH}/application/tensorflow-lite-micro/include |
Kshitij Sisodia | e2da7ee | 2022-02-14 11:22:58 +0000 | [diff] [blame] | 113 | ${SRC_PATH}/application/main/include) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 114 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 115 | # Source files for application module: |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 116 | file(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" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 123 | "${SRC_PATH}/application/tensorflow-lite-micro/**/*.cc" |
| 124 | "${SRC_PATH}/application/tensorflow-lite-micro/*.cc" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 125 | ) |
| 126 | list(FILTER SRC_APPLICATION EXCLUDE REGEX ".*main\\.c.*$") |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 127 | set(SRC_MAIN "${SRC_PATH}/application/main/Main.cc") |
| 128 | set_source_files_properties(${SRC_MAIN} |
| 129 | PROPERTIES COMPILE_DEFINITIONS |
| 130 | "PRJ_VER_STR=\"${PROJECT_VERSION}\";PRJ_DES_STR=\"${PROJECT_DESCRIPTION}\"") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 131 | |
| 132 | list(JOIN USE_CASE_BUILD "" USE_CASE_BUILD_STR) |
Isabella Gottardi | 8520983 | 2021-04-20 14:08:52 +0100 | [diff] [blame] | 133 | list(APPEND USE_CASES_SEARCH_DIR_LIST ${SRC_PATH}/use_case) |
| 134 | message(STATUS "Use-cases source paths: ${USE_CASES_SEARCH_DIR_LIST}.") |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 135 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 136 | if (${USE_CASE_BUILD_STR} STREQUAL all) |
Isabella Gottardi | 8520983 | 2021-04-20 14:08:52 +0100 | [diff] [blame] | 137 | foreach(USE_CASES_SEARCH_DIR ${USE_CASES_SEARCH_DIR_LIST}) |
| 138 | SUBDIRLIST(USE_CASES_SUBDIRS ${USE_CASES_SEARCH_DIR}) |
| 139 | list(APPEND USE_CASES ${USE_CASES_SUBDIRS}) |
| 140 | endforeach() |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 141 | else() |
| 142 | set(USE_CASES ${USE_CASE_BUILD}) |
| 143 | endif() |
| 144 | |
alexander | cb8a987 | 2022-02-11 13:23:22 +0000 | [diff] [blame] | 145 | list(REMOVE_ITEM USE_CASES "" ${EXCLUDED_USE_CASES}) |
| 146 | message(STATUS "Use-cases excluded by platform configuration: ${EXCLUDED_USE_CASES}") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 147 | message(STATUS "Building use-cases: ${USE_CASES}.") |
| 148 | foreach(use_case ${USE_CASES}) |
| 149 | |
Isabella Gottardi | 8520983 | 2021-04-20 14:08:52 +0100 | [diff] [blame] | 150 | set(SRC_USE_CASE "") |
| 151 | foreach(USE_CASES_SEARCH_DIR ${USE_CASES_SEARCH_DIR_LIST}) |
| 152 | if (EXISTS ${USE_CASES_SEARCH_DIR}/${use_case}) |
| 153 | message(STATUS "Found sources for use-case ${use_case}") |
| 154 | set(SRC_USE_CASE ${USE_CASES_SEARCH_DIR}) |
| 155 | break() |
| 156 | endif () |
| 157 | endforeach() |
| 158 | |
| 159 | if (${SRC_USE_CASE} STREQUAL "") |
| 160 | message(FATAL_ERROR "Failed to find sources for ${use_case}!") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 161 | endif () |
Kshitij Sisodia | 661959c | 2021-11-24 10:39:52 +0000 | [diff] [blame] | 162 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 163 | # Executable application: |
| 164 | set(TARGET_NAME "ethos-u-${use_case}") |
| 165 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 166 | set(DEFAULT_MODEL_DIR ${RESOURCES_DIR}/${use_case}) |
| 167 | set(DEFAULT_TEST_DATA_DIR ${DEFAULT_MODEL_DIR}) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 168 | set(SRC_GEN_DIR ${CMAKE_BINARY_DIR}/generated/${use_case}/src) |
| 169 | set(INC_GEN_DIR ${CMAKE_BINARY_DIR}/generated/${use_case}/include) |
| 170 | |
| 171 | # Remove old files and recreate dirs |
| 172 | file(REMOVE_RECURSE ${SRC_GEN_DIR} ${INC_GEN_DIR}) |
| 173 | file(MAKE_DIRECTORY ${SRC_GEN_DIR} ${INC_GEN_DIR}) |
| 174 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 175 | file(GLOB UC_CMAKE_FILE |
| 176 | "${SRC_USE_CASE}/${use_case}/*.cmake") |
| 177 | |
| 178 | include(${UC_CMAKE_FILE}) |
| 179 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 180 | file(GLOB_RECURSE UC_SRC |
Isabella Gottardi | 8520983 | 2021-04-20 14:08:52 +0100 | [diff] [blame] | 181 | "${SRC_USE_CASE}/${use_case}/src/*.cpp" |
| 182 | "${SRC_USE_CASE}/${use_case}/src/*.cc" |
| 183 | "${SRC_USE_CASE}/${use_case}/src/*.c" |
| 184 | "${SRC_USE_CASE}/${use_case}/src/**/*.cpp" |
| 185 | "${SRC_USE_CASE}/${use_case}/src/**/*.cc" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 186 | "${SRC_USE_CASE}/${use_case}/src/**/*.c") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 187 | |
| 188 | file(GLOB_RECURSE SRC_GEN |
| 189 | "${SRC_GEN_DIR}/*.cc" |
| 190 | "${SRC_GEN_DIR}/*.cpp" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 191 | "${SRC_GEN_DIR}/*.c") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 192 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 193 | set(UC_INCLUDE ${SRC_USE_CASE}/${use_case}/include) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 194 | |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 195 | if (DEFINED ${use_case}_COMPILE_DEFS) |
| 196 | message(STATUS "Additional compilation flags for ${use_case}: ${${use_case}_COMPILE_DEFS}") |
| 197 | set_source_files_properties(${UC_SRC} |
| 198 | PROPERTIES COMPILE_DEFINITIONS |
| 199 | "${${use_case}_COMPILE_DEFS}") |
| 200 | endif() |
| 201 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 202 | set(UC_LIB_NAME lib${TARGET_NAME}) |
| 203 | |
| 204 | # Consolidated application static lib: |
| 205 | add_library(${UC_LIB_NAME} STATIC |
| 206 | ${SRC_APPLICATION} |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 207 | ${UC_SRC} |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 208 | ${SRC_GEN}) |
| 209 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 210 | target_include_directories(${UC_LIB_NAME} PUBLIC |
| 211 | ${APPLICATION_INCLUDE_DIRS} |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 212 | ${UC_INCLUDE} |
| 213 | ${INC_GEN_DIR} |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 214 | ${TENSORFLOW_SRC_PATH}/tensorflow/lite/micro/tools/make/downloads/flatbuffers/include) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 215 | |
| 216 | # Set the activation buffer size |
| 217 | target_compile_definitions(${UC_LIB_NAME} PUBLIC |
| 218 | "ACTIVATION_BUF_SZ=${${use_case}_ACTIVATION_BUF_SZ}") |
| 219 | |
alexander | cb8a987 | 2022-02-11 13:23:22 +0000 | [diff] [blame] | 220 | # Set the CPU profiling defintiion |
| 221 | target_compile_definitions(${UC_LIB_NAME} PRIVATE |
| 222 | $<$<BOOL:${CPU_PROFILE_ENABLED}>:CPU_PROFILE_ENABLED>) |
| 223 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 224 | target_link_libraries(${UC_LIB_NAME} PUBLIC |
| 225 | log |
| 226 | arm_math |
| 227 | hal |
Kshitij Sisodia | e2da7ee | 2022-02-14 11:22:58 +0000 | [diff] [blame] | 228 | profiler |
| 229 | tensorflow-lite-micro) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 230 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 231 | add_executable(${TARGET_NAME} ${SRC_MAIN}) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 232 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 233 | target_link_libraries(${TARGET_NAME} PUBLIC ${UC_LIB_NAME}) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 234 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 235 | platform_custom_post_build(TARGET_NAME ${TARGET_NAME}) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 236 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 237 | endforeach() |
| 238 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 239 | print_useroptions() |