module FFI::Library

def ffi_lib(*names)

def ffi_lib(*names)
  ffi_libs = names.map do |name|
    if name == FFI::CURRENT_PROCESS
      FFI::DynamicLibrary.open(nil, FFI::DynamicLibrary::RTLD_LAZY | FFI::DynamicLibrary::RTLD_GLOBAL)
    else
      libnames = (name.is_a?(::Array) ? name : [ name ]).map { |n| [ n, FFI.map_library_name(n) ].uniq }.flatten.compact
      lib = nil
      errors = {}
      libnames.each do |libname|
        begin
          lib = FFI::DynamicLibrary.open(libname, FFI::DynamicLibrary::RTLD_LAZY | FFI::DynamicLibrary::RTLD_GLOBAL)
          break if lib
        rescue Exception => ex
          errors[libname] = ex
        end
      end
      if lib.nil?
        raise LoadError.new(errors.values.join('. '))
      end
      # return the found lib
      lib
    end
  end
  @ffi_libs = ffi_libs
end