NB. libpath
NB.
NB. shared library path (for UNIX)

NB. nouns:
NB. UNAME        unix platform id
NB. DLL_PATH     standard library load path
NB.
NB. verbs:
NB. find_dll     locate a shared library in (standard) directories.

cocurrent 'z'

NB. =========================================================
NB. *DLL_PATH      n standard library load path.
NB. initialized from a standard UNIX environment variable
NB. or, otherwise, from a sensible default.

3 : 0 ''
if. -. IFUNIX do.
  DLL_PATH=: '' return.
end.

NB. On many systems, LD_LIBRARY_PATH works as an add-on to
NB. to a default system library path.  If we don't find '/lib'
NB. in it, we regard it as an extension and add some default
NB. library path.  Any replicated dirs will be expunged by
NB. the final Nub.
def_path=. '/usr/local/lib:/usr/lib:/usr/lib/ccs/lib:/etc/lib:/lib'
DLL_PATH=: 2!:5 'LD_LIBRARY_PATH'
if. 0-:DLL_PATH do.
  DLL_PATH=: def_path
elseif. -. +./ '/usr/lib' E. DLL_PATH do.
  DLL_PATH=: DLL_PATH,':',def_path
end.
DLL_PATH=: ~. (<;._1~ =&':') ':',DLL_PATH

)

NB. =========================================================
NB. *find_dll   v locate a shared library in (standard) directories.
NB. (monadic and dyadic)
NB. e.g.   find_dll 'X11'
NB.        (find_dll 'c') make_api 'strcmp i *c *c;'
find_dll=: 3 : 0
DLL_PATH find_dll y.
:
if. -. IFUNIX do. '' return. end.
NB. turn scalar lib names (as 'c') into singleton strings to be safe:
y=. ,y.
if. (UNAME -: 'Linux') *. (y -: ,'c') do.
  if. IF64 do. 'libc.so.6' return. end. NB. Suse 9.1 64bit kludge - path doesn't work!
NB. The Linux /lib/libc.so happens to be a special
NB. linker directive file, not a lib suitable for
NB. dlopen.  For now, provide a crude hack to
NB. patch this oh so common problem case.
  '/lib/libc.so.6' return.
end.
if. (UNAME-:'Darwin') do. ext=. '.dylib*' else. ext=. '.so*' end.
for_dir. x. do.
  l=. (>dir), '/lib', y, ext
NB. the sort will prefer the highest version number:
  if. # fns=. \:~ 1!: 0 l do.
    (>dir), '/', > (<0 0) { fns
    return.
  end.
end.
('could not locate dll ',y) 13!:8 ] 24
)
