diff options
author | Samuel Williams <samuel.williams@oriontransfer.co.nz> | 2022-06-24 21:32:38 +1200 |
---|---|---|
committer | Samuel Williams <samuel.williams@oriontransfer.co.nz> | 2022-06-24 21:37:58 +1200 |
commit | c95b8f3664299ff60fa06073ab0ad77f4d5ed3d0 (patch) | |
tree | 0c095e49d48b85bd923f22d4760202f314b1cb48 /lib/ffi/library.rb | |
parent | 269e8f939e497bba749ff54ee4d79197e5c06ead (diff) | |
download | ffi-c95b8f3664299ff60fa06073ab0ad77f4d5ed3d0.tar.gz |
Add support for passing ABI version to `FFI.map_library_name`.
Diffstat (limited to 'lib/ffi/library.rb')
-rw-r--r-- | lib/ffi/library.rb | 59 |
1 files changed, 49 insertions, 10 deletions
diff --git a/lib/ffi/library.rb b/lib/ffi/library.rb index 43b2bfe..c2628ab 100644 --- a/lib/ffi/library.rb +++ b/lib/ffi/library.rb @@ -31,6 +31,53 @@ module FFI CURRENT_PROCESS = USE_THIS_PROCESS_AS_LIBRARY = Object.new + class LibraryPath < ::Struct.new(:name, :abi_number, :root) + PATTERN = /(#{Platform::LIBPREFIX})?(?<name>.*?)(\.|\z)/ + + 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 + if match = PATTERN.match(value) + return self.new(match[:name]) + end + end + + # Otherwise, we assume it's a full path to a library: + return value + end + + 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 use a generic format: + "#{Platform::LIBPREFIX}#{name}.#{Platform::LIBSUFFIX}" + end + end + + 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 + end + # @param [#to_s] lib library name # @return [String] library name formatted for current platform # Transform a generic library name to a platform library name @@ -41,17 +88,9 @@ module FFI # # Windows # FFI.map_library_name 'c' # -> "msvcrt.dll" # FFI.map_library_name 'jpeg' # -> "jpeg.dll" - def self.map_library_name(lib) + def self.map_library_name(value) # Mangle the library name to reflect the native library naming conventions - lib = Library::LIBC if lib == 'c' - - if lib && File.basename(lib) == lib - lib = Platform::LIBPREFIX + lib unless lib =~ /^#{Platform::LIBPREFIX}/ - r = Platform::IS_WINDOWS || Platform::IS_MAC ? "\\.#{Platform::LIBSUFFIX}$" : "\\.so($|\\.[1234567890]+)" - lib += ".#{Platform::LIBSUFFIX}" unless lib =~ /#{r}/ - end - - lib + LibraryPath.wrap(value).to_s end # Exception raised when a function is not found in libraries |