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