diff --git a/basis/alien/libraries/finder/finder-docs.factor b/basis/alien/libraries/finder/finder-docs.factor new file mode 100644 index 0000000000..babbbabf4c --- /dev/null +++ b/basis/alien/libraries/finder/finder-docs.factor @@ -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: << } "." +} ; diff --git a/basis/alien/libraries/finder/finder-tests.factor b/basis/alien/libraries/finder/finder-tests.factor new file mode 100644 index 0000000000..51857036fb --- /dev/null +++ b/basis/alien/libraries/finder/finder-tests.factor @@ -0,0 +1,4 @@ +USING: alien alien.libraries.finder tools.test ; +IN: alien.libraries.finder.tests + +[ f ] [ "dont-exist" find-library ] unit-test diff --git a/basis/alien/libraries/finder/finder.factor b/basis/alien/libraries/finder/finder.factor new file mode 100644 index 0000000000..1c5939959c --- /dev/null +++ b/basis/alien/libraries/finder/finder.factor @@ -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 ;