|
| 1 | +const opencvBuild = require(`@nut-tree/opencv-build-${process.platform}`) |
| 2 | +const fs = require('fs') |
| 3 | +const log = require('npmlog') |
| 4 | +const { exec } = require("child_process"); |
| 5 | +const { resolvePath } = require('../lib/commons') |
| 6 | + |
| 7 | +opencvBuild.applyEnvsFromPackageJson() |
| 8 | + |
| 9 | +const libDir = resolvePath(opencvBuild.opencvLibDir); |
| 10 | + |
| 11 | +log.info('install', 'using lib dir: ' + libDir) |
| 12 | + |
| 13 | +if (!fs.existsSync(libDir)) { |
| 14 | + throw new Error('library dir does not exist: ' + libDir) |
| 15 | +} |
| 16 | + |
| 17 | +const libsFoundInDir = opencvBuild |
| 18 | + .getLibs(libDir) |
| 19 | + .filter(lib => lib.libPath) |
| 20 | + |
| 21 | +if (!libsFoundInDir.length) { |
| 22 | + throw new Error('no OpenCV libraries found in lib dir: ' + libDir) |
| 23 | +} |
| 24 | + |
| 25 | +log.info('install', 'found the following libs:') |
| 26 | +libsFoundInDir.forEach(lib => log.info('install', lib.opencvModule + ' : ' + lib.libPath)) |
| 27 | + |
| 28 | +const defines = libsFoundInDir |
| 29 | + .map(lib => `OPENCV4NODEJS_FOUND_LIBRARY_${lib.opencvModule.toUpperCase()}`) |
| 30 | + |
| 31 | +const includes = [resolvePath(opencvBuild.opencvInclude), resolvePath(opencvBuild.opencv4Include)] |
| 32 | + |
| 33 | +const libs = opencvBuild.isWin() |
| 34 | + ? libsFoundInDir.map(lib => resolvePath(lib.libPath)) |
| 35 | + // dynamically link libs if not on windows |
| 36 | + : ['-L' + libDir] |
| 37 | + .concat(libsFoundInDir.map(lib => '-lopencv_' + lib.opencvModule)) |
| 38 | + .concat('-Wl,-rpath,' + libDir) |
| 39 | + |
| 40 | +console.log() |
| 41 | +log.info('install', 'setting the following defines:') |
| 42 | +defines.forEach(def => log.info('defines', def)) |
| 43 | +console.log() |
| 44 | +log.info('install', 'setting the following includes:') |
| 45 | +includes.forEach(inc => log.info('includes', inc)) |
| 46 | +console.log() |
| 47 | +log.info('install', 'setting the following libs:') |
| 48 | +libs.forEach(lib => log.info('libs', lib)) |
| 49 | + |
| 50 | +process.env['OPENCV4NODEJS_DEFINES'] = defines.join('\n') |
| 51 | +process.env['OPENCV4NODEJS_INCLUDES'] = includes.join('\n') |
| 52 | +process.env['OPENCV4NODEJS_LIBRARIES'] = libs.join('\n') |
| 53 | + |
| 54 | +const UPLOAD_TOKEN_KEY = 'GITHUB_TOKEN'; |
| 55 | +const uploadToken = process.env[UPLOAD_TOKEN_KEY]; |
| 56 | + |
| 57 | +if (!uploadToken) { |
| 58 | + throw new Error(`Missing upload token at env ${UPLOAD_TOKEN_KEY}`) |
| 59 | +} |
| 60 | + |
| 61 | +const prebuildCmd = `prebuild -u ${uploadToken} --include-regex "\.(node|a|so|dylib|lib|dll).*$"` |
| 62 | +log.info('install', `Running prebuild`) |
| 63 | +const child = exec(prebuildCmd, { |
| 64 | + maxBuffer: 1024 * 1024 * 10 |
| 65 | +}, function(err, stdout, stderr) { |
| 66 | + const _err = err || stderr |
| 67 | + if (_err) log.error(_err) |
| 68 | +}) |
| 69 | +child.stdout.pipe(process.stdout) |
| 70 | +child.stderr.pipe(process.stderr) |
0 commit comments