compile.scm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ;;; Compile --- Command-line Guile Scheme compiler -*- coding: iso-8859-1 -*-
  2. ;; Copyright 2005,2008,2009,2010,2011 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 (srfi srfi-1)
  30. #:use-module (srfi srfi-13)
  31. #:use-module (srfi srfi-37)
  32. #:use-module (ice-9 format)
  33. #:export (compile))
  34. (define %summary "Compile a file.")
  35. (define (fail . messages)
  36. (format (current-error-port) "error: ~{~a~}~%" messages)
  37. (exit 1))
  38. (define %options
  39. ;; Specifications of the command-line options.
  40. (list (option '(#\h "help") #f #f
  41. (lambda (opt name arg result)
  42. (alist-cons 'help? #t result)))
  43. (option '("version") #f #f
  44. (lambda (opt name arg result)
  45. (show-version)
  46. (exit 0)))
  47. (option '(#\L "load-path") #t #f
  48. (lambda (opt name arg result)
  49. (let ((load-path (assoc-ref result 'load-path)))
  50. (alist-cons 'load-path (cons arg load-path)
  51. result))))
  52. (option '(#\o "output") #t #f
  53. (lambda (opt name arg result)
  54. (if (assoc-ref result 'output-file)
  55. (fail "`-o' option cannot be specified more than once")
  56. (alist-cons 'output-file arg result))))
  57. (option '(#\W "warn") #t #f
  58. (lambda (opt name arg result)
  59. (if (string=? arg "help")
  60. (begin
  61. (show-warning-help)
  62. (exit 0))
  63. (let ((warnings (assoc-ref result 'warnings)))
  64. (alist-cons 'warnings
  65. (cons (string->symbol arg) warnings)
  66. (alist-delete 'warnings result))))))
  67. (option '(#\O "optimize") #f #f
  68. (lambda (opt name arg result)
  69. (alist-cons 'optimize? #t result)))
  70. (option '(#\f "from") #t #f
  71. (lambda (opt name arg result)
  72. (if (assoc-ref result 'from)
  73. (fail "`--from' option cannot be specified more than once")
  74. (alist-cons 'from (string->symbol arg) result))))
  75. (option '(#\t "to") #t #f
  76. (lambda (opt name arg result)
  77. (if (assoc-ref result 'to)
  78. (fail "`--to' option cannot be specified more than once")
  79. (alist-cons 'to (string->symbol arg) result))))
  80. (option '(#\T "target") #t #f
  81. (lambda (opt name arg result)
  82. (if (assoc-ref result 'target)
  83. (fail "`--target' option cannot be specified more than once")
  84. (alist-cons 'target arg result))))))
  85. (define (parse-args args)
  86. "Parse argument list @var{args} and return an alist with all the relevant
  87. options."
  88. (args-fold args %options
  89. (lambda (opt name arg result)
  90. (format (current-error-port) "~A: unrecognized option" name)
  91. (exit 1))
  92. (lambda (file result)
  93. (let ((input-files (assoc-ref result 'input-files)))
  94. (alist-cons 'input-files (cons file input-files)
  95. result)))
  96. ;; default option values
  97. '((input-files)
  98. (load-path)
  99. (warnings unsupported-warning))))
  100. (define (show-version)
  101. (format #t "compile (GNU Guile) ~A~%" (version))
  102. (format #t "Copyright (C) 2009, 2011 Free Software Foundation, Inc.
  103. License LGPLv3+: GNU LGPL version 3 or later <http://gnu.org/licenses/lgpl.html>.
  104. This is free software: you are free to change and redistribute it.
  105. There is NO WARRANTY, to the extent permitted by law.~%"))
  106. (define (show-warning-help)
  107. (format #t "The available warning types are:~%~%")
  108. (for-each (lambda (wt)
  109. (format #t " ~22A ~A~%"
  110. (format #f "`~A'" (warning-type-name wt))
  111. (warning-type-description wt)))
  112. %warning-types)
  113. (format #t "~%"))
  114. (define (compile . args)
  115. (let* ((options (parse-args args))
  116. (help? (assoc-ref options 'help?))
  117. (compile-opts (let ((o `(#:warnings
  118. ,(assoc-ref options 'warnings))))
  119. (if (assoc-ref options 'optimize?)
  120. (cons #:O o)
  121. o)))
  122. (from (or (assoc-ref options 'from) 'scheme))
  123. (to (or (assoc-ref options 'to) 'objcode))
  124. (target (or (assoc-ref options 'target) %host-type))
  125. (input-files (assoc-ref options 'input-files))
  126. (output-file (assoc-ref options 'output-file))
  127. (load-path (assoc-ref options 'load-path)))
  128. (if (or help? (null? input-files))
  129. (begin
  130. (format #t "Usage: compile [OPTION] FILE...
  131. Compile each Guile source file FILE into a Guile object.
  132. -h, --help print this help message
  133. -L, --load-path=DIR add DIR to the front of the module load path
  134. -o, --output=OFILE write output to OFILE
  135. -W, --warn=WARNING emit warnings of type WARNING; use `--warn=help'
  136. for a list of available warnings
  137. -f, --from=LANG specify a source language other than `scheme'
  138. -t, --to=LANG specify a target language other than `objcode'
  139. -T, --target=TRIPLET produce bytecode for host TRIPLET
  140. Note that auto-compilation will be turned off.
  141. Report bugs to <~A>.~%"
  142. %guile-bug-report-address)
  143. (exit 0)))
  144. (set! %load-path (append load-path %load-path))
  145. (set! %load-should-auto-compile #f)
  146. (if (and output-file
  147. (or (null? input-files)
  148. (not (null? (cdr input-files)))))
  149. (fail "`-o' option can only be specified "
  150. "when compiling a single file"))
  151. (for-each (lambda (file)
  152. (format #t "wrote `~A'\n"
  153. (with-fluids ((*current-warning-prefix* ""))
  154. (with-target target
  155. (lambda ()
  156. (compile-file file
  157. #:output-file output-file
  158. #:from from
  159. #:to to
  160. #:opts compile-opts))))))
  161. input-files)))
  162. (define main compile)