MLECO-2492 Add CPP OD example with TFLITE-ArmnnDelegate

Signed-off-by: Dvir Markovich <dvir.markovich@arm.com>
Change-Id: If412c15ba49abe8370a570260b0a8ed8de305b7c
diff --git a/samples/ObjectDetection/CMakeLists.txt b/samples/ObjectDetection/CMakeLists.txt
index dbcd55f..953c4ed 100644
--- a/samples/ObjectDetection/CMakeLists.txt
+++ b/samples/ObjectDetection/CMakeLists.txt
@@ -2,9 +2,12 @@
 # SPDX-License-Identifier: MIT
 
 cmake_minimum_required(VERSION 3.0.2)
+project (object_detection_example)
 
 set(CMAKE_C_STANDARD                99)
 set(CMAKE_CXX_STANDARD              14)
+#location of FindTfLite.cmake and FindTfLiteSrc.cmake
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/../../delegate/cmake/Modules/")
 
 # Make the standard a requirement => prevent fallback to previous
 # supported standard
@@ -15,14 +18,15 @@
 set(CMAKE_C_EXTENSIONS              OFF)
 set(CMAKE_CXX_EXTENSIONS            OFF)
 
-project (object_detection_example)
-
 set(CMAKE_C_FLAGS_DEBUG         "-DDEBUG -O0 -g -fPIC")
 set(CMAKE_C_FLAGS_RELEASE       "-DNDEBUG -O3 -fPIC")
 
 set(CMAKE_CXX_FLAGS_DEBUG       "-DDEBUG -O0 -g -fPIC")
 set(CMAKE_CXX_FLAGS_RELEASE     "-DNDEBUG -O3 -fPIC")
 
+SET(USE_ARMNN_DELEGATE False CACHE BOOL "Use delegate file")
+message("USE_ARMNN_DELEGATE=${USE_ARMNN_DELEGATE}")
+
 include(ExternalProject)
 
 # Build in release mode by default
@@ -40,9 +44,23 @@
 
 include(../common/cmake/find_opencv.cmake)
 include(../common/cmake/find_armnn.cmake)
+if( USE_ARMNN_DELEGATE )
+    ## Add TfLite dependency
+    find_package(TfLiteSrc REQUIRED MODULE)
+    find_package(TfLite REQUIRED MODULE)
+    ## Add Flatbuffers dependency
+    find_package(Flatbuffers REQUIRED MODULE)
+
+    add_definitions(-DUSE_TF_LITE_DELEGATE)
+endif()
 
 include_directories(include)
-include_directories(../common/include/ArmnnUtils)
+## chose the correct instance of ArmnnNetworkExecutor.hpp
+if( USE_ARMNN_DELEGATE )
+    include_directories(include/delegate)
+else()
+    include_directories(../common/include/ArmnnUtils)
+endif()
 include_directories(../common/include/Utils)
 include_directories(../common/include/CVUtils)
 
@@ -50,7 +68,22 @@
 file(GLOB CVUTILS_SOURCES "../common/src/CVUtils**/*.cpp")
 file(GLOB UTILS_SOURCES "../common/src/Utils**/*.cpp")
 list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/Main.cpp)
-file(GLOB TEST_SOURCES "test/*.cpp")
+if( USE_ARMNN_DELEGATE )
+    file(GLOB TEST_SOURCES "test/delegate/*.cpp" "test/*.cpp")
+
+    # Various tflite header files are not warning clean
+    # We can't change compilation flags on header files directly, so we need to add them to an interface library first
+    add_library(tflite_headers INTERFACE)
+    target_include_directories(tflite_headers INTERFACE $<BUILD_INTERFACE:${TfLite_INCLUDE_DIR}>
+                                                    $<INSTALL_INTERFACE:include/tflite_headers>)
+
+    target_compile_options(tflite_headers INTERFACE -Wno-conversion
+                                                    -Wno-sign-conversion
+                                                    -Wno-unused-parameter
+                                                    -Wno-unused-function)
+else()
+    file(GLOB TEST_SOURCES "test/*.cpp")
+endif()
 file(GLOB APP_MAIN "src/Main.cpp")
 
 if(BUILD_UNIT_TESTS)
@@ -62,6 +95,15 @@
 
 add_executable("${APP_TARGET_NAME}" ${SOURCES} ${CVUTILS_SOURCES} ${UTILS_SOURCES} ${APP_MAIN})
 
+if( USE_ARMNN_DELEGATE )
+    set(CMAKE_CXX_FLAGS " -ldl -lrt -Wl,--copy-dt-needed-entries")
+    target_link_libraries("${APP_TARGET_NAME}" PUBLIC ${TfLite_LIB})
+
+    target_link_libraries("${APP_TARGET_NAME}" PUBLIC tflite_headers)
+    target_include_directories("${APP_TARGET_NAME}" PUBLIC ${Flatbuffers_INCLUDE_DIR})
+    target_link_libraries("${APP_TARGET_NAME}" PUBLIC ${Flatbuffers_LIB})
+endif()
+
 if (NOT OPENCV_LIBS_FOUND)
     message("Building OpenCV libs")
     add_dependencies("${APP_TARGET_NAME}" "${OPENCV_LIB}")