Skip to content

Commit

Permalink
Allow setting libc to glibc on non-glibc platform (#176)
Browse files Browse the repository at this point in the history
Discard libc value on non-linux platforms
  • Loading branch information
joonamo committed Apr 20, 2022
1 parent 4a1ed43 commit f729abb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -72,9 +72,13 @@ prebuild-install [options]
--version (print prebuild-install version and exit)
```

When `prebuild-install` is run via an `npm` script, options `--build-from-source`, `--debug`, `--download`, `--target`, `--runtime`, `--arch` and `--platform` may be passed through via arguments given to the `npm` command.
When `prebuild-install` is run via an `npm` script, options `--build-from-source`, `--debug`, `--download`, `--target`, `--runtime`, `--arch` `--platform` and `--libc` may be passed through via arguments given to the `npm` command.

Alternatively you can set environment variables `npm_config_build_from_source=true`, `npm_config_platform`, `npm_config_arch`, `npm_config_target` and `npm_config_runtime`.
Alternatively you can set environment variables `npm_config_build_from_source=true`, `npm_config_platform`, `npm_config_arch`, `npm_config_target` `npm_config_runtime` and `npm_config_libc`.

### Libc

On non-glibc Linux platforms, the Libc name is appended to platform name. For example, musl-based environments are called `linuxmusl`. If `--libc=glibc` is passed as option, glibc is discarded and platform is called as just `linux`. This can be used for example to build cross-platform packages on Alpine Linux.

### Private Repositories

Expand Down
6 changes: 5 additions & 1 deletion rc.js
Expand Up @@ -5,7 +5,9 @@ const detectLibc = require('detect-libc')
const napi = require('napi-build-utils')

const env = process.env
const libc = env.LIBC || (detectLibc.isNonGlibcLinuxSync() && detectLibc.familySync()) || ''

const libc = env.LIBC || process.env.npm_config_libc ||
(detectLibc.isNonGlibcLinuxSync() && detectLibc.familySync()) || ''

// Get the configuration
module.exports = function (pkg) {
Expand Down Expand Up @@ -51,6 +53,8 @@ module.exports = function (pkg) {

rc.abi = napi.isNapiRuntime(rc.runtime) ? rc.target : getAbi(rc.target, rc.runtime)

rc.libc = rc.platform !== 'linux' || rc.libc === detectLibc.GLIBC ? '' : rc.libc

return rc
}

Expand Down
43 changes: 38 additions & 5 deletions test/rc-test.js
Expand Up @@ -16,7 +16,6 @@ test('custom config and aliases', function (t) {
'--path ../some/other/path',
'--target 1.4.10',
'--runtime electron',
'--libc testlibc',
'--token TOKEN'
]
runRc(t, args.join(' '), {}, function (rc, tmp) {
Expand All @@ -35,7 +34,6 @@ test('custom config and aliases', function (t) {
t.equal(rc.target, rc.t, 'target alias')
t.equal(rc.runtime, 'electron', 'correct runtime')
t.equal(rc.runtime, rc.r, 'runtime alias')
t.equal(rc.libc, 'testlibc', 'libc family')
t.equal(rc.abi, '50', 'correct ABI')
t.equal(rc.token, 'TOKEN', 'correct token')
t.equal(rc['tag-prefix'], 'v', 'correct default tag prefix')
Expand Down Expand Up @@ -70,17 +68,19 @@ test('npm_config_* are passed on from environment into rc', function (t) {
npm_config_local_address: '127.0.0.1',
npm_config_target: '1.4.0',
npm_config_runtime: 'electron',
npm_config_platform: 'PLATFORM',
npm_config_build_from_source: 'true'
npm_config_platform: 'linux',
npm_config_build_from_source: 'true',
npm_config_libc: 'testlibc'
}
runRc(t, '', env, function (rc) {
t.equal(rc.proxy, 'http://localhost/', 'proxy is set')
t.equal(rc['https-proxy'], 'https://localhost/', 'https-proxy is set')
t.equal(rc['local-address'], '127.0.0.1', 'local-address is set')
t.equal(rc.target, '1.4.0', 'target is set')
t.equal(rc.runtime, 'electron', 'runtime is set')
t.equal(rc.platform, 'PLATFORM', 'platform is set')
t.equal(rc.platform, 'linux', 'platform is set')
t.equal(rc.buildFromSource, true, 'build-from-source is set')
t.equal(rc.libc, 'testlibc', 'libc is set')
t.end()
})
})
Expand Down Expand Up @@ -115,6 +115,39 @@ test('using --tag-prefix will set the tag prefix', function (t) {
})
})

test('libc works on linux platform', function (t) {
const args = [
'--libc musl --platform linux'
]
runRc(t, args.join(' '), {}, function (rc, tmp) {
t.equal(rc.libc, 'musl', 'libc family')
t.equal(rc.platform, 'linux', 'platform')
t.end()
})
})

test('libc glibc is passed as empty', function (t) {
const args = [
'--libc glibc --platform linux'
]
runRc(t, args.join(' '), {}, function (rc, tmp) {
t.equal(rc.libc, '', 'libc family')
t.equal(rc.platform, 'linux', 'platform')
t.end()
})
})

test('libc is discarded on non-linux platform', function (t) {
const args = [
'--libc musl --platform windows'
]
runRc(t, args.join(' '), {}, function (rc, tmp) {
t.equal(rc.libc, '', 'libc family')
t.equal(rc.platform, 'windows', 'platform')
t.end()
})
})

function runRc (t, args, env, cb) {
const pkg = {
name: 'test',
Expand Down
2 changes: 1 addition & 1 deletion util.js
Expand Up @@ -20,7 +20,7 @@ function getDownloadUrl (opts) {
runtime: opts.runtime || 'node',
platform: opts.platform,
arch: opts.arch,
libc: opts.libc || process.env.LIBC || '',
libc: opts.libc || '',
configuration: (opts.debug ? 'Debug' : 'Release'),
module_name: opts.pkg.binary && opts.pkg.binary.module_name,
tag_prefix: opts['tag-prefix']
Expand Down

0 comments on commit f729abb

Please sign in to comment.