Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
Add a platform variant for installing a musl binary (#1836)
Browse files Browse the repository at this point in the history
Add a platform variant for installing a musl binary
  • Loading branch information
lox authored and xzyfer committed Dec 18, 2016
1 parent 3c17b03 commit 5521bb6
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions lib/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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');
Expand All @@ -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('');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5521bb6

Please # to comment.