Kristofer Jonsson | 1823930 | 2020-04-17 08:45:38 +0200 | [diff] [blame] | 1 | # |
| 2 | # Copyright (c) 2019-2020 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 | |
| 19 | include(ProcessorCount) |
| 20 | ProcessorCount(J) |
| 21 | |
Per Åstrand | 3f6e07a | 2020-05-12 13:16:37 +0200 | [diff] [blame] | 22 | if (CMAKE_CXX_COMPILER_ID STREQUAL "ARMClang") |
Måns Nilsson | 1aa56dd | 2020-09-17 15:38:58 +0200 | [diff] [blame] | 23 | set(TFLU_TOOLCHAIN "armclang") |
Per Åstrand | 3f6e07a | 2020-05-12 13:16:37 +0200 | [diff] [blame] | 24 | elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
Måns Nilsson | 1aa56dd | 2020-09-17 15:38:58 +0200 | [diff] [blame] | 25 | set(TFLU_TOOLCHAIN "gcc") |
Per Åstrand | 3f6e07a | 2020-05-12 13:16:37 +0200 | [diff] [blame] | 26 | else () |
| 27 | message(FATAL_ERROR "No compiler ID is set") |
| 28 | endif() |
Kristofer Jonsson | 1823930 | 2020-04-17 08:45:38 +0200 | [diff] [blame] | 29 | |
Måns Nilsson | 1aa56dd | 2020-09-17 15:38:58 +0200 | [diff] [blame] | 30 | get_filename_component(TFLU_TARGET_TOOLCHAIN_ROOT ${CMAKE_C_COMPILER} DIRECTORY) |
| 31 | |
| 32 | set(TFLU_TARGET_TOOLCHAIN_ROOT "${TFLU_TARGET_TOOLCHAIN_ROOT}/") |
Kristofer Jonsson | 1823930 | 2020-04-17 08:45:38 +0200 | [diff] [blame] | 33 | set(TFLU_PATH "${TENSORFLOW_PATH}/tensorflow/lite/micro") |
| 34 | set(TFLU_GENDIR ${CMAKE_CURRENT_BINARY_DIR}/tensorflow/) |
Måns Nilsson | 1aa56dd | 2020-09-17 15:38:58 +0200 | [diff] [blame] | 35 | set(TFLU_TARGET "cortex_m_generic") |
Bhavik Patel | f946f6f | 2020-12-11 09:45:40 +0100 | [diff] [blame] | 36 | set(TFLU_TARGET_ARCH ${CMAKE_SYSTEM_PROCESSOR}${CPU_FEATURES} |
| 37 | CACHE STRING "Tensorflow Lite for Microcontrollers target architecture") |
Henrik Hoglind | 3598ef8 | 2020-05-28 12:09:04 +0200 | [diff] [blame] | 38 | set(TFLU_BUILD_TYPE "release" CACHE STRING "Tensorflow Lite Mirco build type, can be release or debug") |
Jonas Skog | 101ccef | 2020-09-10 08:58:16 +0200 | [diff] [blame] | 39 | set(TFLU_OPTIMIZATION_LEVEL CACHE STRING "Tensorflow Lite Micro optimization level") |
Kristofer Jonsson | 1823930 | 2020-04-17 08:45:38 +0200 | [diff] [blame] | 40 | |
Per Åstrand | 94ca355 | 2020-06-30 10:33:58 +0200 | [diff] [blame] | 41 | if(CORE_SOFTWARE_ACCELERATOR STREQUAL NPU) |
| 42 | set(TFLU_ETHOSU_LIBS $<TARGET_FILE:ethosu_core_driver>) |
Måns Nilsson | 98e379c | 2020-09-14 16:30:56 +0200 | [diff] [blame] | 43 | # Set preference for ethos-u over cmsis-nn |
Måns Nilsson | 844d57d | 2021-01-18 11:11:24 +0100 | [diff] [blame] | 44 | set(TFLU_OPTIMIZED_KERNEL_DIR "cmsis_nn") |
| 45 | set(TFLU_CO_PROCESSOR "ethos_u") |
Per Åstrand | 94ca355 | 2020-06-30 10:33:58 +0200 | [diff] [blame] | 46 | elseif(CORE_SOFTWARE_ACCELERATOR STREQUAL CMSIS-NN) |
Måns Nilsson | 844d57d | 2021-01-18 11:11:24 +0100 | [diff] [blame] | 47 | set(TFLU_OPTIMIZED_KERNEL_DIR "cmsis_nn") |
Kristofer Jonsson | 1823930 | 2020-04-17 08:45:38 +0200 | [diff] [blame] | 48 | endif() |
| 49 | |
Kristofer Jonsson | 1823930 | 2020-04-17 08:45:38 +0200 | [diff] [blame] | 50 | # Command and target |
| 51 | add_custom_target(tflu_gen ALL |
Kristofer Jonsson | d273d8e | 2020-10-13 09:35:55 +0200 | [diff] [blame] | 52 | COMMAND make -j${J} -f ${TFLU_PATH}/tools/make/Makefile microlite |
Måns Nilsson | 1aa56dd | 2020-09-17 15:38:58 +0200 | [diff] [blame] | 53 | TARGET_TOOLCHAIN_ROOT=${TFLU_TARGET_TOOLCHAIN_ROOT} |
| 54 | TOOLCHAIN=${TFLU_TOOLCHAIN} |
Kristofer Jonsson | d273d8e | 2020-10-13 09:35:55 +0200 | [diff] [blame] | 55 | GENDIR=${TFLU_GENDIR} |
| 56 | TARGET=${TFLU_TARGET} |
| 57 | TARGET_ARCH=${TFLU_TARGET_ARCH} |
Måns Nilsson | 844d57d | 2021-01-18 11:11:24 +0100 | [diff] [blame] | 58 | OPTIMIZED_KERNEL_DIR="${TFLU_OPTIMIZED_KERNEL_DIR}" |
| 59 | CO_PROCESSOR="${TFLU_CO_PROCESSOR}" |
Kristofer Jonsson | d273d8e | 2020-10-13 09:35:55 +0200 | [diff] [blame] | 60 | $<$<BOOL:${FLOAT}>:FLOAT=${FLOAT}> |
| 61 | BUILD_TYPE=${TFLU_BUILD_TYPE} |
| 62 | $<$<BOOL:${TFLU_OPTIMIZATION_LEVEL}>:OPTIMIZATION_LEVEL=${TFLU_OPTIMIZATION_LEVEL}> |
| 63 | CMSIS_PATH=${CMSIS_PATH} |
| 64 | ETHOSU_DRIVER_PATH=${CORE_DRIVER_PATH} |
| 65 | ETHOSU_DRIVER_LIBS=${TFLU_ETHOSU_LIBS} |
Per Åstrand | 69c6aa9 | 2020-10-12 10:55:52 +0200 | [diff] [blame] | 66 | BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/tensorflow/tensorflow/lite/micro/tools/make/downloads |
Kristofer Jonsson | 1823930 | 2020-04-17 08:45:38 +0200 | [diff] [blame] | 67 | WORKING_DIRECTORY ${TENSORFLOW_PATH}) |
| 68 | |
| 69 | # Create library and link library to custom target |
| 70 | add_library(tflu STATIC IMPORTED) |
| 71 | set_property(TARGET tflu PROPERTY IMPORTED_LOCATION ${TFLU_GENDIR}/lib/libtensorflow-microlite.a) |
| 72 | add_dependencies(tflu tflu_gen) |
| 73 | target_include_directories(tflu INTERFACE ${TENSORFLOW_PATH}) |
Kristofer Jonsson | 6f144be | 2020-11-16 09:21:20 +0100 | [diff] [blame] | 74 | target_compile_definitions(tflu INTERFACE TF_LITE_MICRO TF_LITE_STATIC_MEMORY) |
Bhavik Patel | 50aa51d | 2020-07-27 17:23:31 +0200 | [diff] [blame] | 75 | |
Henrik Hoglind | 78f7f94 | 2020-10-29 10:12:15 +0100 | [diff] [blame] | 76 | if(CORE_SOFTWARE_ACCELERATOR STREQUAL NPU) |
| 77 | target_link_libraries(tflu INTERFACE ethosu_core_driver) |
| 78 | endif() |
| 79 | |
Bhavik Patel | 50aa51d | 2020-07-27 17:23:31 +0200 | [diff] [blame] | 80 | # Install libraries and header files |
| 81 | get_target_property(TFLU_IMPORTED_LOCATION tflu IMPORTED_LOCATION) |
| 82 | install(FILES ${TFLU_IMPORTED_LOCATION} DESTINATION "lib") |