From 5c8d4bee525e3ee31285e199a3189885c33884c9 Mon Sep 17 00:00:00 2001 From: Jeremy Hughes Date: Thu, 2 Jul 2009 10:43:51 +1200 Subject: [PATCH] Inline C --- basis/alien/c-syntax/c-syntax.factor | 69 ++++++++++++++++++++++++++++ basis/alien/compile/compile.factor | 31 +++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 basis/alien/c-syntax/c-syntax.factor create mode 100644 basis/alien/compile/compile.factor diff --git a/basis/alien/c-syntax/c-syntax.factor b/basis/alien/c-syntax/c-syntax.factor new file mode 100644 index 0000000000..0ea61da301 --- /dev/null +++ b/basis/alien/c-syntax/c-syntax.factor @@ -0,0 +1,69 @@ +USING: accessors alien.compile alien.libraries alien.parser +arrays fry generalizations io.files.info io.files.temp kernel +lexer math.order multiline namespaces sequences system +vocabs.loader vocabs.parser words ; +IN: alien.c-syntax + +> ] bi@ <=> +lt+ = ; + +: compile-library ( -- ) + "library-is-c++" get [ "g++" ] [ "gcc" ] if + "c-compiler-args" get + "c-library-vector" get "\n" join + "c-library" get compile-to-library ; + +: (;C-LIBRARY) ( -- ) + compile-library? [ compile-library ] when + "c-library" get library-path "cdecl" add-library ; +PRIVATE> + +SYNTAX: C-LIBRARY: (C-LIBRARY:) ; + +SYNTAX: C++-LIBRARY: (C-LIBRARY:) t "library-is-c++" set ; + +SYNTAX: C-LINK: (C-LINK:) ; + +SYNTAX: C-FRAMEWORK: (C-FRAMEWORK:) ; + +SYNTAX: C-LINK/FRAMEWORK: + os macosx? [ (C-LINK:) ] [ (C-FRAMEWORK:) ] if ; + +SYNTAX: C-INCLUDE: + "#include " scan append "c-library-vector" get push ; + +SYNTAX: C-FUNCTION: + return-library-function-params + [ make-function define-declared ] + 4 nkeep (C-FUNCTION:) + " {\n" append parse-here append "\n}\n" append + "c-library-vector" get push ; + +SYNTAX: ;C-LIBRARY (;C-LIBRARY) ; + +SYNTAX: ;C++-LIBRARY (;C-LIBRARY) ; diff --git a/basis/alien/compile/compile.factor b/basis/alien/compile/compile.factor new file mode 100644 index 0000000000..6f1bc20545 --- /dev/null +++ b/basis/alien/compile/compile.factor @@ -0,0 +1,31 @@ +USING: accessors arrays combinators generalizations +io.encodings.ascii io.files io.files.temp io.launcher kernel +sequences system ; +IN: alien.compile + +: library-suffix ( -- str ) + os { + { [ dup macosx? ] [ drop ".dylib" ] } + { [ dup unix? ] [ drop ".so" ] } + { [ dup windows? ] [ drop ".dll" ] } + } cond ; + +: compile-to-object ( compiler contents name -- ) + [ ".src" append ] [ ".o" append ] bi [ temp-file ] bi@ + [ tuck ascii set-file-contents ] dip + swap 2array { "-fPIC" "-c" "-o" } prepend + swap prefix try-process ; + +: link-object ( compiler args name -- ) + [ "lib" prepend library-suffix append ] [ ".o" append ] bi + [ temp-file ] bi@ 2array + os { + { [ dup linux? ] + [ drop { "-shared" "-o" } ] } + { [ dup macosx? ] + [ drop { "-g" "-prebind" "-dynamiclib" "-o" } ] } + [ name>> "unimplemented for: " prepend throw ] + } cond prepend prepend swap prefix try-process ; + +: compile-to-library ( compiler args contents name -- ) + [ [ nip ] dip compile-to-object ] 4 nkeep nip link-object ;