onnxruntime/cmake/onnxruntime_nodejs.cmake
Jon Campbell 768c79317c
Enable QNN HTP support for Node (#20576)
### Description
Add support for using Onnx Runtime with Node

### Motivation and Context
Onnx Runtime supports the QNN HTP, but does not support it for Node.js.
This adds baseline support for the Onnx Runtime to be used with Node.

Note it does not update the node packages that are distributed
officially. This simply patches the onnxruntime.dll to allow 'qnn' to be
used as an execution provider.

Testing was done using the existing onnxruntime-node package. The
`onnxruntime.dll` and `onnxruntime_binding.node` were swapped into
`node_modules\onnxruntime-node\bin\napi-v3\win32\arm64` with the newly
built version, then the various QNN dlls and .so files were placed next
to the onnxruntime.dll. Testing was performed on a variety of models and
applications, but the easiest test is to modify the [node quickstart
example](https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js/quick-start_onnxruntime-node).
2024-05-09 13:11:07 -07:00

103 lines
3.3 KiB
CMake

# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
# Licensed under the MIT License.
set(JS_ROOT ${REPO_ROOT}/js)
set(JS_COMMON_ROOT ${JS_ROOT}/common)
set(JS_NODE_ROOT ${JS_ROOT}/node)
find_program(NPM_CLI
NAMES "npm.cmd" "npm"
DOC "NPM command line client"
REQUIRED
)
# verify Node.js and NPM
execute_process(COMMAND node --version
WORKING_DIRECTORY ${JS_NODE_ROOT}
OUTPUT_VARIABLE node_version
RESULT_VARIABLE had_error
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(had_error)
message(FATAL_ERROR "Failed to find Node.js: " ${had_error})
endif()
execute_process(COMMAND ${NPM_CLI} --version
WORKING_DIRECTORY ${JS_NODE_ROOT}
OUTPUT_VARIABLE npm_version
RESULT_VARIABLE had_error
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(had_error)
message(FATAL_ERROR "Failed to find NPM: " ${had_error})
endif()
# setup ARCH
if (APPLE)
if (CMAKE_OSX_ARCHITECTURES_LEN GREATER 1)
message(FATAL_ERROR "CMake.js does not support multi-architecture for macOS")
endif()
if (CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
set(NODEJS_BINDING_ARCH arm64)
else()
set(NODEJS_BINDING_ARCH x64)
endif()
elseif (WIN32)
if (NOT MSVC)
message(FATAL_ERROR "Only support MSVC for building Node.js binding on Windows.")
endif()
if(onnxruntime_target_platform STREQUAL "ARM64")
set(NODEJS_BINDING_ARCH arm64)
elseif(onnxruntime_target_platform STREQUAL "x64")
set(NODEJS_BINDING_ARCH x64)
else()
message(FATAL_ERROR "Unsupported target platform for Node.js binding:" ${onnxruntime_target_platform})
endif()
else()
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
set(NODEJS_BINDING_ARCH arm64)
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
set(NODEJS_BINDING_ARCH x64)
else()
message(FATAL_ERROR "Unsupported target platform for Node.js binding:" ${onnxruntime_target_platform})
endif()
endif()
# setup providers
if (onnxruntime_USE_CUDA)
set(NODEJS_BINDING_USE_CUDA "--use_cuda")
endif()
if (onnxruntime_USE_DML)
set(NODEJS_BINDING_USE_DML "--use_dml")
endif()
if (onnxruntime_USE_TENSORRT)
set(NODEJS_BINDING_USE_TENSORRT "--use_tensorrt")
endif()
if (onnxruntime_USE_COREML)
set(NODEJS_BINDING_USE_COREML "--use_coreml")
endif()
if (onnxruntime_USE_QNN)
set(NODEJS_BINDING_USE_QNN "--use_qnn")
endif()
if(NOT onnxruntime_ENABLE_STATIC_ANALYSIS)
# add custom target
add_custom_target(js_npm_ci ALL
COMMAND ${NPM_CLI} ci
WORKING_DIRECTORY ${JS_ROOT}
COMMENT "NPM install on /js")
add_custom_target(js_common_npm_ci ALL
COMMAND ${NPM_CLI} ci
WORKING_DIRECTORY ${JS_COMMON_ROOT}
COMMENT "NPM install on /js/common")
add_custom_target(nodejs_binding_wrapper ALL
COMMAND ${NPM_CLI} ci
COMMAND ${NPM_CLI} run build -- --onnxruntime-build-dir=${CMAKE_CURRENT_BINARY_DIR} --config=${CMAKE_BUILD_TYPE} --onnxruntime-generator=${CMAKE_GENERATOR}
--arch=${NODEJS_BINDING_ARCH} ${NODEJS_BINDING_USE_CUDA} ${NODEJS_BINDING_USE_DML} ${NODEJS_BINDING_USE_TENSORRT}
${NODEJS_BINDING_USE_COREML} ${NODEJS_BINDING_USE_QNN}
WORKING_DIRECTORY ${JS_NODE_ROOT}
COMMENT "Using cmake-js to build OnnxRuntime Node.js binding")
add_dependencies(js_common_npm_ci js_npm_ci)
add_dependencies(nodejs_binding_wrapper js_common_npm_ci)
add_dependencies(nodejs_binding_wrapper onnxruntime)
endif()