Skip to content

Commit e9493c6

Browse files
fix(ffi): handle cargo library naming conventions for windows binaries (#74)
* fix(ffi): handle cargo library naming conventions for windows binaries * fix(ffi): check for built binary both with and without lib prefix * fix: more reliable path substitution for windows binary * fix(ffi): sed command in build script
1 parent 28fe661 commit e9493c6

File tree

3 files changed

+75
-65
lines changed

3 files changed

+75
-65
lines changed

build-ffi-bindings.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ end
4444
$(cat "$(pwd)/lua/blink/cmp/fuzzy/ffi.lua")
4545
" > "$(pwd)/lua/blink/cmp/fuzzy/ffi.lua"
4646

47-
sed -i "s/ffi\.load.'blink-cmp-fuzzy'./ffi.load(debug.getinfo(1).source:match('@?(.*\/)') .. '..\/..\/..\/..\/target\/release\/libblink_cmp_fuzzy\' .. get_shared_lib_extension())/" "$(pwd)/lua/blink/cmp/fuzzy/ffi.lua"
47+
sed -i "s|local rust = 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"

lua/blink/cmp/fuzzy/download.lua

+8-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ end
5858

5959
--- @param cb fun(downloaded: boolean)
6060
function download.is_downloaded(cb)
61-
return vim.uv.fs_stat(lib_path, function(err) cb(not err) end)
61+
vim.uv.fs_stat(lib_path, function(err)
62+
if not err then
63+
cb(true)
64+
else
65+
-- If not found, check without 'lib' prefix
66+
vim.uv.fs_stat(string.gsub(lib_path, 'libblink_cmp_fuzzy', 'blink_cmp_fuzzy'), function(error) cb(not error) end)
67+
end
68+
end)
6269
end
6370

6471
--- @param cb fun(err: string | nil, tag: string | nil)

lua/blink/cmp/fuzzy/ffi.lua

+66-63
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,24 @@ end
1919

2020

2121

22+
2223
typedef struct {
23-
const char * *ptr;
24-
size_t len;
25-
size_t capacity;
26-
} blink_cmp_fuzzy__Vec___string_ptr;
24+
const int32_t *ptr;
25+
} blink_cmp_fuzzy__Option_int32_t;
2726
typedef struct {
2827
const int32_t *ptr;
2928
size_t len;
3029
size_t capacity;
3130
} blink_cmp_fuzzy__Vec_int32_t;
31+
3232
typedef struct {
33-
const int32_t *ptr;
34-
} blink_cmp_fuzzy__Option_int32_t;
35-
typedef struct {
36-
const uint32_t *ptr;
33+
const char * *ptr;
3734
size_t len;
3835
size_t capacity;
39-
} blink_cmp_fuzzy__Vec_uint32_t;
40-
41-
42-
36+
} blink_cmp_fuzzy__Vec___string_ptr;
4337
typedef struct {
4438
const char * *ptr;
4539
} blink_cmp_fuzzy__Option___string_ptr;
46-
47-
typedef struct {
48-
const char * label;
49-
const blink_cmp_fuzzy__Option___string_ptr sort_text;
50-
const blink_cmp_fuzzy__Option___string_ptr filter_text;
51-
const uint32_t kind;
52-
const blink_cmp_fuzzy__Option_int32_t score_offset;
53-
const char * source;
54-
} blink_cmp_fuzzy__LspItem;
55-
5640
typedef struct {
5741
const blink_cmp_fuzzy__Vec___string_ptr *ptr;
5842
} blink_cmp_fuzzy__Option_Vec___string_ptr;
@@ -65,6 +49,22 @@ typedef struct {
6549
const blink_cmp_fuzzy__Vec___string_ptr sorts;
6650
} blink_cmp_fuzzy__FuzzyOptions;
6751

52+
53+
54+
typedef struct {
55+
const char * label;
56+
const blink_cmp_fuzzy__Option___string_ptr sort_text;
57+
const blink_cmp_fuzzy__Option___string_ptr filter_text;
58+
const uint32_t kind;
59+
const blink_cmp_fuzzy__Option_int32_t score_offset;
60+
const char * source;
61+
} blink_cmp_fuzzy__LspItem;
62+
63+
typedef struct {
64+
const uint32_t *ptr;
65+
size_t len;
66+
size_t capacity;
67+
} blink_cmp_fuzzy__Vec_uint32_t;
6868
int32_t init_db(
6969
const char *,
7070
int8_t*);
@@ -97,37 +97,33 @@ int32_t __gc_get_words(
9797

9898
]]
9999

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

102105
local M = {}
103106

104107

105108

