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