compile.scm 11 KB

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