1 ;;; Compile --- Command-line Guile Scheme compiler -*- coding: iso-8859-1 -*-
3 ;; Copyright 2005,2008,2009,2010,2011,2013 Free Software Foundation, Inc.
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.
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.
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
20 ;;; Author: Ludovic Courtès <ludo@gnu.org>
21 ;;; Author: Andy Wingo <wingo@pobox.com>
25 ;; Usage: compile [ARGS]
27 ;; A command-line interface to the Guile compiler.
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)
41 (define %summary "Compile a file.")
44 (define (fail . messages)
45 (format (current-error-port) "error: ~{~a~}~%" messages)
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)
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)
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))))
69 (option '(#\W "warn") #t #f
70 (lambda (opt name arg result)
71 (if (string=? arg "help")
75 (let ((warnings (assoc-ref result 'warnings)))
77 (cons (string->symbol arg) warnings)
78 (alist-delete 'warnings result))))))
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))))))
99 (define (parse-args args)
100 "Parse argument list @var{args} and return an alist with all the relevant
102 (args-fold args %options
103 (lambda (opt name arg result)
104 (format (current-error-port) "~A: unrecognized option" name)
106 (lambda (file result)
107 (let ((input-files (assoc-ref result 'input-files)))
108 (alist-cons 'input-files (cons file input-files)
111 ;; default option values
114 (warnings unsupported-warning))))
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.~%"))
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)))
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?)
141 (from (or (assoc-ref options 'from) 'scheme))
142 (to (or (assoc-ref options 'to) 'rtl))
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))
149 (format #t "Usage: compile [OPTION] FILE...
150 Compile each Guile source file FILE into a Guile object.
152 -h, --help print this help message
154 -L, --load-path=DIR add DIR to the front of the module load path
155 -o, --output=OFILE write output to OFILE
157 -W, --warn=WARNING emit warnings of type WARNING; use `--warn=help'
158 for a list of available warnings
160 -f, --from=LANG specify a source language other than `scheme'
161 -t, --to=LANG specify a target language other than `rtl'
162 -T, --target=TRIPLET produce bytecode for host TRIPLET
164 Note that auto-compilation will be turned off.
166 Report bugs to <~A>.~%"
167 %guile-bug-report-address)
170 (set! %load-path (append load-path %load-path))
171 (set! %load-should-auto-compile #f)
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"))
179 (for-each (lambda (file)
180 (format #t "wrote `~A'\n"
181 (with-fluids ((*current-warning-prefix* ""))
185 #:output-file output-file
188 #:opts compile-opts))))))
191 (define main compile)