Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 1 | # |
Kristofer Jonsson | d0a0882 | 2022-10-10 12:36:03 +0200 | [diff] [blame] | 2 | # SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 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 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 19 | add_library(tflu STATIC) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 20 | |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 21 | set(TFLU_PATH "${TENSORFLOW_PATH}/tensorflow/lite/micro") |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 22 | set(TFLU_BUILD_TYPE "release" CACHE STRING "Tensorflow Lite Mirco build type, can be release or debug") |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 23 | set(TFLU_OPTIMIZATION_LEVEL "-O2" CACHE STRING "Tensorflow Lite Micro kernel optimization level") |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 24 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 25 | ############################################################################# |
| 26 | # Helpers |
| 27 | ############################################################################# |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 28 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 29 | include(FetchContent) |
| 30 | |
| 31 | # Download third party |
| 32 | macro(download_third_party target) |
Kristofer Jonsson | eb1d711 | 2022-11-17 14:31:51 +0100 | [diff] [blame] | 33 | cmake_policy(PUSH) |
| 34 | |
| 35 | if(POLICY CMP0135) |
| 36 | cmake_policy(SET CMP0135 NEW) |
| 37 | endif() |
| 38 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 39 | cmake_parse_arguments(DOWNLOAD "" "URL;URL_MD5;SOURCE_DIR" "" ${ARGN}) |
| 40 | |
| 41 | message("Downloading ${DOWNLOAD_URL}") |
| 42 | |
| 43 | FetchContent_Declare(${target} |
| 44 | URL ${DOWNLOAD_URL} |
| 45 | URL_MD5 ${DOWNLOAD_MD5} |
| 46 | SOURCE_DIR ${DOWNLOAD_SOURCE_DIR} |
| 47 | ${PATCH_COMMAND}) |
| 48 | |
| 49 | FetchContent_GetProperties(${target}) |
| 50 | if (NOT ${target}_POPULATED) |
| 51 | FetchContent_Populate(${target}) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 52 | endif() |
Kristofer Jonsson | eb1d711 | 2022-11-17 14:31:51 +0100 | [diff] [blame] | 53 | |
| 54 | cmake_policy(POP) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 55 | endmacro() |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 56 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 57 | function(tensorflow_source_exists RESULT TARGET SOURCE) |
| 58 | get_target_property(SOURCES ${TARGET} SOURCES) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 59 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 60 | # Loop over source files already added to this target |
| 61 | foreach(TMP ${SOURCES}) |
| 62 | get_filename_component(SOURCE_NAME ${SOURCE} NAME) |
| 63 | get_filename_component(TMP_NAME ${TMP} NAME) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 64 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 65 | # Check if file already exists |
| 66 | if (${SOURCE_NAME} STREQUAL ${TMP_NAME}) |
| 67 | set(${RESULT} TRUE PARENT_SCOPE) |
| 68 | return() |
| 69 | endif() |
| 70 | endforeach() |
| 71 | |
| 72 | set(${RESULT} FALSE PARENT_SCOPE) |
| 73 | endfunction() |
| 74 | |
| 75 | function(tensorflow_target_sources_glob TARGET GLOB UNIQUE) |
| 76 | foreach (EXPR ${ARGN}) |
| 77 | # Find files matching globbing expression |
| 78 | file(${GLOB} SOURCES ${EXPR}) |
| 79 | |
| 80 | # Remove tests |
| 81 | list(FILTER SOURCES EXCLUDE REGEX ".*_test\.cc") |
| 82 | |
| 83 | # Add files to target |
| 84 | foreach(SOURCE ${SOURCES}) |
| 85 | tensorflow_source_exists(SOURCE_EXISTS ${TARGET} ${SOURCE}) |
| 86 | if (NOT ${UNIQUE} OR NOT ${SOURCE_EXISTS}) |
| 87 | target_sources(${TARGET} PRIVATE ${SOURCE}) |
| 88 | endif() |
| 89 | endforeach() |
| 90 | endforeach() |
| 91 | endfunction() |
| 92 | |
| 93 | ############################################################################# |
| 94 | # Download thirdparty |
| 95 | ############################################################################# |
| 96 | |
| 97 | # Flatbuffers |
| 98 | # Synch revision with 'tensorflow/lite/micro/tools/make/flatbuffers_download.sh' |
| 99 | download_third_party(tensorflow-flatbuffers |
Kristofer Jonsson | b708e70 | 2022-08-29 09:11:00 +0200 | [diff] [blame] | 100 | URL "https://github.com/google/flatbuffers/archive/a66de58af9565586832c276fbb4251fc416bf07f.zip" |
| 101 | URL_MD5 51a7a96747e1c33eb4aac6d52513a02f) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 102 | |
| 103 | target_include_directories(tflu PUBLIC |
| 104 | ${tensorflow-flatbuffers_SOURCE_DIR}/include) |
| 105 | |
| 106 | target_compile_definitions(tflu PUBLIC |
| 107 | FLATBUFFERS_LOCALE_INDEPENDENT=0) |
| 108 | |
| 109 | # Gemlowp |
| 110 | # Synch revision with 'tensorflow/lite/micro/tools/make/third_party_downloads.inc' |
| 111 | download_third_party(tensorflow-gemlowp |
| 112 | URL "https://github.com/google/gemmlowp/archive/719139ce755a0f31cbf1c37f7f98adcc7fc9f425.zip" |
| 113 | URL_MD5 7e8191b24853d75de2af87622ad293ba) |
| 114 | |
| 115 | target_include_directories(tflu PUBLIC |
| 116 | ${tensorflow-gemlowp_SOURCE_DIR}) |
| 117 | |
| 118 | # Ruy |
| 119 | # Synch revision with 'tensorflow/lite/micro/tools/make/third_party_downloads.inc' |
| 120 | download_third_party(tensorflow-ruy |
| 121 | URL "https://github.com/google/ruy/archive/d37128311b445e758136b8602d1bbd2a755e115d.zip" |
| 122 | URL_MD5 abf7a91eb90d195f016ebe0be885bb6e) |
| 123 | |
| 124 | target_include_directories(tflu PUBLIC |
| 125 | ${tensorflow-ruy_SOURCE_DIR}) |
| 126 | |
| 127 | ############################################################################# |
| 128 | # CMSIS-NN |
| 129 | ############################################################################# |
| 130 | |
Kristofer Jonsson | 02856bd | 2021-11-24 14:08:10 +0100 | [diff] [blame] | 131 | if (NOT ${CORE_SOFTWARE_ACCELERATOR} STREQUAL "CPU") |
Kristofer Jonsson | d0a0882 | 2022-10-10 12:36:03 +0200 | [diff] [blame] | 132 | add_subdirectory(${CMSIS_NN_PATH} cmsis_nn) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 133 | |
Kristofer Jonsson | 02856bd | 2021-11-24 14:08:10 +0100 | [diff] [blame] | 134 | target_compile_options(cmsis-nn PRIVATE |
| 135 | ${TFLU_OPTIMIZATION_LEVEL}) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 136 | |
Kristofer Jonsson | 02856bd | 2021-11-24 14:08:10 +0100 | [diff] [blame] | 137 | tensorflow_target_sources_glob(tflu GLOB TRUE |
| 138 | ${TFLU_PATH}/kernels/cmsis_nn/*.cc) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 139 | |
Kristofer Jonsson | 02856bd | 2021-11-24 14:08:10 +0100 | [diff] [blame] | 140 | target_include_directories(tflu PUBLIC |
Kristofer Jonsson | d0a0882 | 2022-10-10 12:36:03 +0200 | [diff] [blame] | 141 | ${CMSIS_NN_PATH}) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 142 | |
Kristofer Jonsson | 02856bd | 2021-11-24 14:08:10 +0100 | [diff] [blame] | 143 | target_compile_definitions(tflu PUBLIC |
| 144 | CMSIS_NN) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 145 | |
Kristofer Jonsson | 02856bd | 2021-11-24 14:08:10 +0100 | [diff] [blame] | 146 | target_link_libraries(tflu PUBLIC |
| 147 | cmsis-nn) |
| 148 | endif() |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 149 | |
| 150 | ############################################################################# |
| 151 | # Ethos-U |
| 152 | ############################################################################# |
| 153 | |
| 154 | if(TARGET ethosu_core_driver) |
| 155 | tensorflow_target_sources_glob(tflu GLOB TRUE |
| 156 | ${TFLU_PATH}/kernels/ethos_u/*.cc) |
| 157 | |
| 158 | target_link_libraries(tflu PUBLIC |
| 159 | ethosu_core_driver) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 160 | endif() |
| 161 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 162 | ############################################################################# |
| 163 | # Cortex-M generic |
| 164 | ############################################################################# |
| 165 | |
| 166 | tensorflow_target_sources_glob(tflu GLOB TRUE |
| 167 | ${TFLU_PATH}/cortex_m_generic/*.cc) |
| 168 | |
| 169 | target_include_directories(tflu PRIVATE |
| 170 | ${TFLU_PATH}/cortex_m_generic) |
| 171 | |
Måns Nilsson | 84c4788 | 2022-04-11 14:00:04 +0200 | [diff] [blame] | 172 | # For DWT/PMU counters |
| 173 | target_link_libraries(tflu PRIVATE cmsis_device) |
| 174 | target_compile_definitions(tflu PRIVATE ${ARM_CPU}) |
| 175 | |
Måns Nilsson | e48fa7a | 2022-08-02 14:52:21 +0200 | [diff] [blame] | 176 | if(("${ARM_CPU}" STREQUAL "ARMCM55") OR ("${ARM_CPU}" STREQUAL "ARMCM85")) |
Måns Nilsson | 84c4788 | 2022-04-11 14:00:04 +0200 | [diff] [blame] | 177 | target_compile_definitions(tflu PRIVATE |
| 178 | ARM_MODEL_USE_PMU_COUNTERS) |
| 179 | endif() |
| 180 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 181 | ############################################################################# |
| 182 | # Tensorflow micro lite |
| 183 | ############################################################################# |
| 184 | |
| 185 | tensorflow_target_sources_glob(tflu GLOB TRUE |
| 186 | ${TFLU_PATH}/*.cc |
Kristofer Jonsson | c5b831f | 2022-04-29 12:19:05 +0200 | [diff] [blame] | 187 | ${TFLU_PATH}/arena_allocator/*.cc |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 188 | ${TFLU_PATH}/memory_planner/*.cc |
Kristofer Jonsson | 1fed1d5 | 2022-11-21 13:39:45 +0100 | [diff] [blame] | 189 | ${TFLU_PATH}/kernels/*.cc |
| 190 | ${TFLU_PATH}/tflite_bridge/*.cc) |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 191 | |
| 192 | tensorflow_target_sources_glob(tflu GLOB_RECURSE FALSE |
Måns Nilsson | 3a3d6fa | 2022-03-23 08:57:51 +0100 | [diff] [blame] | 193 | ${TFLU_PATH}/../c/*.cc |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 194 | ${TFLU_PATH}/../core/*.cc |
Kristofer Jonsson | 1fed1d5 | 2022-11-21 13:39:45 +0100 | [diff] [blame] | 195 | ${TFLU_PATH}/../core/api/*.cc |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 196 | ${TFLU_PATH}/../kernels/*.cc |
| 197 | ${TFLU_PATH}/../schema/*.cc) |
| 198 | |
| 199 | target_include_directories(tflu PUBLIC |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 200 | ${TENSORFLOW_PATH}) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 201 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 202 | target_compile_definitions(tflu PUBLIC |
| 203 | TF_LITE_STATIC_MEMORY |
| 204 | $<$<STREQUAL:${TFLU_BUILD_TYPE},"release">:"NDEBUG;TF_LITE_STRIP_ERROR_STRINGS"> |
| 205 | $<$<STREQUAL:${TFLU_BUILD_TYPE},"release_with_logs">:"NDEBUG">) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 206 | |
Kristofer Jonsson | e4f09e5 | 2021-11-26 16:13:58 +0100 | [diff] [blame] | 207 | target_compile_options(tflu |
| 208 | PRIVATE |
| 209 | ${TFLU_OPTIMIZATION_LEVEL} |
| 210 | -fno-unwind-tables |
| 211 | -ffunction-sections |
| 212 | -fdata-sections |
| 213 | -fmessage-length=0 |
| 214 | -funsigned-char |
| 215 | "$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti;-fno-exceptions;-fno-threadsafe-statics>" |
| 216 | |
| 217 | -Wall |
| 218 | -Wextra |
| 219 | |
| 220 | -Wdouble-promotion |
| 221 | -Wmissing-field-initializers |
| 222 | -Wshadow |
| 223 | -Wstrict-aliasing |
| 224 | -Wswitch |
| 225 | -Wunused-variable |
| 226 | -Wunused-function |
| 227 | -Wvla |
| 228 | |
| 229 | PUBLIC |
| 230 | -Wno-cast-align |
| 231 | -Wno-null-dereference |
| 232 | -Wno-unused-parameter |
| 233 | -Wno-switch-default |
| 234 | ) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 235 | |
| 236 | # Install libraries and header files |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame] | 237 | install(TARGETS tflu DESTINATION "lib") |