Skip to content

Commit

Permalink
fix compat with semistatic libvips
Browse files Browse the repository at this point in the history
we must fetch `g_*()` funcs from libvips if we can
  • Loading branch information
jcupitt committed Mar 5, 2024
1 parent badc358 commit 2c55d8b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

* fix compat with unified (semistatic) libvips binaries [kleisauke]

## Version 2.2.1 (2024-02-21)

* add `Vips.block_untrusted` method to block all untrusted operations. Only for libvips >= 8.13. [Docs](https://www.libvips.org/API/current/libvips-vips.html#vips-block-untrusted-set). [#382](https://github.com/libvips/ruby-vips/pull/382) [aglushkov](https://github.com/aglushkov)
Expand Down
50 changes: 32 additions & 18 deletions lib/vips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ def library_name(name, abi_number)
end
end

# we can sometimes get dependent libraries from libvips -- either the platform
# will open dependencies for us automatically, or the libvips binary has been
# built to includes all main dependencies (common on windows, can happen
# elsewhere)
#
# we must get glib functions from libvips if we can, since it will be the
# one that libvips itself is using, and they will share runtime types
module Vips
extend FFI::Library

ffi_lib library_name("vips", 42)

begin
attach_function :g_malloc, [:size_t], :pointer
@@is_unified = true
rescue => e
@@is_unified = false
end

def self.unified?
@@is_unified
end
end

module GLib
class << self
attr_accessor :logger
Expand All @@ -42,18 +66,10 @@ class << self

extend FFI::Library

if FFI::Platform.windows?
# On Windows, `GetProcAddress()` can only search in a specified DLL and
# doesn't look into its dependent libraries for symbols. Therefore, we
# check if the GLib DLLs are available. If these can not be found, we
# assume that GLib is statically linked into libvips.
ffi_lib ["libglib-2.0-0.dll", "libvips-42.dll"]
else
# macOS and *nix uses `dlsym()`, which also searches for named symbols
# in the dependencies of the shared library. Therefore, we can support
# a single shared libvips library with all dependencies statically
# linked.
if Vips::unified?
ffi_lib library_name("vips", 42)
else
ffi_lib library_name("glib-2.0", 0)
end

attach_function :g_malloc, [:size_t], :pointer
Expand Down Expand Up @@ -146,10 +162,10 @@ def self.set_log_domain domain
module GObject
extend FFI::Library

if FFI::Platform.windows?
ffi_lib ["libgobject-2.0-0.dll", "libvips-42.dll"]
else
if Vips::unified?
ffi_lib library_name("vips", 42)
else
ffi_lib library_name("gobject-2.0", 0)
end

# we can't just use ulong, windows has different int sizing rules
Expand Down Expand Up @@ -584,10 +600,8 @@ module GObject
# {Image#median}.

module Vips
extend FFI::Library

ffi_lib library_name("vips", 42)

# we've already opened the libvips library

LOG_DOMAIN = "VIPS"
GLib.set_log_domain LOG_DOMAIN

Expand Down

0 comments on commit 2c55d8b

Please sign in to comment.