Export Armnn cmake

Change-Id: I3c317e835aba5feafda240393ca26b6aebe8d655
Signed-off-by: Derek Lamberti <derek.lamberti@arm.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9c2f331..3eb2e78 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -567,11 +567,17 @@
 
 target_compile_definitions(armnn PRIVATE "ARMNN_COMPILING_DLL")
 
-target_include_directories(armnn PRIVATE src/armnn)
-target_include_directories(armnn PRIVATE src/armnnUtils)
-target_include_directories(armnn PRIVATE src/backends)
-target_include_directories(armnn PRIVATE src/profiling)
-target_include_directories(armnn PRIVATE profiling/common/include)
+target_include_directories(armnn
+    PUBLIC
+        $<INSTALL_INTERFACE:include>
+        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
+    PRIVATE
+        src/armnn
+        src/armnnUtils
+        src/backends
+        src/profiling
+        profiling/common/include
+)
 
 target_link_libraries(armnn armnnUtils pipeCommon)
 
@@ -580,9 +586,6 @@
     target_link_libraries(armnn Ws2_32.lib)
 endif()
 
-install(TARGETS armnn
-        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
-        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
 if(BUILD_CAFFE_PARSER)
     install(TARGETS armnnCaffeParser
             LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
@@ -601,9 +604,11 @@
 
 install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
 
-target_link_libraries(armnn ${Boost_THREAD_LIBRARY}
-                            ${Boost_FILESYSTEM_LIBRARY}
-                            ${Boost_SYSTEM_LIBRARY})
+target_link_libraries(armnn
+        ${Boost_THREAD_LIBRARY}
+        ${Boost_SYSTEM_LIBRARY}
+        ${Boost_FILESYSTEM_LIBRARY}
+    )
 
 if(ARMCOMPUTENEON OR ARMCOMPUTECL)
     target_link_libraries(armnn ${ARMCOMPUTE_LIBRARIES})
@@ -1027,7 +1032,7 @@
         ${Boost_PROGRAM_OPTIONS_LIBRARY}
         ${Boost_FILESYSTEM_LIBRARY})
     addDllCopyCommands(ArmnnConverter)
-  endif()
+endif()
 
 if(BUILD_BASE_PIPE_SERVER)
     add_subdirectory(profiling/server/src/basePipeServer)
@@ -1101,6 +1106,67 @@
 
 endif()
 
+####################################################
+# export targets
+set(armnn_export_targets)
+list(APPEND armnn_export_targets
+    armnn
+    armnnUtils
+)
+
+install(
+    TARGETS ${armnn_export_targets}
+    EXPORT  armnn-targets
+    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
+)
+
+####################################################
+## Set export alias
+set_target_properties(armnn
+    PROPERTIES
+    EXPORT_NAME Armnn
+)
+
+## Export target scrips
+install(
+    EXPORT      armnn-targets
+    FILE        ArmnnTargets.cmake
+    NAMESPACE   Armnn::
+    DESTINATION ${CMAKE_INSTALL_LIBDIR}
+)
+
+####################################################
+## Create ArmnnConfig.cmake
+include(CMakePackageConfigHelpers)
+set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR})
+
+message(STATUS "CMAKE_CURRENT_LIST_DIR ${CMAKE_CURRENT_LIST_DIR}" )
+message(STATUS "CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}" )
+configure_package_config_file(
+    ${CMAKE_CURRENT_LIST_DIR}/cmake/ArmnnConfig.cmake.in
+    ${CMAKE_CURRENT_BINARY_DIR}/ArmnnConfig.cmake
+    INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
+)
+
+####################################################
+## Install Armnn config file
+install(
+    FILES
+    ${CMAKE_CURRENT_BINARY_DIR}/ArmnnConfig.cmake
+    DESTINATION ${INSTALL_CONFIGDIR}
+)
+
+####################################################
+## Export from build tree
+export(
+    EXPORT      armnn-targets
+    FILE        ${CMAKE_CURRENT_BINARY_DIR}/ArmnnTargets.cmake
+    NAMESPACE   Armnn::
+)
+
+####################################################
+## Build Python bindings
 if (BUILD_PYTHON_WHL OR BUILD_PYTHON_SRC)
     add_subdirectory(python/pyarmnn)
