src/frontend.lisp: Prepare the builtin module at load time.
[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 Sensble 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 #: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 (make-builtin-module)
36
37 ;;;--------------------------------------------------------------------------
38 ;;; The main program.
39
40 (eval-when (:compile-toplevel :load-toplevel :execute)
41 (defopthandler dirpath (var arg) ()
42 "Convert the argument into a pathname with a directory component
43 and no file component, suitable for merging."
44
45 ;; This is really fiddly and annoying. Unix pathnames don't tell you
46 ;; whether the thing named is meant to be a directory or not, and
47 ;; implementations differ as to how they cope with pathnames which do or
48 ;; don't name directories when they're expecting files, or vice versa.
49
50 (let ((path (ignore-errors (pathname arg))))
51 (cond ((null path)
52 ;; The namestring couldn't be parsed, or something else went
53 ;; horribly wrong.
54
55 (option-parse-error "Can't parse `~A' as a path" arg))
56
57 #+unix
58 ((or (pathname-name path) (pathname-type path))
59 ;; If this is Unix, or similar, then stick the filename piece on
60 ;; the end of the directory and hope that was sensible.
61
62 (setf var (make-pathname
63 :name nil :type nil :defaults path
64 :directory (append (or (pathname-directory path)
65 (list :relative))
66 (list (file-namestring path))))))
67
68 (t
69 ;; This actually looks like a plain directory name.
70
71 (setf var path))))))
72
73 (export 'main)
74 (defun main ()
75
76 ;; Initialize the argument parser.
77 (set-command-line-arguments)
78
79 ;; Collect information from the command line options.
80 (let ((output-reasons nil)
81 (output-path (make-pathname :directory '(:relative)))
82 (builtinsp nil)
83 (stdoutp nil)
84 (args nil))
85
86 ;; Option definitions.
87 (define-program
88 :help "Process SOD input files to produce (e.g.) C output."
89 :version *sod-version*
90 :usage "SOURCES..."
91 :options (options
92 (help-options :short-version #\V)
93 "Translator options"
94 (#\I "include" (:arg "DIR")
95 ("Search DIR for module imports.")
96 (list *module-dirs* 'string))
97 ("builtins"
98 ("Process the builtin `sod-base' module.")
99 (set builtinsp))
100 (#\d "directory" (:arg "DIR")
101 ("Write output files to DIR.")
102 (dirpath output-path))
103 (#\p "stdout"
104 ("Write output files to standard output.")
105 (set stdoutp))
106 (#\t "type" (:arg "OUT-TYPE")
107 ("Produce output of type OUT-TYPE.")
108 (list output-reasons 'keyword))))
109
110 ;; Actually parse the options.
111 (unless (and (option-parse-try
112 (do-options ()
113 (nil (rest)
114 (setf args rest))))
115 (or builtinsp args))
116 (die-usage))
117
118 ;; Do the main parsing job.
119 (multiple-value-bind (hunoz nerror nwarn)
120 (count-and-report-errors ()
121 (with-default-error-location ((make-file-location *program-name*))
122
123 (flet ((hack-module (module)
124 ;; Process the MODULE, writing out the generated code.
125
126 ;; Work through each output type in turn.
127 (dolist (reason output-reasons)
128
129 ;; Arrange to be able to recover from errors.
130 (restart-case
131
132 ;; Collect information for constructing the output
133 ;; filenames here. In particular,
134 ;; `output-type-pathname' will sanity-check the
135 ;; output type for us, which is useful even if
136 ;; we're writing to stdout.
137 (let ((outpath (output-type-pathname reason))
138 (modpath (module-name module)))
139
140 (if stdoutp
141
142 ;; If we're writing to stdout then just do
143 ;; that.
144 (output-module module reason
145 *standard-output*)
146
147 ;; Otherwise we have to construct an output
148 ;; filename the hard way.
149 (with-open-file
150 (stream
151 (reduce #'merge-pathnames
152 (list output-path
153 outpath
154 (make-pathname
155 :directory nil
156 :defaults modpath))
157 :from-end t)
158 :direction :output
159 :if-exists :supersede
160 :if-does-not-exist :create)
161 (output-module module reason stream))))
162
163 ;; Error recovery.
164 (continue ()
165 :report (lambda (stream)
166 (format stream
167 "Skip output type `~(~A~)'"
168 reason))
169 nil)))))
170
171 ;; If we're writing the builtin module then now seems like a
172 ;; good time to do that.
173 (when builtinsp
174 (hack-module *builtin-module*))
175
176 ;; Parse and write out the remaining modules.
177 (dolist (arg args)
178 (hack-module (read-module arg))))))
179
180 ;; Report on how well everything worked.
181 (declare (ignore hunoz))
182 (when (or (plusp nerror) (plusp nwarn))
183 (format *error-output* "~A: Finished with~
184 ~[~:; ~:*~D error~:P~[~:; and~]~:*~]~
185 ~[~:; ~:*~D warning~:P~]~%"
186 *program-name* nerror nwarn))
187
188 ;; Exit with a sensible status.
189 (exit (if (plusp nerror) 2 0)))))
190
191 ;;;----- That's all, folks --------------------------------------------------