[Node.js API] optimize prebuild (#3844)

* optimize prebuild

* fix base folder for prepack

* bump dev version
This commit is contained in:
Yulong Wang 2020-05-06 15:48:13 -07:00 committed by GitHub
parent 9cca219b1a
commit 65bfece19d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 125 additions and 753 deletions

View file

@ -7,6 +7,14 @@ set(CMAKE_CXX_STANDARD 14)
add_compile_definitions(NAPI_VERSION=${napi_build_version})
add_compile_definitions(NAPI_EXPERIMENTAL)
# dist variables
execute_process(COMMAND node -e "console.log(process.arch)"
OUTPUT_VARIABLE node_arch OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND node -e "console.log(process.platform)"
OUTPUT_VARIABLE node_platform OUTPUT_STRIP_TRAILING_WHITESPACE)
file(READ ${CMAKE_SOURCE_DIR}/../VERSION_NUMBER ort_version)
set(dist_folder "${CMAKE_SOURCE_DIR}/bin/napi-v3/${node_platform}/${node_arch}/")
include_directories(${CMAKE_JS_INC})
include_directories(${CMAKE_SOURCE_DIR}/../include/onnxruntime)
include_directories(${CMAKE_SOURCE_DIR}/node_modules/node-addon-api)
@ -25,19 +33,20 @@ if (WIN32)
${CMAKE_SOURCE_DIR}/../build/Windows/${CMAKE_BUILD_TYPE}/${CMAKE_BUILD_TYPE}/onnxruntime.lib)
elseif (APPLE)
target_link_libraries(onnxruntime_binding PRIVATE
${CMAKE_SOURCE_DIR}/../build/Linux/${CMAKE_BUILD_TYPE}/libonnxruntime.1.2.0.dylib)
${CMAKE_SOURCE_DIR}/../build/Linux/${CMAKE_BUILD_TYPE}/libonnxruntime.${ort_version}.dylib)
set_target_properties(onnxruntime_binding PROPERTIES INSTALL_RPATH "@loader_path")
else()
target_link_libraries(onnxruntime_binding PRIVATE
${CMAKE_SOURCE_DIR}/../build/Linux/${CMAKE_BUILD_TYPE}/libonnxruntime.so.1.2.0)
${CMAKE_SOURCE_DIR}/../build/Linux/${CMAKE_BUILD_TYPE}/libonnxruntime.so.${ort_version})
set_target_properties(onnxruntime_binding PROPERTIES INSTALL_RPATH "$ORIGIN/")
endif()
# post build
add_custom_command(
TARGET onnxruntime_binding POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_SOURCE_DIR}/bin
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:onnxruntime_binding> ${CMAKE_SOURCE_DIR}/bin/
COMMAND ${CMAKE_COMMAND} -E make_directory ${dist_folder}
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:onnxruntime_binding> ${dist_folder}
)
if (WIN32)
@ -45,30 +54,30 @@ if (WIN32)
TARGET onnxruntime_binding POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/../build/Windows/${CMAKE_BUILD_TYPE}/${CMAKE_BUILD_TYPE}/onnxruntime.dll
${CMAKE_SOURCE_DIR}/bin/
${dist_folder}
)
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_custom_command(
TARGET onnxruntime_binding POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/../build/Windows/${CMAKE_BUILD_TYPE}/${CMAKE_BUILD_TYPE}/onnxruntime.pdb
${CMAKE_SOURCE_DIR}/bin/
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE_DIR:onnxruntime_binding>/onnxruntime_binding.pdb ${CMAKE_SOURCE_DIR}/bin/
${dist_folder}
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE_DIR:onnxruntime_binding>/onnxruntime_binding.pdb ${dist_folder}
)
endif()
elseif (APPLE)
add_custom_command(
TARGET onnxruntime_binding POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/../build/Linux/${CMAKE_BUILD_TYPE}/libonnxruntime.1.2.0.dylib
${CMAKE_SOURCE_DIR}/bin/
${CMAKE_SOURCE_DIR}/../build/Linux/${CMAKE_BUILD_TYPE}/libonnxruntime.${ort_version}.dylib
${dist_folder}
)
elseif (UNIX)
add_custom_command(
TARGET onnxruntime_binding POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/../build/Linux/${CMAKE_BUILD_TYPE}/libonnxruntime.so.1.2.0
${CMAKE_SOURCE_DIR}/bin/
${CMAKE_SOURCE_DIR}/../build/Linux/${CMAKE_BUILD_TYPE}/libonnxruntime.so.${ort_version}
${dist_folder}
)
else()
message(FATAL_ERROR "Platform not supported.")

View file

