0
|
1 require "smartptr"
|
|
2
|
|
3 local dl = require "ldynload"
|
|
4
|
|
5 --- load library given by name using operating-system level service.
|
|
6 -- @return smartptr that will free library if the object gets garbage collected.
|
|
7
|
|
8 function loadlib(name)
|
|
9 local handle = dl.dlLoadLibrary(name)
|
|
10 if handle then return smartptr.new( handle, dl.dlFreeLibrary ) end
|
|
11 end
|
|
12
|
|
13 --- resolve symbols
|
|
14 -- @param lib smartptr
|
|
15 -- @param name symbol to resolve to address
|
|
16 -- @return address light userpointer
|
|
17 function dynsym(lib, name)
|
|
18 local handle = lib()
|
|
19 return dl.dlFindSymbol( handle, name )
|
|
20 end
|
|
21
|
|
22 --- load shared library code
|
|
23 -- This mechanism uses a platform-independent interface to search for
|
|
24 -- the libraries.
|
|
25 -- On Linux, BSD, UNIX and Mac OS X the standard places such as "/", "/lib" are searched.
|
|
26 -- @param libnames a string separated by '|' (pipe) for the pure library name without prefix/suffixes.
|
|
27
|
|
28
|
|
29 function dynload(libnames)
|
|
30
|
|
31 local lib
|
|
32
|
|
33 if libnames == "" then
|
|
34 return loadlib(nil)
|
|
35 end
|
|
36
|
|
37 -- Unix search paths
|
|
38
|
|
39 local paths = { "", "./", "/lib/", "/usr/lib/", "/usr/local/lib/", "/opt/lib/", "/opt/local/lib/" }
|
|
40 local prefixes = { "", "lib" }
|
|
41 local suffixes = { "", ".dylib", ".so", ".so.0", ".dll" }
|
|
42
|
|
43 -- Mac OS X Framework search paths
|
|
44 -- local fwpaths = { "", "/System" }
|
|
45
|
|
46 for libname in libnames:gmatch("[^|]+") do
|
|
47
|
|
48 for k,path in pairs(paths) do
|
|
49 for k,prefix in pairs(prefixes) do
|
|
50 for k,suffix in pairs(suffixes) do
|
|
51 local libpath = path .. prefix .. libname .. suffix
|
|
52 lib = loadlib(libpath)
|
|
53 if lib then return lib end
|
|
54 end
|
|
55 end
|
|
56 end
|
|
57
|
|
58 -- search Mac OS X frameworks:
|
|
59
|
|
60 lib = loadlib( libname .. ".framework/" .. libname )
|
|
61
|
|
62 --[[
|
|
63 for k,fwpath in pairs(fwpaths) do
|
|
64 local libpath = fwpath .. "/Library/Frameworks/" .. libname .. ".framework/" .. libname
|
|
65 lib = ldynload.dlLoadLibrary(libpath)
|
|
66 if libhandle then break end
|
|
67 end
|
|
68 ]]
|
|
69
|
|
70 if lib then return lib end
|
|
71
|
|
72 end
|
|
73
|
|
74 if not lib then error("unable to locate library "..libnames) end
|
|
75
|
|
76 end
|
|
77
|