Skip to content

Commit 0cd4e34

Browse files
committed
Fix findlib and findheader on windows
1 parent 62b9bc7 commit 0cd4e34

File tree

1 file changed

+23
-53
lines changed

1 file changed

+23
-53
lines changed

src/base/os.lua

+23-53
Original file line numberDiff line numberDiff line change
@@ -63,44 +63,31 @@
6363
end
6464

6565
local function get_library_search_path()
66-
local path
6766
if os.istarget("windows") then
68-
path = os.getenv("PATH") or ""
67+
return (os.getenv("PATH") or ""):explode(";")
6968
elseif os.istarget("haiku") then
70-
path = os.getenv("LIBRARY_PATH") or ""
69+
return (os.getenv("LIBRARY_PATH") or ""):explode(":")
7170
else
71+
local paths
7272
if os.istarget("darwin") then
73-
path = os.getenv("DYLD_LIBRARY_PATH") or ""
73+
paths = (os.getenv("DYLD_LIBRARY_PATH") or ""):explode(":")
7474
else
75-
path = os.getenv("LD_LIBRARY_PATH") or ""
75+
paths = (os.getenv("LD_LIBRARY_PATH") or ""):explode(":")
7676

7777
for _, prefix in ipairs({"", "/opt"}) do
7878
local conf_file = prefix .. "/etc/ld.so.conf"
7979
if os.isfile(conf_file) then
80-
for _, v in ipairs(parse_ld_so_conf(conf_file)) do
81-
if (#path > 0) then
82-
path = path .. ":" .. v
83-
else
84-
path = v
85-
end
86-
end
80+
paths = table.join(paths, parse_ld_so_conf(conf_file))
8781
end
8882
end
8983
end
9084

91-
path = path or ""
92-
local archpath = "/lib:/usr/lib:/usr/local/lib"
85+
local archpaths = {"/lib", "/usr/lib", "/usr/local/lib"}
9386
if os.is64bit() and not (os.istarget("darwin")) then
94-
archpath = "/lib64:/usr/lib64/:usr/local/lib64" .. ":" .. archpath
95-
end
96-
if (#path > 0) then
97-
path = path .. ":" .. archpath
98-
else
99-
path = archpath
87+
archpaths = table.join({"/lib64", "/usr/lib64/", "usr/local/lib64"}, archpaths)
10088
end
89+
return table.join(paths, archpaths)
10190
end
102-
103-
return path
10491
end
10592

10693

@@ -123,7 +110,7 @@
123110
-- The full path to the library if found; `nil` otherwise.
124111
---
125112
function os.findlib(libname, libdirs)
126-
local path = get_library_search_path()
113+
local paths = get_library_search_path()
127114
local formats
128115

129116
-- assemble a search path, depending on the platform
@@ -141,25 +128,17 @@
141128
table.insert(formats, "%s")
142129
end
143130

144-
local userpath = ""
131+
local userpaths = {}
145132

146133
if type(libdirs) == "string" then
147-
userpath = libdirs
134+
userpaths = {libdirs}
148135
elseif type(libdirs) == "table" then
149-
userpath = table.implode(libdirs, "", "", ":")
136+
userpaths = libdirs
150137
end
151-
152-
if (#userpath > 0) then
153-
if (#path > 0) then
154-
path = userpath .. ":" .. path
155-
else
156-
path = userpath
157-
end
158-
end
159-
138+
paths = table.join(userpaths, paths)
160139
for _, fmt in ipairs(formats) do
161140
local name = string.format(fmt, libname)
162-
local result = os.pathsearch(name, path)
141+
local result = os.pathsearch(name, table.unpack(paths))
163142
if result then return result end
164143
end
165144
end
@@ -168,30 +147,21 @@
168147
-- headerpath: a partial header file path
169148
-- headerdirs: additional header search paths
170149

171-
local path = get_library_search_path()
150+
local paths = get_library_search_path()
172151

173-
-- replace all /lib by /include
174-
path = path .. ':'
175-
path = path:gsub ('/lib[0-9]*([:/])', '/include%1')
176-
path = path:sub (1, #path - 1)
152+
-- replace all /lib and /bin by /include
153+
paths = table.translate(paths, function (path) return path:gsub('[/\\]lib[0-9]*', '/include'):gsub('[/\\]bin', '/include') end)
177154

178-
local userpath = ""
155+
local userpaths = {}
179156

180157
if type(headerdirs) == "string" then
181-
userpath = headerdirs
158+
userpaths = { headerdirs }
182159
elseif type(headerdirs) == "table" then
183-
userpath = table.implode(headerdirs, "", "", ":")
184-
end
185-
186-
if (#userpath > 0) then
187-
if (#path > 0) then
188-
path = userpath .. ":" .. path
189-
else
190-
path = userpath
191-
end
160+
userpaths = headerdirs
192161
end
162+
paths = table.join(userpaths, paths)
193163

194-
local result = os.pathsearch (headerpath, path)
164+
local result = os.pathsearch (headerpath, table.unpack(paths))
195165
return result
196166
end
197167

0 commit comments

Comments
 (0)