|
| 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) |
0 commit comments