src/: Write dependency-tracking Makefile fragments.
[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 Sensible 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 #:sod-utilities #: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 (clear-the-decks)
36 (exercise)
37
38 ;;;--------------------------------------------------------------------------
39 ;;; The main program.
40
41 (defvar-unbound *option-parser*
42 "The program's main option parser.")
43
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."
48
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.
53
54 (let ((path (ignore-errors (pathname arg))))
55 (cond ((null path)
56 ;; The namestring couldn't be parsed, or something else went
57 ;; horribly wrong.
58
59 (option-parse-error "Can't parse `~A' as a path" arg))
60
61 #+unix
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.
65
66 (setf var (make-pathname
67 :name nil :type nil :defaults path
68 :directory (append (or (pathname-directory path)
69 (list :relative))
70 (list (file-namestring path))))))
71
72 (t
73 ;; This actually looks like a plain directory name.
74
75 (setf var path))))))
76
77 (defun update-usage ()
78 (setf *usage* (simple-usage *options* "SOURCES...")))
79
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*)
85 (update-usage))
86
87 (use-package "SOD-FRONTEND" "SOD-USER")
88
89 (export 'main)
90 (defun main ()
91
92 ;; Initialize the argument parser.
93 (set-command-line-arguments)
94
95 ;; Collect information from the command line options.
96 (let ((output-reasons nil)
97 (output-path (make-pathname :directory '(:relative)))
98 (backtracep nil)
99 (builtinsp nil)
100 (stdoutp nil)
101 (track-deps-p nil)
102 (args nil))
103
104 ;; Option definitions.
105 (define-program
106 :help "Process SOD input files to produce (e.g.) C output."
107 :version *sod-version*
108 :options (options
109 (help-options :short-version #\V)
110 "Translator options"
111 (#\I "include" (:arg "DIR")
112 ("Search DIR for module imports.")
113 (list *module-dirs* 'string))
114 ("backtrace"
115 ("Print a Lisp backtrace on error (for debugging).")
116 (set backtracep))
117 ("builtins"
118 ("Process the builtin `sod-base' module.")
119 (set builtinsp))
120 (#\d "directory" (:arg "DIR")
121 ("Write output files to DIR.")
122 (dirpath output-path))
123 (#\e "eval" (:arg "LISP")
124 ("Evaluate raw Lisp code.")
125 (lambda (lisp)
126 (handler-case
127 (let ((*package* (find-package "SOD-USER")))
128 (eval (read-from-string lisp)))
129 (error (error)
130 (option-parse-error "~A" error)))))
131 (#\l "load" (:arg "FILE")
132 ("Load a file of Lisp code.")
133 (lambda (file)
134 (let ((file (merge-pathnames file
135 (make-pathname
136 :type "LISP"
137 :case :common))))
138 (handler-case
139 (let ((*package* (find-package "SOD-USER")))
140 (find-file *default-pathname-defaults* file
141 "Lisp file"
142 (lambda (path true)
143 (declare (ignore path))
144 (load true
145 :verbose nil
146 :print nil))))
147 (error (error)
148 (option-parse-error "~A" error))))))
149 (#\M "track-dependencies"
150 "Write make(1) fragments recording dependencies."
151 (set track-deps-p))
152 (#\p "stdout"
153 ("Write output files to standard output.")
154 (set stdoutp))
155 (#\t "type" (:arg "OUT-TYPE")
156 ("Produce output of type OUT-TYPE.")
157 (list output-reasons 'keyword))))
158 (update-usage)
159
160 ;; Actually parse the options.
161 (let ((*option-parser* (make-option-parser)))
162 (unless (and (option-parse-try
163 (do-options (:parser *option-parser*)
164 (nil (rest)
165 (setf args rest))))
166 (or builtinsp args))
167 (die-usage)))
168
169 ;; Do the main parsing job.
170 (labels ((hack-module (module)
171 ;; Process the MODULE, writing out the generated code.
172
173 ;; Work through each output type in turn.
174 (dolist (reason output-reasons)
175
176 ;; Arrange to be able to recover from errors.
177 (restart-case
178 (cond
179
180 (stdoutp
181 ;; If we're writing to stdout then use
182 ;; `output-type-pathname' to check the output type
183 ;; for us.
184
185 (output-type-pathname reason)
186 (output-module module reason *standard-output*))
187
188 (t
189 ;; Otherwise we have to construct an output
190 ;; filename the hard way.
191 (with-open-file
192 (stream
193 (module-output-file module reason output-path)
194 :direction :output
195 :if-exists :supersede
196 :if-does-not-exist :create)
197 (output-module module reason stream))
198
199 (when track-deps-p
200 (write-dependency-file module reason
201 output-path))))
202
203 ;; Error recovery.
204 (continue ()
205 :report (lambda (stream)
206 (format stream
207 "Skip output type `~(~A~)'"
208 reason))
209 nil))))
210
211 (hack-modules ()
212
213 ;; If there are no output types then there's nothing to do.
214 (unless output-reasons
215 (error "No output types given: nothing to do"))
216
217 ;; If we're writing the builtin module then now seems like a
218 ;; good time to do that.
219 (when builtinsp
220 (hack-module *builtin-module*))
221
222 ;; Parse and write out the remaining modules.
223 (dolist (arg args)
224 (let ((module (read-module arg)))
225 (when (zerop (module-errors module))
226 (hack-module module))))))
227
228 (if backtracep (hack-modules)
229 (multiple-value-bind (hunoz nerror nwarn)
230 (count-and-report-errors ()
231 (with-default-error-location
232 ((make-file-location *program-name*))
233 (hack-modules)))
234 (declare (ignore hunoz))
235 (when (or (plusp nerror) (plusp nwarn))
236 (format *error-output* "~A: Finished with~
237 ~[~:; ~:*~D error~:P~[~:; and~]~:*~]~
238 ~[~:; ~:*~D warning~:P~]~%"
239 *program-name* nerror nwarn))
240 (exit (if (plusp nerror) 2 0)))))))
241
242 ;;;----- That's all, folks --------------------------------------------------