compile.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. ;;; Compile --- Command-line Guile Scheme compiler -*- coding: iso-8859-1 -*-
  2. ;; Copyright 2005,2008-2011,2013-2015,2017-2018 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 language) #:select (lookup-language))
  27. #:use-module ((system base compile) #:select (compile-file))
  28. #:use-module (system base target)
  29. #:use-module (system base message)
  30. #:use-module (system base 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 %options
  42. ;; Specifications of the command-line options.
  43. (list (option '(#\h "help") #f #f
  44. (lambda (opt name arg result)
  45. (alist-cons 'help? #t result)))
  46. (option '("version") #f #f
  47. (lambda (opt name arg result)
  48. (show-version)
  49. (exit 0)))
  50. (option '(#\L "load-path") #t #f
  51. (lambda (opt name arg result)
  52. (let ((load-path (assoc-ref result 'load-path)))
  53. (alist-cons 'load-path (cons arg load-path)
  54. result))))
  55. (option '(#\o "output") #t #f
  56. (lambda (opt name arg result)
  57. (if (assoc-ref result 'output-file)
  58. (fail "`-o' option cannot be specified more than once")
  59. (alist-cons 'output-file arg result))))
  60. (option '(#\x) #t #f
  61. (lambda (opt name arg result)
  62. (set! %load-extensions (cons arg %load-extensions))
  63. result))
  64. (option '(#\W "warn") #t #f
  65. (lambda (opt name arg result)
  66. (if (string=? arg "help")
  67. (begin
  68. (show-warning-help)
  69. (exit 0))
  70. (let ((warnings (assoc-ref result 'warnings)))
  71. (alist-cons 'warnings
  72. (cons (string->symbol arg) warnings)
  73. (alist-delete 'warnings result))))))
  74. (option '(#\O "optimize") #t #f
  75. (lambda (opt name arg result)
  76. (define (return val)
  77. (alist-cons 'optimizations val result))
  78. (define (return-option name val)
  79. (let ((kw (symbol->keyword
  80. (string->symbol (string-append name "?")))))
  81. (unless (assq kw (available-optimizations))
  82. (fail "Unknown optimization pass `~a'" name))
  83. (return (list kw val))))
  84. (cond
  85. ((string=? arg "help")
  86. (show-optimization-help)
  87. (exit 0))
  88. ((equal? arg "0") (return (optimizations-for-level 0)))
  89. ((equal? arg "1") (return (optimizations-for-level 1)))
  90. ((equal? arg "2") (return (optimizations-for-level 2)))
  91. ((equal? arg "3") (return (optimizations-for-level 3)))
  92. ((string-prefix? "no-" arg)
  93. (return-option (substring arg 3) #f))
  94. (else
  95. (return-option arg #t)))))
  96. (option '(#\f "from") #t #f
  97. (lambda (opt name arg result)
  98. (if (assoc-ref result 'from)
  99. (fail "`--from' option cannot be specified more than once")
  100. (alist-cons 'from (string->symbol arg) result))))
  101. (option '(#\t "to") #t #f
  102. (lambda (opt name arg result)
  103. (if (assoc-ref result 'to)
  104. (fail "`--to' option cannot be specified more than once")
  105. (alist-cons 'to (string->symbol arg) result))))
  106. (option '(#\T "target") #t #f
  107. (lambda (opt name arg result)
  108. (if (assoc-ref result 'target)
  109. (fail "`--target' option cannot be specified more than once")
  110. (alist-cons 'target arg result))))))
  111. (define (parse-args args)
  112. "Parse argument list @var{args} and return an alist with all the relevant
  113. options."
  114. (args-fold args %options
  115. (lambda (opt name arg result)
  116. (format (current-error-port) "~A: unrecognized option~%" name)
  117. (exit 1))
  118. (lambda (file result)
  119. (let ((input-files (assoc-ref result 'input-files)))
  120. (alist-cons 'input-files (cons file input-files)
  121. result)))
  122. ;; default option values
  123. '((input-files)
  124. (load-path)
  125. (warnings unsupported-warning))))
  126. (define (show-version)
  127. (format #t "compile (GNU Guile) ~A~%" (version))
  128. (format #t "Copyright (C) 2018 Free Software Foundation, Inc.
  129. License LGPLv3+: GNU LGPL version 3 or later <http://gnu.org/licenses/lgpl.html>.
  130. This is free software: you are free to change and redistribute it.
  131. There is NO WARRANTY, to the extent permitted by law.~%"))
  132. (define (show-warning-help)
  133. (format #t "The available warning types are:~%~%")
  134. (for-each (lambda (wt)
  135. (format #t " ~22A ~A~%"
  136. (format #f "`~A'" (warning-type-name wt))
  137. (warning-type-description wt)))
  138. %warning-types)
  139. (format #t "~%"))
  140. (define (show-optimization-help)
  141. (format #t "The available optimizations are:~%~%")
  142. (let lp ((options (available-optimizations)))
  143. (match options
  144. (() #t)
  145. (((kw level) . options)
  146. (let ((name (string-trim-right (symbol->string (keyword->symbol kw))
  147. #\?)))
  148. (format #t " -O~a~%" name)
  149. (lp options)))))
  150. (format #t "~%")
  151. (format #t "To disable an optimization, prepend it with `no-', for example~%")
  152. (format #t "`-Ono-cse.'~%~%")
  153. (format #t "You may also specify optimization levels as `-O0', `-O1',~%")
  154. (format #t "`-O2', or `-O3'. Currently `-O0' turns off all optimizations,~%")
  155. (format #t "`-O1' turns on partial evaluation, and `-O2' and `-O3' turn on~%")
  156. (format #t "everything. The default is equivalent to `-O2'.")
  157. (format #t "~%"))
  158. (define (compile . args)
  159. (let* ((options (parse-args args))
  160. (help? (assoc-ref options 'help?))
  161. (compile-opts `(#:warnings
  162. ,(assoc-ref options 'warnings)
  163. ,@(append-map
  164. (lambda (opt)
  165. (match opt
  166. (('optimizations . opts) opts)
  167. (_ '())))
  168. options)))
  169. (from (or (assoc-ref options 'from) 'scheme))
  170. (to (or (assoc-ref options 'to) 'bytecode))
  171. (target (or (assoc-ref options 'target) %host-type))
  172. (input-files (assoc-ref options 'input-files))
  173. (output-file (assoc-ref options 'output-file))
  174. (load-path (assoc-ref options 'load-path)))
  175. (if (or help? (null? input-files))
  176. (begin
  177. (format #t "Usage: compile [OPTION] FILE...
  178. Compile each Guile source file FILE into a Guile object.
  179. -h, --help print this help message
  180. -L, --load-path=DIR add DIR to the front of the module load path
  181. -o, --output=OFILE write output to OFILE
  182. -x EXTENSION add EXTENSION to the set of source file extensions
  183. -W, --warn=WARNING emit warnings of type WARNING; use `--warn=help'
  184. for a list of available warnings
  185. -O, --optimize=OPT specify optimization passes to run; use `-Ohelp'
  186. for a list of available optimizations
  187. -f, --from=LANG specify a source language other than `scheme'
  188. -t, --to=LANG specify a target language other than `bytecode'
  189. -T, --target=TRIPLET produce bytecode for host TRIPLET
  190. Note that auto-compilation will be turned off.
  191. Report bugs to <~A>.~%"
  192. %guile-bug-report-address)
  193. (exit 0)))
  194. ;; Load FROM and TO before we have changed the load path. That way, when
  195. ;; cross-compiling Guile itself, we can be sure we're loading our own
  196. ;; language modules and not those of the Guile being compiled, which may
  197. ;; have incompatible .go files.
  198. (lookup-language from)
  199. (lookup-language to)
  200. (set! %load-path (append load-path %load-path))
  201. (set! %load-should-auto-compile #f)
  202. (if (and output-file
  203. (or (null? input-files)
  204. (not (null? (cdr input-files)))))
  205. (fail "`-o' option can only be specified "
  206. "when compiling a single file"))
  207. ;; Install a SIGINT handler. As a side effect, this gives unwind
  208. ;; handlers an opportunity to run upon SIGINT; this includes that of
  209. ;; 'call-with-output-file/atomic', called by 'compile-file', which
  210. ;; removes the temporary output file.
  211. (sigaction SIGINT
  212. (lambda args
  213. (fail "interrupted by the user")))
  214. (for-each (lambda (file)
  215. (format #t "wrote `~A'\n"
  216. (with-fluids ((*current-warning-prefix* ""))
  217. (with-target target
  218. (lambda ()
  219. (compile-file file
  220. #:output-file output-file
  221. #:from from
  222. #:to to
  223. #:opts compile-opts))))))
  224. input-files)))
  225. (define main compile)