alien.libraries.finder: automatically find library files based on library names

To make it easier to support cross-platform library bindings use the
find-library word. Factor is then supposed to automatically locate the
correct library file depending on the users' platform. Then you dont
have to specify the full so-name on unixes which breaks if the user
has a newer library version. The same library lookups that
ctypes.find_library uses will be implemented in future commits.
db4
Björn Lindqvist 2013-11-04 17:28:45 +01:00
parent a3fcaf4a11
commit 21bd4cc6ea
3 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,18 @@
USING: help.markup help.syntax ;
IN: alien.libraries.finder
HELP: find-library
{ $values
{ "name" "a shared library name" }
{ "path/f" "a filesystem path or f" }
}
{ $description
"Returns a filesystem path for a plain shared library name, or f if no library can be found."
}
{ $examples
"Use " { $link find-library } " to load libraries whose exact filenames is not known in advance:"
{ $code
"<< \"sqlite\" \"sqlite3\" find-library cdecl add-library >>"
}
"Note the parse time evaluation with " { $link POSTPONE: << } "."
} ;

View File

@ -0,0 +1,4 @@
USING: alien alien.libraries.finder tools.test ;
IN: alien.libraries.finder.tests
[ f ] [ "dont-exist" find-library ] unit-test

View File

@ -0,0 +1,38 @@
USING:
alien.libraries
arrays
assocs
formatting
io.pathnames
kernel
sequences
system ;
IN: alien.libraries.finder
CONSTANT: name-formats {
{ windows { "lib%s.dll" "%s.dll" } }
{ unix { "lib%s.so.0" } }
{ macosx { "lib%s.0.dylib" } }
}
! On Windows, bundled dlls are shipped in a directory named "dlls" in
! the Factor distribution. On other operating systems, the dynamic
! linker can itself figure out where libraries are located.
CONSTANT: search-paths {
{ windows { "" "dlls" } }
{ unix { "" } }
{ macosx { "" } }
}
: vsprintf1 ( obj fmt -- str )
[ 1array ] dip vsprintf ;
: path-formats ( -- path-formats )
search-paths name-formats [ os of ] bi@
[ append-path ] cartesian-map concat ;
: candidate-paths ( name -- paths )
path-formats [ vsprintf1 ] with map ;
: find-library ( name -- path/f )
candidate-paths [ dlopen dll-valid? ] map-find nip ;