1 ! Copyright (C) 2009 Jeremy Hughes.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays combinators fry generalizations
4 io.encodings.ascii io.files io.files.temp io.launcher kernel
5 locals make sequences system vocabs.parser words io.directories
7 IN: alien.inline.compiler
12 : inline-libs-directory ( -- path )
13 "alien-inline-libs" resource-path dup make-directories ;
15 : inline-library-file ( name -- path )
16 inline-libs-directory prepend-path ;
18 : library-suffix ( -- str )
20 { [ dup macosx? ] [ drop ".dylib" ] }
21 { [ dup unix? ] [ drop ".so" ] }
22 { [ dup windows? ] [ drop ".dll" ] }
25 : library-path ( str -- path )
26 '[ "lib" % _ % library-suffix % ] "" make inline-library-file ;
28 HOOK: compiler os ( lang -- str )
48 HOOK: compiler-descr os ( lang -- descr )
50 M: word compiler-descr compiler 1array ;
51 M: macosx compiler-descr
52 call-next-method cpu x86.64?
53 [ { "-arch" "x86_64" } append ] when ;
55 HOOK: link-descr os ( lang -- descr )
57 M: word link-descr drop { "-shared" "-o" } ;
59 drop { "-g" "-prebind" "-dynamiclib" "-o" }
60 cpu x86.64? [ { "-arch" "x86_64" } prepend ] when ;
63 { C [ { "-mno-cygwin" "-shared" "-o" } ] }
64 { C++ [ { "-lstdc++" "-mno-cygwin" "-shared" "-o" } ] }
68 : src-suffix ( lang -- str )
74 : link-command ( args in out lang -- descr )
75 [ 2array ] dip [ compiler 1array ] [ link-descr ] bi
76 append prepend prepend ;
78 :: compile-to-object ( lang contents name -- )
79 name ".o" append temp-file
80 contents name lang src-suffix append temp-file
81 [ ascii set-file-contents ] keep 2array
82 lang compiler-descr { "-fPIC" "-c" "-o" } append prepend
85 :: link-object ( lang args name -- )
86 args name [ library-path ]
87 [ ".o" append temp-file ] bi
88 lang link-command try-process ;
91 :: compile-to-library ( lang args contents name -- )
92 lang contents name compile-to-object
93 lang args name link-object ;