forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenlib.jl
47 lines (39 loc) · 1.14 KB
/
openlib.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
load("trie.jl")
type DLCache
libnames::Trie{Bool}
prefix::String
suffix::String
function DLCache()
libnames = Trie{Bool}()
prefix = "lib"
suffix = ".so"
for line in readlines(stdout(`/sbin/ldconfig -p`))
m = match(r"^\s+lib(\S+)", line)
if m != nothing
name = m.captures[1]
libnames[name] = true
end
end
new(libnames, prefix, suffix)
end
end
function lookup(cache::DLCache, name::String)
re = Regex(I"^(?:$(cache.prefix))?(\S+)(?:$(cache.suffix))?\$")
name = match(re,name).captures[1]
name_with_suffix = strcat(name, cache.suffix)
candidates = keys_with_prefix(cache.libnames, name_with_suffix)
if length(candidates) > 0
name_with_suffix = candidates[1]
end
strcat(cache.prefix, name_with_suffix)
end
const UNAME = strip(readall(stdout(`uname`)))
if UNAME == "Linux"
global const _jl_dlcache = DLCache()
else
global const _jl_dlcache = nothing
end
function openlib(name::String)
libname = _jl_dlcache != nothing ? lookup(_jl_dlcache, name) : name
dlopen(libname)
end