106-
local __const_c_typename_int32_t = ffi.typeof("const int32_t[?]")
107-
local __c_function_argument_int32_t = ffi.typeof("int32_t[?]")
108-
local __c_mut_function_argument_int32_t = ffi.typeof("int32_t[?]")
109-
110-
111-
local __const_c_typename___string_ptr = ffi.typeof("const char *[?]")
112-
local __c_function_argument___string_ptr = ffi.typeof("const char *[?]")
113-
local __c_mut_function_argument___string_ptr = ffi.typeof("char *[?]")
109+
local __const_c_typename_bool = ffi.typeof("const int8_t[?]")
110+
local __c_function_argument_bool = ffi.typeof("int8_t[?]")
111+
local __c_mut_function_argument_bool = ffi.typeof("int8_t[?]")
114112

115113

116-
local __const_c_typename_uint32_t = ffi.typeof("const uint32_t[?]")
117-
local __c_function_argument_uint32_t = ffi.typeof("uint32_t[?]")
118-
local __c_mut_function_argument_uint32_t = ffi.typeof("uint32_t[?]")
114+
local __const_c_typename_size_t = ffi.typeof("const size_t[?]")
115+
local __c_function_argument_size_t = ffi.typeof("size_t[?]")
116+
local __c_mut_function_argument_size_t = ffi.typeof("size_t[?]")
119117

120118

121-
local __typename_Vec___string_ptr = ffi.metatype("blink_cmp_fuzzy__Vec___string_ptr", {})
122-
local __const_c_typename_Vec___string_ptr = ffi.typeof("const blink_cmp_fuzzy__Vec___string_ptr[?]")
123-
local __c_function_argument_Vec___string_ptr = ffi.typeof("const blink_cmp_fuzzy__Vec___string_ptr*[?]")
124-
local __c_mut_function_argument_Vec___string_ptr = ffi.typeof("blink_cmp_fuzzy__Vec___string_ptr*[?]")
119+
local __const_c_typename_uint16_t = ffi.typeof("const uint16_t[?]")
120+
local __c_function_argument_uint16_t = ffi.typeof("uint16_t[?]")
121+
local __c_mut_function_argument_uint16_t = ffi.typeof("uint16_t[?]")
125122

126123

127-
local __typename_Vec_int32_t = ffi.metatype("blink_cmp_fuzzy__Vec_int32_t", {})
128-
local __const_c_typename_Vec_int32_t = ffi.typeof("const blink_cmp_fuzzy__Vec_int32_t[?]")
129-
local __c_function_argument_Vec_int32_t = ffi.typeof("const blink_cmp_fuzzy__Vec_int32_t*[?]")
130-
local __c_mut_function_argument_Vec_int32_t = ffi.typeof("blink_cmp_fuzzy__Vec_int32_t*[?]")
124+
local __const_c_typename_int32_t = ffi.typeof("const int32_t[?]")
125+
local __c_function_argument_int32_t = ffi.typeof("int32_t[?]")
126+
local __c_mut_function_argument_int32_t = ffi.typeof("int32_t[?]")
131127

132128

