3 exec ${GUILE-guile} -e '(@ (scripts compile) compile)' -s $0 "$@"
5 ;;; Compile
--- Command-line Guile Scheme compiler
7 ;; Copyright
2005,2008,2009 Free Software Foundation
, Inc.
9 ;; This program is free software
; you can redistribute it and
/or
10 ;; modify it under the terms of the GNU General Public License as
11 ;; published by the Free Software Foundation
; either version
2, or
12 ;; (at your option
) any later version.
14 ;; This program is distributed
in the hope that it will be useful
,
15 ;; but WITHOUT ANY WARRANTY
; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License
for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this software
; see the
file COPYING. If not
, write to
21 ;; the Free Software Foundation
, Inc.
, 51 Franklin Street
, Fifth Floor
,
22 ;; Boston
, MA
02110-1301 USA
24 ;;; Author
: Ludovic Courtès
<ludo@gnu.org
>
25 ;;; Author
: Andy Wingo
<wingo@pobox.com
>
29 ;; Usage
: compile
[ARGS
]
31 ;; PROGRAM does something.
37 (define-module
(scripts compile
)
38 #:use-module ((system base compile) #:select (compile-file))
39 #:use-module (srfi srfi-1)
40 #:use-module (srfi srfi-13)
41 #:use-module (srfi srfi-37)
45 (define
(fail . messages
)
46 (format
(current-error-port
)
47 (string-concatenate
`("error: " ,@messages "~%")))
51 ;; Specifications of the command-line options.
52 (list (option '(#\h "help") #f #f
53 (lambda (opt name arg result)
54 (alist-cons 'help? #t result)))
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)
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
))))
67 (option
'(#\O "optimize") #f #f
68 (lambda (opt name arg result)
69 (alist-cons 'optimize?
#t result)))
70 (option
'(#\e "expand-only") #f #f
71 (lambda (opt name arg result)
72 (alist-cons 'expand-only?
#t result)))
73 (option
'(#\t "translate-only") #f #f
74 (lambda (opt name arg result)
75 (alist-cons 'translate-only?
#t result)))
76 (option
'(#\c "compile-only") #f #f
77 (lambda (opt name arg result)
78 (alist-cons 'compile-only?
#t result)))))
80 (define
(parse-args args
)
81 "Parse argument list @var{args} and return an alist with all the relevant
83 (args-fold args
%options
84 (lambda
(opt name arg result
)
85 (format
(current-error-port
) "~A: unrecognized option" opt
)
88 (let ((input-files
(assoc-ref result
'input-files)))
89 (alist-cons 'input-files
(cons
file input-files
)
92 ;; default option values
97 (define (compile args)
98 (let* ((options (parse-args (cdr args)))
99 (help? (assoc-ref options 'help?
))
100 (optimize?
(assoc-ref options
'optimize?))
101 (expand-only? (assoc-ref options 'expand-only?
))
102 (translate-only?
(assoc-ref options
'translate-only?))
103 (compile-only? (assoc-ref options 'compile-only?
))
104 (input-files
(assoc-ref options
'input-files))
105 (output-file (assoc-ref options 'output-file
))
106 (load-path
(assoc-ref options
'load-path)))
107 (if (or help? (null? input-files))
109 (format #t "Usage: compile [OPTION] FILE...
110 Compile each Guile Scheme source file FILE into a Guile object.
112 -h, --help print this help message
114 -L, --load-path=DIR add DIR to the front of the module load path
115 -o, --output=OFILE write output to OFILE
117 -O, --optimize turn on optimizations
118 -e, --expand-only only go through the code expansion stage
119 -t, --translate-only stop after the translation to GHIL
120 -c, --compile-only stop after the compilation to GLIL
122 Report bugs to <guile-user@gnu.org>.~%")
125 (set! %load-path (append load-path %load-path))
127 (let ((compile-opts (append (if optimize? '(#:O) '())
128 (if expand-only?
'(#:e) '())
129 (if translate-only?
'(#:t) '())
130 (if compile-only?
'(#:c) '()))))
132 (if (and
(not
(null? input-files
))
133 (null?
(cdr input-files
)))
134 (compile-file
(car input-files
) output-file
)
135 (fail
"`-o' option can only be specified "
136 "when compiling a single file"))
137 (for-each
(lambda
(file)
138 (apply compile-file
file compile-opts
))