blob: ef30fa89c6af651291fc55d7b5e019680999cbb1 [file] [log] [blame]
Jens Elofssona5e90fd2021-06-01 19:06:30 +02001#
Kristofer Jonssond0a08822022-10-10 12:36:03 +02002# SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
Jens Elofssona5e90fd2021-06-01 19:06:30 +02003#
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 Jonssond55ecdc2021-10-27 17:07:04 +020019add_library(tflu STATIC)
Jens Elofssona5e90fd2021-06-01 19:06:30 +020020
Jens Elofssona5e90fd2021-06-01 19:06:30 +020021set(TFLU_PATH "${TENSORFLOW_PATH}/tensorflow/lite/micro")
Jens Elofssona5e90fd2021-06-01 19:06:30 +020022set(TFLU_BUILD_TYPE "release" CACHE STRING "Tensorflow Lite Mirco build type, can be release or debug")
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020023set(TFLU_OPTIMIZATION_LEVEL "-O2" CACHE STRING "Tensorflow Lite Micro kernel optimization level")
Jens Elofssona5e90fd2021-06-01 19:06:30 +020024
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020025#############################################################################
26# Helpers
27#############################################################################
Jens Elofssona5e90fd2021-06-01 19:06:30 +020028
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020029include(FetchContent)
30
31# Download third party
32macro(download_third_party target)
Kristofer Jonssoneb1d7112022-11-17 14:31:51 +010033 cmake_policy(PUSH)
34
35 if(POLICY CMP0135)
36 cmake_policy(SET CMP0135 NEW)
37 endif()
38
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020039 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 Elofssona5e90fd2021-06-01 19:06:30 +020052 endif()
Kristofer Jonssoneb1d7112022-11-17 14:31:51 +010053
54 cmake_policy(POP)
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020055endmacro()
Jens Elofssona5e90fd2021-06-01 19:06:30 +020056
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020057function(tensorflow_source_exists RESULT TARGET SOURCE)
58 get_target_property(SOURCES ${TARGET} SOURCES)
Jens Elofssona5e90fd2021-06-01 19:06:30 +020059
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020060 # 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 Elofssona5e90fd2021-06-01 19:06:30 +020064
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020065 # 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)
73endfunction()
74
75function(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()
91endfunction()
92
93#############################################################################
94# Download thirdparty
95#############################################################################
96
97# Flatbuffers
98# Synch revision with 'tensorflow/lite/micro/tools/make/flatbuffers_download.sh'
99download_third_party(tensorflow-flatbuffers
Kristofer Jonssonb708e702022-08-29 09:11:00 +0200100 URL "https://github.com/google/flatbuffers/archive/a66de58af9565586832c276fbb4251fc416bf07f.zip"
101 URL_MD5 51a7a96747e1c33eb4aac6d52513a02f)
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200102
103target_include_directories(tflu PUBLIC
104 ${tensorflow-flatbuffers_SOURCE_DIR}/include)
105
106target_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'
111download_third_party(tensorflow-gemlowp
112 URL "https://github.com/google/gemmlowp/archive/719139ce755a0f31cbf1c37f7f98adcc7fc9f425.zip"
113 URL_MD5 7e8191b24853d75de2af87622ad293ba)
114
115target_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'
120download_third_party(tensorflow-ruy
121 URL "https://github.com/google/ruy/archive/d37128311b445e758136b8602d1bbd2a755e115d.zip"
122 URL_MD5 abf7a91eb90d195f016ebe0be885bb6e)
123
124target_include_directories(tflu PUBLIC
125 ${tensorflow-ruy_SOURCE_DIR})
126
127#############################################################################
128# CMSIS-NN
129#############################################################################
130
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100131if (NOT ${CORE_SOFTWARE_ACCELERATOR} STREQUAL "CPU")
Kristofer Jonssond0a08822022-10-10 12:36:03 +0200132 add_subdirectory(${CMSIS_NN_PATH} cmsis_nn)
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200133
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100134 target_compile_options(cmsis-nn PRIVATE
135 ${TFLU_OPTIMIZATION_LEVEL})
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200136
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100137 tensorflow_target_sources_glob(tflu GLOB TRUE
138 ${TFLU_PATH}/kernels/cmsis_nn/*.cc)
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200139
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100140 target_include_directories(tflu PUBLIC
Kristofer Jonssond0a08822022-10-10 12:36:03 +0200141 ${CMSIS_NN_PATH})
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200142
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100143 target_compile_definitions(tflu PUBLIC
144 CMSIS_NN)
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200145
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100146 target_link_libraries(tflu PUBLIC
147 cmsis-nn)
148endif()
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200149
150#############################################################################
151# Ethos-U
152#############################################################################
153
154if(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 Elofssona5e90fd2021-06-01 19:06:30 +0200160endif()
161
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200162#############################################################################
163# Cortex-M generic
164#############################################################################
165
166tensorflow_target_sources_glob(tflu GLOB TRUE
167 ${TFLU_PATH}/cortex_m_generic/*.cc)
168
169target_include_directories(tflu PRIVATE
170 ${TFLU_PATH}/cortex_m_generic)
171
Måns Nilsson84c47882022-04-11 14:00:04 +0200172# For DWT/PMU counters
173target_link_libraries(tflu PRIVATE cmsis_device)
174target_compile_definitions(tflu PRIVATE ${ARM_CPU})
175
Måns Nilssone48fa7a2022-08-02 14:52:21 +0200176if(("${ARM_CPU}" STREQUAL "ARMCM55") OR ("${ARM_CPU}" STREQUAL "ARMCM85"))
Måns Nilsson84c47882022-04-11 14:00:04 +0200177 target_compile_definitions(tflu PRIVATE
178 ARM_MODEL_USE_PMU_COUNTERS)
179endif()
180
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200181#############################################################################
182# Tensorflow micro lite
183#############################################################################
184
185tensorflow_target_sources_glob(tflu GLOB TRUE
186 ${TFLU_PATH}/*.cc
Kristofer Jonssonc5b831f2022-04-29 12:19:05 +0200187 ${TFLU_PATH}/arena_allocator/*.cc
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200188 ${TFLU_PATH}/memory_planner/*.cc
Kristofer Jonsson1fed1d52022-11-21 13:39:45 +0100189 ${TFLU_PATH}/kernels/*.cc
190 ${TFLU_PATH}/tflite_bridge/*.cc)
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200191
192tensorflow_target_sources_glob(tflu GLOB_RECURSE FALSE
Måns Nilsson3a3d6fa2022-03-23 08:57:51 +0100193 ${TFLU_PATH}/../c/*.cc
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200194 ${TFLU_PATH}/../core/*.cc
Kristofer Jonsson1fed1d52022-11-21 13:39:45 +0100195 ${TFLU_PATH}/../core/api/*.cc
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200196 ${TFLU_PATH}/../kernels/*.cc
197 ${TFLU_PATH}/../schema/*.cc)
198
199target_include_directories(tflu PUBLIC
Jens Elofssona5e90fd2021-06-01 19:06:30 +0200200 ${TENSORFLOW_PATH})
Jens Elofssona5e90fd2021-06-01 19:06:30 +0200201
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200202target_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 Elofssona5e90fd2021-06-01 19:06:30 +0200206
Kristofer Jonssone4f09e52021-11-26 16:13:58 +0100207target_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 Elofssona5e90fd2021-06-01 19:06:30 +0200235
236# Install libraries and header files
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200237install(TARGETS tflu DESTINATION "lib")