]> gitweb.factorcode.org Git - factor.git/blob - unmaintained/alien/inline/compiler/compiler.factor
tools.test: Make the flag public. Finish porting tester changes to fuzzer.
[factor.git] / unmaintained / alien / inline / compiler / compiler.factor
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
6 io.pathnames ;
7 IN: alien.inline.compiler
8
9 SYMBOL: C
10 SYMBOL: C++
11
12 : inline-libs-directory ( -- path )
13     "alien-inline-libs" resource-path dup make-directories ;
14
15 : inline-library-file ( name -- path )
16     inline-libs-directory prepend-path ;
17
18 : library-suffix ( -- str )
19     os {
20         { [ dup macosx? ]  [ drop ".dylib" ] }
21         { [ dup unix? ]    [ drop ".so" ] }
22         { [ dup windows? ] [ drop ".dll" ] }
23     } cond ;
24
25 : library-path ( str -- path )
26     '[ "lib" % _ % library-suffix % ] "" make inline-library-file ;
27
28 HOOK: compiler os ( lang -- str )
29
30 M: word compiler
31     {
32         { C [ "gcc" ] }
33         { C++ [ "g++" ] }
34     } case ;
35
36 M: openbsd compiler
37     {
38         { C [ "gcc" ] }
39         { C++ [ "eg++" ] }
40     } case ;
41
42 M: windows compiler
43     {
44         { C [ "gcc" ] }
45         { C++ [ "g++" ] }
46     } case ;
47
48 HOOK: compiler-descr os ( lang -- descr )
49
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 ;
54
55 HOOK: link-descr os ( lang -- descr )
56
57 M: word link-descr drop { "-shared" "-o" } ;
58 M: macosx link-descr
59     drop { "-g" "-prebind" "-dynamiclib" "-o" }
60     cpu x86.64? [ { "-arch" "x86_64" } prepend ] when ;
61 M: windows link-descr
62     {
63         { C [ { "-mno-cygwin" "-shared" "-o" } ] }
64         { C++ [ { "-lstdc++" "-mno-cygwin" "-shared" "-o" } ] }
65     } case ;
66
67 <PRIVATE
68 : src-suffix ( lang -- str )
69     {
70         { C [ ".c" ] }
71         { C++ [ ".cpp" ] }
72     } case ;
73
74 : link-command ( args in out lang -- descr )
75     [ 2array ] dip [ compiler 1array ] [ link-descr ] bi
76     append prepend prepend ;
77
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
83     try-process ;
84
85 :: link-object ( lang args name -- )
86     args name [ library-path ]
87     [ ".o" append temp-file ] bi
88     lang link-command try-process ;
89 PRIVATE>
90
91 :: compile-to-library ( lang args contents name -- )
92     lang contents name compile-to-object
93     lang args name link-object ;