Skip to content

Commit

Permalink
Optionally support prebuilds for libc and arm flavors (#14)
Browse files Browse the repository at this point in the history
* optionally support prebuilds for libc and arm flavors

* Remove detect-libc in favor of isAlpine() check
  • Loading branch information
vweevers authored and mafintosh committed Jan 21, 2019
1 parent 06f368c commit 994b71d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 25 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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 || (isAlpine(platform) ? 'musl' : 'glibc')
var armv = process.env.ARM_VERSION || (arch === 'arm64' ? '8' : process.config.variables.arm_version) || ''

module.exports = load

Expand All @@ -30,16 +32,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 napiRuntime = getFirst(path.join(dir, 'prebuilds/' + names[i]), matchNapiRuntime)
if (napiRuntime) return napiRuntime

var napi = getFirst(path.join(dir, 'prebuilds/' + platform + '-' + arch), matchNapi)
if (napi) return napi
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) {
Expand Down Expand Up @@ -73,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')
}

0 comments on commit 994b71d

Please sign in to comment.