From 0402678d3bdc8a5b5950ddff077c8864622700c8 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Mon, 6 Apr 2015 13:34:34 -0700 Subject: fixing libname guessing logic run through multiple different possible library names and extensions, favoring the libyajl2 gem versions first. --- lib/ffi_yajl/ext.rb | 9 +----- lib/ffi_yajl/ffi.rb | 10 +----- lib/ffi_yajl/map_library_name.rb | 70 +++++++++++++++++++++++++--------------- 3 files changed, 46 insertions(+), 43 deletions(-) (limited to 'lib') diff --git a/lib/ffi_yajl/ext.rb b/lib/ffi_yajl/ext.rb index 12f74d9..937db99 100644 --- a/lib/ffi_yajl/ext.rb +++ b/lib/ffi_yajl/ext.rb @@ -2,19 +2,12 @@ require 'rubygems' require 'ffi_yajl/encoder' require 'ffi_yajl/parser' -require 'libyajl2' require 'ffi_yajl/map_library_name' -require 'ffi_yajl/ext/dlopen' module FFI_Yajl extend FFI_Yajl::MapLibraryName - extend FFI_Yajl::Ext::Dlopen - - libname = map_library_name - libpath = File.expand_path(File.join(Libyajl2.opt_path, libname)) - - dlopen(libpath) + dlopen_yajl_library class Parser require 'ffi_yajl/ext/parser' diff --git a/lib/ffi_yajl/ffi.rb b/lib/ffi_yajl/ffi.rb index 32335ac..b38d5fb 100644 --- a/lib/ffi_yajl/ffi.rb +++ b/lib/ffi_yajl/ffi.rb @@ -16,15 +16,7 @@ module FFI_Yajl extend FFI_Yajl::MapLibraryName - libname = map_library_name - libpath = File.expand_path(File.join(Libyajl2.opt_path, libname)) - - if File.file?(libpath) - # use our vendored version of libyajl2 if we find it installed - ffi_lib libpath - else - ffi_lib 'yajl' - end + ffi_open_yajl_library class YajlCallbacks < ::FFI::Struct layout :yajl_null, :pointer, diff --git a/lib/ffi_yajl/map_library_name.rb b/lib/ffi_yajl/map_library_name.rb index 3518d07..c4ea744 100644 --- a/lib/ffi_yajl/map_library_name.rb +++ b/lib/ffi_yajl/map_library_name.rb @@ -1,37 +1,55 @@ - +require 'libyajl2' require 'ffi_yajl/platform' +require 'ffi_yajl/ext/dlopen' module FFI_Yajl module MapLibraryName include FFI_Yajl::Platform - def map_library_name - # this is the right answer for the internally built libyajl on windows - return "libyajl.so" if windows? + include FFI_Yajl::Ext::Dlopen + + def library_names + case RbConfig::CONFIG['host_os'].downcase + when /mingw|mswin/ + [ "libyajl.so", "yajl.dll" ] + when /cygwin/ + [ "libyajl.so", "cygyajl.dll" ] + when /darwin/ + [ "libyajl.bundle", "libyajl.dylib" ] + else + [ "libyajl.so" ] + end + end - # this is largely copied from the FFI.map_library_name algorithm, we most likely need - # the windows code eventually to support not using the embedded libyajl2-gem - libprefix = - case RbConfig::CONFIG['host_os'].downcase - when /mingw|mswin/ - '' - when /cygwin/ - 'cyg' - else - 'lib' + def expanded_library_names + library_names.map do |libname| + pathname = File.expand_path(File.join(Libyajl2.opt_path, libname)) + pathname if File.file?(pathname) + end.compact + end + + def dlopen_yajl_library + found = false + ( expanded_library_names + library_names ).each do |libname| + begin + dlopen(libname) + found = true + break + rescue ArgumentError end - libsuffix = - case RbConfig::CONFIG['host_os'].downcase - when /darwin/ - 'bundle' - when /linux|bsd|solaris|sunos/ - 'so' - when /mingw|mswin|cygwin/ - 'dll' - else - # Punt and just assume a sane unix (i.e. anything but AIX) - 'so' + end + raise "cannot find yajl library for platform" unless found + end + + def ffi_open_yajl_library + found = false + expanded_library_names.each do |libname| + begin + ffi_lib libname + found = true + rescue end - libprefix + "yajl" + ".#{libsuffix}" + end + ffi_lib 'yajl' unless found end end end -- cgit v1.2.1