Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 1 | # |
| 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 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) |
| 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 Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 46 | endif() |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame^] | 47 | endmacro() |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 48 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame^] | 49 | function(tensorflow_source_exists RESULT TARGET SOURCE) |
| 50 | get_target_property(SOURCES ${TARGET} SOURCES) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 51 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame^] | 52 | # 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 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 | # 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) |
| 65 | endfunction() |
| 66 | |
| 67 | function(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() |
| 83 | endfunction() |
| 84 | |
| 85 | ############################################################################# |
| 86 | # Download thirdparty |
| 87 | ############################################################################# |
| 88 | |
| 89 | # Flatbuffers |
| 90 | # Synch revision with 'tensorflow/lite/micro/tools/make/flatbuffers_download.sh' |
| 91 | download_third_party(tensorflow-flatbuffers |
| 92 | URL "https://github.com/google/flatbuffers/archive/dca12522a9f9e37f126ab925fd385c807ab4f84e.zip" |
| 93 | URL_MD5 aa9adc93eb9b33fa1a2a90969e48baee) |
| 94 | |
| 95 | target_include_directories(tflu PUBLIC |
| 96 | ${tensorflow-flatbuffers_SOURCE_DIR}/include) |
| 97 | |
| 98 | target_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' |
| 103 | download_third_party(tensorflow-gemlowp |
| 104 | URL "https://github.com/google/gemmlowp/archive/719139ce755a0f31cbf1c37f7f98adcc7fc9f425.zip" |
| 105 | URL_MD5 7e8191b24853d75de2af87622ad293ba) |
| 106 | |
| 107 | target_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' |
| 112 | download_third_party(tensorflow-ruy |
| 113 | URL "https://github.com/google/ruy/archive/d37128311b445e758136b8602d1bbd2a755e115d.zip" |
| 114 | URL_MD5 abf7a91eb90d195f016ebe0be885bb6e) |
| 115 | |
| 116 | target_include_directories(tflu PUBLIC |
| 117 | ${tensorflow-ruy_SOURCE_DIR}) |
| 118 | |
| 119 | ############################################################################# |
| 120 | # CMSIS-NN |
| 121 | ############################################################################# |
| 122 | |
| 123 | add_subdirectory(${CMSIS_PATH}/CMSIS/NN cmsis_nn) |
| 124 | |
| 125 | target_compile_options(cmsis-nn PRIVATE |
| 126 | ${TFLU_OPTIMIZATION_LEVEL}) |
| 127 | |
| 128 | tensorflow_target_sources_glob(tflu GLOB TRUE |
| 129 | ${TFLU_PATH}/kernels/cmsis_nn/*.cc) |
| 130 | |
| 131 | target_include_directories(tflu PUBLIC |
| 132 | ${CMSIS_PATH}) |
| 133 | |
| 134 | target_compile_definitions(tflu PUBLIC |
| 135 | CMSIS_NN) |
| 136 | |
| 137 | target_link_libraries(tflu PUBLIC |
| 138 | cmsis-nn) |
| 139 | |
| 140 | ############################################################################# |
| 141 | # Ethos-U |
| 142 | ############################################################################# |
| 143 | |
| 144 | if(TARGET ethosu_core_driver) |
| 145 | tensorflow_target_sources_glob(tflu GLOB TRUE |
| 146 | ${TFLU_PATH}/kernels/ethos_u/*.cc) |
| 147 | |
| 148 | target_link_libraries(tflu PUBLIC |
| 149 | ethosu_core_driver) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 150 | endif() |
| 151 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame^] | 152 | ############################################################################# |
| 153 | # Cortex-M generic |
| 154 | ############################################################################# |
| 155 | |
| 156 | tensorflow_target_sources_glob(tflu GLOB TRUE |
| 157 | ${TFLU_PATH}/cortex_m_generic/*.cc) |
| 158 | |
| 159 | target_include_directories(tflu PRIVATE |
| 160 | ${TFLU_PATH}/cortex_m_generic) |
| 161 | |
| 162 | ############################################################################# |
| 163 | # Tensorflow micro lite |
| 164 | ############################################################################# |
| 165 | |
| 166 | tensorflow_target_sources_glob(tflu GLOB TRUE |
| 167 | ${TFLU_PATH}/*.cc |
| 168 | ${TFLU_PATH}/memory_planner/*.cc |
| 169 | ${TFLU_PATH}/kernels/*.cc) |
| 170 | |
| 171 | tensorflow_target_sources_glob(tflu GLOB_RECURSE FALSE |
| 172 | ${TFLU_PATH}/../c/*.c |
| 173 | ${TFLU_PATH}/../core/*.cc |
| 174 | ${TFLU_PATH}/../kernels/*.cc |
| 175 | ${TFLU_PATH}/../schema/*.cc) |
| 176 | |
| 177 | target_include_directories(tflu PUBLIC |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 178 | ${TENSORFLOW_PATH}) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 179 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame^] | 180 | target_compile_definitions(tflu PUBLIC |
| 181 | TF_LITE_STATIC_MEMORY |
| 182 | $<$<STREQUAL:${TFLU_BUILD_TYPE},"release">:"NDEBUG;TF_LITE_STRIP_ERROR_STRINGS"> |
| 183 | $<$<STREQUAL:${TFLU_BUILD_TYPE},"release_with_logs">:"NDEBUG">) |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 184 | |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame^] | 185 | target_compile_options(tflu PRIVATE |
| 186 | ${TFLU_OPTIMIZATION_LEVEL} |
| 187 | -fno-unwind-tables |
| 188 | -ffunction-sections |
| 189 | -fdata-sections |
| 190 | -fmessage-length=0 |
| 191 | -funsigned-char |
| 192 | "$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti;-fno-exceptions;-fno-threadsafe-statics>") |
Jens Elofsson | a5e90fd | 2021-06-01 19:06:30 +0200 | [diff] [blame] | 193 | |
| 194 | # Install libraries and header files |
Kristofer Jonsson | d55ecdc | 2021-10-27 17:07:04 +0200 | [diff] [blame^] | 195 | install(TARGETS tflu DESTINATION "lib") |