|
63 | 63 | end
|
64 | 64 |
|
65 | 65 | local function get_library_search_path()
|
66 |
| - local path |
67 | 66 | if os.istarget("windows") then
|
68 |
| - path = os.getenv("PATH") or "" |
| 67 | + return (os.getenv("PATH") or ""):explode(";") |
69 | 68 | elseif os.istarget("haiku") then
|
70 |
| - path = os.getenv("LIBRARY_PATH") or "" |
| 69 | + return (os.getenv("LIBRARY_PATH") or ""):explode(":") |
71 | 70 | else
|
| 71 | + local paths |
72 | 72 | if os.istarget("darwin") then
|
73 |
| - path = os.getenv("DYLD_LIBRARY_PATH") or "" |
| 73 | + paths = (os.getenv("DYLD_LIBRARY_PATH") or ""):explode(":") |
74 | 74 | else
|
75 |
| - path = os.getenv("LD_LIBRARY_PATH") or "" |
| 75 | + paths = (os.getenv("LD_LIBRARY_PATH") or ""):explode(":") |
76 | 76 |
|
77 | 77 | for _, prefix in ipairs({"", "/opt"}) do
|
78 | 78 | local conf_file = prefix .. "/etc/ld.so.conf"
|
79 | 79 | 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)) |
87 | 81 | end
|
88 | 82 | end
|
89 | 83 | end
|
90 | 84 |
|
91 |
| - path = path or "" |
92 |
| - local archpath = "/lib:/usr/lib:/usr/local/lib" |
| 85 | + local archpaths = {"/lib", "/usr/lib", "/usr/local/lib"} |
93 | 86 | 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) |
100 | 88 | end
|
| 89 | + return table.join(paths, archpaths) |
101 | 90 | end
|
102 |
| - |
103 |
| - return path |
104 | 91 | end
|
105 | 92 |
|
106 | 93 |
|
|
123 | 110 | -- The full path to the library if found; `nil` otherwise.
|
124 | 111 | ---
|
125 | 112 | function os.findlib(libname, libdirs)
|
126 |
| - local path = get_library_search_path() |
| 113 | + local paths = get_library_search_path() |
127 | 114 | local formats
|
128 | 115 |
|
129 | 116 | -- assemble a search path, depending on the platform
|
|
141 | 128 | table.insert(formats, "%s")
|
142 | 129 | end
|
143 | 130 |
|
144 |
| - local userpath = "" |
| 131 | + local userpaths = {} |
145 | 132 |
|
146 | 133 | if type(libdirs) == "string" then
|
147 |
| - userpath = libdirs |
| 134 | + userpaths = {libdirs} |
148 | 135 | elseif type(libdirs) == "table" then
|
149 |
| - userpath = table.implode(libdirs, "", "", ":") |
| 136 | + userpaths = libdirs |
150 | 137 | 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) |
160 | 139 | for _, fmt in ipairs(formats) do
|
161 | 140 | local name = string.format(fmt, libname)
|
162 |
| - local result = os.pathsearch(name, path) |
| 141 | + local result = os.pathsearch(name, table.unpack(paths)) |
163 | 142 | if result then return result end
|
164 | 143 | end
|
165 | 144 | end
|
|
168 | 147 | -- headerpath: a partial header file path
|
169 | 148 | -- headerdirs: additional header search paths
|
170 | 149 |
|
171 |
| - local path = get_library_search_path() |
| 150 | + local paths = get_library_search_path() |
172 | 151 |
|
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) |
177 | 154 |
|
178 |
| - local userpath = "" |
| 155 | + local userpaths = {} |
179 | 156 |
|
180 | 157 | if type(headerdirs) == "string" then
|
181 |
| - userpath = headerdirs |
| 158 | + userpaths = { headerdirs } |
182 | 159 | 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 |
192 | 161 | end
|
| 162 | + paths = table.join(userpaths, paths) |
193 | 163 |
|
194 |
| - local result = os.pathsearch (headerpath, path) |
| 164 | + local result = os.pathsearch (headerpath, table.unpack(paths)) |
195 | 165 | return result
|
196 | 166 | end
|
197 | 167 |
|
|
0 commit comments