src/frontend.lisp: Add a function to update the `*usage*' list.
[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 'main)
81 (defun main ()
82
83 ;; Initialize the argument parser.
84 (set-command-line-arguments)
85
86 ;; Collect information from the command line options.
87 (let ((output-reasons nil)
88 (output-path (make-pathname :directory '(:relative)))
89 (backtracep nil)
90 (builtinsp nil)
91 (stdoutp nil)
92 (args nil))
93
94 ;; Option definitions.
95 (define-program
96 :help "Process SOD input files to produce (e.g.) C output."
97 :version *sod-version*
98 :options (options
99 (help-options :short-version #\V)
100 "Translator options"
101 (#\I "include" (:arg "DIR")
102 ("Search DIR for module imports.")
103 (list *module-dirs* 'string))
104 ("backtrace"
105 ("Print a Lisp backtrace on error (for debugging).")
106 (set backtracep))
107 ("builtins"
108 ("Process the builtin `sod-base' module.")
109 (set builtinsp))
110 (#\d "directory" (:arg "DIR")
111 ("Write output files to DIR.")
112 (dirpath output-path))
113 (#\p "stdout"
114 ("Write output files to standard output.")
115 (set stdoutp))
116 (#\t "type" (:arg "OUT-TYPE")
117 ("Produce output of type OUT-TYPE.")
118 (list output-reasons 'keyword))))
119 (update-usage)
120
121 ;; Actually parse the options.
122 (let ((*option-parser* (make-option-parser)))
123 (unless (and (option-parse-try
124 (do-options (:parser *option-parser*)
125 (nil (rest)
126 (setf args rest))))
127 (or builtinsp args))
128 (die-usage)))
129
130 ;; Do the main parsing job.
131 (labels ((hack-module (module)
132 ;; Process the MODULE, writing out the generated code.
133
134 ;; Work through each output type in turn.
135 (dolist (reason output-reasons)
136
137 ;; Arrange to be able to recover from errors.
138 (restart-case
139
140 ;; Collect information for constructing the output
141 ;; filenames here. In particular,
142 ;; `output-type-pathname' will sanity-check the
143 ;; output type for us, which is useful even if
144 ;; we're writing to stdout.
145 (let ((outpath (output-type-pathname reason))
146 (modpath (module-name module)))
147
148 (if stdoutp
149
150 ;; If we're writing to stdout then just do
151 ;; that.
152 (output-module module reason
153 *standard-output*)
154
155 ;; Otherwise we have to construct an output
156 ;; filename the hard way.
157 (with-open-file
158 (stream
159 (reduce #'merge-pathnames
160 (list output-path
161 outpath
162 (make-pathname
163 :directory nil
164 :defaults modpath))
165 :from-end t)
166 :direction :output
167 :if-exists :supersede
168 :if-does-not-exist :create)
169 (output-module module reason stream))))
170
171 ;; Error recovery.
172 (continue ()
173 :report (lambda (stream)
174 (format stream
175 "Skip output type `~(~A~)'"
176 reason))
177 nil))))
178
179 (hack-modules ()
180
181 ;; If there are no output types then there's nothing to do.
182 (unless output-reasons
183 (error "No output types given: nothing to do"))
184
185 ;; If we're writing the builtin module then now seems like a
186 ;; good time to do that.
187 (when builtinsp
188 (hack-module *builtin-module*))
189
190 ;; Parse and write out the remaining modules.
191 (dolist (arg args)
192 (let ((module (read-module arg)))
193 (when (zerop (module-errors module))
194 (hack-module module))))))
195
196 (if backtracep (hack-modules)
197 (multiple-value-bind (hunoz nerror nwarn)
198 (count-and-report-errors ()
199 (with-default-error-location
200 ((make-file-location *program-name*))
201 (hack-modules)))
202 (declare (ignore hunoz))
203 (when (or (plusp nerror) (plusp nwarn))
204 (format *error-output* "~A: Finished with~
205 ~[~:; ~:*~D error~:P~[~:; and~]~:*~]~
206 ~[~:; ~:*~D warning~:P~]~%"
207 *program-name* nerror nwarn))
208 (exit (if (plusp nerror) 2 0)))))))
209
210 ;;;----- That's all, folks --------------------------------------------------