From 034e43c47cac5a8fadebd70cda0ed587c60ffff8 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Wed, 26 Dec 2018 14:39:03 +0100 Subject: [PATCH 1/2] optionally support prebuilds for libc and arm flavors --- README.md | 14 ++++++++++++++ index.js | 29 ++++++++++++++++++++++------- package.json | 3 +++ 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index abe28fe..c55ffb6 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,20 @@ without having to compile on install time AND will work in both node and electro Users can override `node-gyp-build` and force compiling by doing `npm install --build-from-source`. +## Supported prebuild names + +If so desired you can bundle more specific flavors, for example `musl` builds to support Alpine, or targeting a numbered ARM architecture version. + +These prebuilds can be bundled in addition to generic prebuilds; `node-gyp-build` will try to find the most specific flavor first. In order of precedence: + +- If `arch` is `'arm'` or `'arm64'`: + - `${platform}${libc}-${arch}-v${arm_version}` + - `${platform}-${arch}-v${arm_version}` +- `${platform}${libc}-${arch}` +- `${platform}-${arch}` + +The `libc` flavor and `arm_version` are auto-detected but can be overridden through the `LIBC` and `ARM_VERSION` environment variables, respectively. + ## License MIT diff --git a/index.js b/index.js index 5e25bda..7630165 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ var fs = require('fs') var path = require('path') var os = require('os') +var detectLibc = require('detect-libc') // Workaround to fix webpack's build warnings: 'the request of a dependency is an expression' var runtimeRequire = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require // eslint-disable-line @@ -9,6 +10,8 @@ var abi = process.versions.modules // TODO: support old node where this is undef var runtime = isElectron() ? 'electron' : 'node' var arch = os.arch() var platform = os.platform() +var libc = process.env.LIBC || detectLibc.family || '' +var armv = process.env.ARM_VERSION || (arch === 'arm64' ? '8' : process.config.variables.arm_version) || '' module.exports = load @@ -30,16 +33,28 @@ load.path = function (dir) { var debug = getFirst(path.join(dir, 'build/Debug'), matchBuild) if (debug) return debug - var prebuild = getFirst(path.join(dir, 'prebuilds/' + platform + '-' + arch), matchPrebuild) - if (prebuild) return prebuild + var names = [platform + '-' + arch] + if (libc) names.push(platform + libc + '-' + arch) - var napiRuntime = getFirst(path.join(dir, 'prebuilds/' + platform + '-' + arch), matchNapiRuntime) - if (napiRuntime) return napiRuntime + if ((arch === 'arm' || arch === 'arm64') && armv) { + names.forEach(function (name) { + names.push(name + '-v' + armv) + }) + } + + // Find most specific flavor first + for (var i = names.length; i--;) { + var prebuild = getFirst(path.join(dir, 'prebuilds/' + names[i]), matchPrebuild) + if (prebuild) return prebuild - var napi = getFirst(path.join(dir, 'prebuilds/' + platform + '-' + arch), matchNapi) - if (napi) return napi + var napiRuntime = getFirst(path.join(dir, 'prebuilds/' + names[i]), matchNapiRuntime) + if (napiRuntime) return napiRuntime + + var napi = getFirst(path.join(dir, 'prebuilds/' + names[i]), matchNapi) + if (napi) return napi + } - throw new Error('No native build was found for runtime=' + runtime + ' abi=' + abi + ' platform=' + platform + ' arch=' + arch) + throw new Error('No native build was found for runtime=' + runtime + ' abi=' + abi + ' platform=' + platform + libc + ' arch=' + arch) } function getFirst (dir, filter) { diff --git a/package.json b/package.json index 7ba4688..8b063da 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,9 @@ "node-gyp-build-optional": "./optional.js", "node-gyp-build-test": "./build-test.js" }, + "dependencies": { + "detect-libc": "~1.0.3" + }, "repository": { "type": "git", "url": "https://github.com/mafintosh/node-gyp-build.git" From 26ebddfa2f106d5b007a276d27e918c672545079 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sun, 20 Jan 2019 19:47:25 +0100 Subject: [PATCH 2/2] Remove detect-libc in favor of isAlpine() check --- index.js | 7 +++++-- package.json | 3 --- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 7630165..d56fc03 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,6 @@ var fs = require('fs') var path = require('path') var os = require('os') -var detectLibc = require('detect-libc') // Workaround to fix webpack's build warnings: 'the request of a dependency is an expression' var runtimeRequire = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require // eslint-disable-line @@ -10,7 +9,7 @@ var abi = process.versions.modules // TODO: support old node where this is undef var runtime = isElectron() ? 'electron' : 'node' var arch = os.arch() var platform = os.platform() -var libc = process.env.LIBC || detectLibc.family || '' +var libc = process.env.LIBC || (isAlpine(platform) ? 'musl' : 'glibc') var armv = process.env.ARM_VERSION || (arch === 'arm64' ? '8' : process.config.variables.arm_version) || '' module.exports = load @@ -88,3 +87,7 @@ function isElectron () { if (process.env.ELECTRON_RUN_AS_NODE) return true return typeof window !== 'undefined' && window.process && window.process.type === 'renderer' } + +function isAlpine (platform) { + return platform === 'linux' && fs.existsSync('/etc/alpine-release') +} diff --git a/package.json b/package.json index 8b063da..7ba4688 100644 --- a/package.json +++ b/package.json @@ -14,9 +14,6 @@ "node-gyp-build-optional": "./optional.js", "node-gyp-build-test": "./build-test.js" }, - "dependencies": { - "detect-libc": "~1.0.3" - }, "repository": { "type": "git", "url": "https://github.com/mafintosh/node-gyp-build.git"