5 ;;; (c) 2013 Straylight/Edgeware
8 ;;;----- Licensing notice ---------------------------------------------------
10 ;;; This file is part of the Sensible Object Design, an object system for C.
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.
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.
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.
26 (cl:defpackage #:sod-frontend
27 (:use #:common-lisp #:sod-utilities #:optparse #:sod #:sod-parser)
28 (:shadowing-import-from #:optparse #:int))
30 (cl:in-package #:sod-frontend)
32 ;;;--------------------------------------------------------------------------
33 ;;; Preparation for dumping.
38 ;;;--------------------------------------------------------------------------
41 (defvar-unbound *option-parser*
42 "The program's main option parser.")
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."
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.
54 (let ((path (ignore-errors (pathname arg))))
56 ;; The namestring couldn't be parsed, or something else went
59 (option-parse-error "Can't parse `~A' as a path" arg))
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.
66 (setf var (make-pathname
67 :name nil :type nil :defaults path
68 :directory (append (or (pathname-directory path)
70 (list (file-namestring path))))))
73 ;; This actually looks like a plain directory name.
77 (defun update-usage ()
78 (setf *usage* (simple-usage *options* "SOURCES...")))
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*)
87 (use-package "SOD-FRONTEND" "SOD-USER")
92 ;; Initialize the argument parser.
93 (set-command-line-arguments)
95 ;; Collect information from the command line options.
96 (let ((output-reasons nil)
97 (output-path (make-pathname :directory '(:relative)))
103 ;; Option definitions.
105 :help "Process SOD input files to produce (e.g.) C output."
106 :version *sod-version*
108 (help-options :short-version #\V)
110 (#\I "include" (:arg "DIR")
111 ("Search DIR for module imports.")
112 (list *module-dirs* 'string))
114 ("Print a Lisp backtrace on error (for debugging).")
117 ("Process the builtin `sod-base' module.")
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.")
126 (let ((*package* (find-package "SOD-USER")))
127 (eval (read-from-string lisp)))
129 (option-parse-error "~A" error)))))
130 (#\l "load" (:arg "FILE")
131 ("Load a file of Lisp code.")
133 (let ((file (merge-pathnames file
138 (let ((*package* (find-package "SOD-USER")))
139 (find-file *default-pathname-defaults* file
142 (declare (ignore path))
147 (option-parse-error "~A" error))))))
149 ("Write output files to standard output.")
151 (#\t "type" (:arg "OUT-TYPE")
152 ("Produce output of type OUT-TYPE.")
153 (list output-reasons 'keyword))))
156 ;; Actually parse the options.
157 (let ((*option-parser* (make-option-parser)))
158 (unless (and (option-parse-try
159 (do-options (:parser *option-parser*)
165 ;; Do the main parsing job.
166 (labels ((hack-module (module)
167 ;; Process the MODULE, writing out the generated code.
169 ;; Work through each output type in turn.
170 (dolist (reason output-reasons)
172 ;; Arrange to be able to recover from errors.
175 ;; Collect information for constructing the output
176 ;; filenames here. In particular,
177 ;; `output-type-pathname' will sanity-check the
178 ;; output type for us, which is useful even if
179 ;; we're writing to stdout.
180 (let ((outpath (output-type-pathname reason))
181 (modpath (module-name module)))
185 ;; If we're writing to stdout then just do
187 (output-module module reason
190 ;; Otherwise we have to construct an output
191 ;; filename the hard way.
194 (reduce #'merge-pathnames
202 :if-exists :supersede
203 :if-does-not-exist :create)
204 (output-module module reason stream))))
208 :report (lambda (stream)
210 "Skip output type `~(~A~)'"
216 ;; If there are no output types then there's nothing to do.
217 (unless output-reasons
218 (error "No output types given: nothing to do"))
220 ;; If we're writing the builtin module then now seems like a
221 ;; good time to do that.
223 (hack-module *builtin-module*))
225 ;; Parse and write out the remaining modules.
227 (let ((module (read-module arg)))
228 (when (zerop (module-errors module))
229 (hack-module module))))))
231 (if backtracep (hack-modules)
232 (multiple-value-bind (hunoz nerror nwarn)
233 (count-and-report-errors ()
234 (with-default-error-location
235 ((make-file-location *program-name*))
237 (declare (ignore hunoz))
238 (when (or (plusp nerror) (plusp nwarn))
239 (format *error-output* "~A: Finished with~
240 ~[~:; ~:*~D error~:P~[~:; and~]~:*~]~
241 ~[~:; ~:*~D warning~:P~]~%"
242 *program-name* nerror nwarn))
243 (exit (if (plusp nerror) 2 0)))))))
245 ;;;----- That's all, folks --------------------------------------------------