From 0236212987548a71dfd16c6cd048360ac726840f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Lindqvist?= Date: Fri, 8 Nov 2013 00:30:23 +0100 Subject: [PATCH] alien.libraries.finder: linux code path for find-library /sbin/ldconfig -p is used to figure out what the correct lib name is. It is the same method as used in python ctypes. --- basis/alien/libraries/finder/finder.factor | 40 ++++++++++++++++++---- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/basis/alien/libraries/finder/finder.factor b/basis/alien/libraries/finder/finder.factor index 1c5939959c..906865b8a2 100644 --- a/basis/alien/libraries/finder/finder.factor +++ b/basis/alien/libraries/finder/finder.factor @@ -2,16 +2,23 @@ USING: alien.libraries arrays assocs + combinators formatting - io.pathnames + io io.encodings.utf8 io.launcher io.pathnames kernel sequences + splitting system ; IN: alien.libraries.finder +! Util +: vsprintf1 ( obj fmt -- str ) + [ 1array ] dip vsprintf ; + CONSTANT: name-formats { { windows { "lib%s.dll" "%s.dll" } } - { unix { "lib%s.so.0" } } + { linux { "lib%s.so" } } + { unix { "lib%s.so" } } { macosx { "lib%s.0.dylib" } } } @@ -24,15 +31,36 @@ CONSTANT: search-paths { { macosx { "" } } } -: vsprintf1 ( obj fmt -- str ) - [ 1array ] dip vsprintf ; - : path-formats ( -- path-formats ) search-paths name-formats [ os of ] bi@ [ append-path ] cartesian-map concat ; +! Find lib using ldconfig +CONSTANT: mach-map { + { ppc.64 "libc6,64bit" } + { x86.32 "libc6,x86-32" } + { x86.64 "libc6,x86-64" } +} + +: ldconfig-cache ( -- seq ) + "/sbin/ldconfig -p" utf8 [ lines ] with-process-reader rest + [ "=>" "" replace "\t " split harvest ] map ; + +: ldconfig-filter ( -- str ) + mach-map cpu of dup "libc6" ? "(" ")" surround ; + +: ldconfig-matches? ( lib this-lib this-arch -- ? ) + [ start 0 = ] [ ldconfig-filter = ] bi* and ; + +: ldconfig-find-soname ( lib -- seq ) + name-formats os of first vsprintf1 + ldconfig-cache [ first2 ldconfig-matches? ] with filter [ first ] map ; + : candidate-paths ( name -- paths ) - path-formats [ vsprintf1 ] with map ; + { + { [ os windows? ] [ path-formats [ vsprintf1 ] with map ] } + { [ os linux? ] [ ldconfig-find-soname ] } + } cond ; : find-library ( name -- path/f ) candidate-paths [ dlopen dll-valid? ] map-find nip ;