@ -36,4 +36,5 @@ export declare namespace Binding {
// export native binding
export const binding =
// eslint-disable-next-line @typescript-eslint/no-require-imports, import/no-internal-modules
require('../bin/onnxruntime_binding.node') as {InferenceSession: Binding.InferenceSessionConstructor};
require(`../bin/napi-v3/${process.platform}/${process.arch}/onnxruntime_binding.node`) as
{InferenceSession: Binding.InferenceSessionConstructor};

761
nodejs/package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,7 +1,7 @@
{
"name": "onnxruntime",
"description": "Node.js binding of ONNXRuntime",
"version": "0.0.1-dev.20200504.1",
"version": "0.0.1-dev.20200506.1",
"main": "./lib/index.js",
"types": "./types/lib/index.d.ts",
"scripts": {
@ -12,7 +12,7 @@
"rebuildd": "tsc && node ./script/build --debug --rebuild",
"test": "mocha ./test/test-main",
"lint": "eslint . --ext .ts",
"prepack": "node ./script/build --prebuild",
"prepack": "node ./script/pack-prebuild",
"format": "clang-format --glob=\"{{lib,test,script}/**/*.ts,src/**/*.{cc,h}}\" --style=file -i"
},
"repository": {
@ -39,7 +39,9 @@
"license": "MIT",
"devDependencies": {
"@types/fs-extra": "^8.1.0",
"@types/klaw-sync": "^6.0.0",
"@types/mocha": "^7.0.2",
"@types/tar-stream": "^2.1.0",
"@typescript-eslint/eslint-plugin": "^2.29.0",
"@typescript-eslint/parser": "^2.29.0",
"clang-format": "^1.4.0",
@ -51,11 +53,12 @@
"fs-extra": "^9.0.0",
"globby": "^11.0.0",
"jsonc": "^2.0.0",
"klaw-sync": "^6.0.0",
"mocha": "^7.1.1",
"node-addon-api": "^2.0.0",
"node-pre-gyp-github": "^1.4.3",
"onnx-proto": "^4.0.4",
"prebuild": "^10.0.0",
"tar-stream": "2.1.2",
"typedoc": "^0.17.3",
"typescript": "^3.8.3"
},

View file

@ -5,43 +5,26 @@ import * as path from 'path';
// command line flags
const DEBUG = process.argv.slice(2).indexOf('--debug') !== -1;
const REBUILD = process.argv.slice(2).indexOf('--rebuild') !== -1;
const USE_PREBUILD = process.argv.slice(2).indexOf('--prebuild') !== -1;
// build path
const ROOT_FOLDER = path.join(__dirname, '..');
const BIN_FOLDER = path.join(ROOT_FOLDER, 'bin');
const PREBUILDS_FOLDER = path.join(ROOT_FOLDER, 'prebuilds');
const NPM_BIN_FOLDER = execSync('npm bin', {encoding: 'utf8'}).trim();
const CMAKE_JS_FULL_PATH = path.join(NPM_BIN_FOLDER, 'cmake-js');
const PREBUILD_FULL_PATH = path.join(NPM_BIN_FOLDER, 'prebuild');
// if rebuild, clean up the dist folders
if (REBUILD) {
fs.removeSync(BIN_FOLDER);
if (USE_PREBUILD) {
fs.removeSync(PREBUILDS_FOLDER);
}
}
let command: string;
let args: string[];
if (USE_PREBUILD) {
command = PREBUILD_FULL_PATH;
args = ['--backend', 'cmake-js', '--runtime', 'napi', '--include-regex', '"\\.+"', '--prepack', '"npm test"'];
if (DEBUG) {
args.push('--debug');
}
} else {
command = CMAKE_JS_FULL_PATH;
args = [(REBUILD ? 'rebuild' : 'compile'), '--arch=x64', '--CDnapi_build_version=3'];
if (DEBUG) {
args.push('-D');
}
const command = CMAKE_JS_FULL_PATH;
const args = [(REBUILD ? 'rebuild' : 'compile'), '--arch=x64', '--CDnapi_build_version=3'];
if (DEBUG) {
args.push('-D');
}
// launch cmake-js/prebuild
// launch cmake-js
const proc = spawnSync(command, args, {shell: true, stdio: 'inherit', cwd: ROOT_FOLDER});
if (proc.status !== 0) {
if (proc.error) {

View file

@ -0,0 +1,47 @@
import * as fs from 'fs-extra';
import klawSync from 'klaw-sync';
import * as path from 'path';
import {pack} from 'tar-stream';
import * as zlib from 'zlib';
// build path
const ROOT_FOLDER = path.join(__dirname, '..');
const BIN_FOLDER = path.join(ROOT_FOLDER, 'bin');
const PREBUILDS_FOLDER = path.join(ROOT_FOLDER, 'prebuilds');
// start pack
const tarName = `${process.env.npm_package_name}-v${process.env.npm_package_version}-napi-v3-${process.platform}-${
process.arch}.tar.gz`;
const tarPath = path.join(PREBUILDS_FOLDER, tarName);
const tarStream = pack();
fs.ensureDirSync(PREBUILDS_FOLDER);
const ws = fs.createWriteStream(tarPath);
tarStream.pipe(zlib.createGzip({level: 9})).pipe(ws);
// enumerate all files under BIN folder
const entries = klawSync(BIN_FOLDER, {nodir: true}).map(i => ({
path: i.path,
name: path.relative(ROOT_FOLDER, i.path),
size: i.stats.size,
mode: i.stats.mode | parseInt('444', 8) | parseInt('222', 8),
gid: i.stats.gid,
uid: i.stats.uid
}));
console.log(`=== start to pack prebuild: ${tarName}`);
packNextFile();
function packNextFile(): void {
const nextEntry = entries.shift();
if (nextEntry) {
console.log(` adding file: ${nextEntry.name}`);
const stream = tarStream.entry(nextEntry);
fs.createReadStream(nextEntry.path).pipe(stream).on('finish', packNextFile);
} else {
console.log('=== finished packing prebuild.');
tarStream.finalize();
}
}