chiark / gitweb /
8138a5a42ea134535a2b84cbd4fa823932d2a54b
[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:in-package #:sod)
27
28 ;;;--------------------------------------------------------------------------
29 ;;; The main program.
30
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."
35
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.
40
41     (let ((path (ignore-errors (pathname arg))))
42       (cond ((null path)
43              ;; The namestring couldn't be parsed, or something else went
44              ;; horribly wrong.
45
46              (option-parse-error "Can't parse `~A' as a path" arg))
47
48             #+unix
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.
52
53              (setf var (make-pathname
54                         :name nil :type nil :defaults path
55                         :directory (append (or (pathname-directory path)
56                                                (list :relative))
57                                            (list (file-namestring path))))))
58
59             (t
60              ;; This actually looks like a plain directory name.
61
62              (setf var path))))))
63
64 (export 'main)
65 (defun main ()
66
67   ;; Initialize the argument parser.
68   (set-command-line-arguments)
69
70   ;; Collect information from the command line options.
71   (let ((output-reasons nil)
72         (output-path (make-pathname :directory '(:relative)))
73         (builtinsp nil)
74         (stdoutp nil)
75         (args nil))
76
77     ;; Option definitions.
78     (define-program
79       :help "Process SOD input files to produce (e.g.) C output."
80       :version "0.1.0"
81       :usage "SOURCES..."
82       :options (options
83                 (help-options :short-version #\V)
84                 "Crazy options"
85                 (#\I "include" (:arg "DIR")
86                      ("Search DIR for module imports.")
87                      (list *module-dirs* 'string))
88                 ("builtins"
89                      ("Process the builtin `sod-base' module.")
90                      (set builtinsp))
91                 (#\d "directory" (:arg "DIR")
92                      ("Write output files to DIR.")
93                      (dirpath output-path))
94                 (#\p "stdout"
95                      ("Write output files to standard output.")
96                      (set stdoutp))
97                 (#\t "type" (:arg "OUT-TYPE")
98                      ("Produce output of type OUT-TYPE.")
99                      (list output-reasons 'keyword))))
100
101     ;; Actually parse the options.
102     (unless (and (option-parse-try
103                    (do-options ()
104                      (nil (rest)
105                           (setf args rest))))
106                  (or builtinsp args))
107       (die-usage))
108
109     ;; Prepare the builtins.
110     (make-builtin-module)
111
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*))
116
117             (flet ((hack-module (module)
118                      ;; Process the MODULE, writing out the generated code.
119
120                      ;; Work through each output type in turn.
121                      (dolist (reason output-reasons)
122
123                        ;; Arrange to be able to recover from errors.
124                        (restart-case
125
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)))
133
134                              (if stdoutp
135
136                                  ;; If we're writing to stdout then just do
137                                  ;; that.
138                                  (output-module module reason
139                                                 *standard-output*)
140
141                                  ;; Otherwise we have to construct an output
142                                  ;; filename the hard way.
143                                  (with-open-file
144                                      (stream
145                                       (reduce #'merge-pathnames
146                                               (list output-path
147                                                     outpath
148                                                     (make-pathname
149                                                      :directory nil
150                                                      :defaults modpath))
151                                               :from-end t)
152                                       :direction :output
153                                       :if-exists :supersede
154                                       :if-does-not-exist :create)
155                                    (output-module module reason stream))))
156
157                          ;; Error recovery.
158                          (continue ()
159                            :report (lambda (stream)
160                                      (format stream
161                                              "Skip output type `~(~A~)'"
162                                              reason))
163                            nil)))))
164
165               ;; If we're writing the builtin module then now seems like a
166               ;; good time to do that.
167               (when builtinsp
168                 (clear-the-decks)
169                 (hack-module *builtin-module*))
170
171               ;; Parse and write out the remaining modules.
172               (dolist (arg args)
173                 (clear-the-decks)
174                 (hack-module (read-module arg))))))
175
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))
183
184       ;; Exit with a sensible status.
185       (exit (if (plusp nerror) 2 0)))))
186
187 ;;;----- That's all, folks --------------------------------------------------