]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/libraries/libraries.factor
alien.libraries: add a "deploy-library" word that marks a library to have its dll...
[factor.git] / basis / alien / libraries / libraries.factor
1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.strings assocs io.backend
4 kernel namespaces destructors sequences system io.pathnames ;
5 IN: alien.libraries
6
7 : dlopen ( path -- dll ) native-string>alien (dlopen) ;
8
9 : dlsym ( name dll -- alien ) [ string>symbol ] dip (dlsym) ;
10
11 SYMBOL: libraries
12 SYMBOL: deploy-libraries
13
14 libraries [ H{ } clone ] initialize
15 deploy-libraries [ V{ } clone ] initialize
16
17 TUPLE: library path abi dll ;
18
19 ERROR: no-library name ;
20
21 : library ( name -- library ) libraries get at ;
22
23 : <library> ( path abi -- library )
24     over dup [ dlopen ] when \ library boa ;
25
26 : load-library ( name -- dll )
27     library dup [ dll>> ] when ;
28
29 M: dll dispose dlclose ;
30
31 M: library dispose dll>> [ dispose ] when* ;
32
33 : remove-library ( name -- )
34     libraries get delete-at* [ dispose ] [ drop ] if ;
35
36 : add-library ( name path abi -- )
37     [ 2drop remove-library ]
38     [ <library> swap libraries get set-at ] 3bi ;
39
40 : deploy-library ( name -- )
41     dup libraries get key?
42     [ deploy-libraries get 2dup member? [ 2drop ] [ push ] if ]
43     [ no-library ] if ;
44
45 <PRIVATE
46 HOOK: >deployed-library-path os ( path -- path' )
47
48 M: windows >deployed-library-path
49     file-name ;
50 M: unix >deployed-library-path
51     file-name "$ORIGIN" prepend-path ;
52 M: macosx >deployed-library-path
53     file-name "@executable_path/../Frameworks" prepend-path ;
54 PRIVATE>