PageRenderTime 28ms CodeModel.GetById 11ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/unmaintained/alien/inline/compiler/compiler.factor

http://github.com/abeaumont/factor
Unknown | 93 lines | 75 code | 18 blank | 0 comment | 0 complexity | ce2d3a989f82d1118e3d478b2d999fbc MD5 | raw file
 1! Copyright (C) 2009 Jeremy Hughes.
 2! See http://factorcode.org/license.txt for BSD license.
 3USING: accessors arrays combinators fry generalizations
 4io.encodings.ascii io.files io.files.temp io.launcher kernel
 5locals make sequences system vocabs.parser words io.directories
 6io.pathnames ;
 7IN: alien.inline.compiler
 8
 9SYMBOL: C
10SYMBOL: 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
28HOOK: compiler os ( lang -- str )
29
30M: word compiler
31    {
32        { C [ "gcc" ] }
33        { C++ [ "g++" ] }
34    } case ;
35
36M: openbsd compiler
37    {
38        { C [ "gcc" ] }
39        { C++ [ "eg++" ] }
40    } case ;
41
42M: windows compiler
43    {
44        { C [ "gcc" ] }
45        { C++ [ "g++" ] }
46    } case ;
47
48HOOK: compiler-descr os ( lang -- descr )
49
50M: word compiler-descr compiler 1array ;
51M: macosx compiler-descr
52    call-next-method cpu x86.64?
53    [ { "-arch" "x86_64" } append ] when ;
54
55HOOK: link-descr os ( lang -- descr )
56
57M: word link-descr drop { "-shared" "-o" } ;
58M: macosx link-descr
59    drop { "-g" "-prebind" "-dynamiclib" "-o" }
60    cpu x86.64? [ { "-arch" "x86_64" } prepend ] when ;
61M: 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 ;
89PRIVATE>
90
91:: compile-to-library ( lang args contents name -- )
92    lang contents name compile-to-object
93    lang args name link-object ;