1 ! Copyright (C) 2009 Jeremy Hughes.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.inline.compiler alien.inline.types
4 alien.libraries alien.parser arrays assocs effects fry
5 generalizations grouping io.directories io.files
6 io.files.info io.files.temp kernel lexer math math.order
7 math.ranges multiline namespaces sequences source-files
8 splitting strings system vocabs.loader vocabs.parser words
9 alien.c-types alien.structs make parser continuations ;
13 SYMBOL: library-is-c++
18 : cleanup-variables ( -- )
19 { c-library library-is-c++ linker-args c-strings }
22 : arg-list ( types -- params )
23 CHAR: a swap length CHAR: a + [a,b]
26 : compile-library? ( -- ? )
27 c-library get library-path dup exists? [
30 [ file-info modified>> ] bi@ <=> +lt+ =
34 : compile-library ( -- )
35 library-is-c++ get [ C++ ] [ C ] if
37 c-strings get "\n" join
38 c-library get compile-to-library ;
40 : c-library-name ( name -- name' )
41 [ current-vocab name>> % "_" % % ] "" make ;
44 : append-function-body ( prototype-str body -- str )
45 [ swap % " {\n" % % "\n}\n" % ] "" make ;
47 : function-types-effect ( -- function types effect )
48 scan scan swap ")" parse-tokens
49 [ "(" subseq? not ] filter swap parse-arglist ;
51 : prototype-string ( function types effect -- str )
52 [ [ cify-type ] map ] dip
53 types-effect>params-return cify-type -rot
54 [ " " join ] map ", " join
55 "(" prepend ")" append 3array " " join
56 library-is-c++ get [ "extern \"C\" " prepend ] when ;
58 : prototype-string' ( function types return -- str )
59 [ dup arg-list ] <effect> prototype-string ;
61 : factor-function ( function types effect -- word quot effect )
62 annotate-effect [ c-library get ] 3dip
63 [ [ factorize-type ] map ] dip
64 types-effect>params-return factorize-type -roll
65 concat make-function ;
67 : define-c-library ( name -- )
68 c-library-name [ c-library set ] [ "c-library" set ] bi
69 V{ } clone c-strings set
70 V{ } clone linker-args set ;
72 : compile-c-library ( -- )
73 compile-library? [ compile-library ] when
74 c-library get dup library-path "cdecl" add-library ;
76 : define-c-function ( function types effect body -- )
78 [ factor-function define-declared ]
79 [ prototype-string ] 3bi
80 ] dip append-function-body c-strings get push ;
82 : define-c-function' ( function effect body -- )
85 [ factor-function define-declared ]
86 [ out>> prototype-string' ] 3bi
87 ] dip append-function-body c-strings get push ;
89 : c-link-to ( str -- )
90 "-l" prepend linker-args get push ;
92 : c-use-framework ( str -- )
93 "-framework" swap linker-args get '[ _ push ] bi@ ;
95 : c-link-to/use-framework ( str -- )
96 os macosx? [ c-use-framework ] [ c-link-to ] if ;
98 : c-include ( str -- )
99 "#include " prepend c-strings get push ;
101 : define-c-typedef ( old new -- )
103 [ swap "typedef " % % " " % % ";" % ]
104 "" make c-strings get push
107 : define-c-struct ( name fields -- )
108 [ current-vocab swap define-struct ] [
111 "typedef struct " % "_" % % " {\n" %
112 [ first2 swap % " " % % ";\n" % ] each
114 ] "" make c-strings get push
117 : delete-inline-library ( name -- )
118 c-library-name [ remove-library ]
119 [ library-path dup exists? [ delete-file ] [ drop ] if ] bi ;
121 : with-c-library ( name quot -- )
122 [ [ define-c-library ] dip call compile-c-library ]
123 [ cleanup-variables ] [ ] cleanup ; inline
126 [ "\n" % % "\n" % ] "" make c-strings get push ;