annotate lua/luadyncall/src/dynload.lua @ 3:f5d4d5c2f750

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