5 ;;; (c) 2013 Straylight/Edgeware
8 ;;;----- Licensing notice ---------------------------------------------------
10 ;;; This file is part of the Sensble 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.
28 ;;;--------------------------------------------------------------------------
31 (eval-when (:compile-toplevel :load-toplevel :execute)
32 (defopthandler dirpath (var arg) ()
33 "Convert the argument into a pathname with a directory component
34 and no file component, suitable for merging."
36 ;; This is really fiddly and annoying. Unix pathnames don't tell you
37 ;; whether the thing named is meant to be a directory or not, and
38 ;; implementations differ as to how they cope with pathnames which do or
39 ;; don't name directories when they're expecting files, or vice versa.
41 (let ((path (ignore-errors (pathname arg))))
43 ;; The namestring couldn't be parsed, or something else went
46 (option-parse-error "Can't parse `~A' as a path" arg))
49 ((or (pathname-name path) (pathname-type path))
50 ;; If this is Unix, or similar, then stick the filename piece on
51 ;; the end of the directory and hope that was sensible.
53 (setf var (make-pathname
54 :name nil :type nil :defaults path
55 :directory (append (or (pathname-directory path)
57 (list (file-namestring path))))))
60 ;; This actually looks like a plain directory name.
67 ;; Initialize the argument parser.
68 (set-command-line-arguments)
70 ;; Collect information from the command line options.
71 (let ((output-reasons nil)
72 (output-path (make-pathname :directory '(:relative)))
77 ;; Option definitions.
79 :help "Process SOD input files to produce (e.g.) C output."
83 (help-options :short-version #\V)
85 (#\I "include" (:arg "DIR")
86 ("Search DIR for module imports.")
87 (list *module-dirs* 'string))
89 ("Process the builtin `sod-base' module.")
91 (#\d "directory" (:arg "DIR")
92 ("Write output files to DIR.")
93 (dirpath output-path))
95 ("Write output files to standard output.")
97 (#\t "type" (:arg "OUT-TYPE")
98 ("Produce output of type OUT-TYPE.")
99 (list output-reasons 'keyword))))
101 ;; Actually parse the options.
102 (unless (and (option-parse-try
109 ;; Prepare the builtins.
110 (make-builtin-module)
112 ;; Do the main parsing job.
113 (multiple-value-bind (hunoz nerror nwarn)
114 (count-and-report-errors ()
115 (with-default-error-location ((make-file-location *program-name*))
117 (flet ((hack-module (module)
118 ;; Process the MODULE, writing out the generated code.
120 ;; Work through each output type in turn.
121 (dolist (reason output-reasons)
123 ;; Arrange to be able to recover from errors.
126 ;; Collect information for constructing the output
127 ;; filenames here. In particular,
128 ;; `output-type-pathname' will sanity-check the
129 ;; output type for us, which is useful even if
130 ;; we're writing to stdout.
131 (let ((outpath (output-type-pathname reason))
132 (modpath (module-name module)))
136 ;; If we're writing to stdout then just do
138 (output-module module reason
141 ;; Otherwise we have to construct an output
142 ;; filename the hard way.
145 (reduce #'merge-pathnames
153 :if-exists :supersede
154 :if-does-not-exist :create)
155 (output-module module reason stream))))
159 :report (lambda (stream)
161 "Skip output type `~(~A~)'"
165 ;; If we're writing the builtin module then now seems like a
166 ;; good time to do that.
169 (hack-module *builtin-module*))
171 ;; Parse and write out the remaining modules.
174 (hack-module (read-module arg))))))
176 ;; Report on how well everything worked.
177 (declare (ignore hunoz))
178 (when (or (plusp nerror) (plusp nwarn))
179 (format *error-output* "~A: Finished with~
180 ~[~:; ~:*~D error~:P~[~:; and~]~:*~]~
181 ~[~:; ~:*~D warning~:P~]~%"
182 *program-name* nerror nwarn))
184 ;; Exit with a sensible status.
185 (exit (if (plusp nerror) 2 0)))))
187 ;;;----- That's all, folks --------------------------------------------------