From 0530df59d37b12a467661aaf001c045dd10795dd Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Sat, 21 Sep 2019 11:24:48 +0100 Subject: [PATCH] improve libname generation library names for ffi vary by platform -- this patch adds a helper function which tries to generate the right format for max / linux / win. See https://github.com/libvips/ruby-vips/issues/176 --- lib/vips.rb | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/lib/vips.rb b/lib/vips.rb index 26cb7711..61bcf709 100644 --- a/lib/vips.rb +++ b/lib/vips.rb @@ -10,6 +10,29 @@ # This module uses FFI to make a simple layer over the glib and gobject # libraries. +# Generate a library name for ffi. +# +# Platform notes: +# linux: +# Some distros allow "libvips.so", but only if the -dev headers have been +# installed. To work everywhere, you must include the ABI number. +# Confusingly, the file extension is not at the end. ffi adds the "lib" +# prefix. +# mac: +# As linux, but the extension is at the end and is added by ffi. +# windows: +# The ABI number must be included, but with a hyphen. ffi does not add a +# "lib" prefix or a ".dll" suffix. +def library_name(name, abi_number) + if FFI::Platform.windows? + return "lib#{name}-#{abi_number}.dll" + elsif FFI::Platform.mac? + return "#{name}.#{abi_number}" + else + return "#{name}.so.#{abi_number}" + end +end + module GLib class << self attr_accessor :logger @@ -19,13 +42,7 @@ class << self extend FFI::Library - if FFI::Platform.windows? - glib_libname = 'libglib-2.0-0.dll' - else - glib_libname = 'glib-2.0' - end - - ffi_lib glib_libname + ffi_lib library_name('glib-2.0', 0) attach_function :g_malloc, [:size_t], :pointer @@ -117,13 +134,7 @@ def self.set_log_domain domain module GObject extend FFI::Library - if FFI::Platform.windows? - gobject_libname = 'libgobject-2.0-0.dll' - else - gobject_libname = 'gobject-2.0' - end - - ffi_lib gobject_libname + ffi_lib library_name('gobject-2.0', 0) # we can't just use ulong, windows has different int sizing rules if FFI::Platform::ADDRESS_SIZE == 64 @@ -459,13 +470,7 @@ module GObject module Vips extend FFI::Library - if FFI::Platform.windows? - vips_libname = 'libvips-42.dll' - else - vips_libname = 'vips' - end - - ffi_lib vips_libname + ffi_lib library_name('vips', 42) LOG_DOMAIN = "VIPS" GLib::set_log_domain LOG_DOMAIN