From 5521bb6b870d0275d7e0e090ea6d7d9e71e17264 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Sun, 18 Dec 2016 18:56:23 +1100 Subject: [PATCH] Add a platform variant for installing a musl binary (#1836) Add a platform variant for installing a musl binary --- lib/extensions.js | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/lib/extensions.js b/lib/extensions.js index 854efbd5e..7d5fd9e88 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -23,6 +23,7 @@ function getHumanPlatform(platform) { case 'darwin': return 'OS X'; case 'freebsd': return 'FreeBSD'; case 'linux': return 'Linux'; + case 'linux_musl': return 'Linux/musl'; case 'win32': return 'Windows'; default: return false; } @@ -171,7 +172,9 @@ function getArgument(name, args) { */ function getBinaryName() { - var binaryName; + var binaryName, + variant, + platform = process.platform; if (getArgument('--sass-binary-name')) { binaryName = getArgument('--sass-binary-name'); @@ -182,8 +185,13 @@ function getBinaryName() { } else if (pkg.nodeSassConfig && pkg.nodeSassConfig.binaryName) { binaryName = pkg.nodeSassConfig.binaryName; } else { + variant = getPlatformVariant(); + if (variant) { + platform += '_' + variant; + } + binaryName = [ - process.platform, '-', + platform, '-', process.arch, '-', process.versions.modules ].join(''); @@ -256,7 +264,7 @@ function getBinaryPath() { } else if (pkg.nodeSassConfig && pkg.nodeSassConfig.binaryPath) { binaryPath = pkg.nodeSassConfig.binaryPath; } else { - binaryPath = path.join(defaultBinaryPath, getBinaryName().replace(/_/, '/')); + binaryPath = path.join(defaultBinaryPath, getBinaryName().replace(/_(?=binding\.node)/, '/')); } return binaryPath; @@ -359,6 +367,36 @@ function getVersionInfo(binding) { ].join(eol); } +/** + * Gets the platform variant, currently either an empty string or 'musl' for Linux/musl platforms. + * + * @api public + */ + +function getPlatformVariant() { + var contents = ''; + + if (process.platform !== 'linux') { + return ''; + } + + try { + contents = fs.readFileSync(process.execPath); + + // Buffer.indexOf was added in v1.5.0 so cast to string for old node + // Delay contents.toStrings because it's expensive + if (!contents.indexOf) { + contents = contents.toString(); + } + + if (contents.indexOf('libc.musl-x86_64.so.1') !== -1) { + return 'musl'; + } + } catch (err) { } // eslint-disable-line no-empty + + return ''; +} + module.exports.hasBinary = hasBinary; module.exports.getBinaryUrl = getBinaryUrl; module.exports.getBinaryName = getBinaryName;