11# frozen_string_literal: true
22
3- require_relative 'ld_proofs'
43require 'ffi'
54require 'os'
6- require 'okapi/keys/v1/keys_pb'
7- require 'okapi/examples/v1/examples_pb'
8- require 'okapi/proofs/v1/proofs_pb'
9- require 'okapi/transport/v1/transport_pb'
10- require 'okapi/security/v1/security_pb'
11-
12- Examples_V1 = Okapi ::Examples ::V1
13- Hashing_V1 = Okapi ::Hashing ::V1
14- Keys_V1 = Okapi ::Keys ::V1
15- Proofs_V1 = Okapi ::Proofs ::V1
16- Security_V1 = Okapi ::Security ::V1
17- Transport_V1 = Okapi ::Transport ::V1
185
196# Okapi wrapper module
207module Okapi
218 extend FFI ::Library
229 @library_path = nil
2310 @library_linked = false
11+
2412 def self . library_path
2513 @library_path
2614 end
@@ -31,7 +19,7 @@ def self.library_path=(path)
3119
3220 def self . library_directory
3321 return 'windows' if OS . windows?
34- return 'linux' if OS . linux?
22+ return 'linux' if OS . linux? # TODO: Support linux on ARM
3523 return 'macos' if OS . mac?
3624
3725 raise NotImplementedError
@@ -46,13 +34,11 @@ def self.library_name
4634 end
4735
4836 class ByteBuffer < FFI ::Struct
49- layout :len , :int64 ,
50- :data , :pointer
37+ layout :len , :int64 , :data , :pointer
5138 end
5239
5340 class ExternError < FFI ::Struct
54- layout :code , :int32 ,
55- :message , :string
41+ layout :code , :int32 , :message , :string
5642 end
5743
5844 # rubocop:disable Metrics/MethodLength
@@ -61,16 +47,17 @@ def self.load_native_library
6147 return if @library_linked
6248
6349 @library_linked = true
64-
65- full_path = library_name
66- full_path = File . expand_path ( File . join ( library_path , library_name ) ) unless library_path . nil?
67- begin
68- ffi_lib full_path
50+ # Get the environment variable RUBY_DLL_PATH on all platforms as a failsafe,
51+ # MacOS system integrity protection, I'm looking at you.
52+ possible_library_paths = [ File . expand_path ( File . join ( __dir__ , '..' , 'libs' , library_directory , library_name ) ) ,
53+ library_name ,
54+ File . expand_path ( File . join ( library_path || '' , library_name ) ) ]
55+ possible_library_paths . each do |lib_path |
56+ puts ( "Attempting to load binary: #{ lib_path } " )
57+ ffi_lib lib_path
58+ break
6959 rescue LoadError
70- # Get the environment variable RUBY_DLL_PATH on all platforms as a failsafe,
71- # MacOS system integrity protection, I'm looking at you.
72- full_path = File . expand_path ( File . join ( ENV [ 'RUBY_DLL_PATH' ] , library_name ) )
73- ffi_lib full_path
60+ # Ignored
7461 end
7562
7663 attach_function :didkey_generate , [ ByteBuffer . by_value , ByteBuffer . by_ref , ExternError . by_ref ] , :int
@@ -118,6 +105,7 @@ def self.ffi_call(function, request, response_klass)
118105 byte_buffer_free ( response_buffer )
119106 response
120107 end
108+
121109 # rubocop:enable Metrics/MethodLength
122110 # rubocop:enable Metrics/AbcSize
123111
0 commit comments