xref: /aosp_15_r20/external/armnn/delegate/CMakeLists.txt (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1#
2# Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved.
3# SPDX-License-Identifier: MIT
4#
5
6cmake_minimum_required (VERSION 3.7.0)
7project(armnnDelegate)
8set(CMAKE_CXX_STANDARD 17)
9set(CMAKE_CXX_STANDARD_REQUIRED ON)
10set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -Wextra -Werror -Wold-style-cast -Wno-missing-braces -Wconversion -Wsign-conversion -Wno-comment")
11set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/Modules/")
12
13option(BUILD_UNIT_TESTS "Build unit tests" ON)
14option(BUILD_CLASSIC_DELEGATE "Build classic delegate" ON)
15option(BUILD_OPAQUE_DELEGATE "Build opaque delegate" OFF)
16option(BUILD_SHARED_LIBS "Build share libs" ON)
17option(BUILD_DELEGATE_JNI_INTERFACE "Builds a library to allow accessing the Arm NN delegate from Java code.
18                                     This is an experimental feature." ON)
19
20set(armnnDelegate_sources)
21list(APPEND armnnDelegate_sources
22        common/include/DelegateOptions.hpp
23        common/src/DelegateOptions.cpp
24        common/src/DelegateUtils.hpp)
25
26## Add Armnn as a Dependency
27if(NOT ARMNN_SUB_PROJECT)
28    find_package(Armnn REQUIRED CONFIG HINTS ${Armnn_DIR})
29endif()
30
31if (BUILD_CLASSIC_DELEGATE)
32    add_subdirectory(classic)
33    add_library(armnnDelegate ${armnnDelegate_sources})
34
35    target_include_directories(armnnDelegate
36            PUBLIC
37                $<INSTALL_INTERFACE:include>
38                $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/common/include>
39            PRIVATE
40                ${CMAKE_CURRENT_SOURCE_DIR}/common/src)
41endif()
42if (BUILD_OPAQUE_DELEGATE)
43    add_subdirectory(opaque)
44    add_library(armnnOpaqueDelegate ${armnnDelegate_sources})
45
46    target_include_directories(armnnOpaqueDelegate
47            PUBLIC
48                $<INSTALL_INTERFACE:include>
49                $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/common/include>
50            PRIVATE
51                ${CMAKE_CURRENT_SOURCE_DIR}/common/src)
52endif()
53
54include(GNUInstallDirs)
55
56if (BUILD_CLASSIC_DELEGATE)
57    target_link_libraries(armnnDelegate PUBLIC Armnn::Armnn)
58
59    ## Add armnnClassicDelegateObject as a Dependency
60    target_link_libraries(armnnDelegate PUBLIC armnnClassicDelegateObject)
61endif()
62if (BUILD_OPAQUE_DELEGATE)
63    target_link_libraries(armnnOpaqueDelegate PUBLIC Armnn::Armnn)
64
65    ## Add armnnOpaqueDelegateObject as a Dependency
66    target_link_libraries(armnnOpaqueDelegate PUBLIC armnnOpaqueDelegateObject)
67endif()
68
69## Add TfLite dependency
70find_package(TfLiteSrc REQUIRED MODULE)
71find_package(TfLite REQUIRED MODULE)
72if (BUILD_CLASSIC_DELEGATE)
73    target_link_libraries(armnnDelegate PUBLIC ${TfLite_LIB})
74
75    #  lpthread and ldl are not required for Android
76    if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL Android)
77        target_link_libraries(armnnDelegate PUBLIC -lpthread)
78        target_link_libraries(armnnDelegate PUBLIC -ldl)
79    endif()
80endif()
81if (BUILD_OPAQUE_DELEGATE)
82    target_link_libraries(armnnOpaqueDelegate PUBLIC ${TfLite_LIB})
83
84    #  lpthread and ldl are not required for Android
85    if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL Android)
86        target_link_libraries(armnnOpaqueDelegate PUBLIC -lpthread)
87        target_link_libraries(armnnOpaqueDelegate PUBLIC -ldl)
88    endif()
89endif()
90
91# Add libraries from armnn third-party libraries
92# Third-party header files are not warning clean
93# We can't change compilation flags on header files directly, so we need to add them to an interface library first
94add_library(thirdparty_headers INTERFACE)
95target_include_directories(thirdparty_headers INTERFACE $<BUILD_INTERFACE:${ARMNN_SOURCE_DIR}/third-party>
96                                                        $<INSTALL_INTERFACE:include/thirdparty_headers>)
97
98target_compile_options(thirdparty_headers INTERFACE -Wno-old-style-cast)
99if (BUILD_CLASSIC_DELEGATE)
100    target_link_libraries(armnnDelegate PUBLIC thirdparty_headers)
101endif()
102if (BUILD_OPAQUE_DELEGATE)
103    target_link_libraries(armnnOpaqueDelegate PUBLIC thirdparty_headers)
104endif()
105
106add_library(profiling_library_headers INTERFACE)
107target_include_directories(profiling_library_headers INTERFACE $<BUILD_INTERFACE:${ARMNN_SOURCE_DIR}/profiling>
108                                                              $<INSTALL_INTERFACE:include/profiling_library_headers>)
109
110if (BUILD_CLASSIC_DELEGATE)
111    target_link_libraries(armnnDelegate PUBLIC profiling_library_headers)
112    target_link_libraries(armnnDelegate PUBLIC Armnn::armnnUtils)
113
114    set_target_properties(armnnDelegate PROPERTIES VERSION ${DELEGATE_LIB_VERSION} SOVERSION ${DELEGATE_LIB_SOVERSION})
115endif()
116if (BUILD_OPAQUE_DELEGATE)
117    target_link_libraries(armnnOpaqueDelegate PUBLIC profiling_library_headers)
118    target_link_libraries(armnnOpaqueDelegate PUBLIC Armnn::armnnUtils)
119
120    set_target_properties(armnnOpaqueDelegate PROPERTIES VERSION ${OPAQUE_DELEGATE_LIB_VERSION} SOVERSION ${OPAQUE_DELEGATE_LIB_SOVERSION})
121endif()
122
123if(BUILD_UNIT_TESTS)
124    set(commonDelegate_unittest_sources)
125    list(APPEND commonDelegate_unittest_sources
126        common/src/test/DelegateTestInterpreter.hpp
127        common/src/test/DelegateTestInterpreterUtils.hpp
128        test/ActivationTest.cpp
129        test/ActivationTestHelper.hpp
130        test/ArgMinMaxTest.cpp
131        test/ArgMinMaxTestHelper.hpp
132        test/BatchMatMulTest.cpp
133        test/BatchMatMulTestHelper.hpp
134        test/BatchSpaceTest.cpp
135        test/BatchSpaceTestHelper.hpp
136        test/CastTest.cpp
137        test/CastTestHelper.hpp
138        test/ComparisonTest.cpp
139        test/ComparisonTestHelper.hpp
140        test/ControlTest.cpp
141        test/ControlTestHelper.hpp
142        test/Convolution2dTest.cpp
143        test/Convolution3dTest.cpp
144        test/ConvolutionTestHelper.hpp
145        test/DelegateOptionsTest.cpp
146        test/DelegateOptionsTestHelper.hpp
147        test/DepthwiseConvolution2dTest.cpp
148        test/ElementwiseBinaryTest.cpp
149        test/ElementwiseBinaryTestHelper.hpp
150        test/ElementwiseUnaryTest.cpp
151        test/ElementwiseUnaryTestHelper.hpp
152        test/FillTest.cpp
153        test/FillTestHelper.hpp
154        test/FullyConnectedTest.cpp
155        test/FullyConnectedTestHelper.hpp
156        test/GatherTest.cpp
157        test/GatherTestHelper.hpp
158        test/GatherNdTest.cpp
159        test/GatherNdTestHelper.hpp
160        test/LogicalTest.cpp
161        test/LogicalTestHelper.hpp
162        test/LstmTest.cpp
163        test/LstmTestHelper.hpp
164        test/MirrorPadTest.cpp
165        test/NormalizationTest.cpp
166        test/NormalizationTestHelper.hpp
167        test/PackTest.cpp
168        test/PackTestHelper.hpp
169        test/PadTest.cpp
170        test/PadTestHelper.hpp
171        test/Pooling2dTest.cpp
172        test/Pooling2dTestHelper.hpp
173        test/Pooling3dTest.cpp
174        test/Pooling3dTestHelper.hpp
175        test/PreluTest.cpp
176        test/PreluTestHelper.hpp
177        test/QuantizationTest.cpp
178        test/QuantizationTestHelper.hpp
179        test/RedefineTestHelper.hpp
180        test/ReduceTest.cpp
181        test/ReduceTestHelper.hpp
182        test/ReshapeTest.cpp
183        test/ResizeTest.cpp
184        test/ResizeTestHelper.hpp
185        test/RoundTest.cpp
186        test/RoundTestHelper.hpp
187        test/SoftmaxTest.cpp
188        test/SoftmaxTestHelper.hpp
189        test/SpaceDepthTest.cpp
190        test/SpaceDepthTestHelper.hpp
191        test/ShapeTest.cpp
192        test/ShapeTestHelper.hpp
193        test/SliceTest.cpp
194        test/SliceTestHelper.hpp
195        test/StridedSliceTest.cpp
196        test/StridedSliceTestHelper.hpp
197        test/SplitTest.cpp
198        test/SplitTestHelper.hpp
199        test/TestUtils.hpp
200        test/TestUtils.cpp
201        test/TransposeTest.cpp
202        test/TransposeTestHelper.hpp
203        test/UnidirectionalSequenceLstmTest.cpp
204        test/UnidirectionalSequenceLstmTestHelper.hpp
205        test/UnpackTest.cpp
206        test/UnpackTestHelper.hpp)
207
208    # There's a known Android NDK bug which causes a subset of NeonLayerTests to
209    # fail. We'll exclude these tests in NeonLayerTests_NDK_Bug.cpp if we're doing
210    # a debug build and NDK is less than r21.
211    # https://github.com/android/ndk/issues/1135
212
213    # Default to always including these tests.
214    set(INCLUDE_NDK_BUG_TESTS "ON")
215    # Reconsider if we in a debug build.
216    string( TOLOWER ${CMAKE_BUILD_TYPE} BUILD_TYPE_LOWERCASE )
217    if ( NOT BUILD_TYPE_LOWERCASE STREQUAL "release" )
218        message("CMAKE:: BUILD TYPE IS ${CMAKE_BUILD_TYPE}")
219        # And NDK_VERSION has been set.
220        if ( DEFINED NDK_VERSION )
221            message("CMAKE:: NDK DEFINED")
222            # And the version is less than r21.
223            if ( ${NDK_VERSION} STRLESS "r21" )
224                message("CMAKE:: BUG TESTS OFF")
225                set(INCLUDE_NDK_BUG_TESTS "OFF")
226            endif()
227        endif()
228    endif()
229
230    if ( INCLUDE_NDK_BUG_TESTS STREQUAL "ON" )
231        list(APPEND commonDelegate_unittest_sources
232             test/NeonDelegateTests_NDK_Issue.cpp)
233    endif()
234
235    if (BUILD_CLASSIC_DELEGATE)
236        set(classicDelegate_unittest_sources)
237        list(APPEND classicDelegate_unittest_sources
238             classic/src/test/ArmnnClassicDelegateTest.cpp
239             classic/src/test/DelegateTestInterpreter.cpp)
240
241        add_executable(DelegateUnitTests ${commonDelegate_unittest_sources} ${classicDelegate_unittest_sources})
242
243        target_include_directories(DelegateUnitTests SYSTEM PRIVATE "${TF_LITE_SCHEMA_INCLUDE_PATH}")
244        target_include_directories(DelegateUnitTests SYSTEM PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/common/src/test")
245        target_include_directories(DelegateUnitTests PUBLIC ${PROJECT_SOURCE_DIR})
246
247        # Add half library from armnn third-party libraries
248        target_link_libraries(DelegateUnitTests PRIVATE thirdparty_headers)
249        target_link_libraries(DelegateUnitTests PRIVATE armnnDelegate)
250        target_link_libraries(DelegateUnitTests PRIVATE Armnn::armnnUtils)
251        target_link_libraries(DelegateUnitTests PRIVATE profiling_library_headers)
252    endif()
253
254    if (BUILD_OPAQUE_DELEGATE)
255        set(opaqueDelegate_unittest_sources)
256        list(APPEND opaqueDelegate_unittest_sources
257             common/src/test/DelegateTestInterpreter.hpp
258             common/src/test/DelegateTestInterpreterUtils.hpp
259             opaque/src/test/ArmnnOpaqueDelegateTest.cpp
260             opaque/src/test/DelegateTestInterpreter.cpp
261             test/TestUtils.hpp
262             test/TestUtils.cpp
263             test/CastTest.cpp
264             test/CastTestHelper.hpp)
265
266        # Until all operators are supported, we have to add tests one by one above to opaqueDelegate_unittest_sources.
267        # After we add can add commonDelegate_unittest_sources to the add_executable below.
268        add_executable(OpaqueDelegateUnitTests ${opaqueDelegate_unittest_sources})
269
270        target_include_directories(OpaqueDelegateUnitTests SYSTEM PRIVATE "${TF_LITE_SCHEMA_INCLUDE_PATH}")
271        target_include_directories(OpaqueDelegateUnitTests SYSTEM PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/common/src/test")
272        target_include_directories(OpaqueDelegateUnitTests PUBLIC ${PROJECT_SOURCE_DIR})
273
274        # Add half library from armnn third-party libraries
275        target_link_libraries(OpaqueDelegateUnitTests PRIVATE thirdparty_headers)
276        target_link_libraries(OpaqueDelegateUnitTests PRIVATE armnnOpaqueDelegate)
277        target_link_libraries(OpaqueDelegateUnitTests PRIVATE Armnn::armnnUtils)
278        target_link_libraries(OpaqueDelegateUnitTests PRIVATE profiling_library_headers)
279    endif()
280endif()
281
282if(BUILD_DELEGATE_JNI_INTERFACE)
283    add_subdirectory(armnnDelegateJNI)
284endif()
285
286####################################################
287## Export targets
288set(armnn_delegate_export_targets)
289list(APPEND armnn_delegate_export_targets
290            armnnClassicDelegateObject
291            armnnDelegate
292            tflite_headers
293            flatbuffer_headers
294            profiling_library_headers
295            thirdparty_headers)
296
297install(
298        TARGETS ${armnn_delegate_export_targets}
299        EXPORT  armnn-delegate-targets
300        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
301        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
302        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
303
304## Set export alias
305set_target_properties(armnnDelegate
306        PROPERTIES
307        EXPORT_NAME ArmnnDelegate)
308
309## Export target scrips
310install(
311        EXPORT      armnn-delegate-targets
312        FILE        ArmnnDelegateTargets.cmake
313        NAMESPACE   ArmnnDelegate::
314        DESTINATION ${CMAKE_INSTALL_LIBDIR})
315
316## Create ArmnnDelegateConfig.cmake
317include(CMakePackageConfigHelpers)
318set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR})
319message(STATUS "CMAKE_CURRENT_LIST_DIR ${CMAKE_CURRENT_LIST_DIR}" )
320message(STATUS "CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}" )
321SET(Armnn_DIR "${Armnn_DIR}")
322
323configure_package_config_file(
324        ${CMAKE_CURRENT_LIST_DIR}/cmake/Modules/ArmnnDelegateConfig.cmake.in
325        ${CMAKE_CURRENT_BINARY_DIR}/ArmnnDelegateConfig.cmake
326        INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
327        PATH_VARS  Armnn_DIR)
328
329## Install ArmNN Delegate config file
330install(
331        FILES
332        ${CMAKE_CURRENT_BINARY_DIR}/ArmnnDelegateConfig.cmake
333        DESTINATION ${INSTALL_CONFIGDIR})
334
335## Export from build tree
336export(
337        EXPORT      armnn-delegate-targets
338        FILE        ${CMAKE_CURRENT_BINARY_DIR}/ArmnnDelegateTargets.cmake
339        NAMESPACE   ArmnnDelegate::)
340add_library(ArmnnDelegate::ArmnnDelegate ALIAS armnnDelegate)
341
342
343####################################################
344