compile.scm 12 KB

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