Class: FFI::LibraryPath

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi/library_path.rb

Overview

Transform a generic library name and ABI number to a platform library name

Example:

module LibVips
  extend FFI::Library
  ffi_lib LibraryPath.new("vips", abi_number: 42)
end

This translates to the following library file names:

libvips-42.dll    on Windows
libvips.so.42     on Linux
libvips.42.dylib  on Macos

See packaging.ubuntu.com/html/libraries.html for more information about library naming.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, abi_number: nil, root: nil) ⇒ LibraryPath

Build a new library path

  • name : The name of the library without file prefix or suffix.

  • abi_number : The ABI number of the library.

  • root : An optional base path prepended to the library name.



56
57
58
59
60
# File 'lib/ffi/library_path.rb', line 56

def initialize(name, abi_number: nil, root: nil)
  @name = name
  @abi_number = abi_number
  @root = root
end

Instance Attribute Details

#abi_numberObject (readonly)

Returns the value of attribute abi_number.



48
49
50
# File 'lib/ffi/library_path.rb', line 48

def abi_number
  @abi_number
end

#nameObject (readonly)

Returns the value of attribute name.



47
48
49
# File 'lib/ffi/library_path.rb', line 47

def name
  @name
end

#rootObject (readonly)

Returns the value of attribute root.



49
50
51
# File 'lib/ffi/library_path.rb', line 49

def root
  @root
end

Class Method Details

.wrap(value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ffi/library_path.rb', line 62

def self.wrap(value)
  # We allow instances of LibraryPath to pass through transparently:
  return value if value.is_a?(self)

  # We special case a library named 'c' to be the standard C library:
  return Library::LIBC if value == 'c'

  # If provided a relative file name we convert it into a library path:
  if value && File.basename(value) == value
    return self.new(value)
  end

  # Otherwise, we assume it's a full path to a library:
  return value
end

Instance Method Details

#full_nameObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ffi/library_path.rb', line 78

def full_name
  # If the abi_number is given, we format it specifically according to platform rules:
  if abi_number
    if Platform.windows?
      "#{Platform::LIBPREFIX}#{name}-#{abi_number}.#{Platform::LIBSUFFIX}"
    elsif Platform.mac?
      "#{Platform::LIBPREFIX}#{name}.#{abi_number}.#{Platform::LIBSUFFIX}"
    else # Linux? BSD? etc.
      "#{Platform::LIBPREFIX}#{name}.#{Platform::LIBSUFFIX}.#{abi_number}"
    end
  else
    # Otherwise we just add prefix and suffix:
    lib = name
    # Add library prefix if missing
    lib = Platform::LIBPREFIX + lib unless lib =~ /^#{Platform::LIBPREFIX}/
    # Add library extension if missing
    r = Platform.windows? || Platform.mac? ? "\\.#{Platform::LIBSUFFIX}$" : "\\.so($|\\.[1234567890]+)"
    lib += ".#{Platform::LIBSUFFIX}" unless lib =~ /#{r}/
    lib
  end
end

#to_sObject



100
101
102
103
104
105
106
107
# File 'lib/ffi/library_path.rb', line 100

def to_s
  if root
    # If the root path is given, we generate the full path:
    File.join(root, full_name)
  else
    full_name
  end
end