]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/libraries/finder/linux/linux.factor
hex-strings: fix using
[factor.git] / basis / alien / libraries / finder / linux / linux.factor
1 ! Copyright (C) 2013 Björn Lindqvist, Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license
3 USING: accessors alien.libraries.finder arrays assocs
4 combinators.short-circuit environment io io.encodings.utf8
5 io.launcher kernel make sequences sets splitting system
6 unicode ;
7 IN: alien.libraries.finder.linux
8
9 <PRIVATE
10
11 CONSTANT: mach-map {
12     { ppc.64 { "libc6" "64bit" } }
13     { x86.32 { "libc6" "x32" } }
14     { x86.64 { "libc6" "x86-64" } }
15 }
16
17 : parse-ldconfig-lines ( string -- triple )
18     [
19         "=>" split1 [ [ unicode:blank? ] trim ] bi@
20         [
21             " " split1 [ "()" in? ] trim "," split
22             [ [ unicode:blank? ] trim ] map
23             [ ": Linux" subseq-of? ] reject
24         ] dip 3array
25     ] map ;
26
27 : load-ldconfig-cache ( -- seq )
28     "/sbin/ldconfig -p" utf8 [ read-lines ] with-process-reader*
29     2drop [ f ] [ rest parse-ldconfig-lines ] if-empty ;
30
31 : ldconfig-arch ( -- str )
32     mach-map cpu of { "libc6" } or ;
33
34 : name-matches? ( lib triple -- ? )
35     first swap ?head [ ?first CHAR: . = ] [ drop f ] if ;
36
37 : arch-matches? ( lib triple -- ? )
38     [ drop ldconfig-arch ] [ second swap subset? ] bi* ;
39
40 : ldconfig-matches? ( lib triple -- ? )
41     { [ name-matches? ] [ arch-matches? ] } 2&& ;
42
43 : find-ldconfig ( name -- path/f )
44     "lib" prepend load-ldconfig-cache
45     [ ldconfig-matches? ] with find nip ?last ;
46
47 :: find-ld ( name -- path/f )
48     "lib" name append <process>
49         [
50             "ld" , "-t" ,
51             "LD_LIBRARY_PATH" os-env ":" split [ "-L" , , ] each
52             cpu x86.64? "-melf_x86_64" "-melf_i386" ? ,
53             "-o" , "/dev/null" , "-l" name append ,
54         ] { } make >>command
55         +closed+ >>stderr
56     utf8 [ read-lines ] with-process-reader* 2drop
57     [ subseq? ] with find nip ;
58
59 PRIVATE>
60
61 M: linux find-library*
62     { [ find-ldconfig ] [ find-ld ] } 1|| ;