Skip to content

Commit

Permalink
fix(ffi): check for built binary both with and without lib prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Oct 10, 2024
1 parent 585658c commit 07a9649
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 32 deletions.
11 changes: 1 addition & 10 deletions build-ffi-bindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,7 @@ local function get_shared_lib_extension()
end
end
local function get_shared_lib_prefix()
local os = jit.os:lower()
if os == 'windows' then
return ''
else
return 'lib'
end
end
$(cat "$(pwd)/lua/blink/cmp/fuzzy/ffi.lua")
" > "$(pwd)/lua/blink/cmp/fuzzy/ffi.lua"

sed -i "s/ffi\.load.'blink-cmp-fuzzy'./ffi.load(debug.getinfo(1).source:match('@?(.*\/)') .. '..\/..\/..\/..\/target\/release\/' .. get_shared_lib_prefix() .. 'blink_cmp_fuzzy\' .. get_shared_lib_extension())/" "$(pwd)/lua/blink/cmp/fuzzy/ffi.lua"
sed -i "s|ffi\.load('blink-cmp-fuzzy').|local ok, rust = pcall(function() return ffi.load(debug.getinfo(1).source:match('@?(.*/)') .. '../../../../target/release/libblink_cmp_fuzzy' .. get_shared_lib_extension()) end)\nif not ok then\n rust = ffi.load(debug.getinfo(1).source:match('@?(.*/)') .. '../../../../target/release/blink_cmp_fuzzy' .. get_shared_lib_extension())\nend|" "$(pwd)/lua/blink/cmp/fuzzy/ffi.lua"
21 changes: 9 additions & 12 deletions lua/blink/cmp/fuzzy/download.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,8 @@ function download.get_lib_extension()
return '.so'
end

--- @return string
function download.get_lib_prefix()
if jit.os:lower() == 'windows' then return '' end
return 'lib'
end

local root_dir = debug.getinfo(1).source:match('@?(.*/)')
local lib_path = root_dir
.. '../../../../target/release/'
.. download.get_lib_prefix()
.. 'blink_cmp_fuzzy'
.. download.get_lib_extension()
local lib_path = root_dir .. '../../../../target/release/libblink_cmp_fuzzy' .. download.get_lib_extension()
local version_path = root_dir .. '../../../../target/release/version.txt'

--- @param callback fun(err: string | nil)
Expand Down Expand Up @@ -68,7 +58,14 @@ end

--- @param cb fun(downloaded: boolean)
function download.is_downloaded(cb)
return vim.uv.fs_stat(lib_path, function(err) cb(not err) end)
vim.uv.fs_stat(lib_path, function(err)
if not err then
cb(true)
else
-- If not found, check without 'lib' prefix
vim.uv.fs_stat(string.gsub(lib_path, 'lib', ''), function(error) cb(not error) end)
end
end)
end

--- @param cb fun(err: string | nil, tag: string | nil)
Expand Down
14 changes: 4 additions & 10 deletions lua/blink/cmp/fuzzy/ffi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ local function get_shared_lib_extension()
end
end

local function get_shared_lib_prefix()
local os = jit.os:lower()
if os == 'windows' then
return ''
else
return 'lib'
end
end

-- Code generated by Rust Lua interface. DO NOT EDIT.

local ffi = require("ffi")
Expand Down Expand Up @@ -106,7 +97,10 @@ int32_t __gc_get_words(

]]

local rust = ffi.load(debug.getinfo(1).source:match('@?(.*/)') .. '../../../../target/release/' .. get_shared_lib_prefix() .. 'blink_cmp_fuzzy' .. get_shared_lib_extension())
local ok, rust = pcall(function() return ffi.load(debug.getinfo(1).source:match('@?(.*/)') .. '../../../../target/release/libblink_cmp_fuzzy' .. get_shared_lib_extension()) end)
if not ok then
rust = ffi.load(debug.getinfo(1).source:match('@?(.*/)') .. '../../../../target/release/blink_cmp_fuzzy' .. get_shared_lib_extension())
end

local M = {}

Expand Down

0 comments on commit 07a9649

Please sign in to comment.