Skip to content

Commit 8f24a56

Browse files
read defines, includes and libs in npm process and pass them as string list to node-gyp process + read build envs from package.json
1 parent 036a545 commit 8f24a56

File tree

14 files changed

+173
-115
lines changed

14 files changed

+173
-115
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ script:
199199
fi
200200

201201
after_success:
202-
- if [ $BUILD_TASK = 'cover' ]; then
202+
- if [ "$BUILD_TASK" == "cover" ]; then
203203
npm install;
204204
npm run codecov -- -t $CODECOV_TOKEN;
205205
fi

appveyor.yml

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,22 @@ environment:
4343
- nodejs_version: 6
4444
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
4545
OPENCV_VERSION: "%OPENCV4_LATEST%"
46+
- nodejs_version: 12
47+
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
48+
OPENCV_VERSION: "%OPENCV4_LATEST%"
49+
BUILD_TASK: "ENVS"
4650

4751
install:
4852
- cmd: choco install OpenCV -y -version %OPENCV_VERSION%
49-
- IF EXIST c:\tools\opencv* CD c:\tools\opencv*
50-
- SET OPENCV_INCLUDE_DIR=%CD%\build\include
51-
- SET OPENCV_LIB_DIR=%CD%\build\x64\vc14\lib
52-
- SET OPENCV_BIN_DIR=%CD%\build\x64\vc14\bin
53-
- SET PATH=%PATH%;%OPENCV_BIN_DIR%;
54-
53+
if ($env:BUILD_TASK -neq "ENVS") {
54+
appveyor DownloadFile https://github.com/bazelbuild/bazel/releases/download/0.28.1/bazel-0.28.1-windows-x86_64.exe -FileName bazel.exe
55+
}
56+
- if not "%BUILD_TASK%" == "ENVS" (
57+
SET OPENCV_INCLUDE_DIR=c:\tools\opencv\build\include
58+
SET OPENCV_LIB_DIR=c:\tools\opencv\build\x64\vc14\lib
59+
SET OPENCV_BIN_DIR=c:\tools\opencv\build\x64\vc14\bin
60+
SET PATH=%PATH%;%OPENCV_BIN_DIR%;
61+
)
5562
- ps: Install-Product node $env:nodejs_version x64
5663
- node --version
5764
- npm install -g node-gyp
@@ -62,7 +69,11 @@ build: off
6269

6370
test_script:
6471
- node --version
65-
- cmd: cd c:\projects\opencv4nodejs\test
66-
- npm install
67-
- npm run test-appveyor
68-
- npm run test-externalMemTracking
72+
- if "%BUILD_TASK%" == "ENVS" (
73+
cmd: cd .\ci\envs
74+
) else (
75+
cmd: cd c:\projects\opencv4nodejs\test
76+
npm install
77+
npm run test-appveyor
78+
npm run test-externalMemTracking
79+
)

