committed by
GitHub
5 changed files with 119 additions and 67 deletions
@ -0,0 +1,111 @@ |
|||||
|
var query = process.argv[2] |
||||
|
var fs = require('fs') |
||||
|
var childProcess = require('child_process') |
||||
|
|
||||
|
var SYSTEM_PATHS = [ |
||||
|
'/lib', |
||||
|
'/usr/lib', |
||||
|
'/usr/local/lib', |
||||
|
'/opt/local/lib', |
||||
|
'/usr/lib/x86_64-linux-gnu', |
||||
|
'/usr/lib/i386-linux-gnu' |
||||
|
] |
||||
|
|
||||
|
/** |
||||
|
* Checks for lib using ldconfig if present, or searching SYSTEM_PATHS |
||||
|
* otherwise. |
||||
|
* @param String library name, e.g. 'jpeg' in 'libjpeg64.so' (see first line) |
||||
|
* @return Boolean exists |
||||
|
*/ |
||||
|
function hasSystemLib (lib) { |
||||
|
var libName = 'lib' + lib + '.+(so|dylib)' |
||||
|
var libNameRegex = new RegExp(libName) |
||||
|
|
||||
|
// Try using ldconfig on linux systems
|
||||
|
if (hasLdconfig()) { |
||||
|
try { |
||||
|
if (childProcess.execSync('ldconfig -p 2>/dev/null | grep -E "' + libName + '"').length) { |
||||
|
return true |
||||
|
} |
||||
|
} catch (err) { |
||||
|
// noop -- proceed to other search methods
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Try checking common library locations
|
||||
|
return SYSTEM_PATHS.some(function (systemPath) { |
||||
|
try { |
||||
|
var dirListing = fs.readdirSync(systemPath) |
||||
|
return dirListing.some(function (file) { |
||||
|
return libNameRegex.test(file) |
||||
|
}) |
||||
|
} catch (err) { |
||||
|
return false |
||||
|
} |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Checks for ldconfig on the path and /sbin |
||||
|
* @return Boolean exists |
||||
|
*/ |
||||
|
function hasLdconfig () { |
||||
|
try { |
||||
|
// Add /sbin to path as ldconfig is located there on some systems -- e.g.
|
||||
|
// Debian (and it can still be used by unprivileged users):
|
||||
|
childProcess.execSync('export PATH="$PATH:/sbin"') |
||||
|
process.env.PATH = '...' |
||||
|
// execSync throws on nonzero exit
|
||||
|
childProcess.execSync('hash ldconfig 2>/dev/null') |
||||
|
return true |
||||
|
} catch (err) { |
||||
|
return false |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Checks for freetype2 with --cflags-only-I |
||||
|
* @return Boolean exists |
||||
|
*/ |
||||
|
function hasFreetype () { |
||||
|
try { |
||||
|
if (childProcess.execSync('pkg-config cairo --cflags-only-I 2>/dev/null | grep freetype2').length) { |
||||
|
return true |
||||
|
} |
||||
|
} catch (err) { |
||||
|
// noop
|
||||
|
} |
||||
|
return false |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Checks for lib using pkg-config. |
||||
|
* @param String library name |
||||
|
* @return Boolean exists |
||||
|
*/ |
||||
|
function hasPkgconfigLib (lib) { |
||||
|
try { |
||||
|
// execSync throws on nonzero exit
|
||||
|
childProcess.execSync('pkg-config --exists "' + lib + '" 2>/dev/null') |
||||
|
return true |
||||
|
} catch (err) { |
||||
|
return false |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function main (query) { |
||||
|
switch (query) { |
||||
|
case 'gif': |
||||
|
case 'jpeg': |
||||
|
case 'cairo': |
||||
|
return hasSystemLib(query) |
||||
|
case 'pango': |
||||
|
return hasPkgconfigLib(query) |
||||
|
case 'freetype': |
||||
|
return hasFreetype() |
||||
|
default: |
||||
|
throw new Error('Unknown library: ' + query) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
process.stdout.write(main(query).toString()) |
@ -1,64 +0,0 @@ |
|||||
#!/bin/sh |
|
||||
|
|
||||
has_ldconfig() { |
|
||||
hash ldconfig 2>/dev/null |
|
||||
} |
|
||||
|
|
||||
has_system_lib() { |
|
||||
regex="lib$1.+(so|dylib)" |
|
||||
|
|
||||
# Add /sbin to path as ldconfig is located there on some systems - e.g. Debian |
|
||||
# (and it still can be used by unprivileged users): |
|
||||
PATH="$PATH:/sbin" |
|
||||
export PATH |
|
||||
|
|
||||
# Try using ldconfig on Linux systems |
|
||||
if has_ldconfig; then |
|
||||
for _ in $(ldconfig -p 2>/dev/null | grep -E "$regex"); do |
|
||||
return 0 |
|
||||
done |
|
||||
fi |
|
||||
|
|
||||
# Try just checking common library locations |
|
||||
for dir in /lib /usr/lib /usr/local/lib /opt/local/lib /usr/lib/x86_64-linux-gnu /usr/lib/i386-linux-gnu; do |
|
||||
test -d "$dir" && echo "$dir"/* | grep -E "$regex" && return 0 |
|
||||
done |
|
||||
|
|
||||
return 1 |
|
||||
} |
|
||||
|
|
||||
has_freetype() { |
|
||||
pkg-config cairo --cflags-only-I | grep freetype2 |
|
||||
} |
|
||||
|
|
||||
has_pkgconfig_lib() { |
|
||||
pkg-config --exists "$1" |
|
||||
} |
|
||||
|
|
||||
case "$1" in |
|
||||
gif) |
|
||||
has_system_lib "gif" > /dev/null |
|
||||
result=$? |
|
||||
;; |
|
||||
jpeg) |
|
||||
has_system_lib "jpeg" > /dev/null |
|
||||
result=$? |
|
||||
;; |
|
||||
pango) |
|
||||
has_pkgconfig_lib "pango" > /dev/null |
|
||||
result=$? |
|
||||
;; |
|
||||
freetype) |
|
||||
has_freetype > /dev/null |
|
||||
result=$? |
|
||||
;; |
|
||||
*) |
|
||||
>&2 echo "Unknown library: $1" |
|
||||
exit 1 |
|
||||
esac |
|
||||
|
|
||||
if test $result -eq 0; then |
|
||||
echo "true" |
|
||||
else |
|
||||
echo "false" |
|
||||
fi |
|
Loading…
Reference in new issue