xref: /aosp_15_r20/external/executorch/backends/xnnpack/CMakeLists.txt (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1# Copyright (c) Meta Platforms, Inc. and affiliates.
2# All rights reserved.
3#
4# This source code is licensed under the BSD-style license found in the
5# LICENSE file in the root directory of this source tree.
6
7# Please this file formatted by running:
8# ~~~
9# cmake-format -i CMakeLists.txt
10# ~~~
11
12cmake_minimum_required(VERSION 3.19)
13
14set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
15
16if(NOT CMAKE_CXX_STANDARD)
17  set(CMAKE_CXX_STANDARD 17)
18endif()
19
20if(NOT FLATC_EXECUTABLE)
21  set(FLATC_EXECUTABLE flatc)
22endif()
23
24# Source root directory for executorch.
25if(NOT EXECUTORCH_ROOT)
26  set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
27endif()
28
29include(${EXECUTORCH_ROOT}/build/Utils.cmake)
30
31if(NOT PYTHON_EXECUTABLE)
32  resolve_python_executable()
33endif()
34
35# NB: Enabling this will serialize execution of delegate instances
36# Keeping this OFF by default to maintain existing behavior, to be revisited.
37option(EXECUTORCH_XNNPACK_SHARED_WORKSPACE
38  "Enable workspace sharing across different delegate instances" ON)
39# Keeping this OFF by default due to regressions in decode
40# and model load with kleidi kernels
41option(EXECUTORCH_XNNPACK_ENABLE_KLEIDI
42  "Enable Arm Kleidi kernels" OFF)
43if(EXECUTORCH_XNNPACK_SHARED_WORKSPACE)
44  add_definitions(-DENABLE_XNNPACK_SHARED_WORKSPACE)
45endif()
46if(EXECUTORCH_XNNPACK_ENABLE_KLEIDI)
47  add_definitions(-DENABLE_XNNPACK_KLEIDI)
48endif()
49
50set(_common_include_directories ${EXECUTORCH_ROOT}/..)
51set(_common_compile_options -Wno-deprecated-declarations -fPIC)
52
53set(_xnnpack_schema__include_dir "${CMAKE_BINARY_DIR}/schema/include")
54# Paths to headers generated from the .fbs files.
55set(_xnnpack_flatbuffer__outputs)
56foreach(fbs_file ${_xnnpack_schema__srcs})
57  string(REGEX REPLACE "([^/]+)[.]fbs$" "\\1_generated.h" generated
58                       "${fbs_file}"
59  )
60  list(APPEND _xnnpack_flatbuffer__outputs
61       "${_xnnpack_schema__include_dir}/executorch/${generated}"
62  )
63endforeach()
64
65set(_xnnpack_schema__outputs)
66foreach(fbs_file ${_xnnpack_schema__srcs})
67  string(REGEX REPLACE "runtime_([^/]+)[.]fbs$" "\\1_generated.h" generated
68                       "${fbs_file}"
69  )
70  list(APPEND _xnnpack_schema__outputs
71       "${_xnnpack_schema__include_dir}/executorch/${generated}"
72  )
73endforeach()
74
75# Generate the headers from the .fbs files.
76add_custom_command(
77  OUTPUT ${_xnnpack_schema__outputs}
78  COMMAND
79    ${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --scoped-enums -o
80    "${_xnnpack_schema__include_dir}/executorch/backends/xnnpack/serialization"
81    ${_xnnpack_schema__srcs}
82  COMMAND mv ${_xnnpack_flatbuffer__outputs} ${_xnnpack_schema__outputs}
83  WORKING_DIRECTORY ${EXECUTORCH_ROOT}
84  COMMENT "Generating xnnpack_schema headers"
85  VERBATIM
86)
87
88add_library(xnnpack_schema INTERFACE ${_xnnpack_schema__outputs})
89set_target_properties(xnnpack_schema PROPERTIES LINKER_LANGUAGE CXX)
90target_include_directories(
91  xnnpack_schema INTERFACE ${_xnnpack_schema__include_dir}
92                           ${EXECUTORCH_ROOT}/third-party/flatbuffers/include
93)
94
95set(xnnpack_third_party pthreadpool cpuinfo)
96
97include(cmake/Dependencies.cmake)
98
99list(TRANSFORM _xnnpack_backend__srcs PREPEND "${EXECUTORCH_ROOT}/")
100add_library(xnnpack_backend STATIC ${_xnnpack_backend__srcs})
101target_link_libraries(
102  xnnpack_backend PRIVATE ${xnnpack_third_party} executorch_core
103                          xnnpack_schema
104)
105
106target_include_directories(
107  xnnpack_backend PUBLIC ${_common_include_directories}
108)
109target_include_directories(xnnpack_backend PUBLIC ${XNNPACK_INCLUDE_DIR})
110target_include_directories(
111  xnnpack_backend
112  PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/third-party/pthreadpool/include
113)
114target_include_directories(
115  xnnpack_backend
116  PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/third-party/cpuinfo/include
117)
118target_compile_options(xnnpack_backend PUBLIC ${_common_compile_options})
119target_link_options_shared_lib(xnnpack_backend)
120
121list(APPEND xnn_executor_runner_libs xnnpack_backend)
122
123# ios can only build library but not binary
124if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$")
125  #
126  # xnn_executor_runner: Like executor_runner but with XNNPACK, the binary will
127  # be at ${CMAKE_BINARY_DIR}/backends/xnnpack
128  #
129  list(TRANSFORM _xnn_executor_runner__srcs PREPEND "${EXECUTORCH_ROOT}/")
130  add_executable(xnn_executor_runner ${_xnn_executor_runner__srcs})
131  target_link_libraries(
132    xnn_executor_runner xnnpack_backend gflags portable_ops_lib
133  )
134  target_compile_options(xnn_executor_runner PUBLIC ${_common_compile_options})
135endif()
136
137install(
138  TARGETS xnnpack_backend
139  DESTINATION lib
140  INCLUDES
141  DESTINATION ${_common_include_directories}
142)
143