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.
80 ;; Initialize the argument parser.
81 (set-command-line-arguments)
83 ;; Collect information from the command line options.
84 (let ((output-reasons nil)
85 (output-path (make-pathname :directory '(:relative)))
91 ;; Option definitions.
93 :help "Process SOD input files to produce (e.g.) C output."
94 :version *sod-version*
97 (help-options :short-version #\V)
99 (#\I "include" (:arg "DIR")
100 ("Search DIR for module imports.")
101 (list *module-dirs* 'string))
103 ("Print a Lisp backtrace on error (for debugging).")
106 ("Process the builtin `sod-base' module.")
108 (#\d "directory" (:arg "DIR")
109 ("Write output files to DIR.")
110 (dirpath output-path))
112 ("Write output files to standard output.")
114 (#\t "type" (:arg "OUT-TYPE")
115 ("Produce output of type OUT-TYPE.")
116 (list output-reasons 'keyword))))
118 ;; Actually parse the options.
119 (let ((*option-parser* (make-option-parser)))
120 (unless (and (option-parse-try
121 (do-options (:parser *option-parser*)
127 ;; Do the main parsing job.
128 (labels ((hack-module (module)
129 ;; Process the MODULE, writing out the generated code.
131 ;; Work through each output type in turn.
132 (dolist (reason output-reasons)
134 ;; Arrange to be able to recover from errors.
137 ;; Collect information for constructing the output
138 ;; filenames here. In particular,
139 ;; `output-type-pathname' will sanity-check the
140 ;; output type for us, which is useful even if
141 ;; we're writing to stdout.
142 (let ((outpath (output-type-pathname reason))
143 (modpath (module-name module)))
147 ;; If we're writing to stdout then just do
149 (output-module module reason
152 ;; Otherwise we have to construct an output
153 ;; filename the hard way.
156 (reduce #'merge-pathnames
164 :if-exists :supersede
165 :if-does-not-exist :create)
166 (output-module module reason stream))))
170 :report (lambda (stream)
172 "Skip output type `~(~A~)'"
178 ;; If there are no output types then there's nothing to do.
179 (unless output-reasons
180 (error "No output types given: nothing to do"))
182 ;; If we're writing the builtin module then now seems like a
183 ;; good time to do that.
185 (hack-module *builtin-module*))
187 ;; Parse and write out the remaining modules.
189 (let ((module (read-module arg)))
190 (when (zerop (module-errors module))
191 (hack-module module))))))
193 (if backtracep (hack-modules)
194 (multiple-value-bind (hunoz nerror nwarn)
195 (count-and-report-errors ()
196 (with-default-error-location
197 ((make-file-location *program-name*))
199 (declare (ignore hunoz))
200 (when (or (plusp nerror) (plusp nwarn))
201 (format *error-output* "~A: Finished with~
202 ~[~:; ~:*~D error~:P~[~:; and~]~:*~]~
203 ~[~:; ~:*~D warning~:P~]~%"
204 *program-name* nerror nwarn))
205 (exit (if (plusp nerror) 2 0)))))))
207 ;;;----- That's all, folks --------------------------------------------------