Permalink
Browse files

Merge pull request #2 from tekwiz/search-paths

Search paths
  • Loading branch information...
cfis committed Aug 31, 2011
2 parents 7bc5265 + cad43c7 commit da5f20aa6e85e86633fd4bf9496c20b6e15147b2
Showing with 37 additions and 6 deletions.
  1. +37 −6 lib/free-image.rb
View
@@ -14,16 +14,47 @@ def self.msvc?
FFI::Platform.windows?
end
- def self.library
- if FFI::Platform.windows?
- 'FreeImaged'
- else
- 'free_image'
+ def self.search_paths
+ @search_paths ||= begin
+ if ENV['FREE_IMAGE_LIBRARY_PATH']
+ [ ENV['FREE_IMAGE_LIBRARY_PATH'] ]
+ elsif FFI::Platform::IS_WINDOWS
+ ENV['PATH'].split(File::PATH_SEPARATOR)
+ else
+ [ '/usr/local/{lib64,lib32,lib}', '/opt/local/{lib64,lib32,lib}', '/usr/{lib64,lib32,lib}' ]
+ end
+ end
+ end
+
+ def self.find_lib(lib)
+ files = search_paths.inject(Array.new) do |array, path|
+ file_name = File.expand_path(File.join(path, "#{lib}.#{FFI::Platform::LIBSUFFIX}"))
+ array << Dir.glob(file_name)
+ array
+ end
+ files.flatten.compact.first
+ end
+
+ def self.free_image_library_paths
+ @free_image_library_paths ||= begin
+ libs = %w{libfreeimage libfreeimage.3 FreeImaged}
+
+ libs.map do |lib|
+ find_lib(lib)
+ end.compact
end
end
extend ::FFI::Library
- ffi_lib(library)
+
+ if free_image_library_paths.any?
+ ffi_lib(*free_image_library_paths)
+ elsif FFI::Platform.windows?
+ ffi_lib("FreeImaged")
+ else
+ ffi_lib("free_image")
+ end
+
ffi_convention :stdcall if FFI::Platform.windows?
end

0 comments on commit da5f20a

Please sign in to comment.