Skip to content

Commit f729abb

Browse files
authored
Allow setting libc to glibc on non-glibc platform (#176)
Discard libc value on non-linux platforms
1 parent 4a1ed43 commit f729abb

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,13 @@ prebuild-install [options]
7272
--version (print prebuild-install version and exit)
7373
```
7474

75-
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.
75+
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.
7676

77-
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`.
77+
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`.
78+
79+
### Libc
80+
81+
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.
7882

7983
### Private Repositories
8084

rc.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ const detectLibc = require('detect-libc')
55
const napi = require('napi-build-utils')
66

77
const env = process.env
8-
const libc = env.LIBC || (detectLibc.isNonGlibcLinuxSync() && detectLibc.familySync()) || ''
8+
9+
const libc = env.LIBC || process.env.npm_config_libc ||
10+
(detectLibc.isNonGlibcLinuxSync() && detectLibc.familySync()) || ''
911

1012
// Get the configuration
1113
module.exports = function (pkg) {
@@ -51,6 +53,8 @@ module.exports = function (pkg) {
5153

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

56+
rc.libc = rc.platform !== 'linux' || rc.libc === detectLibc.GLIBC ? '' : rc.libc
57+
5458
return rc
5559
}
5660

test/rc-test.js

+38-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ test('custom config and aliases', function (t) {
1616
'--path ../some/other/path',
1717
'--target 1.4.10',
1818
'--runtime electron',
19-
'--libc testlibc',
2019
'--token TOKEN'
2120
]
2221
runRc(t, args.join(' '), {}, function (rc, tmp) {
@@ -35,7 +34,6 @@ test('custom config and aliases', function (t) {
3534
t.equal(rc.target, rc.t, 'target alias')
3635
t.equal(rc.runtime, 'electron', 'correct runtime')
3736
t.equal(rc.runtime, rc.r, 'runtime alias')
38-
t.equal(rc.libc, 'testlibc', 'libc family')
3937
t.equal(rc.abi, '50', 'correct ABI')
4038
t.equal(rc.token, 'TOKEN', 'correct token')
4139
t.equal(rc['tag-prefix'], 'v', 'correct default tag prefix')
@@ -70,17 +68,19 @@ test('npm_config_* are passed on from environment into rc', function (t) {
7068
npm_config_local_address: '127.0.0.1',
7169
npm_config_target: '1.4.0',
7270
npm_config_runtime: 'electron',
73-
npm_config_platform: 'PLATFORM',
74-
npm_config_build_from_source: 'true'
71+
npm_config_platform: 'linux',
72+
npm_config_build_from_source: 'true',
73+
npm_config_libc: 'testlibc'
7574
}
7675
runRc(t, '', env, function (rc) {
7776
t.equal(rc.proxy, 'http://localhost/', 'proxy is set')
7877
t.equal(rc['https-proxy'], 'https://localhost/', 'https-proxy is set')
7978
t.equal(rc['local-address'], '127.0.0.1', 'local-address is set')
8079
t.equal(rc.target, '1.4.0', 'target is set')
8180
t.equal(rc.runtime, 'electron', 'runtime is set')
82-
t.equal(rc.platform, 'PLATFORM', 'platform is set')
81+
t.equal(rc.platform, 'linux', 'platform is set')
8382
t.equal(rc.buildFromSource, true, 'build-from-source is set')
83+
t.equal(rc.libc, 'testlibc', 'libc is set')
8484
t.end()
8585
})
8686
})
@@ -115,6 +115,39 @@ test('using --tag-prefix will set the tag prefix', function (t) {
115115
})
116116
})
117117

118+
test('libc works on linux platform', function (t) {
119+
const args = [
120+
'--libc musl --platform linux'
121+
]
122+
runRc(t, args.join(' '), {}, function (rc, tmp) {
123+
t.equal(rc.libc, 'musl', 'libc family')
124+
t.equal(rc.platform, 'linux', 'platform')
125+
t.end()
126+
})
127+
})
128+
129+
test('libc glibc is passed as empty', function (t) {
130+
const args = [
131+
'--libc glibc --platform linux'
132+
]
133+
runRc(t, args.join(' '), {}, function (rc, tmp) {
134+
t.equal(rc.libc, '', 'libc family')
135+
t.equal(rc.platform, 'linux', 'platform')
136+
t.end()
137+
})
138+
})
139+
140+
test('libc is discarded on non-linux platform', function (t) {
141+
const args = [
142+
'--libc musl --platform windows'
143+
]
144+
runRc(t, args.join(' '), {}, function (rc, tmp) {
145+
t.equal(rc.libc, '', 'libc family')
146+
t.equal(rc.platform, 'windows', 'platform')
147+
t.end()
148+
})
149+
})
150+
118151
function runRc (t, args, env, cb) {
119152
const pkg = {
120153
name: 'test',

util.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function getDownloadUrl (opts) {
2020
runtime: opts.runtime || 'node',
2121
platform: opts.platform,
2222
arch: opts.arch,
23-
libc: opts.libc || process.env.LIBC || '',
23+
libc: opts.libc || '',
2424
configuration: (opts.debug ? 'Debug' : 'Release'),
2525
module_name: opts.pkg.binary && opts.pkg.binary.module_name,
2626
tag_prefix: opts['tag-prefix']

0 commit comments

Comments
 (0)