From 8f24a562bfc906a8840ac7603609e9d53c05cec6 Mon Sep 17 00:00:00 2001 From: vincent Date: Sun, 8 Sep 2019 16:44:40 +0200 Subject: [PATCH 01/13] read defines, includes and libs in npm process and pass them as string list to node-gyp process + read build envs from package.json --- .travis.yml | 2 +- appveyor.yml | 31 ++++++++++------ binding.gyp | 6 ++-- ci/envs/package.json | 14 ++++++++ ci/envs/test.js | 5 +++ install/install.js | 85 ++++++++++++++++++++++++++++++++++++++++++++ install/parseEnv.js | 7 ++++ lib/commons.js | 28 ++++----------- lib/cv.js | 35 ++++++++++++------ lib/defines.js | 14 -------- lib/includes.js | 17 --------- lib/libs.js | 25 ------------- package-lock.json | 14 ++------ package.json | 5 +-- 14 files changed, 173 insertions(+), 115 deletions(-) create mode 100644 ci/envs/package.json create mode 100644 ci/envs/test.js create mode 100644 install/install.js create mode 100644 install/parseEnv.js delete mode 100644 lib/defines.js delete mode 100644 lib/includes.js delete mode 100644 lib/libs.js diff --git a/.travis.yml b/.travis.yml index e20295f98..8bb676aa3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -199,7 +199,7 @@ script: fi after_success: - - if [ $BUILD_TASK = 'cover' ]; then + - if [ "$BUILD_TASK" == "cover" ]; then npm install; npm run codecov -- -t $CODECOV_TOKEN; fi diff --git a/appveyor.yml b/appveyor.yml index 6d1321fad..d8d6c2403 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -43,15 +43,22 @@ environment: - nodejs_version: 6 APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 OPENCV_VERSION: "%OPENCV4_LATEST%" + - nodejs_version: 12 + APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 + OPENCV_VERSION: "%OPENCV4_LATEST%" + BUILD_TASK: "ENVS" install: - cmd: choco install OpenCV -y -version %OPENCV_VERSION% - - IF EXIST c:\tools\opencv* CD c:\tools\opencv* - - SET OPENCV_INCLUDE_DIR=%CD%\build\include - - SET OPENCV_LIB_DIR=%CD%\build\x64\vc14\lib - - SET OPENCV_BIN_DIR=%CD%\build\x64\vc14\bin - - SET PATH=%PATH%;%OPENCV_BIN_DIR%; - + if ($env:BUILD_TASK -neq "ENVS") { + appveyor DownloadFile https://github.com/bazelbuild/bazel/releases/download/0.28.1/bazel-0.28.1-windows-x86_64.exe -FileName bazel.exe + } + - if not "%BUILD_TASK%" == "ENVS" ( + SET OPENCV_INCLUDE_DIR=c:\tools\opencv\build\include + SET OPENCV_LIB_DIR=c:\tools\opencv\build\x64\vc14\lib + SET OPENCV_BIN_DIR=c:\tools\opencv\build\x64\vc14\bin + SET PATH=%PATH%;%OPENCV_BIN_DIR%; + ) - ps: Install-Product node $env:nodejs_version x64 - node --version - npm install -g node-gyp @@ -62,7 +69,11 @@ build: off test_script: - node --version - - cmd: cd c:\projects\opencv4nodejs\test - - npm install - - npm run test-appveyor - - npm run test-externalMemTracking \ No newline at end of file + - if "%BUILD_TASK%" == "ENVS" ( + cmd: cd .\ci\envs + ) else ( + cmd: cd c:\projects\opencv4nodejs\test + npm install + npm run test-appveyor + npm run test-externalMemTracking + ) \ No newline at end of file diff --git a/binding.gyp b/binding.gyp index de2d46b84..a19da5993 100644 --- a/binding.gyp +++ b/binding.gyp @@ -2,17 +2,17 @@ "targets": [{ "target_name": "opencv4nodejs", "defines": [ - " lib.libPath) + +if (!libsFoundInDir.length) { + throw new Error('no OpenCV libraries found in lib dir: ' + libDir) +} + +log.info('install', 'found the following libs:') +libsFoundInDir.forEach(lib => log.info('install', lib.opencvModule + ' : ' + lib.libPath)) + +const defines = libsFoundInDir + .map(lib => `OPENCV4NODEJS_FOUND_LIBRARY_${lib.opencvModule.toUpperCase()}`) + +const explicitIncludeDir = resolvePath(process.env.OPENCV_INCLUDE_DIR) +const includes = opencvBuild.isAutoBuildDisabled() + ? (explicitIncludeDir ? [explicitIncludeDir] : getDefaultIncludeDirs()) + : [resolvePath(opencvBuild.opencvInclude), resolvePath(opencvBuild.opencv4Include)] + +const libs = opencvBuild.isWin() + ? libsFoundInDir.map(lib => resolvePath(lib.libPath)) + // dynamically link libs if not on windows + : ['-L' + libDir] + .concat(libsFoundInDir.map(lib => '-lopencv_' + lib.opencvModule)) + .concat('-Wl,-rpath,' + libDir) + +console.log() +log.info('install', 'setting the following defines:') +defines.forEach(def => log.info('defines', def)) +console.log() +log.info('install', 'setting the following includes:') +includes.forEach(def => log.info('includes', def)) +console.log() +log.info('install', 'setting the following libs:') +libs.forEach(def => log.info('libs', def)) + +process.env['OPENCV4NODEJS_DEFINES'] = defines.join('\n') +process.env['OPENCV4NODEJS_INCLUDES'] = includes.join('\n') +process.env['OPENCV4NODEJS_LIBRARIES'] = libs.join('\n') + +const child = child_process.exec('node-gyp rebuild --jobs max', {}, function(err, stdout, stderr) { + const _err = err || stderr + if (_err) log.error(_err) +}) +child.stdout.pipe(process.stdout) +child.stderr.pipe(process.stderr) \ No newline at end of file diff --git a/install/parseEnv.js b/install/parseEnv.js new file mode 100644 index 000000000..8861cf686 --- /dev/null +++ b/install/parseEnv.js @@ -0,0 +1,7 @@ +const envName = process.argv[2] + +if (!envName) { + throw new Error('no env name passed to parseEnv') +} +const outputs = (process.env[envName] || '').split('\n') +outputs.forEach(o => console.log(o)) \ No newline at end of file diff --git a/lib/commons.js b/lib/commons.js index 9da59b2f0..3ee60bc67 100644 --- a/lib/commons.js +++ b/lib/commons.js @@ -1,29 +1,13 @@ -const fs = require('fs'); -const path = require('path'); +const fs = require('fs') +const path = require('path') function resolvePath(filePath, file) { if (!filePath) { - return undefined; + return undefined } - return (file ? path.resolve(filePath, file) : path.resolve(filePath)).replace(/\\/g, '/'); -} - -const defaultDir = '/usr/local'; -const defaultIncludeDir = `${defaultDir}/include`; -const defaultIncludeDirOpenCV4 = `${defaultIncludeDir}/opencv4`; - -function getLibDir() { - const libPath = resolvePath(process.env.OPENCV_LIB_DIR) - if (process.platform === 'win32' && !libPath) { - throw new Error('OPENCV_LIB_DIR is not defined') - } - return libPath || `${defaultDir}/lib`; + return (file ? path.resolve(filePath, file) : path.resolve(filePath)).replace(/\\/g, '/') } module.exports = { - resolvePath, - defaultDir, - defaultIncludeDir, - defaultIncludeDirOpenCV4, - getLibDir -}; + resolvePath +} diff --git a/lib/cv.js b/lib/cv.js index 44e24dd96..3a90d68c6 100644 --- a/lib/cv.js +++ b/lib/cv.js @@ -2,19 +2,34 @@ const path = require('path'); const opencvBuild = require('opencv-build'); const { resolvePath } = require('./commons'); -// ensure binaries are added to path on windows -if (!opencvBuild.isAutoBuildDisabled() && process.platform === 'win32') { - // append opencv binary path to node process - if (!process.env.path.includes(opencvBuild.opencvBinDir)) { - process.env.path = `${process.env.path};${opencvBuild.opencvBinDir};` +const requirePath = path.join(__dirname, process.env.BINDINGS_DEBUG ? '../build/Debug/opencv4nodejs' : '../build/Release/opencv4nodejs') + +function tryGetOpencvBinDir() { + // if the auto build is disabled via environment do not even attempt + // to read package.json + if (opencvBuild.isAutoBuildDisabled()) { + return opencvBuild.opencvBinDir } + + const envs = opencvBuild.readEnvsFromPackageJson() + return envs.disableAutoBuild + ? (envs.opencvBinDir || process.env.OPENCV_BIN_DIR) + : opencvBuild.opencvBinDir } -let cv; -if (process.env.BINDINGS_DEBUG) { - cv = require(path.join(__dirname, '../build/Debug/opencv4nodejs')); -} else { - cv = require(path.join(__dirname, '../build/Release/opencv4nodejs')); +let cv = null +try { + cv = require(requirePath); +} catch (err) { + + // TODO: non windows? + const opencvBinDir = tryGetOpencvBinDir() + + // ensure binaries are added to path on windows + if (!process.env.path.includes(opencvBinDir)) { + process.env.path = `${process.env.path};${opencvBinDir};` + } + cv = require(requirePath); } // resolve haarcascade files diff --git a/lib/defines.js b/lib/defines.js deleted file mode 100644 index 01c8c0973..000000000 --- a/lib/defines.js +++ /dev/null @@ -1,14 +0,0 @@ -const opencvBuild = require('opencv-build'); -const { getLibDir } = require('./commons'); - -if (opencvBuild.isAutoBuildDisabled()) { - opencvBuild.getLibs(getLibDir()) - .filter(lib => lib.libPath) - .map(lib => lib.opencvModule) - .forEach(opencvModule => console.log(`OPENCV4NODEJS_FOUND_LIBRARY_${opencvModule.toUpperCase()}`)); - - return; -} - -// set defines from auto build -opencvBuild.opencvModules.forEach(m => console.log(`OPENCV4NODEJS_FOUND_LIBRARY_${m.toUpperCase()}`)); \ No newline at end of file diff --git a/lib/includes.js b/lib/includes.js deleted file mode 100644 index ffdc10b6d..000000000 --- a/lib/includes.js +++ /dev/null @@ -1,17 +0,0 @@ -const opencvBuild = require('opencv-build'); -const { resolvePath, defaultIncludeDir, defaultIncludeDirOpenCV4 } = require('./commons'); - -if (opencvBuild.isAutoBuildDisabled()) { - const explicitIncludeDir = resolvePath(process.env.OPENCV_INCLUDE_DIR); - if (explicitIncludeDir) { - console.log(explicitIncludeDir); - return; - } - console.log(defaultIncludeDir); - console.log(defaultIncludeDirOpenCV4); - return; -} - -// set include dir from auto build -console.log(resolvePath(opencvBuild.opencvInclude)); -console.log(resolvePath(opencvBuild.opencv4Include)); diff --git a/lib/libs.js b/lib/libs.js deleted file mode 100644 index 90cdefaed..000000000 --- a/lib/libs.js +++ /dev/null @@ -1,25 +0,0 @@ -const opencvBuild = require('opencv-build'); -const { resolvePath, getLibDir } = require('./commons'); - -function linkLibs(libs) { - libs - .map(lib => lib.libPath) - .filter(libPath => libPath) - .forEach(libPath => console.log(resolvePath(libPath))); -} - -if (opencvBuild.isAutoBuildDisabled()) { - linkLibs(opencvBuild.getLibs(getLibDir())); - return; -} - -// get libs from auto build -if (process.platform === 'win32') { - linkLibs(opencvBuild.getLibs(resolvePath(opencvBuild.opencvLibDir))); - return; -} - -// if not windows, link libs dynamically -console.log('-L' + resolvePath(opencvBuild.opencvLibDir)); -opencvBuild.opencvModules.forEach(lib => console.log('-lopencv_' + lib)); -console.log('-Wl,-rpath,' + resolvePath(opencvBuild.opencvLibDir)); diff --git a/package-lock.json b/package-lock.json index db67b0595..aa2654ddc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -122,19 +122,11 @@ "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" }, "opencv-build": { - "version": "0.1.5", - "resolved": "https://registry.npmjs.org/opencv-build/-/opencv-build-0.1.5.tgz", - "integrity": "sha512-KcK3+EwgPowTMogTxC5+iZgn8EraQlof339EIV3dupbMqBWe2ZgWzQDEGlt00DgMPmcFkn0+bSm4ifuQSevGow==", + "version": "0.1.9", + "resolved": "https://registry.npmjs.org/opencv-build/-/opencv-build-0.1.9.tgz", + "integrity": "sha512-tgT/bnJAcYROen9yaPynfK98IMl62mPSgMLmTx41911m5bczlq21xtE5r+UWLB/xEo/0hKk6tl5zHyxV/JS5Rg==", "requires": { - "@types/node": "^11.10.5", "npmlog": "^4.1.2" - }, - "dependencies": { - "@types/node": { - "version": "11.13.19", - "resolved": "https://registry.npmjs.org/@types/node/-/node-11.13.19.tgz", - "integrity": "sha512-tLRDU1hmcWamtgRT2iVRdraAQVGFQGgtcqracSo9XyMN1VeZLSVGb8RJJxVqab7UGbijoUijGPVFMjmqzyZIUw==" - } } }, "process-nextick-args": { diff --git a/package.json b/package.json index cc9f3bf90..c470f6f76 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "main": "./lib/opencv4nodejs.js", "typings": "./lib/index.d.ts", "scripts": { - "install": "node-gyp rebuild --jobs max", + "install": "node ./install/install.js", "configure": "node-gyp configure", "build": "node-gyp configure build --jobs max", "rebuild": "node-gyp rebuild --jobs max", @@ -40,7 +40,8 @@ "dependencies": { "nan": "^2.14.0", "native-node-utils": "^0.2.7", - "opencv-build": "^0.1.5" + "npmlog": "^4.1.2", + "opencv-build": "^0.1.9" }, "optionalDependencies": { "@types/node": ">6" From 8a25693c127d22ffbffb4c722773745a91270a90 Mon Sep 17 00:00:00 2001 From: vincent Date: Sun, 8 Sep 2019 17:06:26 +0200 Subject: [PATCH 02/13] fixes appveyor.yml --- appveyor.yml | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index d8d6c2403..788a1f800 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -50,15 +50,11 @@ environment: install: - cmd: choco install OpenCV -y -version %OPENCV_VERSION% - if ($env:BUILD_TASK -neq "ENVS") { - appveyor DownloadFile https://github.com/bazelbuild/bazel/releases/download/0.28.1/bazel-0.28.1-windows-x86_64.exe -FileName bazel.exe - } - - if not "%BUILD_TASK%" == "ENVS" ( - SET OPENCV_INCLUDE_DIR=c:\tools\opencv\build\include - SET OPENCV_LIB_DIR=c:\tools\opencv\build\x64\vc14\lib - SET OPENCV_BIN_DIR=c:\tools\opencv\build\x64\vc14\bin + - if not "%BUILD_TASK%" == "ENVS" \ + SET OPENCV_INCLUDE_DIR=c:\tools\opencv\build\include && \ + SET OPENCV_LIB_DIR=c:\tools\opencv\build\x64\vc14\lib && \ + SET OPENCV_BIN_DIR=c:\tools\opencv\build\x64\vc14\bin && \ SET PATH=%PATH%;%OPENCV_BIN_DIR%; - ) - ps: Install-Product node $env:nodejs_version x64 - node --version - npm install -g node-gyp @@ -69,11 +65,13 @@ build: off test_script: - node --version - - if "%BUILD_TASK%" == "ENVS" ( - cmd: cd .\ci\envs - ) else ( - cmd: cd c:\projects\opencv4nodejs\test - npm install - npm run test-appveyor - npm run test-externalMemTracking + - if "%BUILD_TASK%" == "ENVS" ( \ + cd .\ci\envs \ + npm install \ + npm test \ + ) else ( \ + cd c:\projects\opencv4nodejs\test \ + npm install \ + npm run test-appveyor \ + npm run test-externalMemTracking \ ) \ No newline at end of file From ffdb08e8dc385ce9b76261d4d5095ec64913c38d Mon Sep 17 00:00:00 2001 From: vincent Date: Sun, 8 Sep 2019 17:17:57 +0200 Subject: [PATCH 03/13] fixes appveyor.yml --- appveyor.yml | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 788a1f800..0de688286 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -50,11 +50,12 @@ environment: install: - cmd: choco install OpenCV -y -version %OPENCV_VERSION% - - if not "%BUILD_TASK%" == "ENVS" \ - SET OPENCV_INCLUDE_DIR=c:\tools\opencv\build\include && \ - SET OPENCV_LIB_DIR=c:\tools\opencv\build\x64\vc14\lib && \ - SET OPENCV_BIN_DIR=c:\tools\opencv\build\x64\vc14\bin && \ - SET PATH=%PATH%;%OPENCV_BIN_DIR%; + - if not "%BUILD_TASK%" == "ENVS" ( + SET OPENCV_INCLUDE_DIR=c:\tools\opencv\build\include + SET OPENCV_LIB_DIR=c:\tools\opencv\build\x64\vc14\lib + SET OPENCV_BIN_DIR=c:\tools\opencv\build\x64\vc14\bin + SET PATH=%PATH%;%OPENCV_BIN_DIR%; + ) - ps: Install-Product node $env:nodejs_version x64 - node --version - npm install -g node-gyp @@ -65,13 +66,13 @@ build: off test_script: - node --version - - if "%BUILD_TASK%" == "ENVS" ( \ - cd .\ci\envs \ - npm install \ - npm test \ + - if "%BUILD_TASK%" == "ENVS" ( + cd .\ci\envs + npm install + npm test ) else ( \ - cd c:\projects\opencv4nodejs\test \ - npm install \ - npm run test-appveyor \ - npm run test-externalMemTracking \ + cd c:\projects\opencv4nodejs\test + npm install + npm run test-appveyor + npm run test-externalMemTracking ) \ No newline at end of file From bd46d39b267de71c5d5382f081a883bda610dadd Mon Sep 17 00:00:00 2001 From: vincent Date: Sun, 8 Sep 2019 19:36:21 +0200 Subject: [PATCH 04/13] fixes appveyor.yml + and add install to docker whitelist --- .dockerignore | 1 + appveyor.yml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.dockerignore b/.dockerignore index f5d6cbc4e..69375ec74 100644 --- a/.dockerignore +++ b/.dockerignore @@ -4,6 +4,7 @@ ci/coverage-report !package.json !binding.gyp !lib +!install !test !cc !data/got.jpg diff --git a/appveyor.yml b/appveyor.yml index 0de688286..2a1c3cf4f 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -70,7 +70,7 @@ test_script: cd .\ci\envs npm install npm test - ) else ( \ + ) else ( cd c:\projects\opencv4nodejs\test npm install npm run test-appveyor From 3d20b61e756387098c4cec7d67bec3a8c50fcb19 Mon Sep 17 00:00:00 2001 From: vincent Date: Sun, 8 Sep 2019 19:44:45 +0200 Subject: [PATCH 05/13] fixes appveyor.yml --- appveyor.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 2a1c3cf4f..f54335b97 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -51,9 +51,9 @@ environment: install: - cmd: choco install OpenCV -y -version %OPENCV_VERSION% - if not "%BUILD_TASK%" == "ENVS" ( - SET OPENCV_INCLUDE_DIR=c:\tools\opencv\build\include - SET OPENCV_LIB_DIR=c:\tools\opencv\build\x64\vc14\lib - SET OPENCV_BIN_DIR=c:\tools\opencv\build\x64\vc14\bin + SET OPENCV_INCLUDE_DIR=c:\tools\opencv\build\include && + SET OPENCV_LIB_DIR=c:\tools\opencv\build\x64\vc14\lib && + SET OPENCV_BIN_DIR=c:\tools\opencv\build\x64\vc14\bin && SET PATH=%PATH%;%OPENCV_BIN_DIR%; ) - ps: Install-Product node $env:nodejs_version x64 @@ -67,12 +67,12 @@ build: off test_script: - node --version - if "%BUILD_TASK%" == "ENVS" ( - cd .\ci\envs - npm install + cd .\ci\envs && + npm install && npm test ) else ( - cd c:\projects\opencv4nodejs\test - npm install - npm run test-appveyor + cd c:\projects\opencv4nodejs\test && + npm install && + npm run test-appveyor && npm run test-externalMemTracking ) \ No newline at end of file From 6dcbc179b9ee3c633adbbea4297da165afb8a0f2 Mon Sep 17 00:00:00 2001 From: vincent Date: Sun, 8 Sep 2019 19:49:09 +0200 Subject: [PATCH 06/13] fixes appveyor.yml --- appveyor.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index f54335b97..d1d589856 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -51,10 +51,10 @@ environment: install: - cmd: choco install OpenCV -y -version %OPENCV_VERSION% - if not "%BUILD_TASK%" == "ENVS" ( - SET OPENCV_INCLUDE_DIR=c:\tools\opencv\build\include && - SET OPENCV_LIB_DIR=c:\tools\opencv\build\x64\vc14\lib && - SET OPENCV_BIN_DIR=c:\tools\opencv\build\x64\vc14\bin && - SET PATH=%PATH%;%OPENCV_BIN_DIR%; + SET OPENCV_INCLUDE_DIR="c:\tools\opencv\build\include" && + SET OPENCV_LIB_DIR="c:\tools\opencv\build\x64\vc14\lib" && + SET OPENCV_BIN_DIR="c:\tools\opencv\build\x64\vc14\bin" && + SET PATH="%PATH%;%OPENCV_BIN_DIR%;" ) - ps: Install-Product node $env:nodejs_version x64 - node --version From 74ec512dc660b549e289e31e9c890b7c57b3d1f6 Mon Sep 17 00:00:00 2001 From: vincent Date: Sat, 21 Sep 2019 12:14:41 +0200 Subject: [PATCH 07/13] assert that opencv version matches version passed to build script + fixed default include dir --- ci/test/test.sh | 2 +- install/install.js | 6 +++--- test/tests/index.test.js | 12 ++++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/ci/test/test.sh b/ci/test/test.sh index 76a4cadff..c6c90c4fa 100755 --- a/ci/test/test.sh +++ b/ci/test/test.sh @@ -1,4 +1,4 @@ #!/bin/sh image=opencv4nodejs-ci:$1-node$2 docker build -t $image -f ./Dockerfile --build-arg TAG=$1 --build-arg NODE_MAJOR_VERSION=$2 ../../ -docker run -e TEST_MODULE_LIST=$TEST_MODULE_LIST $image \ No newline at end of file +docker run -e OPENCV_VERSION=$OPENCV_VERSION -e TEST_MODULE_LIST=$TEST_MODULE_LIST $image \ No newline at end of file diff --git a/install/install.js b/install/install.js index b92865bc9..93830fbac 100644 --- a/install/install.js +++ b/install/install.js @@ -14,7 +14,7 @@ function getDefaultIncludeDirs() { if (opencvBuild.isWin()) { throw new Error('OPENCV_INCLUDE_DIR has to be defined on windows when auto build is disabled') } - return [defaultLibDir, defaultIncludeDirOpenCV4] + return [defaultIncludeDir, defaultIncludeDirOpenCV4] } function getDefaultLibDir() { @@ -68,10 +68,10 @@ log.info('install', 'setting the following defines:') defines.forEach(def => log.info('defines', def)) console.log() log.info('install', 'setting the following includes:') -includes.forEach(def => log.info('includes', def)) +includes.forEach(inc => log.info('includes', inc)) console.log() log.info('install', 'setting the following libs:') -libs.forEach(def => log.info('libs', def)) +libs.forEach(lib => log.info('libs', lib)) process.env['OPENCV4NODEJS_DEFINES'] = defines.join('\n') process.env['OPENCV4NODEJS_INCLUDES'] = includes.join('\n') diff --git a/test/tests/index.test.js b/test/tests/index.test.js index ebd2b9d36..7797b7bc3 100644 --- a/test/tests/index.test.js +++ b/test/tests/index.test.js @@ -64,9 +64,21 @@ describe('cv', () => { builtModules = builtModules.filter(m => m !== 'dnn') } + const opencvVersionString = `${cv.version.major}.${cv.version.minor}.${cv.version.revision}` + + console.log('envs are:') + console.log('OPENCV_VERSION:', process.env.OPENCV_VERSION) + console.log('TEST_MODULE_LIST:', process.env.TEST_MODULE_LIST) + console.log('APPVEYOR_BUILD:', process.env.APPVEYOR_BUILD) + console.log() + console.log('OpenCV version is:', opencvVersionString) console.log('compiled with the following modules:', cv.modules) console.log('expected modules to be built:', builtModules) + it('OpenCV version should match', () => { + expect(process.env.OPENCV_VERSION).to.equal(opencvVersionString) + }) + it('all modules should be built', () => { builtModules.forEach(m => expect(cv.modules).to.have.property(m)); }) From 76b01862b4e99245c7278c554f6cbecbcb072c07 Mon Sep 17 00:00:00 2001 From: vincent Date: Sat, 21 Sep 2019 12:42:10 +0200 Subject: [PATCH 08/13] fixes opencv version check + attempt to fix appveyor.yml --- appveyor.yml | 10 ++++------ test/tests/index.test.js | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index d1d589856..486dee127 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -50,12 +50,10 @@ environment: install: - cmd: choco install OpenCV -y -version %OPENCV_VERSION% - - if not "%BUILD_TASK%" == "ENVS" ( - SET OPENCV_INCLUDE_DIR="c:\tools\opencv\build\include" && - SET OPENCV_LIB_DIR="c:\tools\opencv\build\x64\vc14\lib" && - SET OPENCV_BIN_DIR="c:\tools\opencv\build\x64\vc14\bin" && - SET PATH="%PATH%;%OPENCV_BIN_DIR%;" - ) + - if not "%BUILD_TASK%" == "ENVS" SET OPENCV_INCLUDE_DIR=c:\tools\opencv\build\include + - if not "%BUILD_TASK%" == "ENVS" SET OPENCV_LIB_DIR=c:\tools\opencv\build\x64\vc14\lib + - if not "%BUILD_TASK%" == "ENVS" SET OPENCV_BIN_DIR=c:\tools\opencv\build\x64\vc14\bin + - if not "%BUILD_TASK%" == "ENVS" SET PATH=%PATH%;%OPENCV_BIN_DIR%; - ps: Install-Product node $env:nodejs_version x64 - node --version - npm install -g node-gyp diff --git a/test/tests/index.test.js b/test/tests/index.test.js index 7797b7bc3..90c38ed52 100644 --- a/test/tests/index.test.js +++ b/test/tests/index.test.js @@ -76,7 +76,7 @@ describe('cv', () => { console.log('expected modules to be built:', builtModules) it('OpenCV version should match', () => { - expect(process.env.OPENCV_VERSION).to.equal(opencvVersionString) + expect((process.env.OPENCV_VERSION || '').substr(0, 5)).to.equal(opencvVersionString) }) it('all modules should be built', () => { From 5890acf7ddb6d493bf88322ac211316418ea52cd Mon Sep 17 00:00:00 2001 From: vincent Date: Sat, 21 Sep 2019 17:02:26 +0200 Subject: [PATCH 09/13] fixes OpenCV version check on osx + fixes build-debug for cover task + fixes appveyor env test --- appveyor.yml | 5 ++--- install/install.js | 3 ++- package.json | 2 +- test/tests/index.test.js | 5 ++++- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 486dee127..88d19146c 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -56,9 +56,6 @@ install: - if not "%BUILD_TASK%" == "ENVS" SET PATH=%PATH%;%OPENCV_BIN_DIR%; - ps: Install-Product node $env:nodejs_version x64 - node --version - - npm install -g node-gyp - - cd c:\projects\opencv4nodejs - - npm install build: off @@ -69,6 +66,8 @@ test_script: npm install && npm test ) else ( + cd c:\projects\opencv4nodejs && + npm install && cd c:\projects\opencv4nodejs\test && npm install && npm run test-appveyor && diff --git a/install/install.js b/install/install.js index 93830fbac..7edaab797 100644 --- a/install/install.js +++ b/install/install.js @@ -77,7 +77,8 @@ process.env['OPENCV4NODEJS_DEFINES'] = defines.join('\n') process.env['OPENCV4NODEJS_INCLUDES'] = includes.join('\n') process.env['OPENCV4NODEJS_LIBRARIES'] = libs.join('\n') -const child = child_process.exec('node-gyp rebuild --jobs max', {}, function(err, stdout, stderr) { +const flags = process.env.BINDINGS_DEBUG ? '--jobs max --debug' : '--jobs max' +const child = child_process.exec('node-gyp rebuild ' + flags, {}, function(err, stdout, stderr) { const _err = err || stderr if (_err) log.error(_err) }) diff --git a/package.json b/package.json index c470f6f76..1549964a4 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "build": "node-gyp configure build --jobs max", "rebuild": "node-gyp rebuild --jobs max", "clean": "node-gyp clean", - "build-debug": "node-gyp rebuild --debug --jobs max" + "build-debug": "set BINDINGS_DEBUG=1 && node ./install/install.js" }, "gypfile": true, "dependencies": { diff --git a/test/tests/index.test.js b/test/tests/index.test.js index 90c38ed52..cb023f3d5 100644 --- a/test/tests/index.test.js +++ b/test/tests/index.test.js @@ -70,13 +70,16 @@ describe('cv', () => { console.log('OPENCV_VERSION:', process.env.OPENCV_VERSION) console.log('TEST_MODULE_LIST:', process.env.TEST_MODULE_LIST) console.log('APPVEYOR_BUILD:', process.env.APPVEYOR_BUILD) + console.log('process.platform:', process.platform) console.log() console.log('OpenCV version is:', opencvVersionString) console.log('compiled with the following modules:', cv.modules) console.log('expected modules to be built:', builtModules) it('OpenCV version should match', () => { - expect((process.env.OPENCV_VERSION || '').substr(0, 5)).to.equal(opencvVersionString) + expect((process.env.OPENCV_VERSION || '').substr(0, 5)).to.equal( + process.platform === 'osx' ? `${cv.version.major}` : opencvVersionString + ) }) it('all modules should be built', () => { From 9479518c0d99d594de4f80db845582d68a7ec634 Mon Sep 17 00:00:00 2001 From: vincent Date: Sat, 21 Sep 2019 19:22:10 +0200 Subject: [PATCH 10/13] fixes OpenCV version check on osx + fixes build-debug for cover task + fixes appveyor env test --- appveyor.yml | 2 +- ci/envs/package.json | 2 +- install/install.js | 4 +++- package.json | 2 +- test/tests/index.test.js | 3 ++- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 88d19146c..7ad8b9a2c 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -62,7 +62,7 @@ build: off test_script: - node --version - if "%BUILD_TASK%" == "ENVS" ( - cd .\ci\envs && + cd c:\projects\opencv4nodejs\ci\envs && npm install && npm test ) else ( diff --git a/ci/envs/package.json b/ci/envs/package.json index 2e9338796..e7a99befc 100644 --- a/ci/envs/package.json +++ b/ci/envs/package.json @@ -3,7 +3,7 @@ "test": "node ./test.js" }, "dependencies": { - "opencv4nodejs": "../" + "opencv4nodejs": "../../" }, "opencv4nodejs": { "disableAutoBuild": 1, diff --git a/install/install.js b/install/install.js index 7edaab797..df5369fe7 100644 --- a/install/install.js +++ b/install/install.js @@ -78,7 +78,9 @@ process.env['OPENCV4NODEJS_INCLUDES'] = includes.join('\n') process.env['OPENCV4NODEJS_LIBRARIES'] = libs.join('\n') const flags = process.env.BINDINGS_DEBUG ? '--jobs max --debug' : '--jobs max' -const child = child_process.exec('node-gyp rebuild ' + flags, {}, function(err, stdout, stderr) { +const nodegypCmd = 'node-gyp rebuild ' + flags +log.info('install', `spawning node gyp process: ${nodegypCmd}`) +const child = child_process.exec(nodegypCmd, {}, function(err, stdout, stderr) { const _err = err || stderr if (_err) log.error(_err) }) diff --git a/package.json b/package.json index 1549964a4..fc557890f 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "build": "node-gyp configure build --jobs max", "rebuild": "node-gyp rebuild --jobs max", "clean": "node-gyp clean", - "build-debug": "set BINDINGS_DEBUG=1 && node ./install/install.js" + "build-debug": "BINDINGS_DEBUG=true && node ./install/install.js" }, "gypfile": true, "dependencies": { diff --git a/test/tests/index.test.js b/test/tests/index.test.js index cb023f3d5..46c45ef44 100644 --- a/test/tests/index.test.js +++ b/test/tests/index.test.js @@ -78,7 +78,8 @@ describe('cv', () => { it('OpenCV version should match', () => { expect((process.env.OPENCV_VERSION || '').substr(0, 5)).to.equal( - process.platform === 'osx' ? `${cv.version.major}` : opencvVersionString + // on osx latest opencv package for major version is installed via brew + process.platform === 'darwin' ? `${cv.version.major}` : opencvVersionString ) }) From cbb7870c3297bce094915509aa5b2c0ae223f007 Mon Sep 17 00:00:00 2001 From: vincent Date: Sun, 22 Sep 2019 10:51:16 +0200 Subject: [PATCH 11/13] attempt to fix setting of env in build-debug command for cover task + added option to enable debug logging of require script via OPENCV4NODES_DEBUG_REQUIRE env --- ci/envs/test.js | 4 ++-- lib/cv.js | 38 ++++++++++++++++++++++++++++++++------ package.json | 2 +- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/ci/envs/test.js b/ci/envs/test.js index 2f6ea3bab..e5f13132f 100644 --- a/ci/envs/test.js +++ b/ci/envs/test.js @@ -1,5 +1,5 @@ -const cv = require('opencv4nodejs') +process.env.OPENCV4NODES_DEBUG_REQUIRE = true -if (!cv) { +if (!require('opencv4nodejs')) { throw new Error('failed to require opencv4nodejs') } \ No newline at end of file diff --git a/lib/cv.js b/lib/cv.js index 3a90d68c6..1bfac377f 100644 --- a/lib/cv.js +++ b/lib/cv.js @@ -4,31 +4,57 @@ const { resolvePath } = require('./commons'); const requirePath = path.join(__dirname, process.env.BINDINGS_DEBUG ? '../build/Debug/opencv4nodejs' : '../build/Release/opencv4nodejs') +const logDebug = process.env.OPENCV4NODES_DEBUG_REQUIRE ? require('npmlog').info : () => {} + function tryGetOpencvBinDir() { - // if the auto build is disabled via environment do not even attempt + if (process.env.OPENCV_BIN_DIR) { + logDebug('tryGetOpencvBinDir', 'OPENCV_BIN_DIR environment variable is set') + return process.env.OPENCV_BIN_DIR + } + // if the auto build is not disabled via environment do not even attempt // to read package.json - if (opencvBuild.isAutoBuildDisabled()) { + if (!opencvBuild.isAutoBuildDisabled()) { + logDebug('tryGetOpencvBinDir', 'auto build has not been disabled via environment variable, using opencv bin dir of opencv-build') return opencvBuild.opencvBinDir } + logDebug('tryGetOpencvBinDir', 'auto build has not been explicitly disabled via environment variable, attempting to read envs from package.json...') const envs = opencvBuild.readEnvsFromPackageJson() - return envs.disableAutoBuild - ? (envs.opencvBinDir || process.env.OPENCV_BIN_DIR) - : opencvBuild.opencvBinDir + + if (!envs.disableAutoBuild) { + logDebug('tryGetOpencvBinDir', 'auto build has not been disabled via package.json, using opencv bin dir of opencv-build') + return opencvBuild.opencvBinDir + } + + if (envs.opencvBinDir) { + logDebug('tryGetOpencvBinDir', 'found opencv binary environment variable in package.json') + return envs.opencvBinDir + } + logDebug('tryGetOpencvBinDir', 'failed to find opencv binary environment variable in package.json') + return null } let cv = null try { + logDebug('require', 'require path is ' + requirePath) cv = require(requirePath); } catch (err) { + logDebug('require', 'failed to require cv with exception: ' + err.toString()) + logDebug('require', 'attempting to add opencv binaries to path') + + if (!process.env.path) { + logDebug('require', 'there is no path environment variable, skipping...') + throw err + } - // TODO: non windows? const opencvBinDir = tryGetOpencvBinDir() + logDebug('require', 'adding opencv binary dir to path: ' + opencvBinDir) // ensure binaries are added to path on windows if (!process.env.path.includes(opencvBinDir)) { process.env.path = `${process.env.path};${opencvBinDir};` } + logDebug('require', 'process.env.path: ' + process.env.path) cv = require(requirePath); } diff --git a/package.json b/package.json index fc557890f..7ded66460 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "build": "node-gyp configure build --jobs max", "rebuild": "node-gyp rebuild --jobs max", "clean": "node-gyp clean", - "build-debug": "BINDINGS_DEBUG=true && node ./install/install.js" + "build-debug": "BINDINGS_DEBUG=true node ./install/install.js" }, "gypfile": true, "dependencies": { From 07fc7989229564a176086f372812a8cef52f5e0c Mon Sep 17 00:00:00 2001 From: vincent Date: Sun, 22 Sep 2019 11:57:25 +0200 Subject: [PATCH 12/13] pass build envs to cover script --- ci/cover/cover.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/cover/cover.sh b/ci/cover/cover.sh index a8c943fd3..c8cb8bc5d 100644 --- a/ci/cover/cover.sh +++ b/ci/cover/cover.sh @@ -1,4 +1,4 @@ #!/bin/sh image=opencv4nodejs-ci:$1-node$2-with-coverage docker build -t $image -f ./Dockerfile --build-arg TAG=$1 --build-arg NODE_MAJOR_VERSION=$2 ../../ -docker run -v $PWD/coverage-report:/test/coverage-report $image \ No newline at end of file +docker run -v $PWD/coverage-report:/test/coverage-report -e OPENCV_VERSION=$OPENCV_VERSION -e TEST_MODULE_LIST=$TEST_MODULE_LIST $image \ No newline at end of file From b8fd44294eb0acf31893ba9848c515185b1779c4 Mon Sep 17 00:00:00 2001 From: vincent Date: Sun, 22 Sep 2019 15:34:47 +0200 Subject: [PATCH 13/13] added Configuring Environments via package.json section to readme --- README.md | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5ae297011..13ba9a0e1 100644 --- a/README.md +++ b/README.md @@ -174,9 +174,40 @@ You can specify the Version of OpenCV you want to install via the script by sett If you only want to build a subset of the OpenCV modules you can pass the *-DBUILD_LIST* cmake flag via the *OPENCV4NODEJS_AUTOBUILD_FLAGS* environment variable. For example `export OPENCV4NODEJS_AUTOBUILD_FLAGS=-DBUILD_LIST=dnn` will build only modules required for `dnn` and reduces the size and compilation time of the OpenCV package. +## Configuring Environments via package.json + +It's possible to specify build environment variables by inserting them into the `package.json` as follows: + +```json +{ + "name": "my-project", + "version": "0.0.0", + "dependencies": { + "opencv4nodejs": "^X.X.X" + }, + "opencv4nodejs": { + "disableAutoBuild": 1, + "opencvIncludeDir": "C:\\tools\\opencv\\build\\include", + "opencvLibDir": "C:\\tools\\opencv\\build\\x64\\vc14\\lib", + "opencvBinDir": "C:\\tools\\opencv\\build\\x64\\vc14\\bin" + } +} +``` + +The following environment variables can be passed: + +- autoBuildBuildCuda +- autoBuildFlags +- autoBuildOpencvVersion +- autoBuildWithoutContrib +- disableAutoBuild +- opencvIncludeDir +- opencvLibDir +- opencvBinDir + -## Usage with Docker +# Usage with Docker ### [opencv-express](https://github.com/justadudewhohacks/opencv-express) - example for opencv4nodejs with express.js and docker @@ -192,7 +223,7 @@ Different OpenCV 3.x base images can be found here: https://hub.docker.com/r/jus -## Usage with Electron +# Usage with Electron ### [opencv-electron](https://github.com/justadudewhohacks/opencv-electron) - example for opencv4nodejs with electron @@ -213,7 +244,7 @@ const cv = require('opencv4nodejs'); -## Usage with NW.js +# Usage with NW.js Any native modules, including opencv4nodejs, must be recompiled to be used with [NW.js](https://nwjs.io/). Instructions on how to do this are available in the **[Use Native Modules](http://docs.nwjs.io/en/latest/For%20Users/Advanced/Use%20Native%20Node%20Modules/)** section of the the NW.js documentation.