compile.scm 6.5 KB

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