-endif()
+endif()
\ No newline at end of file
diff --git a/cmake/ArmnnConfig.cmake.in b/cmake/ArmnnConfig.cmake.in
new file mode 100644
index 0000000..1e572fe
--- /dev/null
+++ b/cmake/ArmnnConfig.cmake.in
@@ -0,0 +1,20 @@
+
+get_filename_component(ARMNN_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" DIRECTORY)
+set(ARMNN_CONFIG_FILE ${CMAKE_CURRENT_LIST_FILE})
+MESSAGE(STATUS "Found Armnn: ${ARMNN_CONFIG_FILE}")
+
+include(CMakeFindDependencyMacro)
+
+list(APPEND CMAKE_MODULE_PATH ${ARMNN_CMAKE_DIR})
+
+add_definitions("-DBOOST_ALL_NO_LIB")
+find_package(Boost 1.59 REQUIRED COMPONENTS system filesystem program_options)
+include_directories(SYSTEM "${Boost_INCLUDE_DIRS}")
+link_directories(${Boost_LIBRARY_DIRS})
+
+if(NOT TARGET Armnn::Armnn)
+    MESSAGE(STATUS "Armnn Import: ${ARMNN_CMAKE_DIR}/ArmnnTargets.cmake")
+    include("${ARMNN_CMAKE_DIR}/ArmnnTargets.cmake")
+endif()
+
+set(ARMNN_LIBRARIES Armnn::Armnn)
\ No newline at end of file
diff --git a/cmake/GlobalConfig.cmake b/cmake/GlobalConfig.cmake
index b093e5d..cc44efd 100644
--- a/cmake/GlobalConfig.cmake
+++ b/cmake/GlobalConfig.cmake
@@ -123,6 +123,8 @@
 
 set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
 
+include(CMakeFindDependencyMacro)
+
 if (NOT BUILD_PIPE_ONLY)
   # Boost
   message(STATUS "Finding Boost")
@@ -151,7 +153,7 @@
 endif()
 
 # pthread
-find_package (Threads)
+find_dependency(Threads)
 
 # Favour the protobuf passed on command line
 if(BUILD_TF_PARSER OR BUILD_CAFFE_PARSER OR BUILD_ONNX_PARSER)
diff --git a/profiling/common/src/CMakeLists.txt b/profiling/common/src/CMakeLists.txt
index 9505fb8..3a27a53 100644
--- a/profiling/common/src/CMakeLists.txt
+++ b/profiling/common/src/CMakeLists.txt
@@ -27,4 +27,11 @@
     # to simplify the build. No extra .so file to deploy to boards etc.
     add_library_ex(pipeCommon STATIC ${pipeCommon_sources})
 
+    # install the target
+    install(
+        TARGETS pipeCommon
+        EXPORT  armnn-targets
+        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
+    )
 endif()
diff --git a/src/armnnSerializer/CMakeLists.txt b/src/armnnSerializer/CMakeLists.txt
index ccc924e..9c90af2 100755
--- a/src/armnnSerializer/CMakeLists.txt
+++ b/src/armnnSerializer/CMakeLists.txt
@@ -42,6 +42,9 @@
     target_link_libraries(armnnSerializer armnn ${FLATBUFFERS_LIBRARY})
 
     install(TARGETS armnnSerializer
-            LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
+            EXPORT  armnn-targets
+            LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+            ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
+    )
     set_target_properties(armnnSerializer PROPERTIES VERSION ${GENERIC_LIB_VERSION} SOVERSION ${GENERIC_LIB_SOVERSION} )
 endif()
diff --git a/src/backends/reference/RefLayerSupport.cpp b/src/backends/reference/RefLayerSupport.cpp
index f6c214e..5e3c96d 100644
--- a/src/backends/reference/RefLayerSupport.cpp
+++ b/src/backends/reference/RefLayerSupport.cpp
@@ -1916,7 +1916,7 @@
 
     supported &= CheckSupportRule(TypeAnyOf(input, supportedTypes), reasonIfUnsupported,
                                   "Reference splitter: output type not supported");
-    for (const TensorInfo output : outputs)
+    for (const TensorInfo& output : outputs)
     {
         supported &= CheckSupportRule(TypeAnyOf(input, supportedTypes), reasonIfUnsupported,
                                       "Reference splitter: input type not supported");
diff --git a/src/profiling/PeriodicCounterCapture.cpp b/src/profiling/PeriodicCounterCapture.cpp
index 8ff5e7e..d914e85 100644
--- a/src/profiling/PeriodicCounterCapture.cpp
+++ b/src/profiling/PeriodicCounterCapture.cpp
@@ -59,7 +59,7 @@
     const armnn::BackendId& backendId, const std::vector<Timestamp>& timestampValues)
 {
     // Report counter values
-    for (const auto timestampInfo : timestampValues)
+    for (const auto& timestampInfo : timestampValues)
     {
         std::vector<CounterValue> backendCounterValues = timestampInfo.counterValues;
         for_each(backendCounterValues.begin(), backendCounterValues.end(), [&](CounterValue& backendCounterValue)
diff --git a/src/profiling/backends/BackendProfiling.cpp b/src/profiling/backends/BackendProfiling.cpp
index 4e6de93..135174b 100644
--- a/src/profiling/backends/BackendProfiling.cpp
+++ b/src/profiling/backends/BackendProfiling.cpp
@@ -32,7 +32,7 @@
 
 void BackendProfiling::ReportCounters(const std::vector<Timestamp>& timestamps)
 {
-    for (const auto timestampInfo : timestamps)
+    for (const auto& timestampInfo : timestamps)
     {
         std::vector<CounterValue> backendCounterValues = timestampInfo.counterValues;
         for_each(backendCounterValues.begin(), backendCounterValues.end(), [&](CounterValue& backendCounterValue)