compile.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. ;;; Compile --- Command-line Guile Scheme compiler -*- coding: iso-8859-1 -*-
  2. ;; Copyright 2005, 2008-2011, 2013, 2014, 2015 Free Software Foundation, Inc.
  3. ;;
  4. ;; This program is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public License
  6. ;; as published by the Free Software Foundation; either version 3, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this software; see the file COPYING.LESSER. If
  16. ;; not, write to the Free Software Foundation, Inc., 51 Franklin
  17. ;; Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;; Author: Ludovic Courtès <ludo@gnu.org>
  19. ;;; Author: Andy Wingo <wingo@pobox.com>
  20. ;;; Commentary:
  21. ;; Usage: compile [ARGS]
  22. ;;
  23. ;; A command-line interface to the Guile compiler.
  24. ;;; Code:
  25. (define-module (scripts compile)
  26. #:use-module ((system base compile) #:select (compile-file))
  27. #:use-module (system base target)
  28. #:use-module (system base message)
  29. #:use-module (language tree-il optimize)
  30. #:use-module (language cps optimize)
  31. #:use-module (srfi srfi-1)
  32. #:use-module (srfi srfi-13)
  33. #:use-module (srfi srfi-37)
  34. #:use-module (ice-9 format)
  35. #:use-module (ice-9 match)
  36. #:export (compile))
  37. (define %summary "Compile a file.")
  38. (define (fail . messages)
  39. (format (current-error-port) "error: ~{~a~}~%" messages)
  40. (exit 1))
  41. (define (available-optimizations)
  42. (append (tree-il-default-optimization-options)
  43. (cps-default-optimization-options)))
  44. ;; Turn on all optimizations unless -O0.
  45. (define (optimizations-for-level level)
  46. (let lp ((options (available-optimizations)))
  47. (match options
  48. (() '())
  49. ((#:partial-eval? val . options)
  50. (cons* #:partial-eval? (> level 0) (lp options)))
  51. ((kw val . options)
  52. (cons* kw (> level 1) (lp options))))))
  53. (define %options
  54. ;; Specifications of the command-line options.
  55. (list (option '(#\h "help") #f #f
  56. (lambda (opt name arg result)
  57. (alist-cons 'help? #t result)))
  58. (option '("version") #f #f
  59. (lambda (opt name arg result)
  60. (show-version)
  61. (exit 0)))
  62. (option '(#\L "load-path") #t #f
  63. (lambda (opt name arg result)
  64. (let ((load-path (assoc-ref result 'load-path)))
  65. (alist-cons 'load-path (cons arg load-path)
  66. result))))
  67. (option '(#\o "output") #t #f
  68. (lambda (opt name arg result)
  69. (if (assoc-ref result 'output-file)
  70. (fail "`-o' option cannot be specified more than once")
  71. (alist-cons 'output-file arg result))))
  72. (option '(#\W "warn") #t #f
  73. (lambda (opt name arg result)
  74. (if (string=? arg "help")
  75. (begin
  76. (show-warning-help)
  77. (exit 0))
  78. (let ((warnings (assoc-ref result 'warnings)))
  79. (alist-cons 'warnings
  80. (cons (string->symbol arg) warnings)
  81. (alist-delete 'warnings result))))))
  82. (option '(#\O "optimize") #t #f
  83. (lambda (opt name arg result)
  84. (define (return val)
  85. (alist-cons 'optimizations val result))
  86. (define (return-option name val)
  87. (let ((kw (symbol->keyword
  88. (string->symbol (string-append name "?")))))
  89. (unless (memq kw (available-optimizations))
  90. (fail "Unknown optimization pass `~a'" name))
  91. (return (list kw val))))
  92. (cond
  93. ((string=? arg "help")
  94. (show-optimization-help)
  95. (exit 0))
  96. ((equal? arg "0") (return (optimizations-for-level 0)))
  97. ((equal? arg "1") (return (optimizations-for-level 1)))
  98. ((equal? arg "2") (return (optimizations-for-level 2)))
  99. ((equal? arg "3") (return (optimizations-for-level 3)))
  100. ((string-prefix? "no-" arg)
  101. (return-option (substring arg 3) #f))
  102. (else
  103. (return-option arg #t)))))
  104. (option '(#\f "from") #t #f
  105. (lambda (opt name arg result)
  106. (if (assoc-ref result 'from)
  107. (fail "`--from' option cannot be specified more than once")
  108. (alist-cons 'from (string->symbol arg) result))))
  109. (option '(#\t "to") #t #f
  110. (lambda (opt name arg result)
  111. (if (assoc-ref result 'to)
  112. (fail "`--to' option cannot be specified more than once")
  113. (alist-cons 'to (string->symbol arg) result))))
  114. (option '(#\T "target") #t #f
  115. (lambda (opt name arg result)
  116. (if (assoc-ref result 'target)
  117. (fail "`--target' option cannot be specified more than once")
  118. (alist-cons 'target arg result))))))
  119. (define (parse-args args)
  120. "Parse argument list @var{args} and return an alist with all the relevant
  121. options."
  122. (args-fold args %options
  123. (lambda (opt name arg result)
  124. (format (current-error-port) "~A: unrecognized option" name)
  125. (exit 1))
  126. (lambda (file result)
  127. (let ((input-files (assoc-ref result 'input-files)))
  128. (alist-cons 'input-files (cons file input-files)
  129. result)))
  130. ;; default option values
  131. '((input-files)
  132. (load-path)
  133. (warnings unsupported-warning))))
  134. (define (show-version)
  135. (format #t "compile (GNU Guile) ~A~%" (version))
  136. (format #t "Copyright (C) 2009, 2011 Free Software Foundation, Inc.
  137. License LGPLv3+: GNU LGPL version 3 or later <http://gnu.org/licenses/lgpl.html>.
  138. This is free software: you are free to change and redistribute it.
  139. There is NO WARRANTY, to the extent permitted by law.~%"))
  140. (define (show-warning-help)
  141. (format #t "The available warning types are:~%~%")
  142. (for-each (lambda (wt)
  143. (format #t " ~22A ~A~%"
  144. (format #f "`~A'" (warning-type-name wt))
  145. (warning-type-description wt)))
  146. %warning-types)
  147. (format #t "~%"))
  148. (define (show-optimization-help)
  149. (format #t "The available optimizations are:~%~%")
  150. (let lp ((options (available-optimizations)))
  151. (match options
  152. (() #t)
  153. ((kw val . options)
  154. (let ((name (string-trim-right (symbol->string (keyword->symbol kw))
  155. #\?)))
  156. (format #t " -O~a~%"
  157. (if val name (string-append "no-" name)))
  158. (lp options)))))
  159. (format #t "~%")
  160. (format #t "To disable an optimization, prepend it with `no-', for example~%")
  161. (format #t "`-Ono-cse.'~%~%")
  162. (format #t "You may also specify optimization levels as `-O0', `-O1',~%")
  163. (format #t "`-O2', or `-O3'. Currently `-O0' turns off all optimizations,~%")
  164. (format #t "`-O1' turns on partial evaluation, and `-O2' and `-O3' turn on~%")
  165. (format #t "everything. The default is equivalent to `-O2'.")
  166. (format #t "~%"))
  167. (define (compile . args)
  168. (let* ((options (parse-args args))
  169. (help? (assoc-ref options 'help?))
  170. (compile-opts `(#:warnings
  171. ,(assoc-ref options 'warnings)
  172. ,@(append-map
  173. (lambda (opt)
  174. (match opt
  175. (('optimizations . opts) opts)
  176. (_ '())))
  177. options)))
  178. (from (or (assoc-ref options 'from) 'scheme))
  179. (to (or (assoc-ref options 'to) 'bytecode))
  180. (target (or (assoc-ref options 'target) %host-type))
  181. (input-files (assoc-ref options 'input-files))
  182. (output-file (assoc-ref options 'output-file))
  183. (load-path (assoc-ref options 'load-path)))
  184. (if (or help? (null? input-files))
  185. (begin
  186. (format #t "Usage: compile [OPTION] FILE...
  187. Compile each Guile source file FILE into a Guile object.
  188. -h, --help print this help message
  189. -L, --load-path=DIR add DIR to the front of the module load path
  190. -o, --output=OFILE write output to OFILE
  191. -W, --warn=WARNING emit warnings of type WARNING; use `--warn=help'
  192. for a list of available warnings
  193. -O, --optimize=OPT specify optimization passes to run; use `-Ohelp'
  194. for a list of available optimizations
  195. -f, --from=LANG specify a source language other than `scheme'
  196. -t, --to=LANG specify a target language other than `bytecode'
  197. -T, --target=TRIPLET produce bytecode for host TRIPLET
  198. Note that auto-compilation will be turned off.
  199. Report bugs to <~A>.~%"
  200. %guile-bug-report-address)
  201. (exit 0)))
  202. (set! %load-path (append load-path %load-path))
  203. (set! %load-should-auto-compile #f)
  204. (if (and output-file
  205. (or (null? input-files)
  206. (not (null? (cdr input-files)))))
  207. (fail "`-o' option can only be specified "
  208. "when compiling a single file"))
  209. ;; Install a SIGINT handler. As a side effect, this gives unwind
  210. ;; handlers an opportunity to run upon SIGINT; this includes that of
  211. ;; 'call-with-output-file/atomic', called by 'compile-file', which
  212. ;; removes the temporary output file.
  213. (sigaction SIGINT
  214. (lambda args
  215. (fail "interrupted by the user")))
  216. (for-each (lambda (file)
  217. (format #t "wrote `~A'\n"
  218. (with-fluids ((*current-warning-prefix* ""))
  219. (with-target target
  220. (lambda ()
  221. (compile-file file
  222. #:output-file output-file
  223. #:from from
  224. #:to to
  225. #:opts compile-opts))))))
  226. input-files)))
  227. (define main compile)