IVGCVSW-6232 Integrate static library libtensorflow-lite.a with ArmNN

Signed-off-by: Keith Davis <keith.davis@arm.com>
Change-Id: Ieb5fa0c4bb6753f0af21cfd80f1bf9faba55f7d0
diff --git a/delegate/CMakeLists.txt b/delegate/CMakeLists.txt
index effb093..aed77bf 100644
--- a/delegate/CMakeLists.txt
+++ b/delegate/CMakeLists.txt
@@ -72,6 +72,12 @@
 
 target_link_libraries(armnnDelegate PUBLIC ${TfLite_LIB})
 
+#  lpthread and ldl are not required for Android
+if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL Android)
+    target_link_libraries(armnnDelegate PUBLIC -lpthread)
+    target_link_libraries(armnnDelegate PUBLIC -ldl)
+endif()
+
 # 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)
diff --git a/delegate/cmake/Modules/FindTfLite.cmake b/delegate/cmake/Modules/FindTfLite.cmake
index 41f55e3..7940e9c 100644
--- a/delegate/cmake/Modules/FindTfLite.cmake
+++ b/delegate/cmake/Modules/FindTfLite.cmake
@@ -6,32 +6,48 @@
 include(FindPackageHandleStandardArgs)
 unset(TFLITE_FOUND)
 
-find_path(TfLite_INCLUDE_DIR
-        NAMES
-            tensorflow/lite
-            third_party
-        HINTS
-            ${TENSORFLOW_ROOT})
+# First look for the static version of tensorflow lite
+find_library(TfLite_LIB NAMES "libtensorflow-lite.a" HINTS ${TFLITE_LIB_ROOT} ${TFLITE_LIB_ROOT}/tensorflow/lite)
+# If not found then, look for the dynamic library of tensorflow lite
+find_library(TfLite_LIB NAMES "libtensorflow_lite_all.so" "libtensorflowlite.so" HINTS ${TFLITE_LIB_ROOT} ${TFLITE_LIB_ROOT}/tensorflow/lite)
 
-find_library(TfLite_LIB
-        NAMES
-            "libtensorflow_lite_all.so"
-            "libtensorflowlite.so"
-        HINTS
-            ${TFLITE_LIB_ROOT}
-            ${TFLITE_LIB_ROOT}/tensorflow/lite)
+# Set relevant paths
+find_path(TfLite_INCLUDE_DIR NAMES tensorflow/lite third_party HINTS ${TENSORFLOW_ROOT})
+find_path(TfLite_Schema_INCLUDE_PATH schema_generated.h HINTS ${TENSORFLOW_ROOT}/tensorflow/lite/schema)
 
-find_path(TfLite_Schema_INCLUDE_PATH
-            schema_generated.h
-        HINTS
-            ${TFLITE_LIB_ROOT}/tensorflow/lite/schema)
+# If the static library was found, gather all of its dependencies
+if (TfLite_LIB MATCHES .a$)
+    message("-- Static tensorflow lite library found, using for ArmNN build")
+    find_library(TfLite_abseilstrings_LIB "libabsl_strings.a"
+                 PATH ${TFLITE_LIB_ROOT}/_deps/abseil-cpp-build/absl/strings)
+    find_library(TfLite_farmhash_LIB "libfarmhash.a"
+                 PATH ${TFLITE_LIB_ROOT}/_deps/farmhash-build)
+    find_library(TfLite_fftsg_LIB "libfft2d_fftsg.a"
+                 PATH ${TFLITE_LIB_ROOT}/_deps/fft2d-build)
+    find_library(TfLite_fftsg2d_LIB "libfft2d_fftsg2d.a"
+                 PATH ${TFLITE_LIB_ROOT}/_deps/fft2d-build)
+    find_library(TfLite_ruy_LIB "libruy.a" PATH
+                 ${TFLITE_LIB_ROOT}/_deps/ruy-build)
+    find_library(TfLite_flatbuffers_LIB "libflatbuffers.a"
+                 PATH ${TFLITE_LIB_ROOT}/_deps/flatbuffers-build)
 
-## Set TFLITE_FOUND
-find_package_handle_standard_args(TfLite DEFAULT_MSG TfLite_INCLUDE_DIR TfLite_LIB TfLite_Schema_INCLUDE_PATH)
-
-## Set external variables for usage in CMakeLists.txt
-if(TFLITE_FOUND)
-    set(TfLite_LIB ${TfLite_LIB})
-    set(TfLite_INCLUDE_DIR ${TfLite_INCLUDE_DIR})
-    set(TfLite_Schema_INCLUDE_PATH ${TfLite_Schema_INCLUDE_PATH})
-endif()
\ No newline at end of file
+    ## Set TFLITE_FOUND if all libraries are satisfied for static lib
+    find_package_handle_standard_args(TfLite DEFAULT_MSG TfLite_INCLUDE_DIR TfLite_LIB TfLite_abseilstrings_LIB TfLite_ruy_LIB TfLite_fftsg_LIB TfLite_fftsg2d_LIB TfLite_farmhash_LIB TfLite_flatbuffers_LIB TfLite_Schema_INCLUDE_PATH)
+    # Set external variables for usage in CMakeLists.txt
+    if (TFLITE_FOUND)
+        set(TfLite_LIB ${TfLite_LIB} ${TfLite_abseilstrings_LIB} ${TfLite_ruy_LIB} ${TfLite_fftsg_LIB} ${TfLite_fftsg2d_LIB} ${TfLite_farmhash_LIB} ${TfLite_flatbuffers_LIB})
+        set(TfLite_INCLUDE_DIR ${TfLite_INCLUDE_DIR})
+        set(TfLite_Schema_INCLUDE_PATH ${TfLite_Schema_INCLUDE_PATH})
+    endif ()
+elseif (TfLite_LIB MATCHES .so$)
+    message("-- Dynamic tensorflow lite library found, using for ArmNN build")
+    find_package_handle_standard_args(TfLite DEFAULT_MSG TfLite_INCLUDE_DIR TfLite_LIB TfLite_Schema_INCLUDE_PATH)
+    ## Set external variables for usage in CMakeLists.txt
+    if (TFLITE_FOUND)
+        set(TfLite_LIB ${TfLite_LIB})
+        set(TfLite_INCLUDE_DIR ${TfLite_INCLUDE_DIR})
+        set(TfLite_Schema_INCLUDE_PATH ${TfLite_Schema_INCLUDE_PATH})
+    endif ()
+else()
+    message(FATAL_ERROR "Could not find a tensorflow lite library to use")
+endif()