aboutsummaryrefslogtreecommitdiff
path: root/node_modules/detect-libc/lib
diff options
context:
space:
mode:
authorruki <waruqi@gmail.com>2018-11-08 00:38:48 +0800
committerruki <waruqi@gmail.com>2018-11-07 21:53:09 +0800
commit26105034da4fcce7ac883c899d781f016559310d (patch)
treec459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/detect-libc/lib
parent2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff)
downloadxmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz
xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip
switch to vuepress
Diffstat (limited to 'node_modules/detect-libc/lib')
-rw-r--r--node_modules/detect-libc/lib/detect-libc.js92
1 files changed, 92 insertions, 0 deletions
diff --git a/node_modules/detect-libc/lib/detect-libc.js b/node_modules/detect-libc/lib/detect-libc.js
new file mode 100644
index 00000000..1855fe18
--- /dev/null
+++ b/node_modules/detect-libc/lib/detect-libc.js
@@ -0,0 +1,92 @@
+'use strict';
+
+var platform = require('os').platform();
+var spawnSync = require('child_process').spawnSync;
+var readdirSync = require('fs').readdirSync;
+
+var GLIBC = 'glibc';
+var MUSL = 'musl';
+
+var spawnOptions = {
+ encoding: 'utf8',
+ env: process.env
+};
+
+if (!spawnSync) {
+ spawnSync = function () {
+ return { status: 126, stdout: '', stderr: '' };
+ };
+}
+
+function contains (needle) {
+ return function (haystack) {
+ return haystack.indexOf(needle) !== -1;
+ };
+}
+
+function versionFromMuslLdd (out) {
+ return out.split(/[\r\n]+/)[1].trim().split(/\s/)[1];
+}
+
+function safeReaddirSync (path) {
+ try {
+ return readdirSync(path);
+ } catch (e) {}
+ return [];
+}
+
+var family = '';
+var version = '';
+var method = '';
+
+if (platform === 'linux') {
+ // Try getconf
+ var glibc = spawnSync('getconf', ['GNU_LIBC_VERSION'], spawnOptions);
+ if (glibc.status === 0) {
+ family = GLIBC;
+ version = glibc.stdout.trim().split(' ')[1];
+ method = 'getconf';
+ } else {
+ // Try ldd
+ var ldd = spawnSync('ldd', ['--version'], spawnOptions);
+ if (ldd.status === 0 && ldd.stdout.indexOf(MUSL) !== -1) {
+ family = MUSL;
+ version = versionFromMuslLdd(ldd.stdout);
+ method = 'ldd';
+ } else if (ldd.status === 1 && ldd.stderr.indexOf(MUSL) !== -1) {
+ family = MUSL;
+ version = versionFromMuslLdd(ldd.stderr);
+ method = 'ldd';
+ } else {
+ // Try filesystem (family only)
+ var lib = safeReaddirSync('/lib');
+ if (lib.some(contains('-linux-gnu'))) {
+ family = GLIBC;
+ method = 'filesystem';
+ } else if (lib.some(contains('libc.musl-'))) {
+ family = MUSL;
+ method = 'filesystem';
+ } else if (lib.some(contains('ld-musl-'))) {
+ family = MUSL;
+ method = 'filesystem';
+ } else {
+ var usrSbin = safeReaddirSync('/usr/sbin');
+ if (usrSbin.some(contains('glibc'))) {
+ family = GLIBC;
+ method = 'filesystem';
+ }
+ }
+ }
+ }
+}
+
+var isNonGlibcLinux = (family !== '' && family !== GLIBC);
+
+module.exports = {
+ GLIBC: GLIBC,
+ MUSL: MUSL,
+ family: family,
+ version: version,
+ method: method,
+ isNonGlibcLinux: isNonGlibcLinux
+};