Publish npm package with node-addon-api for Windows (#838)

This commit is contained in:
Fangjun Kuang
2024-05-06 16:21:29 +08:00
committed by GitHub
parent e1bb928805
commit 37a4135dd7
15 changed files with 282 additions and 75 deletions

View File

@@ -0,0 +1,78 @@
# See also https://github.com/cmake-js/cmake-js
# npm install cmake-js
# ./node_modules/.bin/cmake-js --help
# ./node_modules/.bin/cmake-js --version
# ./node_modules/.bin/cmake-js compile --help
# ./node_modules/.bin/cmake-js compile --log-level
# ./node_modules/.bin/cmake-js compile --log-level verbose
cmake_minimum_required(VERSION 3.15)
cmake_policy(SET CMP0091 NEW)
cmake_policy(SET CMP0042 NEW)
project(sherpa-onnx)
set(CMAKE_CXX_STANDARD 14)
add_definitions(-DNAPI_VERSION=3)
include_directories(${CMAKE_JS_INC})
set(srcs
src/sherpa-onnx-node-addon-api.cc
src/streaming-asr.cc
src/wave-reader.cc
)
if(NOT DEFINED ENV{SHERPA_ONNX_INSTALL_DIR})
message(FATAL_ERROR "
Please run:
git clone https://github.com/k2-fsa/sherpa-onnx
cd sherpa-onnx
mkdir build
cd build
cmake -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=./install ..
make install
export SHERPA_ONNX_INSTALL_DIR=$PWD/install
")
endif()
include_directories($ENV{SHERPA_ONNX_INSTALL_DIR}/include)
# See https://nodejs.github.io/node-addon-examples/build-tools/cmake-js
# Include Node-API wrappers
execute_process(
COMMAND node -p "require('node-addon-api').include"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE NODE_ADDON_API_DIR
)
string(REPLACE "\n" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
string(REPLACE "\"" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
include_directories(${NODE_ADDON_API_DIR})
link_directories($ENV{SHERPA_ONNX_INSTALL_DIR}/lib)
add_library(${PROJECT_NAME} SHARED ${srcs} ${CMAKE_JS_SRC})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB})
target_link_libraries(${PROJECT_NAME}
sherpa-onnx-c-api
sherpa-onnx-core
kaldi-decoder-core
sherpa-onnx-kaldifst-core
sherpa-onnx-fstfar
sherpa-onnx-fst
kaldi-native-fbank-core
piper_phonemize
espeak-ng
ucd
onnxruntime
-Wl,-rpath,$ENV{SHERPA_ONNX_INSTALL_DIR}/lib
)
if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET)
# Generate node.lib
execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
endif()

View File

@@ -1,35 +0,0 @@
{
'targets': [
{
'target_name': 'sherpa-onnx',
'sources': [
'src/sherpa-onnx-node-addon-api.cc',
'src/streaming-asr.cc',
'src/wave-reader.cc'
],
'include_dirs': [
"<!@(node -p \"require('node-addon-api').include\")",
"<!@(pkg-config --variable=includedir sherpa-onnx)"
],
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
'cflags!': [
'-fno-exceptions',
],
'cflags_cc!': [
'-fno-exceptions',
'-std=c++17'
],
'libraries': [
"<!@(pkg-config --libs sherpa-onnx)"
],
'xcode_settings': {
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'CLANG_CXX_LIBRARY': 'libc++',
'MACOSX_DEPLOYMENT_TARGET': '10.7'
},
'msvs_settings': {
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
}
}
]
}

View File

@@ -1,5 +1,9 @@
const os = require('os');
const platform_arch = `${os.platform()}-${os.arch()}`;
// Package name triggered spam for sherpa-onnx-win32-x64
// so we have renamed it to sherpa-onnx-win-x64
const platform_arch =
`${os.platform() == 'win32' ? 'win' : os.platform()}-${os.arch()}`;
const possible_paths = [
'../build/Release/sherpa-onnx.node',
'../build/Debug/sherpa-onnx.node',

View File

@@ -3,12 +3,12 @@
"version": "1.0.0",
"description": "Speech-to-text and text-to-speech using Next-gen Kaldi without internet connection",
"dependencies": {
"cmake-js": "^6.0.0",
"node-addon-api": "^1.1.0",
"bindings": "^1.5.0",
"perf_hooks": "*",
"node-gyp": "^8.3.0"
"perf_hooks": "*"
},
"scripts": {
"install": "cmake-js compile --log-level verbose",
"test": "node --napi-modules ./test/test_binding.js"
},
"repository": {

View File

@@ -3,7 +3,7 @@
// Copyright (c) 2024 Xiaomi Corporation
#include "napi.h" // NOLINT
Napi::Object InitStreamingAsr(Napi::Env env, Napi::Object exports);
void InitStreamingAsr(Napi::Env env, Napi::Object exports);
void InitWaveReader(Napi::Env env, Napi::Object exports);
Napi::Object Init(Napi::Env env, Napi::Object exports) {