src/module-output.lisp: Introduce `module-output-file'.
[sod] / src / frontend.lisp
1 ;;; -*-lisp-*-
2 ;;;
3 ;;; User interface
4 ;;;
5 ;;; (c) 2013 Straylight/Edgeware
6 ;;;
7
8 ;;;----- Licensing notice ---------------------------------------------------
9 ;;;
10 ;;; This file is part of the Sensible Object Design, an object system for C.
11 ;;;
12 ;;; SOD is free software; you can redistribute it and/or modify
13 ;;; it under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 2 of the License, or
15 ;;; (at your option) any later version.
16 ;;;
17 ;;; SOD is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
21 ;;;
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with SOD; if not, write to the Free Software Foundation,
24 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26 (cl:defpackage #:sod-frontend
27 (:use #:common-lisp #:sod-utilities #:optparse #:sod #:sod-parser)
28 (:shadowing-import-from #:optparse #:int))
29
30 (cl:in-package #:sod-frontend)
31
32 ;;;--------------------------------------------------------------------------
33 ;;; Preparation for dumping.
34
35 (clear-the-decks)
36 (exercise)
37
38 ;;;--------------------------------------------------------------------------
39 ;;; The main program.
40
41 (defvar-unbound *option-parser*
42 "The program's main option parser.")
43
44 (eval-when (:compile-toplevel :load-toplevel :execute)
45 (defopthandler dirpath (var arg) ()
46 "Convert the argument into a pathname with a directory component
47 and no file component, suitable for merging."
48
49 ;; This is really fiddly and annoying. Unix pathnames don't tell you
50 ;; whether the thing named is meant to be a directory or not, and
51 ;; implementations differ as to how they cope with pathnames which do or
52 ;; don't name directories when they're expecting files, or vice versa.
53
54 (let ((path (ignore-errors (pathname arg))))
55 (cond ((null path)
56 ;; The namestring couldn't be parsed, or something else went
57 ;; horribly wrong.
58
59 (option-parse-error "Can't parse `~A' as a path" arg))
60
61 #+unix
62 ((or (pathname-name path) (pathname-type path))
63 ;; If this is Unix, or similar, then stick the filename piece on
64 ;; the end of the directory and hope that was sensible.
65
66 (setf var (make-pathname
67 :name nil :type nil :defaults path
68 :directory (append (or (pathname-directory path)
69 (list :relative))
70 (list (file-namestring path))))))
71
72 (t
73 ;; This actually looks like a plain directory name.
74
75 (setf var path))))))
76
77 (defun update-usage ()
78 (setf *usage* (simple-usage *options* "SOURCES...")))
79
80 (export 'augment-options)
81 (defun augment-options (options)
82 "Add OPTIONS to the program's options list."
83 (asetf *options* (append it options))
84 (setf (op-options *option-parser*) *options*)
85 (update-usage))
86
87 (use-package "SOD-FRONTEND" "SOD-USER")
88
89 (export 'main)
90 (defun main ()
91
92 ;; Initialize the argument parser.
93 (set-command-line-arguments)
94
95 ;; Collect information from the command line options.
96 (let ((output-reasons nil)
97 (output-path (make-pathname :directory '(:relative)))
98 (backtracep nil)
99 (builtinsp nil)
100 (stdoutp nil)
101 (args nil))
102
103 ;; Option definitions.
104 (define-program
105 :help "Process SOD input files to produce (e.g.) C output."
106 :version *sod-version*
107 :options (options
108 (help-options :short-version #\V)
109 "Translator options"
110 (#\I "include" (:arg "DIR")
111 ("Search DIR for module imports.")
112 (list *module-dirs* 'string))
113 ("backtrace"
114 ("Print a Lisp backtrace on error (for debugging).")
115 (set backtracep))
116 ("builtins"
117 ("Process the builtin `sod-base' module.")
118 (set builtinsp))
119 (#\d "directory" (:arg "DIR")
120 ("Write output files to DIR.")
121 (dirpath output-path))
122 (#\e "eval" (:arg "LISP")
123 ("Evaluate raw Lisp code.")
124 (lambda (lisp)
125 (handler-case
126 (let ((*package* (find-package "SOD-USER")))
127 (eval (read-from-string lisp)))
128 (error (error)
129 (option-parse-error "~A" error)))))
130 (#\l "load" (:arg "FILE")
131 ("Load a file of Lisp code.")
132 (lambda (file)
133 (let ((file (merge-pathnames file
134 (make-pathname
135 :type "LISP"
136 :case :common))))
137 (handler-case
138 (let ((*package* (find-package "SOD-USER")))
139 (find-file *default-pathname-defaults* file
140 "Lisp file"
141 (lambda (path true)
142 (declare (ignore path))
143 (load true
144 :verbose nil
145 :print nil))))
146 (error (error)
147 (option-parse-error "~A" error))))))
148 (#\p "stdout"
149 ("Write output files to standard output.")
150 (set stdoutp))
151 (#\t "type" (:arg "OUT-TYPE")
152 ("Produce output of type OUT-TYPE.")
153 (list output-reasons 'keyword))))
154 (update-usage)
155
156 ;; Actually parse the options.
157 (let ((*option-parser* (make-option-parser)))
158 (unless (and (option-parse-try
159 (do-options (:parser *option-parser*)
160 (nil (rest)
161 (setf args rest))))
162 (or builtinsp args))
163 (die-usage)))
164
165 ;; Do the main parsing job.
166 (labels ((hack-module (module)
167 ;; Process the MODULE, writing out the generated code.
168
169 ;; Work through each output type in turn.
170 (dolist (reason output-reasons)
171
172 ;; Arrange to be able to recover from errors.
173 (restart-case
174 (cond
175
176 (stdoutp
177 ;; If we're writing to stdout then use
178 ;; `output-type-pathname' to check the output type
179 ;; for us.
180
181 (output-type-pathname reason)
182 (output-module module reason *standard-output*))
183
184 (t
185 ;; Otherwise we have to construct an output
186 ;; filename the hard way.
187 (with-open-file
188 (stream
189 (module-output-file module reason output-path)
190 :direction :output
191 :if-exists :supersede
192 :if-does-not-exist :create)
193 (output-module module reason stream))))
194
195 ;; Error recovery.
196 (continue ()
197 :report (lambda (stream)
198 (format stream
199 "Skip output type `~(~A~)'"
200 reason))
201 nil))))
202
203 (hack-modules ()
204
205 ;; If there are no output types then there's nothing to do.
206 (unless output-reasons
207 (error "No output types given: nothing to do"))
208
209 ;; If we're writing the builtin module then now seems like a
210 ;; good time to do that.
211 (when builtinsp
212 (hack-module *builtin-module*))
213
214 ;; Parse and write out the remaining modules.
215 (dolist (arg args)
216 (let ((module (read-module arg)))
217 (when (zerop (module-errors module))
218 (hack-module module))))))
219
220 (if backtracep (hack-modules)
221 (multiple-value-bind (hunoz nerror nwarn)
222 (count-and-report-errors ()
223 (with-default-error-location
224 ((make-file-location *program-name*))
225 (hack-modules)))
226 (declare (ignore hunoz))
227 (when (or (plusp nerror) (plusp nwarn))
228 (format *error-output* "~A: Finished with~
229 ~[~:; ~:*~D error~:P~[~:; and~]~:*~]~
230 ~[~:; ~:*~D warning~:P~]~%"
231 *program-name* nerror nwarn))
232 (exit (if (plusp nerror) 2 0)))))))
233
234 ;;;----- That's all, folks --------------------------------------------------