1# Copyright (c) Qualcomm Innovation Center, Inc. 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 7set(CMAKE_CXX_STANDARD 17) 8set(CMAKE_CXX_STANDARD_REQUIRED ON) 9set(CMAKE_POSITION_INDEPENDENT_CODE ON) 10 11# 12# get path 13# 14get_filename_component( 15 EXECUTORCH_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE 16) 17get_filename_component( 18 QNN_EXECUTORCH_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR} ABSOLUTE 19) 20# Let files say "include <executorch/path/to/header.h>". 21get_filename_component( 22 _common_include_directories "${EXECUTORCH_SOURCE_DIR}/.." ABSOLUTE 23) 24 25if(NOT DEFINED QNN_SDK_ROOT) 26 message( 27 FATAL_ERROR 28 "Please define QNN_SDK_ROOT, e.g. cmake <..> -DQNN_SDK_ROOT=<...>" 29 ) 30elseif(CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$") 31 message(FATAL_ERROR "ios is not supported by Qualcomm AI Engine Direct") 32endif() 33 34message(STATUS "Using qnn sdk root ${QNN_SDK_ROOT}") 35message(STATUS "Using EXECUTORCH_SOURCE_DIR ${EXECUTORCH_SOURCE_DIR}") 36 37if(${ANDROID}) 38 find_library(android_log log) 39endif() 40 41if(NOT FLATC_EXECUTABLE) 42 set(FLATC_EXECUTABLE flatc) 43endif() 44 45set(qcir_schema_include_dir ${CMAKE_CURRENT_LIST_DIR}/aot/ir) 46set(qcir_schema_output ${qcir_schema_include_dir}/qcir_generated.h) 47add_custom_command( 48 OUTPUT qcir_schema_output 49 COMMAND ${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --scoped-enums -o 50 ${qcir_schema_include_dir} ${qcir_schema_include_dir}/qcir.fbs 51 COMMENT "Generating qualcomm ir schema headers" 52 VERBATIM 53) 54 55add_compile_options("-Wall" "-Werror" "-Wno-sign-compare") 56 57# GNU emit wanring for ignored attributes Unfortunately, we use [[maybe_unused]] 58# which can be ignored by GNU. So we make it a warning, not an error in GNU. 59if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 60 add_compile_options("-Wno-error=attributes") 61 add_link_options("-flto=auto") 62endif() 63 64if(CMAKE_BUILD_TYPE STREQUAL "Release") 65 # strip symbols 66 add_link_options("-s") 67 68 # --gc-sections is added by torch. 69 add_compile_options("-O3" "-ffunction-sections" "-fdata-sections" "-frtti") 70endif() 71 72include_directories( 73 BEFORE ${_common_include_directories} ${QNN_SDK_ROOT}/include/QNN 74 ${EXECUTORCH_SOURCE_DIR}/third-party/flatbuffers/include 75) 76 77set(_qnn_schema__srcs 78 backends/qualcomm/serialization/qc_compiler_spec.fbs 79 backends/qualcomm/serialization/qc_binary_info.fbs 80) 81set(_qnn_schema__include_dir "${CMAKE_BINARY_DIR}/schema/include") 82# Paths to headers generated from the .fbs files. 83set(_qnn_schema__outputs) 84foreach(fbs_file ${_qnn_schema__srcs}) 85 string(REGEX REPLACE "serialization/([^/]+)[.]fbs$" "\\1_generated.h" 86 generated "${fbs_file}" 87 ) 88 list(APPEND _qnn_schema__outputs 89 "${_qnn_schema__include_dir}/executorch/${generated}" 90 ) 91endforeach() 92 93# Generate the headers from the .fbs files. 94add_custom_command( 95 OUTPUT ${_qnn_schema__outputs} 96 COMMAND 97 ${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --scoped-enums -o 98 "${_qnn_schema__include_dir}/executorch/backends/qualcomm" 99 ${_qnn_schema__srcs} 100 WORKING_DIRECTORY ${EXECUTORCH_SOURCE_DIR} 101 COMMENT "Generating qnn_schema headers" 102 VERBATIM 103) 104 105include_directories( 106 BEFORE ${_qnn_schema__include_dir} 107 ${EXECUTORCH_SOURCE_DIR}/third-party/flatbuffers/include 108) 109 110# 111# declare targets 112# 113add_library(executorch_backend INTERFACE) 114add_library(qcir INTERFACE qcir_schema_output) 115add_library(qcir_utils STATIC) 116add_library(qnn_backend STATIC) 117add_library(qnn_backend_cache STATIC) 118add_library(qnn_context STATIC) 119add_library(qnn_device STATIC) 120add_library(qnn_executorch_backend SHARED) 121add_library(qnn_executorch_header INTERFACE) 122add_library(qnn_executorch_logging STATIC) 123add_library(qnn_factory STATIC) 124add_library(qnn_function_interface INTERFACE) 125add_library(qnn_graph STATIC) 126add_library(qnn_header INTERFACE) 127add_library(qnn_implementation STATIC) 128add_library(qnn_logger STATIC) 129add_library(qnn_manager STATIC) 130add_library(qnn_mem_manager STATIC) 131add_library(qnn_profiler STATIC) 132add_library(qnn_schema INTERFACE ${_qnn_schema__outputs}) 133add_library(qnn_sys_function_interface INTERFACE) 134add_library(qnn_sys_implementation STATIC) 135add_library(shared_buffer STATIC) 136add_library(wrappers STATIC) 137add_library(utils STATIC) 138 139# 140# declare dependency 141# 142target_link_libraries(qcir_utils PRIVATE qcir) 143target_link_libraries(wrappers PRIVATE qnn_header qnn_executorch_logging) 144target_link_libraries(qnn_function_interface INTERFACE qnn_header) 145target_link_libraries( 146 qnn_implementation PRIVATE qnn_function_interface qnn_header 147 qnn_executorch_logging ${CMAKE_DL_LIBS} 148) 149target_link_libraries(qnn_sys_function_interface INTERFACE qnn_header) 150target_link_libraries( 151 qnn_sys_implementation PRIVATE qnn_sys_function_interface qnn_header 152 qnn_executorch_logging ${CMAKE_DL_LIBS} 153) 154target_link_libraries(qnn_executorch_logging PRIVATE qnn_schema) 155target_link_libraries(qnn_profiler PRIVATE qnn_executorch_logging) 156target_link_libraries(qnn_logger PRIVATE qnn_implementation ${android_log}) 157target_link_libraries(qnn_backend PRIVATE qnn_implementation qnn_logger) 158target_link_libraries( 159 qnn_device PRIVATE qnn_executorch_logging qnn_implementation qnn_logger 160) 161target_link_libraries( 162 qnn_backend_cache PRIVATE qnn_sys_implementation qcir_utils 163) 164target_link_libraries( 165 qnn_context PRIVATE qnn_implementation qnn_logger qnn_backend qnn_device 166 qnn_backend_cache 167) 168target_link_libraries( 169 qnn_graph PRIVATE qnn_executorch_logging qnn_implementation qnn_context 170 qnn_profiler 171) 172target_link_libraries( 173 qnn_mem_manager PRIVATE qnn_executorch_logging qnn_implementation qnn_context 174) 175 176target_link_libraries( 177 qnn_factory 178 PUBLIC qnn_header 179 PRIVATE qnn_schema qnn_backend qnn_device qnn_context qnn_graph 180 qnn_mem_manager 181) 182target_link_libraries( 183 qnn_manager PRIVATE qnn_factory wrappers qnn_schema utils shared_buffer 184) 185target_link_libraries( 186 qnn_executorch_backend PRIVATE qnn_executorch_header qnn_schema qnn_manager 187 executorch_core qcir_utils extension_tensor 188) 189set_target_properties( 190 qnn_executorch_backend PROPERTIES LINK_FLAGS "-Wl,-rpath='$ORIGIN'" 191) 192target_link_libraries(utils PRIVATE qnn_executorch_logging) 193target_link_libraries( 194 shared_buffer PRIVATE qnn_executorch_logging ${CMAKE_DL_LIBS} 195) 196# 197# add linker option 198# 199target_link_options_shared_lib(qnn_executorch_backend) 200 201# 202# add compile option 203# 204target_compile_options(executorch PUBLIC -DET_EVENT_TRACER_ENABLED) 205 206# 207# add sources 208# 209add_subdirectory( 210 ${QNN_EXECUTORCH_ROOT_DIR}/runtime ${CMAKE_CURRENT_BINARY_DIR}/qnn_executorch 211) 212add_subdirectory( 213 ${QNN_EXECUTORCH_ROOT_DIR}/runtime/backends 214 ${CMAKE_CURRENT_BINARY_DIR}/qnn_executorch/backends 215) 216add_subdirectory( 217 ${QNN_EXECUTORCH_ROOT_DIR}/aot/wrappers 218 ${CMAKE_CURRENT_BINARY_DIR}/qnn_executorch/wrappers 219) 220add_subdirectory( 221 ${QNN_EXECUTORCH_ROOT_DIR}/aot/ir 222 ${CMAKE_CURRENT_BINARY_DIR}/qnn_executorch/ir 223) 224install(TARGETS qnn_executorch_backend DESTINATION lib) 225 226# QNN pybind 227if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64") 228 add_subdirectory( 229 ${EXECUTORCH_SOURCE_DIR}/third-party/pybind11 230 ${CMAKE_CURRENT_BINARY_DIR}/pybind11 231 ) 232 add_library(PyQnnManagerAdaptor MODULE) 233 add_library(PyQnnWrapperAdaptor MODULE) 234 # PyQnnManager containing a pybind type triggers the warning because pybind11 235 # code internally forces hidden visibility. 236 set_target_properties( 237 PyQnnManagerAdaptor PROPERTIES CXX_VISIBILITY_PRESET hidden 238 ) 239 set_target_properties( 240 PyQnnWrapperAdaptor PROPERTIES CXX_VISIBILITY_PRESET hidden 241 ) 242 243 target_link_libraries( 244 PyQnnManagerAdaptor 245 PRIVATE pybind11::module 246 pybind11::lto 247 qnn_schema 248 qnn_manager 249 qnn_executorch_header 250 executorch 251 qcir_utils 252 extension_tensor 253 ) 254 target_link_libraries( 255 PyQnnWrapperAdaptor PRIVATE pybind11::module pybind11::lto wrappers 256 qnn_executorch_logging qnn_executorch_header 257 ) 258 259 pybind11_extension(PyQnnManagerAdaptor) 260 pybind11_extension(PyQnnWrapperAdaptor) 261 if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo) 262 # Strip unnecessary sections of the binary 263 pybind11_strip(PyQnnManagerAdaptor) 264 pybind11_strip(PyQnnWrapperAdaptor) 265 endif() 266 267 if(CMAKE_BUILD_TYPE STREQUAL "Release") 268 # need to allow exceptions in pybind 269 set(_pybind_compile_options -Wno-deprecated-declarations -fPIC -frtti 270 -fexceptions 271 ) 272 target_compile_options( 273 PyQnnManagerAdaptor PUBLIC ${_pybind_compile_options} 274 ) 275 target_compile_options( 276 PyQnnWrapperAdaptor PUBLIC ${_pybind_compile_options} 277 ) 278 endif() 279 280 add_subdirectory( 281 ${QNN_EXECUTORCH_ROOT_DIR}/aot/python 282 ${CMAKE_CURRENT_BINARY_DIR}/qnn_executorch/python 283 ) 284endif() 285