blob: 3d02524835b57b36a86552170189010ff2fe4344 [file] [log] [blame]
Jens Elofssona5e90fd2021-06-01 19:06:30 +02001#
2# Copyright (c) 2021 Arm Limited. All rights reserved.
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 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)
33 cmake_parse_arguments(DOWNLOAD "" "URL;URL_MD5;SOURCE_DIR" "" ${ARGN})
34
35 message("Downloading ${DOWNLOAD_URL}")
36
37 FetchContent_Declare(${target}
38 URL ${DOWNLOAD_URL}
39 URL_MD5 ${DOWNLOAD_MD5}
40 SOURCE_DIR ${DOWNLOAD_SOURCE_DIR}
41 ${PATCH_COMMAND})
42
43 FetchContent_GetProperties(${target})
44 if (NOT ${target}_POPULATED)
45 FetchContent_Populate(${target})
Jens Elofssona5e90fd2021-06-01 19:06:30 +020046 endif()
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020047endmacro()
Jens Elofssona5e90fd2021-06-01 19:06:30 +020048
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020049function(tensorflow_source_exists RESULT TARGET SOURCE)
50 get_target_property(SOURCES ${TARGET} SOURCES)
Jens Elofssona5e90fd2021-06-01 19:06:30 +020051
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020052 # Loop over source files already added to this target
53 foreach(TMP ${SOURCES})
54 get_filename_component(SOURCE_NAME ${SOURCE} NAME)
55 get_filename_component(TMP_NAME ${TMP} NAME)
Jens Elofssona5e90fd2021-06-01 19:06:30 +020056
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +020057 # Check if file already exists
58 if (${SOURCE_NAME} STREQUAL ${TMP_NAME})
59 set(${RESULT} TRUE PARENT_SCOPE)
60 return()
61 endif()
62 endforeach()
63
64 set(${RESULT} FALSE PARENT_SCOPE)
65endfunction()
66
67function(tensorflow_target_sources_glob TARGET GLOB UNIQUE)
68 foreach (EXPR ${ARGN})
69 # Find files matching globbing expression
70 file(${GLOB} SOURCES ${EXPR})
71
72 # Remove tests
73 list(FILTER SOURCES EXCLUDE REGEX ".*_test\.cc")
74
75 # Add files to target
76 foreach(SOURCE ${SOURCES})
77 tensorflow_source_exists(SOURCE_EXISTS ${TARGET} ${SOURCE})
78 if (NOT ${UNIQUE} OR NOT ${SOURCE_EXISTS})
79 target_sources(${TARGET} PRIVATE ${SOURCE})
80 endif()
81 endforeach()
82 endforeach()
83endfunction()
84
85#############################################################################
86# Download thirdparty
87#############################################################################
88
89# Flatbuffers
90# Synch revision with 'tensorflow/lite/micro/tools/make/flatbuffers_download.sh'
91download_third_party(tensorflow-flatbuffers
92 URL "https://github.com/google/flatbuffers/archive/dca12522a9f9e37f126ab925fd385c807ab4f84e.zip"
93 URL_MD5 aa9adc93eb9b33fa1a2a90969e48baee)
94
95target_include_directories(tflu PUBLIC
96 ${tensorflow-flatbuffers_SOURCE_DIR}/include)
97
98target_compile_definitions(tflu PUBLIC
99 FLATBUFFERS_LOCALE_INDEPENDENT=0)
100
101# Gemlowp
102# Synch revision with 'tensorflow/lite/micro/tools/make/third_party_downloads.inc'
103download_third_party(tensorflow-gemlowp
104 URL "https://github.com/google/gemmlowp/archive/719139ce755a0f31cbf1c37f7f98adcc7fc9f425.zip"
105 URL_MD5 7e8191b24853d75de2af87622ad293ba)
106
107target_include_directories(tflu PUBLIC
108 ${tensorflow-gemlowp_SOURCE_DIR})
109
110# Ruy
111# Synch revision with 'tensorflow/lite/micro/tools/make/third_party_downloads.inc'
112download_third_party(tensorflow-ruy
113 URL "https://github.com/google/ruy/archive/d37128311b445e758136b8602d1bbd2a755e115d.zip"
114 URL_MD5 abf7a91eb90d195f016ebe0be885bb6e)
115
116target_include_directories(tflu PUBLIC
117 ${tensorflow-ruy_SOURCE_DIR})
118
119#############################################################################
120# CMSIS-NN
121#############################################################################
122
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100123if (NOT ${CORE_SOFTWARE_ACCELERATOR} STREQUAL "CPU")
124 add_subdirectory(${CMSIS_PATH}/CMSIS/NN cmsis_nn)
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200125
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100126 target_compile_options(cmsis-nn PRIVATE
127 ${TFLU_OPTIMIZATION_LEVEL})
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200128
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100129 tensorflow_target_sources_glob(tflu GLOB TRUE
130 ${TFLU_PATH}/kernels/cmsis_nn/*.cc)
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200131
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100132 target_include_directories(tflu PUBLIC
133 ${CMSIS_PATH})
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200134
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100135 target_compile_definitions(tflu PUBLIC
136 CMSIS_NN)
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200137
Kristofer Jonsson02856bd2021-11-24 14:08:10 +0100138 target_link_libraries(tflu PUBLIC
139 cmsis-nn)
140endif()
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200141
142#############################################################################
143# Ethos-U
144#############################################################################
145
146if(TARGET ethosu_core_driver)
147 tensorflow_target_sources_glob(tflu GLOB TRUE
148 ${TFLU_PATH}/kernels/ethos_u/*.cc)
149
150 target_link_libraries(tflu PUBLIC
151 ethosu_core_driver)
Jens Elofssona5e90fd2021-06-01 19:06:30 +0200152endif()
153
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200154#############################################################################
155# Cortex-M generic
156#############################################################################
157
158tensorflow_target_sources_glob(tflu GLOB TRUE
159 ${TFLU_PATH}/cortex_m_generic/*.cc)
160
161target_include_directories(tflu PRIVATE
162 ${TFLU_PATH}/cortex_m_generic)
163
164#############################################################################
165# Tensorflow micro lite
166#############################################################################
167
168tensorflow_target_sources_glob(tflu GLOB TRUE
169 ${TFLU_PATH}/*.cc
170 ${TFLU_PATH}/memory_planner/*.cc
171 ${TFLU_PATH}/kernels/*.cc)
172
173tensorflow_target_sources_glob(tflu GLOB_RECURSE FALSE
174 ${TFLU_PATH}/../c/*.c
175 ${TFLU_PATH}/../core/*.cc
176 ${TFLU_PATH}/../kernels/*.cc
177 ${TFLU_PATH}/../schema/*.cc)
178
179target_include_directories(tflu PUBLIC
Jens Elofssona5e90fd2021-06-01 19:06:30 +0200180 ${TENSORFLOW_PATH})
Jens Elofssona5e90fd2021-06-01 19:06:30 +0200181
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200182target_compile_definitions(tflu PUBLIC
183 TF_LITE_STATIC_MEMORY
184 $<$<STREQUAL:${TFLU_BUILD_TYPE},"release">:"NDEBUG;TF_LITE_STRIP_ERROR_STRINGS">
185 $<$<STREQUAL:${TFLU_BUILD_TYPE},"release_with_logs">:"NDEBUG">)
Jens Elofssona5e90fd2021-06-01 19:06:30 +0200186
Kristofer Jonssone4f09e52021-11-26 16:13:58 +0100187target_compile_options(tflu
188 PRIVATE
189 ${TFLU_OPTIMIZATION_LEVEL}
190 -fno-unwind-tables
191 -ffunction-sections
192 -fdata-sections
193 -fmessage-length=0
194 -funsigned-char
195 "$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti;-fno-exceptions;-fno-threadsafe-statics>"
196
197 -Wall
198 -Wextra
199
200 -Wdouble-promotion
201 -Wmissing-field-initializers
202 -Wshadow
203 -Wstrict-aliasing
204 -Wswitch
205 -Wunused-variable
206 -Wunused-function
207 -Wvla
208
209 PUBLIC
210 -Wno-cast-align
211 -Wno-null-dereference
212 -Wno-unused-parameter
213 -Wno-switch-default
214)
Jens Elofssona5e90fd2021-06-01 19:06:30 +0200215
216# Install libraries and header files
Kristofer Jonssond55ecdc2021-10-27 17:07:04 +0200217install(TARGETS tflu DESTINATION "lib")