From 7c0afdac03e56c92d54697c2afba2163b096984e Mon Sep 17 00:00:00 2001 From: Zach Bjornson Date: Tue, 21 Feb 2017 10:04:48 -0800 Subject: [PATCH] src(build): port has_lib.js to javascript Ref #754 (fix build on BSD) Ref #813 (static build) --- History.md | 5 +++ binding.gyp | 4 +- util/has_lib.js | 110 ++++++++++++++++++++++++++++++++++++++++++++++++ util/has_lib.sh | 64 ---------------------------- 4 files changed, 117 insertions(+), 66 deletions(-) create mode 100644 util/has_lib.js delete mode 100755 util/has_lib.sh diff --git a/History.md b/History.md index 70c707d72..d9f36d364 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,8 @@ +Unreleased / patch +================== + + * Port has_lib.sh to javascript (#871) + 1.6.0 / 2016-10-16 ================== diff --git a/binding.gyp b/binding.gyp index 4f56e1a4c..8b2a2f4d3 100755 --- a/binding.gyp +++ b/binding.gyp @@ -8,8 +8,8 @@ } }, { # 'OS!="win"' 'variables': { - 'with_jpeg%': '/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 (system_path) { + try { + var dirListing = fs.readdirSync(system_path); + 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 has_ldconfig() { + 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): + child_env.execSync('export PATH="$PATH:/sbin"'); + process.env.PATH = '...'; + // execSync throws on nonzero exit + child_env.execSync('hash ldconfig'); + return true; + } catch (err) { + return false; + } +} + +/** + * Checks for freetype2 with --cflags-only-I + * @return Boolean exists + */ +function has_freetype() { + try { + if (childProcess.execSync('pkg-config cairo --cflags-only-I | grep freetype2').length) + return true; + } catch (err) { + // noop + } + return false; +} + +/** + * Checks for lib using pkg-config. + * @param String library name + * @return Boolean exists + */ +function has_pkgconfig_lib(lib) { + try { + // execSync throws on nonzero exit + child_env.execSync('pkg-config --exists "' + lib + '"'); + return true; + } catch (err) { + return false; + } +} + +function main(query) { + switch (query) { + case 'gif': + case 'jpeg': + case 'cairo': + return has_system_lib(query); + case 'pango': + return has_pkgconfig_lib(query); + case 'freetype': + return has_freetype(); + default: + throw new Error('Unknown library: ' + query); + } +} + +process.stdout.write(main(query).toString()); diff --git a/util/has_lib.sh b/util/has_lib.sh deleted file mode 100755 index 75e216c8c..000000000 --- a/util/has_lib.sh +++ /dev/null @@ -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