Skip to content

Commit 51b4a7c

Browse files
authoredApr 28, 2022
Add support for --libc flag to improve cross-platform install (#3160)
This deprecates the libc-as-suffix approach of --platform=linuxmusl
1 parent 5b03579 commit 51b4a7c

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed
 

‎docs/install.md

+13-4
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,28 @@ The target platform and/or architecture can be manually selected using the follo
7474
npm install --platform=... --arch=... --arm-version=... sharp
7575
```
7676

77-
* `--platform`: one of `linux`, `linuxmusl`, `darwin` or `win32`.
77+
* `--platform`: one of `linux`, `darwin` or `win32`.
7878
* `--arch`: one of `x64`, `ia32`, `arm` or `arm64`.
7979
* `--arm-version`: one of `6`, `7` or `8` (`arm` defaults to `6`, `arm64` defaults to `8`).
80+
* `--libc`: one of `glibc` or `musl`. This option only works with platform `linux`, defaults to `glibc`
8081
* `--sharp-install-force`: skip version compatibility and Subresource Integrity checks.
8182

8283
These values can also be set via environment variables,
83-
`npm_config_platform`, `npm_config_arch`, `npm_config_arm_version`
84+
`npm_config_platform`, `npm_config_arch`, `npm_config_arm_version`, `npm_config_libc`
8485
and `SHARP_INSTALL_FORCE` respectively.
8586

8687
For example, if the target machine has a 64-bit ARM CPU and is running Alpine Linux,
8788
use the following flags:
8889

8990
```sh
90-
npm install --arch=arm64 --platform=linuxmusl sharp
91+
npm install --arch=arm64 --platform=linux --libc=musl sharp
92+
```
93+
94+
If the current machine is Alpine Linux and the target machine is Debian Linux on x64 cpu,
95+
use the following flags:
96+
97+
```sh
98+
npm install --arch=x64 --platform=linux --libc=glibc sharp
9199
```
92100

93101
## Custom libvips
@@ -215,7 +223,8 @@ run the following additional command after `npm install`:
215223

216224
```sh
217225
npm install
218-
SHARP_IGNORE_GLOBAL_LIBVIPS=1 npm install --arch=x64 --platform=linux sharp
226+
rm -rf node_modules/sharp
227+
SHARP_IGNORE_GLOBAL_LIBVIPS=1 npm install --arch=x64 --platform=linux --libc=glibc sharp
219228
```
220229

221230
To get the best performance select the largest memory available.

‎lib/platform.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ const env = process.env;
77
module.exports = function () {
88
const arch = env.npm_config_arch || process.arch;
99
const platform = env.npm_config_platform || process.platform;
10-
/* istanbul ignore next */
11-
const libc = (platform === 'linux' && detectLibc.isNonGlibcLinuxSync()) ? detectLibc.familySync() : '';
10+
const libc = process.env.npm_config_libc ||
11+
/* istanbul ignore next */
12+
(detectLibc.isNonGlibcLinuxSync() ? detectLibc.familySync() : '');
13+
const libcId = platform !== 'linux' || libc === detectLibc.GLIBC ? '' : libc;
1214

13-
const platformId = [`${platform}${libc}`];
15+
const platformId = [`${platform}${libcId}`];
1416

1517
if (arch === 'arm') {
1618
const fallback = process.versions.electron ? '7' : '6';

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
"color": "^4.2.3",
132132
"detect-libc": "^2.0.1",
133133
"node-addon-api": "^4.3.0",
134-
"prebuild-install": "^7.0.1",
134+
"prebuild-install": "^7.1.0",
135135
"semver": "^7.3.7",
136136
"simple-get": "^4.0.1",
137137
"tar-fs": "^2.1.1",

‎test/unit/platform.js

+24
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,28 @@ describe('Platform-detection', function () {
6161
delete process.env.npm_config_arch;
6262
delete process.versions.electron;
6363
});
64+
65+
it('Can override libc if platform is linux', function () {
66+
process.env.npm_config_platform = 'linux';
67+
process.env.npm_config_libc = 'test';
68+
assert.strictEqual('linuxtest', platform().split('-')[0]);
69+
delete process.env.npm_config_platform;
70+
delete process.env.npm_config_libc;
71+
});
72+
73+
it('Handles libc value "glibc" as default linux', function () {
74+
process.env.npm_config_platform = 'linux';
75+
process.env.npm_config_libc = 'glibc';
76+
assert.strictEqual('linux', platform().split('-')[0]);
77+
delete process.env.npm_config_platform;
78+
delete process.env.npm_config_libc;
79+
});
80+
81+
it('Discards libc value on non-linux platform', function () {
82+
process.env.npm_config_platform = 'win32';
83+
process.env.npm_config_libc = 'gnuwin32';
84+
assert.strictEqual('win32', platform().split('-')[0]);
85+
delete process.env.npm_config_platform;
86+
delete process.env.npm_config_libc;
87+
});
6488
});

0 commit comments

Comments
 (0)
Please sign in to comment.