Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 45a125e

Browse files
author
Simon Hofmann
committed
Fetched upstream, added install_dependencies.js to install platform dependent OpenCV package
1 parent 2a440e7 commit 45a125e

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

install/install.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const opencvBuild = require(`@nut-tree/opencv-build-${process.platform}`)
2+
const child_process = require('child_process')
3+
const fs = require('fs')
4+
const log = require('npmlog')
5+
const { resolvePath } = require('../lib/commons')
6+
7+
const defaultDir = '/usr/local'
8+
const defaultLibDir = `${defaultDir}/lib`
9+
const defaultIncludeDir = `${defaultDir}/include`
10+
const defaultIncludeDirOpenCV4 = `${defaultIncludeDir}/opencv4`
11+
12+
function getDefaultIncludeDirs() {
13+
log.info('install', 'OPENCV_INCLUDE_DIR is not set, looking for default include dir')
14+
if (opencvBuild.isWin()) {
15+
throw new Error('OPENCV_INCLUDE_DIR has to be defined on windows when auto build is disabled')
16+
}
17+
return [defaultIncludeDir, defaultIncludeDirOpenCV4]
18+
}
19+
20+
function getDefaultLibDir() {
21+
log.info('install', 'OPENCV_LIB_DIR is not set, looking for default lib dir')
22+
if (opencvBuild.isWin()) {
23+
throw new Error('OPENCV_LIB_DIR has to be defined on windows when auto build is disabled')
24+
}
25+
return defaultLibDir
26+
}
27+
28+
opencvBuild.applyEnvsFromPackageJson()
29+
30+
// const libDir = opencvBuild.isAutoBuildDisabled()
31+
// ? (resolvePath(process.env.OPENCV_LIB_DIR) || getDefaultLibDir())
32+
// : resolvePath(opencvBuild.opencvLibDir)
33+
const libDir = resolvePath(opencvBuild.opencvLibDir);
34+
35+
log.info('install', 'using lib dir: ' + libDir)
36+
37+
if (!fs.existsSync(libDir)) {
38+
throw new Error('library dir does not exist: ' + libDir)
39+
}
40+
41+
const libsFoundInDir = opencvBuild
42+
.getLibs(libDir)
43+
.filter(lib => lib.libPath)
44+
45+
if (!libsFoundInDir.length) {
46+
throw new Error('no OpenCV libraries found in lib dir: ' + libDir)
47+
}
48+
49+
log.info('install', 'found the following libs:')
50+
libsFoundInDir.forEach(lib => log.info('install', lib.opencvModule + ' : ' + lib.libPath))
51+
52+
const defines = libsFoundInDir
53+
.map(lib => `OPENCV4NODEJS_FOUND_LIBRARY_${lib.opencvModule.toUpperCase()}`)
54+
55+
const explicitIncludeDir = resolvePath(process.env.OPENCV_INCLUDE_DIR)
56+
const includes = opencvBuild.isAutoBuildDisabled()
57+
? (explicitIncludeDir ? [explicitIncludeDir] : getDefaultIncludeDirs())
58+
: [resolvePath(opencvBuild.opencvInclude), resolvePath(opencvBuild.opencv4Include)]
59+
60+
const libs = opencvBuild.isWin()
61+
? libsFoundInDir.map(lib => resolvePath(lib.libPath))
62+
// dynamically link libs if not on windows
63+
: ['-L' + libDir]
64+
.concat(libsFoundInDir.map(lib => '-lopencv_' + lib.opencvModule))
65+
.concat('-Wl,-rpath,' + libDir)
66+
67+
console.log()
68+
log.info('install', 'setting the following defines:')
69+
defines.forEach(def => log.info('defines', def))
70+
console.log()
71+
log.info('install', 'setting the following includes:')
72+
includes.forEach(inc => log.info('includes', inc))
73+
console.log()
74+
log.info('install', 'setting the following libs:')
75+
libs.forEach(lib => log.info('libs', lib))
76+
77+
process.env['OPENCV4NODEJS_DEFINES'] = defines.join('\n')
78+
process.env['OPENCV4NODEJS_INCLUDES'] = includes.join('\n')
79+
process.env['OPENCV4NODEJS_LIBRARIES'] = libs.join('\n')
80+
81+
const flags = process.env.BINDINGS_DEBUG ? '--jobs max --debug' : '--jobs max'
82+
const nodegypCmd = 'node-gyp rebuild ' + flags
83+
log.info('install', `spawning node gyp process: ${nodegypCmd}`)
84+
const child = child_process.exec(nodegypCmd, {
85+
maxBuffer: 1024 * 1024 * 10
86+
}, function(err, stdout, stderr) {
87+
const _err = err || stderr
88+
if (_err) log.error(_err)
89+
})
90+
child.stdout.pipe(process.stdout)
91+
child.stderr.pipe(process.stderr)

install/install_dependencies.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const { execSync } = require("child_process");
2+
const isX64 = process.arch === "x64";
3+
4+
const install = (pkg) => {
5+
execSync(`npm install ${pkg}`);
6+
}
7+
8+
const packages = {
9+
"darwin": [
10+
],
11+
"win32": [
12+
],
13+
"linux": [
14+
]
15+
}
16+
17+
if (!isX64) {
18+
console.log("Unsupported platform, only x64 is supported.");
19+
process.exit(-1);
20+
}
21+
22+
const op = process.platform;
23+
24+
console.log(`Installing prebuilt OpenCV v${process.env.npm_package_opencv} for plattform ${op}`);
25+
install(`@nut-tree/opencv-build-${op}@${process.env.npm_package_opencv}`);
26+
packages[op].forEach(pkg => {
27+
console.log(`Installing additional runtime dependency '${pkg}'`);
28+
install(pkg);
29+
});
30+
console.log(`Done.`);

install/parseEnv.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const envName = process.argv[2]
2+
3+
if (!envName) {
4+
throw new Error('no env name passed to parseEnv')
5+
}
6+
const outputs = (process.env[envName] || '').split('\n')
7+
outputs.forEach(o => console.log(o))

0 commit comments

Comments
 (0)