binding.gyp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
"targets": [{
33
"target_name": "opencv4nodejs",
44
"defines": [
5-
"<!@(node ./lib/defines.js)",
5+
"<!@(node ./install/parseEnv.js OPENCV4NODEJS_DEFINES)",
66
],
77
"include_dirs" : [
8-
"<!@(node ./lib/includes.js)",
8+
"<!@(node ./install/parseEnv.js OPENCV4NODEJS_INCLUDES)",
99
"cc",
1010
"cc/core",
1111
"<!(node -e \"require('nan')\")",
1212
"<!(node -e \"require('native-node-utils')\")"
1313
],
1414
"libraries": [
15-
"<!@(node ./lib/libs.js)"
15+
"<!@(node ./install/parseEnv.js OPENCV4NODEJS_LIBRARIES)",
1616
],
1717
"sources": [
1818
"cc/opencv4nodejs.cc",

ci/envs/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"scripts": {
3+
"test": "node ./test.js"
4+
},
5+
"dependencies": {
6+
"opencv4nodejs": "../"
7+
},
8+
"opencv4nodejs": {
9+
"disableAutoBuild": 1,
10+
"opencvIncludeDir": "C:\\tools\\opencv\\build\\include",
11+
"opencvLibDir": "C:\\tools\\opencv\\build\\x64\\vc14\\lib",
12+
"opencvBinDir": "C:\\tools\\opencv\\build\\x64\\vc14\\bin"
13+
}
14+
}

ci/envs/test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const cv = require('opencv4nodejs')
2+
3+
if (!cv) {
4+
throw new Error('failed to require opencv4nodejs')
5+
}

install/install.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const opencvBuild = require('opencv-build')
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 [defaultLibDir, 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+
34+
log.info('install', 'using lib dir: ' + libDir)
35+
36+
if (!fs.existsSync(libDir)) {
37+
throw new Error('library dir does not exist: ' + libDir)
38+
}
39+
40+
const libsFoundInDir = opencvBuild
41+
.getLibs(libDir)
42+
.filter(lib => lib.libPath)
43+
44+
if (!libsFoundInDir.length) {
45+
throw new Error('no OpenCV libraries found in lib dir: ' + libDir)
46+
}
47+
48+
log.info('install', 'found the following libs:')
49+
libsFoundInDir.forEach(lib => log.info('install', lib.opencvModule + ' : ' + lib.libPath))
50+
51+
const defines = libsFoundInDir
52+
.map(lib => `OPENCV4NODEJS_FOUND_LIBRARY_${lib.opencvModule.toUpperCase()}`)
53+
54+
const explicitIncludeDir = resolvePath(process.env.OPENCV_INCLUDE_DIR)
55+
const includes = opencvBuild.isAutoBuildDisabled()
56+
? (explicitIncludeDir ? [explicitIncludeDir] : getDefaultIncludeDirs())
57+
: [resolvePath(opencvBuild.opencvInclude), resolvePath(opencvBuild.opencv4Include)]
58+
59+
const libs = opencvBuild.isWin()
60+
? libsFoundInDir.map(lib => resolvePath(lib.libPath))
61+
// dynamically link libs if not on windows
62+
: ['-L' + libDir]
63+
.concat(libsFoundInDir.map(lib => '-lopencv_' + lib.opencvModule))
64+
.concat('-Wl,-rpath,' + libDir)
65+
66+
console.log()
67+
log.info('install', 'setting the following defines:')
68+
defines.forEach(def => log.info('defines', def))
69+
console.log()
70+
log.info('install', 'setting the following includes:')
71+
includes.forEach(def => log.info('includes', def))
72+
console.log()
73+
log.info('install', 'setting the following libs:')
74+
libs.forEach(def => log.info('libs', def))
75+
76+
process.env['OPENCV4NODEJS_DEFINES'] = defines.join('\n')
77+
process.env['OPENCV4NODEJS_INCLUDES'] = includes.join('\n')
78+
process.env['OPENCV4NODEJS_LIBRARIES'] = libs.join('\n')
79+
80+
const child = child_process.exec('node-gyp rebuild --jobs max', {}, function(err, stdout, stderr) {
81+
const _err = err || stderr
82+
if (_err) log.error(_err)
83+
})
84+
child.stdout.pipe(process.stdout)
85+
child.stderr.pipe(process.stderr)

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))

lib/commons.js

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
1-
const fs = require('fs');
2-
const path = require('path');
1+
const fs = require('fs')
2+
const path = require('path')
33

44
function resolvePath(filePath, file) {
55
if (!filePath) {
6-
return undefined;
6+
return undefined
77
}
8-
return (file ? path.resolve(filePath, file) : path.resolve(filePath)).replace(/\\/g, '/');
9-
}
10-
11-
const defaultDir = '/usr/local';
12-
const defaultIncludeDir = `${defaultDir}/include`;
13-
const defaultIncludeDirOpenCV4 = `${defaultIncludeDir}/opencv4`;
14-
15-
function getLibDir() {
16-
const libPath = resolvePath(process.env.OPENCV_LIB_DIR)
17-
if (process.platform === 'win32' && !libPath) {
18-
throw new Error('OPENCV_LIB_DIR is not defined')
19-
}
20-
return libPath || `${defaultDir}/lib`;
8+
return (file ? path.resolve(filePath, file) : path.resolve(filePath)).replace(/\\/g, '/')
219
}
2210

2311
module.exports = {
24-
resolvePath,
25-
defaultDir,
26-
defaultIncludeDir,
27-
defaultIncludeDirOpenCV4,
28-
getLibDir
29-
};
12+
resolvePath
13+
}

lib/cv.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,34 @@ const path = require('path');
22
const opencvBuild = require('opencv-build');
33
const { resolvePath } = require('./commons');
44

5-
// ensure binaries are added to path on windows
6-
if (!opencvBuild.isAutoBuildDisabled() && process.platform === 'win32') {
7-
// append opencv binary path to node process
8-
if (!process.env.path.includes(opencvBuild.opencvBinDir)) {
9-
process.env.path = `${process.env.path};${opencvBuild.opencvBinDir};`
5+
const requirePath = path.join(__dirname, process.env.BINDINGS_DEBUG ? '../build/Debug/opencv4nodejs' : '../build/Release/opencv4nodejs')
6+
7+
function tryGetOpencvBinDir() {
8+
// if the auto build is disabled via environment do not even attempt
9+
// to read package.json
10+
if (opencvBuild.isAutoBuildDisabled()) {
11+
return opencvBuild.opencvBinDir
1012
}
13+
14+
const envs = opencvBuild.readEnvsFromPackageJson()
15+
return envs.disableAutoBuild
16+
? (envs.opencvBinDir || process.env.OPENCV_BIN_DIR)
17+
: opencvBuild.opencvBinDir
1118
}
1219

13-
let cv;
14-
if (process.env.BINDINGS_DEBUG) {
15-
cv = require(path.join(__dirname, '../build/Debug/opencv4nodejs'));
16-
} else {
17-
cv = require(path.join(__dirname, '../build/Release/opencv4nodejs'));
20+
let cv = null
21+
try {
22+
cv = require(requirePath);
23+
} catch (err) {
24+
25+
// TODO: non windows?
26+
const opencvBinDir = tryGetOpencvBinDir()
27+
28+
// ensure binaries are added to path on windows
29+
if (!process.env.path.includes(opencvBinDir)) {
30+
process.env.path = `${process.env.path};${opencvBinDir};`
31+
}
32+
cv = require(requirePath);
1833
}
1934

2035
// resolve haarcascade files

lib/defines.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)