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 (eval-when (:compile-toplevel :load-toplevel :execute)
27 (handler-bind ((warning #'muffle-warning))
28 (cl:defpackage #:sod-frontend
29 (:use #:common-lisp #:sod-utilities #:optparse #:sod #:sod-parser))))
31 (cl:in-package #:sod-frontend)
33 ;;;--------------------------------------------------------------------------
36 (defvar-unbound *option-parser*
37 "The program's main option parser.")
39 (eval-when (:compile-toplevel :load-toplevel :execute)
40 (defopthandler dirpath (var arg) ()
41 "Convert the argument into a pathname with a directory component
42 and no file component, suitable for merging."
44 ;; This is really fiddly and annoying. Unix pathnames don't tell you
45 ;; whether the thing named is meant to be a directory or not, and
46 ;; implementations differ as to how they cope with pathnames which do or
47 ;; don't name directories when they're expecting files, or vice versa.
49 (let ((path (ignore-errors (pathname arg))))
51 ;; The namestring couldn't be parsed, or something else went
54 (option-parse-error "Can't parse `~A' as a path" arg))
57 ((or (pathname-name path) (pathname-type path))
58 ;; If this is Unix, or similar, then stick the filename piece on
59 ;; the end of the directory and hope that was sensible.
61 (setf var (make-pathname
62 :name nil :type nil :defaults path
63 :directory (append (or (pathname-directory path)
65 (list (file-namestring path))))))
68 ;; This actually looks like a plain directory name.
72 (defun update-usage ()
73 (setf *usage* (simple-usage *options* "SOURCES...")))
75 (export 'augment-options)
76 (defun augment-options (options)
77 "Add OPTIONS to the program's options list."
78 (asetf *options* (append it options))
79 (setf (op-options *option-parser*) *options*)
82 (use-package "SOD-FRONTEND" "SOD-USER")
87 ;; Initialize the argument parser.
88 (set-command-line-arguments)
90 ;; Collect information from the command line options.
91 (let ((output-reasons nil)
92 (output-path (make-pathname :directory '(:relative)))
99 ;; Option definitions.
101 :help "Process SOD input files to produce (e.g.) C output."
102 :version *sod-version*
104 (help-options :short-version #\V)
106 (#\I "include" (:arg "DIR")
107 ("Search DIR for module imports.")
108 (list *module-dirs* 'string))
110 ("Print a Lisp backtrace on error (for debugging).")
113 ("Process the builtin `sod-base' module.")
115 (#\d "directory" (:arg "DIR")
116 ("Write output files to DIR.")
117 (dirpath output-path))
118 (#\e "eval" (:arg "LISP")
119 ("Evaluate raw Lisp code.")
122 (let ((*package* (find-package "SOD-USER")))
123 (eval (read-from-string lisp)))
125 (option-parse-error "~A" error)))))
126 (#\l "load" (:arg "FILE")
127 ("Load a file of Lisp code.")
129 (let ((file (merge-pathnames file
134 (let ((*package* (find-package "SOD-USER")))
135 (find-file *default-pathname-defaults* file
138 (declare (ignore path))
143 (option-parse-error "~A" error))))))
144 (#\M "track-dependencies"
145 "Write make(1) fragments recording dependencies."
148 ("Write output files to standard output.")
150 (#\t "type" (:arg "OUT-TYPE")
151 ("Produce output of type OUT-TYPE.")
152 (list output-reasons 'keyword))))
155 ;; Actually parse the options.
156 (let ((*option-parser* (make-option-parser)))
157 (unless (and (option-parse-try
158 (do-options (:parser *option-parser*)
164 ;; Do the main parsing job.
165 (labels ((hack-module (module)
166 ;; Process the MODULE, writing out the generated code.
168 ;; Work through each output type in turn.
169 (dolist (reason output-reasons)
171 ;; Arrange to be able to recover from errors.
176 ;; If we're writing to stdout then use
177 ;; `output-type-pathname' to check the output type
180 (output-type-pathname reason)
181 (output-module module reason *standard-output*))
184 ;; Otherwise we have to construct an output
185 ;; filename the hard way.
188 (module-output-file module reason output-path)
190 :if-exists :supersede
191 :if-does-not-exist :create)
192 (output-module module reason stream))
195 (write-dependency-file module reason
200 :report (lambda (stream)
202 "Skip output type `~(~A~)'"
208 ;; If there are no output types then there's nothing to do.
209 (unless output-reasons
210 (error "No output types given: nothing to do"))
212 ;; If we're writing the builtin module then now seems like a
213 ;; good time to do that.
215 (hack-module *builtin-module*))
217 ;; Parse and write out the remaining modules.
219 (let ((module (read-module arg)))
220 (when (zerop (module-errors module))
221 (hack-module module))))))
223 (if backtracep (hack-modules)
224 (multiple-value-bind (hunoz nerror nwarn)
225 (count-and-report-errors ()
226 (with-default-error-location
227 ((make-file-location *program-name*))
229 (declare (ignore hunoz))
230 (when (or (plusp nerror) (plusp nwarn))
231 (format *error-output* "~A: Finished with~
232 ~[~:; ~:*~D error~:P~[~:; and~]~:*~]~
233 ~[~:; ~:*~D warning~:P~]~%"
234 *program-name* nerror nwarn))
235 (uiop:quit (if (plusp nerror) 2 0)))))))
237 ;;;----- That's all, folks --------------------------------------------------