Module: Rocksky::Native

Defined in:
lib/rocksky/native.rb

Class Method Summary collapse

Class Method Details

.archObject



36
37
38
39
40
41
42
43
# File 'lib/rocksky/native.rb', line 36

def arch
  a = RbConfig::CONFIG["host_cpu"]
  case a
  when /x86_64|amd64/ then "x86_64"
  when /arm64|aarch64/ then "aarch64"
  else a
  end
end

.cache_dir(tag) ⇒ Object



61
62
63
64
# File 'lib/rocksky/native.rb', line 61

def cache_dir(tag)
  base = ENV["XDG_CACHE_HOME"] || File.join(Dir.home, ".cache")
  File.join(base, "rocksky", tag)
end

.download_verify(repo, tag, triple, sha, dest) ⇒ Object

Raises:



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rocksky/native.rb', line 90

def download_verify(repo, tag, triple, sha, dest)
  url = "https://github.com/#{repo}/releases/download/#{tag}/librocksky_uniffi-#{triple}.#{ext}"
  body = fetch(url)
  got = Digest::SHA256.hexdigest(body)
  raise Error, "checksum mismatch for #{triple}: want #{sha}, got #{got}" unless got == sha

  FileUtils.mkdir_p(File.dirname(dest))
  tmp = "#{dest}.download"
  File.binwrite(tmp, body)
  File.chmod(0o755, tmp)
  File.rename(tmp, dest)
end

.extObject



28
29
30
31
32
33
34
# File 'lib/rocksky/native.rb', line 28

def ext
  case RbConfig::CONFIG["host_os"]
  when /darwin/ then "dylib"
  when /mswin|mingw|cygwin/ then "dll"
  else "so"
  end
end

.fetch(url, limit = 5) ⇒ Object

Raises:



103
104
105
106
107
108
109
110
111
112
# File 'lib/rocksky/native.rb', line 103

def fetch(url, limit = 5)
  raise Error, "too many redirects" if limit.zero?

  res = Net::HTTP.get_response(URI(url))
  case res
  when Net::HTTPSuccess then res.body
  when Net::HTTPRedirection then fetch(res["location"], limit - 1)
  else raise Error, "download failed (#{res.code}) for #{url}"
  end
end

.lib_dirObject



57
58
59
# File 'lib/rocksky/native.rb', line 57

def lib_dir
  __dir__ # sdk/ruby/lib/rocksky
end

.manifestObject



66
67
68
# File 'lib/rocksky/native.rb', line 66

def manifest
  JSON.parse(File.read(File.join(lib_dir, "manifest.json")))
end

.resolveObject

Absolute path to a loadable native library, fetching it if necessary.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rocksky/native.rb', line 71

def resolve
  env = ENV["ROCKSKY_NATIVE_LIB"]
  return env if env && File.exist?(env)

  local = File.join(lib_dir, "librocksky_uniffi.#{ext}")
  return local if File.exist?(local)

  m = manifest
  t = triple
  sha = m.dig("checksums", t) or
    raise Error, "no prebuilt native lib for #{t} (manifest has no checksum) — run ./build-core.sh"
  tag = m["tag"]
  dest = File.join(cache_dir(tag), "librocksky_uniffi-#{t}.#{ext}")
  return dest if File.exist?(dest) && Digest::SHA256.file(dest).hexdigest == sha

  download_verify(m["repo"], tag, t, sha, dest)
  dest
end

.tripleObject

Canonical target triple matching the release asset names.



46
47
48
49
50
51
52
53
54
55
# File 'lib/rocksky/native.rb', line 46

def triple
  case RbConfig::CONFIG["host_os"]
  when /darwin/ then "#{arch}-apple-darwin"
  when /linux/ then "#{arch}-linux-gnu"
  when /freebsd/ then "#{arch}-unknown-freebsd"
  when /netbsd/ then "#{arch}-unknown-netbsd"
  when /openbsd/ then "#{arch}-unknown-openbsd"
  else raise Error, "unsupported platform: #{RbConfig::CONFIG["host_os"]}"
  end
end