Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / infra / nnfw / cmake / packages / ARMComputeConfig.cmake
1 function(_ARMCompute_Import)
2   include(FindPackageHandleStandardArgs)
3
4   list(APPEND ARMCompute_LIB_SEARCH_PATHS ${ARMCompute_PREFIX})
5
6   find_path(INCLUDE_DIR NAMES arm_compute/core/ITensor.h PATHS ${ARMCompute_INCLUDE_SEARCH_PATHS})
7
8   find_library(CORE_LIBRARY NAMES        arm_compute_core  PATHS ${ARMCompute_LIB_SEARCH_PATHS} CMAKE_FIND_ROOT_PATH_BOTH)
9   find_library(RUNTIME_LIBRARY NAMES arm_compute       PATHS ${ARMCompute_LIB_SEARCH_PATHS} CMAKE_FIND_ROOT_PATH_BOTH)
10   find_library(GRAPH_LIBRARY NAMES   arm_compute_graph PATHS ${ARMCompute_LIB_SEARCH_PATHS} CMAKE_FIND_ROOT_PATH_BOTH)
11
12   message(STATUS "Search acl in ${ARMCompute_LIB_SEARCH_PATHS}")
13
14   if(NOT INCLUDE_DIR)
15     nnas_find_package(ARMComputeSource QUIET)
16     if (NOT ARMComputeSource_FOUND)
17       set(ARMCompute_FOUND FALSE PARENT_SCOPE)
18       return()
19     endif()
20     set(INCLUDE_DIR ${ARMComputeSource_DIR} ${ARMComputeSource_DIR}/include)
21   endif(NOT INCLUDE_DIR)
22
23   if(NOT CORE_LIBRARY)
24     set(ARMCompute_FOUND FALSE PARENT_SCOPE)
25     message(STATUS "Cannot find libarm_compute_core.so")
26     return()
27   endif()
28
29   if(NOT RUNTIME_LIBRARY)
30     message(STATUS "Cannot find libarm_compute.so")
31     set(ARMCompute_FOUND FALSE PARENT_SCOPE)
32     return()
33   endif()
34
35   if(NOT GRAPH_LIBRARY)
36     message(STATUS "Cannot find libarm_compute_graph.so")
37     set(ARMCompute_FOUND FALSE PARENT_SCOPE)
38     return()
39   endif()
40
41   if(NOT TARGET arm_compute_core)
42     add_library(arm_compute_core INTERFACE)
43     target_include_directories(arm_compute_core SYSTEM INTERFACE ${INCLUDE_DIR})
44         target_link_libraries(arm_compute_core INTERFACE dl ${LIB_PTHREAD})
45     target_link_libraries(arm_compute_core INTERFACE ${CORE_LIBRARY})
46   endif(NOT TARGET arm_compute_core)
47
48   if(NOT TARGET arm_compute)
49     add_library(arm_compute INTERFACE)
50     target_include_directories(arm_compute SYSTEM INTERFACE ${INCLUDE_DIR})
51     target_link_libraries(arm_compute INTERFACE ${RUNTIME_LIBRARY})
52     target_link_libraries(arm_compute INTERFACE arm_compute_core)
53   endif(NOT TARGET arm_compute)
54
55   if(NOT TARGET arm_compute_graph)
56     add_library(arm_compute_graph INTERFACE)
57     target_include_directories(arm_compute_graph SYSTEM INTERFACE ${INCLUDE_DIR})
58     target_link_libraries(arm_compute_graph INTERFACE ${GRAPH_LIBRARY})
59     target_link_libraries(arm_compute_graph INTERFACE arm_compute)
60   endif(NOT TARGET arm_compute_graph)
61
62   set(ARMCompute_FOUND TRUE PARENT_SCOPE)
63 endfunction(_ARMCompute_Import)
64
65 ### Check whether library exists
66 function(_ARMCompute_Check VAR LIBDIR)
67   set(FOUND TRUE)
68
69   if(NOT EXISTS "${LIBDIR}/libarm_compute_core.so")
70     set(FOUND FALSE)
71   endif()
72
73   if(NOT EXISTS "${LIBDIR}/libarm_compute.so")
74     set(FOUND FALSE)
75   endif()
76
77   if(NOT EXISTS "${LIBDIR}/libarm_compute_graph.so")
78     set(FOUND FALSE)
79   endif()
80
81   set(${VAR} ${FOUND} PARENT_SCOPE)
82 endfunction(_ARMCompute_Check)
83
84 # Let's build and install ARMCompute libraries
85 # NOTE This function silently returns on error
86 function(_ARMCompute_Build ARMCompute_INSTALL_PREFIX)
87   ### Check whether library exists
88   _ARMCompute_Check(ARMCompute_FOUND ${ARMCompute_INSTALL_PREFIX})
89
90   if(ARMCompute_FOUND)
91     return()
92   endif(ARMCompute_FOUND)
93
94   ### Let's build with SCONS
95   nnas_find_package(ARMComputeSource QUIET)
96
97   if(NOT ARMComputeSource_FOUND)
98     return()
99   endif(NOT ARMComputeSource_FOUND)
100
101   find_program(SCONS_PATH scons)
102
103   if(NOT SCONS_PATH)
104     message(WARNING "SCONS NOT FOUND. Please install SCONS to build ARMCompute.")
105     return()
106   endif(NOT SCONS_PATH)
107
108   if(CMAKE_BUILD_TYPE)
109     string(TOLOWER "${CMAKE_BUILD_TYPE}" SCON_BUILD_TYPE)
110   else(CMAKE_BUILD_TYPE)
111     set(SCON_BUILD_TYPE "release")
112   endif(CMAKE_BUILD_TYPE)
113
114   #### Architecture-specific configurations
115   if(TARGET_ARCH STREQUAL "armv7l")
116     set(BUILD_ARCH "armv7a")
117     set(BUILD_DIR "${BUILD_ARCH}-${TARGET_OS}.${SCON_BUILD_TYPE}")
118   endif()
119
120   if(TARGET_ARCH STREQUAL "aarch64")
121     set(BUILD_ARCH "arm64-v8a")
122     set(BUILD_DIR "${BUILD_ARCH}-${TARGET_OS}.${SCON_BUILD_TYPE}")
123   endif()
124
125   #### Platform-specific configurations
126   #### TODO Support android
127
128   #### Mode-specific configurations
129   if(SCON_BUILD_TYPE STREQUAL "debug")
130     list(APPEND SCONS_OPTIONS "debug=1")
131   endif()
132
133   #### Generic configurations
134   list(APPEND SCONS_OPTIONS "neon=1")
135   list(APPEND SCONS_OPTIONS "opencl=1")
136   list(APPEND SCONS_OPTIONS "examples=0")
137   list(APPEND SCONS_OPTIONS "Werror=0")
138   list(APPEND SCONS_OPTIONS "os=${TARGET_OS}")
139
140   if(DEFINED ACL_BUILD_THREADS)
141     set(N ${ACL_BUILD_THREADS})
142   else(DEFINED ACL_BUILD_THREADS)
143     include(ProcessorCount)
144     ProcessorCount(N)
145   endif(DEFINED ACL_BUILD_THREADS)
146
147   if((NOT N EQUAL 0) AND BUILD_EXT_MULTITHREAD)
148     list(APPEND SCONS_OPTIONS -j${N})
149   endif()
150   if(DEFINED BUILD_ARCH)
151     list(APPEND SCONS_OPTIONS "arch=${BUILD_ARCH}")
152   endif(DEFINED BUILD_ARCH)
153
154   if(DEFINED BUILD_DIR)
155     list(APPEND SCONS_OPTIONS "build_dir=${BUILD_DIR}")
156   endif(DEFINED BUILD_DIR)
157
158   message(STATUS "Build ARMCompute with ${SCONS_PATH} ('${SCONS_OPTIONS}'")
159
160   # Build ARMCompute libraries with SCONS
161   # NOTE ARMCompute SConstruct unconditioanlly appends "arm-linux-gnueabihf-" prefix for linux
162   execute_process(COMMAND /usr/bin/env CC=gcc CXX=g++ "${SCONS_PATH}" ${SCONS_OPTIONS}
163                   WORKING_DIRECTORY ${ARMComputeSource_DIR}
164                   RESULT_VARIABLE ARMCompute_BUILD)
165
166   # Install ARMCompute libraries to overlay
167   execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory "${ARMCompute_INSTALL_PREFIX}"
168                   WORKING_DIRECTORY ${ARMComputeSource_DIR}
169                   RESULT_VARIABLE ARMCompute_BUILD)
170   execute_process(COMMAND ${CMAKE_COMMAND} -E copy "build/${BUILD_DIR}/libarm_compute_core.so" "${ARMCompute_INSTALL_PREFIX}"
171                   COMMAND ${CMAKE_COMMAND} -E copy "build/${BUILD_DIR}/libarm_compute.so" "${ARMCompute_INSTALL_PREFIX}"
172                   COMMAND ${CMAKE_COMMAND} -E copy "build/${BUILD_DIR}/libarm_compute_graph.so" "${ARMCompute_INSTALL_PREFIX}"
173                   WORKING_DIRECTORY ${ARMComputeSource_DIR}
174                   RESULT_VARIABLE ARMCompute_BUILD)
175 endfunction(_ARMCompute_Build)
176
177 set(ARMCompute_PREFIX ${EXT_OVERLAY_DIR}/lib)
178 if(BUILD_ARMCOMPUTE)
179   _ARMCompute_Build("${ARMCompute_PREFIX}")
180 endif(BUILD_ARMCOMPUTE)
181 _ARMCompute_Import()