133129
local __typename_Option_int32_t = ffi.metatype("blink_cmp_fuzzy__Option_int32_t", {})
@@ -136,25 +132,21 @@ local __c_function_argument_Option_int32_t = ffi.typeof("const blink_cmp_fuzzy__
136132
local __c_mut_function_argument_Option_int32_t = ffi.typeof("blink_cmp_fuzzy__Option_int32_t*[?]")
137133

138134

139-
local __typename_Vec_uint32_t = ffi.metatype("blink_cmp_fuzzy__Vec_uint32_t", {})
140-
local __const_c_typename_Vec_uint32_t = ffi.typeof("const blink_cmp_fuzzy__Vec_uint32_t[?]")
141-
local __c_function_argument_Vec_uint32_t = ffi.typeof("const blink_cmp_fuzzy__Vec_uint32_t*[?]")
142-
local __c_mut_function_argument_Vec_uint32_t = ffi.typeof("blink_cmp_fuzzy__Vec_uint32_t*[?]")
143-
144-
145-
local __const_c_typename_size_t = ffi.typeof("const size_t[?]")
146-
local __c_function_argument_size_t = ffi.typeof("size_t[?]")
147-
local __c_mut_function_argument_size_t = ffi.typeof("size_t[?]")
135+
local __typename_Vec_int32_t = ffi.metatype("blink_cmp_fuzzy__Vec_int32_t", {})
136+
local __const_c_typename_Vec_int32_t = ffi.typeof("const blink_cmp_fuzzy__Vec_int32_t[?]")
137+
local __c_function_argument_Vec_int32_t = ffi.typeof("const blink_cmp_fuzzy__Vec_int32_t*[?]")
138+
local __c_mut_function_argument_Vec_int32_t = ffi.typeof("blink_cmp_fuzzy__Vec_int32_t*[?]")
148139

149140

150-
local __const_c_typename_bool = ffi.typeof("const int8_t[?]")
151-
local __c_function_argument_bool = ffi.typeof("int8_t[?]")
152-
local __c_mut_function_argument_bool = ffi.typeof("int8_t[?]")
141+
local __const_c_typename___string_ptr = ffi.typeof("const char *[?]")
142+
local __c_function_argument___string_ptr = ffi.typeof("const char *[?]")
143+
local __c_mut_function_argument___string_ptr = ffi.typeof("char *[?]")
153144

154145

155-
local __const_c_typename_uint16_t = ffi.typeof("const uint16_t[?]")
156-
local __c_function_argument_uint16_t = ffi.typeof("uint16_t[?]")
157-
local __c_mut_function_argument_uint16_t = ffi.typeof("uint16_t[?]")
146+
local __typename_Vec___string_ptr = ffi.metatype("blink_cmp_fuzzy__Vec___string_ptr", {})
147+
local __const_c_typename_Vec___string_ptr = ffi.typeof("const blink_cmp_fuzzy__Vec___string_ptr[?]")
148+
local __c_function_argument_Vec___string_ptr = ffi.typeof("const blink_cmp_fuzzy__Vec___string_ptr*[?]")
149+
local __c_mut_function_argument_Vec___string_ptr = ffi.typeof("blink_cmp_fuzzy__Vec___string_ptr*[?]")
158150

159151

160152
local __typename_Option___string_ptr = ffi.metatype("blink_cmp_fuzzy__Option___string_ptr", {})
@@ -163,12 +155,6 @@ local __c_function_argument_Option___string_ptr = ffi.typeof("const blink_cmp_fu
163155
local __c_mut_function_argument_Option___string_ptr = ffi.typeof("blink_cmp_fuzzy__Option___string_ptr*[?]")
164156

165157

166-
local __typename_LspItem = ffi.metatype("blink_cmp_fuzzy__LspItem", {})
167-
local __const_c_typename_LspItem = ffi.typeof("const blink_cmp_fuzzy__LspItem[?]")
168-
local __c_function_argument_LspItem = ffi.typeof("const blink_cmp_fuzzy__LspItem*[?]")
169-
local __c_mut_function_argument_LspItem = ffi.typeof("blink_cmp_fuzzy__LspItem*[?]")
170-
171-
172158
local __typename_Option_Vec___string_ptr = ffi.metatype("blink_cmp_fuzzy__Option_Vec___string_ptr", {})
173159
local __const_c_typename_Option_Vec___string_ptr = ffi.typeof("const blink_cmp_fuzzy__Option_Vec___string_ptr[?]")
174160
local __c_function_argument_Option_Vec___string_ptr = ffi.typeof("const blink_cmp_fuzzy__Option_Vec___string_ptr*[?]")
@@ -180,6 +166,23 @@ local __const_c_typename_FuzzyOptions = ffi.typeof("const blink_cmp_fuzzy__Fuzzy
180166
local __c_function_argument_FuzzyOptions = ffi.typeof("const blink_cmp_fuzzy__FuzzyOptions*[?]")
181167
local __c_mut_function_argument_FuzzyOptions = ffi.typeof("blink_cmp_fuzzy__FuzzyOptions*[?]")
182168

169+
170+
local __const_c_typename_uint32_t = ffi.typeof("const uint32_t[?]")
171+
local __c_function_argument_uint32_t = ffi.typeof("uint32_t[?]")
172+
local __c_mut_function_argument_uint32_t = ffi.typeof("uint32_t[?]")
173+
174+
175+
local __typename_LspItem = ffi.metatype("blink_cmp_fuzzy__LspItem", {})
176+
local __const_c_typename_LspItem = ffi.typeof("const blink_cmp_fuzzy__LspItem[?]")
177+
local __c_function_argument_LspItem = ffi.typeof("const blink_cmp_fuzzy__LspItem*[?]")
178+
local __c_mut_function_argument_LspItem = ffi.typeof("blink_cmp_fuzzy__LspItem*[?]")
179+
180+
181+
local __typename_Vec_uint32_t = ffi.metatype("blink_cmp_fuzzy__Vec_uint32_t", {})
182+
local __const_c_typename_Vec_uint32_t = ffi.typeof("const blink_cmp_fuzzy__Vec_uint32_t[?]")
183+
local __c_function_argument_Vec_uint32_t = ffi.typeof("const blink_cmp_fuzzy__Vec_uint32_t*[?]")
184+
local __c_mut_function_argument_Vec_uint32_t = ffi.typeof("blink_cmp_fuzzy__Vec_uint32_t*[?]")
185+
183186
function M.init_db(
184187
db_path)
185188
local __typeof = __c_mut_function_argument_bool

0 commit comments

Comments
 (0)