Module: Chordsketch::NativeLoader Private

Defined in:
lib/chordsketch.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Constant Summary collapse

PLATFORM_MAP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  /x86_64.*linux/                 => "x86_64-linux",
  /aarch64.*linux/                => "aarch64-linux",
  /arm64.*darwin|aarch64.*darwin/ => "aarch64-darwin",
  /x86_64.*darwin/                => "x86_64-darwin",
  /x64.*mingw|x64.*mswin/        => "x86_64-windows",
}.freeze

Class Method Summary collapse

Class Method Details

.detect_platform_dirObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Detect the platform-specific subdirectory for native libraries.



31
32
33
34
35
36
37
38
# File 'lib/chordsketch.rb', line 31

def self.detect_platform_dir
  arch = RbConfig::CONFIG["arch"]
  PLATFORM_MAP.each do |pattern, dir|
    return dir if arch.match?(pattern)
  end
  raise "Unsupported platform: #{arch}. " \
        "ChordSketch supports x86_64/aarch64 Linux, macOS, and x86_64 Windows."
end

.load!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolve and validate the absolute path to the platform-specific native library, then expose it as ‘Chordsketch::NATIVE_LIB_PATH` so the UniFFI-generated bindings can pass it to `ffi_lib`.

The previous approach of pre-loading via ‘FFI::DynamicLibrary.open` with `RTLD_GLOBAL` was unreliable: ffi gem 1.17+ explicitly opens its own handle by name in `ffi_lib` and ignores already-loaded global handles, so the bindings would fail with “Could not open library ’chordsketch_ffi’” even after a successful pre-load. See #1082.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chordsketch.rb', line 50

def self.load!
  platform_dir = detect_platform_dir
  lib_dir = File.join(File.dirname(__FILE__), platform_dir)
  lib_name = FFI.map_library_name("chordsketch_ffi")
  lib_path = File.join(lib_dir, lib_name)

  unless File.exist?(lib_path)
    raise "Native library not found at #{lib_path}. " \
          "Ensure the gem was built for your platform (#{platform_dir}). " \
          "For local development, generate bindings with:\n" \
          "  cargo run -p chordsketch-ffi --bin uniffi-bindgen generate \\\n" \
          "    --library target/debug/libchordsketch_ffi.so \\\n" \
          "    --language ruby --out-dir packages/ruby/lib/"
  end

  Chordsketch.const_set(:NATIVE_LIB_PATH, lib_path)
end