summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2015-03-25 10:02:43 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2015-03-25 10:02:43 -0700
commit629492b5934a74c97e7030a197f80a7444268a6d (patch)
tree0c256042bd38ecbe1fe978376bb998c1c13e917a
parentb4ad8c022fc77736845805436136e4fc8a41b892 (diff)
parentd21e6256d63bc7f4c1a67ff0a13eb76c61c52ab1 (diff)
downloadffi-yajl-629492b5934a74c97e7030a197f80a7444268a6d.tar.gz
Merge pull request #49 from chef/lcg/ffi-less-ffi-yajl
Remove ffi gem as a hard dependency
-rw-r--r--ffi-yajl-universal-java.gemspec3
-rw-r--r--ffi-yajl.gemspec.shared2
-rw-r--r--lib/ffi_yajl/ext.rb14
-rw-r--r--lib/ffi_yajl/ffi.rb17
-rw-r--r--lib/ffi_yajl/map_library_name.rb37
5 files changed, 55 insertions, 18 deletions
diff --git a/ffi-yajl-universal-java.gemspec b/ffi-yajl-universal-java.gemspec
index 35a1ebb..e712372 100644
--- a/ffi-yajl-universal-java.gemspec
+++ b/ffi-yajl-universal-java.gemspec
@@ -6,5 +6,6 @@ gemspec.platform = "universal-java"
# extensions so can we simplify the gemspecs now?
#gemspec.extensions = %w{ ext/libyajl2/extconf.rb }
-gemspec
+gemspec.add_dependency "ffi", "~> 1.5"
+gemspec
diff --git a/ffi-yajl.gemspec.shared b/ffi-yajl.gemspec.shared
index 2226fbd..62fefa0 100644
--- a/ffi-yajl.gemspec.shared
+++ b/ffi-yajl.gemspec.shared
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "rake-compiler", "~> 0.8.3"
# pin mime-types in order to work on ruby 1.8.7
s.add_development_dependency "mime-types", "~> 1.16"
- s.add_dependency "ffi", "~> 1.5"
+ s.add_development_dependency "ffi", "~> 1.5"
s.add_dependency "libyajl2", "~> 1.2"
s.bindir = "bin"
diff --git a/lib/ffi_yajl/ext.rb b/lib/ffi_yajl/ext.rb
index 5784023..bc4d410 100644
--- a/lib/ffi_yajl/ext.rb
+++ b/lib/ffi_yajl/ext.rb
@@ -2,22 +2,14 @@ require 'rubygems'
require 'ffi_yajl/encoder'
require 'ffi_yajl/parser'
-require 'ffi'
require 'libyajl2'
-require 'ffi_yajl/platform'
+require 'ffi_yajl/map_library_name'
module FFI_Yajl
- extend FFI_Yajl::Platform
+ extend FFI_Yajl::MapLibraryName
- # FIXME: DRY with ffi_yajl/ffi.rb
- # FIXME: extract map_library_name from FFI and stop requiring it at the top level
- # so that the C-library can be installed without FFI
- libname = ::FFI.map_library_name("yajl")
- # awful windows patch, but there is an open issue to entirely replace FFI.map_library_name already
- libname = "libyajl.so" if windows?
+ libname = map_library_name
libpath = File.expand_path(File.join(Libyajl2.opt_path, libname))
- libpath.gsub!(/dylib/, 'bundle')
- libpath = ::FFI.map_library_name("yajl") unless File.exist?(libpath)
#
# FFS, what exactly was so wrong with DL.dlopen that ruby had to get rid of it???
diff --git a/lib/ffi_yajl/ffi.rb b/lib/ffi_yajl/ffi.rb
index 4e685a6..32335ac 100644
--- a/lib/ffi_yajl/ffi.rb
+++ b/lib/ffi_yajl/ffi.rb
@@ -1,16 +1,23 @@
require 'rubygems'
require 'libyajl2'
-require 'ffi'
+begin
+ require 'ffi'
+rescue LoadError
+ $stderr.puts "FATAL: to use the ffi extension instead of the compiled C extension, the ffi gem must be installed"
+ $stderr.puts " (it is optional, so you must include it in your bundle manually)"
+ exit 1
+end
+
+require 'ffi_yajl/map_library_name'
module FFI_Yajl
extend ::FFI::Library
- libname = ::FFI.map_library_name("yajl")
- # XXX: need to replace ::FFI.map_library_name here as well
- libname = "libyajl.so" if libname == "yajl.dll"
+ extend FFI_Yajl::MapLibraryName
+
+ libname = map_library_name
libpath = File.expand_path(File.join(Libyajl2.opt_path, libname))
- libpath.gsub!(/dylib/, 'bundle')
if File.file?(libpath)
# use our vendored version of libyajl2 if we find it installed
diff --git a/lib/ffi_yajl/map_library_name.rb b/lib/ffi_yajl/map_library_name.rb
new file mode 100644
index 0000000..3518d07
--- /dev/null
+++ b/lib/ffi_yajl/map_library_name.rb
@@ -0,0 +1,37 @@
+
+require 'ffi_yajl/platform'
+
+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?
+
+ # 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'
+ 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
+ libprefix + "yajl" + ".#{libsuffix}"
+ end
+ end
+end