Merge branch 'master' into wip-manual-2
[bpt/guile.git] / module / scripts / compile.scm
1 ;;; Compile --- Command-line Guile Scheme compiler -*- coding: iso-8859-1 -*-
2
3 ;; Copyright 2005,2008,2009,2010 Free Software Foundation, Inc.
4 ;;
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public License
7 ;; as published by the Free Software Foundation; either version 3, or
8 ;; (at your option) any later version.
9 ;;
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
14 ;;
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this software; see the file COPYING.LESSER. If
17 ;; not, write to the Free Software Foundation, Inc., 51 Franklin
18 ;; Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 ;;; Author: Ludovic Courtès <ludo@gnu.org>
21 ;;; Author: Andy Wingo <wingo@pobox.com>
22
23 ;;; Commentary:
24
25 ;; Usage: compile [ARGS]
26 ;;
27 ;; A command-line interface to the Guile compiler.
28
29 ;;; Code:
30
31 (define-module (scripts compile)
32 #:use-module ((system base compile) #:select (compile-file))
33 #:use-module (system base message)
34 #:use-module (srfi srfi-1)
35 #:use-module (srfi srfi-13)
36 #:use-module (srfi srfi-37)
37 #:use-module (ice-9 format)
38 #:export (compile))
39
40 \f
41 (define (fail . messages)
42 (format (current-error-port)
43 (string-concatenate `("error: " ,@messages "~%")))
44 (exit 1))
45
46 (define %options
47 ;; Specifications of the command-line options.
48 (list (option '(#\h "help") #f #f
49 (lambda (opt name arg result)
50 (alist-cons 'help? #t result)))
51 (option '("version") #f #f
52 (lambda (opt name arg result)
53 (show-version)
54 (exit 0)))
55
56 (option '(#\L "load-path") #t #f
57 (lambda (opt name arg result)
58 (let ((load-path (assoc-ref result 'load-path)))
59 (alist-cons 'load-path (cons arg load-path)
60 result))))
61 (option '(#\o "output") #t #f
62 (lambda (opt name arg result)
63 (if (assoc-ref result 'output-file)
64 (fail "`-o' option cannot be specified more than once")
65 (alist-cons 'output-file arg result))))
66
67 (option '(#\W "warn") #t #f
68 (lambda (opt name arg result)
69 (if (string=? arg "help")
70 (begin
71 (show-warning-help)
72 (exit 0))
73 (let ((warnings (assoc-ref result 'warnings)))
74 (alist-cons 'warnings
75 (cons (string->symbol arg) warnings)
76 (alist-delete 'warnings result))))))
77
78 (option '(#\O "optimize") #f #f
79 (lambda (opt name arg result)
80 (alist-cons 'optimize? #t result)))
81 (option '(#\f "from") #t #f
82 (lambda (opt name arg result)
83 (if (assoc-ref result 'from)
84 (fail "`--from' option cannot be specified more than once")
85 (alist-cons 'from (string->symbol arg) result))))
86 (option '(#\t "to") #t #f
87 (lambda (opt name arg result)
88 (if (assoc-ref result 'to)
89 (fail "`--to' option cannot be specified more than once")
90 (alist-cons 'to (string->symbol arg) result))))))
91
92 (define (parse-args args)
93 "Parse argument list @var{args} and return an alist with all the relevant
94 options."
95 (args-fold args %options
96 (lambda (opt name arg result)
97 (format (current-error-port) "~A: unrecognized option" name)
98 (exit 1))
99 (lambda (file result)
100 (let ((input-files (assoc-ref result 'input-files)))
101 (alist-cons 'input-files (cons file input-files)
102 result)))
103
104 ;; default option values
105 '((input-files)
106 (load-path)
107 (warnings unsupported-warning))))
108
109 (define (show-version)
110 (format #t "compile (GNU Guile) ~A~%" (version))
111 (format #t "Copyright (C) 2009 Free Software Foundation, Inc.
112 License LGPLv3+: GNU LGPL version 3 or later <http://gnu.org/licenses/lgpl.html>.
113 This is free software: you are free to change and redistribute it.
114 There is NO WARRANTY, to the extent permitted by law.~%"))
115
116 (define (show-warning-help)
117 (format #t "The available warning types are:~%~%")
118 (for-each (lambda (wt)
119 (format #t " ~22A ~A~%"
120 (format #f "`~A'" (warning-type-name wt))
121 (warning-type-description wt)))
122 %warning-types)
123 (format #t "~%"))
124
125 \f
126 (define (compile . args)
127 (let* ((options (parse-args args))
128 (help? (assoc-ref options 'help?))
129 (compile-opts (let ((o `(#:warnings
130 ,(assoc-ref options 'warnings))))
131 (if (assoc-ref options 'optimize?)
132 (cons #:O o)
133 o)))
134 (from (or (assoc-ref options 'from) 'scheme))
135 (to (or (assoc-ref options 'to) 'objcode))
136 (input-files (assoc-ref options 'input-files))
137 (output-file (assoc-ref options 'output-file))
138 (load-path (assoc-ref options 'load-path)))
139 (if (or help? (null? input-files))
140 (begin
141 (format #t "Usage: compile [OPTION] FILE...
142 Compile each Guile source file FILE into a Guile object.
143
144 -h, --help print this help message
145
146 -L, --load-path=DIR add DIR to the front of the module load path
147 -o, --output=OFILE write output to OFILE
148
149 -W, --warn=WARNING emit warnings of type WARNING; use `--warn=help'
150 for a list of available warnings
151
152 -f, --from=LANG specify a source language other than `scheme'
153 -t, --to=LANG specify a target language other than `objcode'
154
155 Note that autocompilation will be turned off.
156
157 Report bugs to <~A>.~%"
158 %guile-bug-report-address)
159 (exit 0)))
160
161 (set! %load-path (append load-path %load-path))
162 (set! %load-should-autocompile #f)
163
164 (if (and output-file
165 (or (null? input-files)
166 (not (null? (cdr input-files)))))
167 (fail "`-o' option can only be specified "
168 "when compiling a single file"))
169
170 (for-each (lambda (file)
171 (format #t "wrote `~A'\n"
172 (compile-file file
173 #:output-file output-file
174 #:from from
175 #:to to
176 #:opts compile-opts)))
177 input-files)))
178
179 (define main compile)