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

http://github.com/abeaumont/factor · Factor · 93 lines · 73 code · 18 blank · 2 comment · 8 complexity · ce2d3a989f82d1118e3d478b2d999fbc MD5 · raw file

  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. SYMBOL: C
  9. SYMBOL: C++
  10. : inline-libs-directory ( -- path )
  11. "alien-inline-libs" resource-path dup make-directories ;
  12. : inline-library-file ( name -- path )
  13. inline-libs-directory prepend-path ;
  14. : library-suffix ( -- str )
  15. os {
  16. { [ dup macosx? ] [ drop ".dylib" ] }
  17. { [ dup unix? ] [ drop ".so" ] }
  18. { [ dup windows? ] [ drop ".dll" ] }
  19. } cond ;
  20. : library-path ( str -- path )
  21. '[ "lib" % _ % library-suffix % ] "" make inline-library-file ;
  22. HOOK: compiler os ( lang -- str )
  23. M: word compiler
  24. {
  25. { C [ "gcc" ] }
  26. { C++ [ "g++" ] }
  27. } case ;
  28. M: openbsd compiler
  29. {
  30. { C [ "gcc" ] }
  31. { C++ [ "eg++" ] }
  32. } case ;
  33. M: windows compiler
  34. {
  35. { C [ "gcc" ] }
  36. { C++ [ "g++" ] }
  37. } case ;
  38. HOOK: compiler-descr os ( lang -- descr )
  39. M: word compiler-descr compiler 1array ;
  40. M: macosx compiler-descr
  41. call-next-method cpu x86.64?
  42. [ { "-arch" "x86_64" } append ] when ;
  43. HOOK: link-descr os ( lang -- descr )
  44. M: word link-descr drop { "-shared" "-o" } ;
  45. M: macosx link-descr
  46. drop { "-g" "-prebind" "-dynamiclib" "-o" }
  47. cpu x86.64? [ { "-arch" "x86_64" } prepend ] when ;
  48. M: windows link-descr
  49. {
  50. { C [ { "-mno-cygwin" "-shared" "-o" } ] }
  51. { C++ [ { "-lstdc++" "-mno-cygwin" "-shared" "-o" } ] }
  52. } case ;
  53. <PRIVATE
  54. : src-suffix ( lang -- str )
  55. {
  56. { C [ ".c" ] }
  57. { C++ [ ".cpp" ] }
  58. } case ;
  59. : link-command ( args in out lang -- descr )
  60. [ 2array ] dip [ compiler 1array ] [ link-descr ] bi
  61. append prepend prepend ;
  62. :: compile-to-object ( lang contents name -- )
  63. name ".o" append temp-file
  64. contents name lang src-suffix append temp-file
  65. [ ascii set-file-contents ] keep 2array
  66. lang compiler-descr { "-fPIC" "-c" "-o" } append prepend
  67. try-process ;
  68. :: link-object ( lang args name -- )
  69. args name [ library-path ]
  70. [ ".o" append temp-file ] bi
  71. lang link-command try-process ;
  72. PRIVATE>
  73. :: compile-to-library ( lang args contents name -- )
  74. lang contents name compile-to-object
  75. lang args name link-object ;